2016-05-08 10:48:03 +00:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
|
|
|
|
module FileSystem.FileOperations.CopyFileOverwriteSpec where
|
|
|
|
|
|
|
|
|
|
|
|
import Test.Hspec
|
2016-05-08 16:48:17 +00:00
|
|
|
import HSFM.FileSystem.Errors
|
2016-05-08 10:48:03 +00:00
|
|
|
import System.IO.Error
|
|
|
|
(
|
|
|
|
ioeGetErrorType
|
|
|
|
)
|
|
|
|
import GHC.IO.Exception
|
|
|
|
(
|
|
|
|
IOErrorType(..)
|
|
|
|
)
|
|
|
|
import System.Exit
|
|
|
|
import System.Process
|
|
|
|
import Utils
|
2016-05-08 22:16:26 +00:00
|
|
|
import qualified Data.ByteString as BS
|
|
|
|
import Data.ByteString.UTF8 (toString)
|
2016-05-08 10:48:03 +00:00
|
|
|
|
|
|
|
|
2016-05-08 22:16:26 +00:00
|
|
|
ba :: BS.ByteString -> BS.ByteString -> BS.ByteString
|
|
|
|
ba = BS.append
|
|
|
|
|
|
|
|
specDir :: BS.ByteString
|
|
|
|
specDir = "test/FileSystem/FileOperations/copyFileOverwriteSpec/"
|
|
|
|
|
|
|
|
specDir' :: String
|
|
|
|
specDir' = toString specDir
|
|
|
|
|
2016-05-08 10:48:03 +00:00
|
|
|
|
2016-05-08 21:45:51 +00:00
|
|
|
spec :: Spec
|
|
|
|
spec =
|
2016-05-08 10:48:03 +00:00
|
|
|
describe "HSFM.FileSystem.FileOperations.copyFileOverwrite" $ do
|
|
|
|
|
|
|
|
-- successes --
|
|
|
|
it "copyFileOverwrite, everything clear" $ do
|
2016-05-08 22:16:26 +00:00
|
|
|
copyFileOverwrite' (specDir `ba` "inputFile")
|
|
|
|
(specDir `ba` "outputFile")
|
|
|
|
removeFileIfExists (specDir `ba` "outputFile")
|
2016-05-08 10:48:03 +00:00
|
|
|
|
|
|
|
it "copyFileOverwrite, output file already exists, all clear" $
|
2016-05-08 22:16:26 +00:00
|
|
|
copyFileOverwrite' (specDir `ba` "inputFile")
|
|
|
|
(specDir `ba` "alreadyExists")
|
2016-05-08 10:48:03 +00:00
|
|
|
|
|
|
|
it "copyFileOverwrite, and compare" $ do
|
2016-05-08 22:16:26 +00:00
|
|
|
copyFileOverwrite' (specDir `ba` "inputFile")
|
|
|
|
(specDir `ba` "outputFile")
|
|
|
|
(system $ "cmp -s " ++ specDir' ++ "inputFile" ++ " "
|
|
|
|
++ specDir' ++ "outputFile")
|
2016-05-08 10:48:03 +00:00
|
|
|
`shouldReturn` ExitSuccess
|
2016-05-08 22:16:26 +00:00
|
|
|
removeFileIfExists (specDir `ba` "outputFile")
|
2016-05-08 10:48:03 +00:00
|
|
|
|
|
|
|
-- posix failures --
|
|
|
|
it "copyFileOverwrite, input file does not exist" $
|
2016-05-08 22:16:26 +00:00
|
|
|
copyFileOverwrite' (specDir `ba` "noSuchFile")
|
|
|
|
(specDir `ba` "outputFile")
|
2016-05-08 10:48:03 +00:00
|
|
|
`shouldThrow`
|
|
|
|
(\e -> ioeGetErrorType e == NoSuchThing)
|
|
|
|
|
|
|
|
it "copyFileOverwrite, no permission to write to output directory" $
|
2016-05-08 22:16:26 +00:00
|
|
|
copyFileOverwrite' (specDir `ba` "inputFile")
|
|
|
|
(specDir `ba` "outputDirNoWrite/outputFile")
|
2016-05-08 10:48:03 +00:00
|
|
|
`shouldThrow`
|
|
|
|
(\e -> ioeGetErrorType e == PermissionDenied)
|
|
|
|
|
|
|
|
it "copyFileOverwrite, cannot open output directory" $
|
2016-05-08 22:16:26 +00:00
|
|
|
copyFileOverwrite' (specDir `ba` "inputFile")
|
|
|
|
(specDir `ba` "noPerms/outputFile")
|
2016-05-08 10:48:03 +00:00
|
|
|
`shouldThrow`
|
|
|
|
(\e -> ioeGetErrorType e == PermissionDenied)
|
|
|
|
|
|
|
|
it "copyFileOverwrite, cannot open source directory" $
|
2016-05-08 22:16:26 +00:00
|
|
|
copyFileOverwrite' (specDir `ba` "noPerms/inputFile")
|
|
|
|
(specDir `ba` "outputFile")
|
2016-05-08 10:48:03 +00:00
|
|
|
`shouldThrow`
|
|
|
|
(\e -> ioeGetErrorType e == PermissionDenied)
|
|
|
|
|
|
|
|
it "copyFileOverwrite, wrong input type (symlink)" $
|
2016-05-08 22:16:26 +00:00
|
|
|
copyFileOverwrite' (specDir `ba` "inputFileSymL")
|
|
|
|
(specDir `ba` "outputFile")
|
2016-05-08 10:48:03 +00:00
|
|
|
`shouldThrow`
|
|
|
|
(\e -> ioeGetErrorType e == InvalidArgument)
|
|
|
|
|
|
|
|
it "copyFileOverwrite, wrong input type (directory)" $
|
2016-05-08 22:16:26 +00:00
|
|
|
copyFileOverwrite' (specDir `ba` "wrongInput")
|
|
|
|
(specDir `ba` "outputFile")
|
2016-05-08 10:48:03 +00:00
|
|
|
`shouldThrow`
|
|
|
|
(\e -> ioeGetErrorType e == InappropriateType)
|
|
|
|
|
|
|
|
it "copyFileOverwrite, output file already exists and is a dir" $
|
2016-05-08 22:16:26 +00:00
|
|
|
copyFileOverwrite' (specDir `ba` "inputFile")
|
|
|
|
(specDir `ba` "alreadyExistsD")
|
2016-05-08 10:48:03 +00:00
|
|
|
`shouldThrow`
|
|
|
|
(\e -> ioeGetErrorType e == InappropriateType)
|
|
|
|
|
2016-05-08 16:48:17 +00:00
|
|
|
-- custom failures --
|
|
|
|
it "copyFileOverwrite, output and input are same file" $
|
2016-05-08 22:16:26 +00:00
|
|
|
copyFileOverwrite' (specDir `ba` "inputFile")
|
|
|
|
(specDir `ba` "inputFile")
|
2016-05-08 16:48:17 +00:00
|
|
|
`shouldThrow` isSameFile
|