hsfm/test/FileSystem/FileOperations/CopyFileOverwriteSpec.hs

92 lines
4.0 KiB
Haskell
Raw Normal View History

2016-05-08 10:48:03 +00:00
{-# LANGUAGE OverloadedStrings #-}
module FileSystem.FileOperations.CopyFileOverwriteSpec where
import Test.Hspec
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
copyFileOverwriteSpec :: Spec
copyFileOverwriteSpec =
describe "HSFM.FileSystem.FileOperations.copyFileOverwrite" $ do
-- successes --
it "copyFileOverwrite, everything clear" $ do
copyFileOverwrite' "test/FileSystem/FileOperations/copyFileOverwriteSpec/inputFile"
"test/FileSystem/FileOperations/copyFileOverwriteSpec/outputFile"
removeFileIfExists "test/FileSystem/FileOperations/copyFileOverwriteSpec/outputFile"
it "copyFileOverwrite, output file already exists, all clear" $
copyFileOverwrite' "test/FileSystem/FileOperations/copyFileOverwriteSpec/inputFile"
"test/FileSystem/FileOperations/copyFileOverwriteSpec/alreadyExists"
it "copyFileOverwrite, and compare" $ do
copyFileOverwrite' "test/FileSystem/FileOperations/copyFileOverwriteSpec/inputFile"
"test/FileSystem/FileOperations/copyFileOverwriteSpec/outputFile"
(system $ "cmp -s " ++ "test/FileSystem/FileOperations/copyFileOverwriteSpec/inputFile" ++ " "
++ "test/FileSystem/FileOperations/copyFileOverwriteSpec/outputFile")
`shouldReturn` ExitSuccess
removeFileIfExists "test/FileSystem/FileOperations/copyFileOverwriteSpec/outputFile"
-- posix failures --
it "copyFileOverwrite, input file does not exist" $
copyFileOverwrite' "test/FileSystem/FileOperations/copyFileOverwriteSpec/noSuchFile"
"test/FileSystem/FileOperations/copyFileOverwriteSpec/outputFile"
`shouldThrow`
(\e -> ioeGetErrorType e == NoSuchThing)
it "copyFileOverwrite, no permission to write to output directory" $
copyFileOverwrite' "test/FileSystem/FileOperations/copyFileOverwriteSpec/inputFile"
"test/FileSystem/FileOperations/copyFileOverwriteSpec/outputDirNoWrite/outputFile"
`shouldThrow`
(\e -> ioeGetErrorType e == PermissionDenied)
it "copyFileOverwrite, cannot open output directory" $
copyFileOverwrite' "test/FileSystem/FileOperations/copyFileOverwriteSpec/inputFile"
"test/FileSystem/FileOperations/copyFileOverwriteSpec/noPerms/outputFile"
`shouldThrow`
(\e -> ioeGetErrorType e == PermissionDenied)
it "copyFileOverwrite, cannot open source directory" $
copyFileOverwrite' "test/FileSystem/FileOperations/copyFileOverwriteSpec/noPerms/inputFile"
"test/FileSystem/FileOperations/copyFileOverwriteSpec/outputFile"
`shouldThrow`
(\e -> ioeGetErrorType e == PermissionDenied)
it "copyFileOverwrite, wrong input type (symlink)" $
copyFileOverwrite' "test/FileSystem/FileOperations/copyFileOverwriteSpec/inputFileSymL"
"test/FileSystem/FileOperations/copyFileOverwriteSpec/outputFile"
`shouldThrow`
(\e -> ioeGetErrorType e == InvalidArgument)
it "copyFileOverwrite, wrong input type (directory)" $
copyFileOverwrite' "test/FileSystem/FileOperations/copyFileOverwriteSpec/wrongInput"
"test/FileSystem/FileOperations/copyFileOverwriteSpec/outputFile"
`shouldThrow`
(\e -> ioeGetErrorType e == InappropriateType)
it "copyFileOverwrite, output file already exists and is a dir" $
copyFileOverwrite' "test/FileSystem/FileOperations/copyFileOverwriteSpec/inputFile"
"test/FileSystem/FileOperations/copyFileOverwriteSpec/alreadyExistsD"
`shouldThrow`
(\e -> ioeGetErrorType e == InappropriateType)
-- custom failures --
it "copyFileOverwrite, output and input are same file" $
copyFileOverwrite' "test/FileSystem/FileOperations/copyFileOverwriteSpec/inputFile"
"test/FileSystem/FileOperations/copyFileOverwriteSpec/inputFile"
`shouldThrow` isSameFile