hsfm/test/FileSystem/FileOperations/CopyFileSpec.hs
Julian Ospald 9c6cf51825
LIB: refactor FileOperation and related Errors
* move FileOperation/Copy/Move types to its own UtilTypes module
* remove runFileOp, since it's hard to really do the correct
  thing here for all possible exceptions... instead, let the
  GUI logic handle this
* introduce copyDirRecursiveOverwrite, copyFileOverwrite and
  easyCopyOverwrite
* use our own throwSameFile on functions to distinguish between
  "same file" and "file already exists"
* don't follow destination in copyFile* either
* improve throwSameFile, by examining device and file ids
* add isWritable
* improve documentation
* adjust and fix tests
2016-05-08 18:48:17 +02:00

95 lines
3.6 KiB
Haskell

{-# LANGUAGE OverloadedStrings #-}
module FileSystem.FileOperations.CopyFileSpec where
import Test.Hspec
import HSFM.FileSystem.Errors
import System.IO.Error
(
ioeGetErrorType
)
import GHC.IO.Exception
(
IOErrorType(..)
)
import System.Exit
import System.Process
import Utils
copyFileSpec :: Spec
copyFileSpec =
describe "HSFM.FileSystem.FileOperations.copyFile" $ do
-- successes --
it "copyFile, everything clear" $ do
copyFile' "test/FileSystem/FileOperations/copyFileSpec/inputFile"
"test/FileSystem/FileOperations/copyFileSpec/outputFile"
removeFileIfExists "test/FileSystem/FileOperations/copyFileSpec/outputFile"
it "copyFile, and compare" $ do
copyFile' "test/FileSystem/FileOperations/copyFileSpec/inputFile"
"test/FileSystem/FileOperations/copyFileSpec/outputFile"
(system $ "cmp -s " ++ "test/FileSystem/FileOperations/copyFileSpec/inputFile" ++ " "
++ "test/FileSystem/FileOperations/copyFileSpec/outputFile")
`shouldReturn` ExitSuccess
removeFileIfExists "test/FileSystem/FileOperations/copyFileSpec/outputFile"
-- posix failures --
it "copyFile, input file does not exist" $
copyFile' "test/FileSystem/FileOperations/copyFileSpec/noSuchFile"
"test/FileSystem/FileOperations/copyFileSpec/outputFile"
`shouldThrow`
(\e -> ioeGetErrorType e == NoSuchThing)
it "copyFile, no permission to write to output directory" $
copyFile' "test/FileSystem/FileOperations/copyFileSpec/inputFile"
"test/FileSystem/FileOperations/copyFileSpec/outputDirNoWrite/outputFile"
`shouldThrow`
(\e -> ioeGetErrorType e == PermissionDenied)
it "copyFile, cannot open output directory" $
copyFile' "test/FileSystem/FileOperations/copyFileSpec/inputFile"
"test/FileSystem/FileOperations/copyFileSpec/noPerms/outputFile"
`shouldThrow`
(\e -> ioeGetErrorType e == PermissionDenied)
it "copyFile, cannot open source directory" $
copyFile' "test/FileSystem/FileOperations/copyFileSpec/noPerms/inputFile"
"test/FileSystem/FileOperations/copyFileSpec/outputFile"
`shouldThrow`
(\e -> ioeGetErrorType e == PermissionDenied)
it "copyFile, wrong input type (symlink)" $
copyFile' "test/FileSystem/FileOperations/copyFileSpec/inputFileSymL"
"test/FileSystem/FileOperations/copyFileSpec/outputFile"
`shouldThrow`
(\e -> ioeGetErrorType e == InvalidArgument)
it "copyFile, wrong input type (directory)" $
copyFile' "test/FileSystem/FileOperations/copyFileSpec/wrongInput"
"test/FileSystem/FileOperations/copyFileSpec/outputFile"
`shouldThrow`
(\e -> ioeGetErrorType e == InappropriateType)
it "copyFile, output file already exists" $
copyFile' "test/FileSystem/FileOperations/copyFileSpec/inputFile"
"test/FileSystem/FileOperations/copyFileSpec/alreadyExists"
`shouldThrow`
(\e -> ioeGetErrorType e == AlreadyExists)
it "copyFile, output file already exists and is a dir" $
copyFile' "test/FileSystem/FileOperations/copyFileSpec/inputFile"
"test/FileSystem/FileOperations/copyFileSpec/alreadyExistsD"
`shouldThrow`
(\e -> ioeGetErrorType e == AlreadyExists)
-- custom failures --
it "copyFile, output and input are same file" $
copyFile' "test/FileSystem/FileOperations/copyFileSpec/inputFile"
"test/FileSystem/FileOperations/copyFileSpec/inputFile"
`shouldThrow`
isSameFile