hpath/test/HPath/IO/CopyFileSpec.hs

144 lines
3.7 KiB
Haskell
Raw Normal View History

2016-05-09 14:53:31 +00:00
{-# LANGUAGE OverloadedStrings #-}
2016-05-09 14:53:31 +00:00
module HPath.IO.CopyFileSpec where
import Test.Hspec
import HPath.IO
2016-05-09 14:53:31 +00:00
import HPath.IO.Errors
import System.IO.Error
(
ioeGetErrorType
)
import GHC.IO.Exception
(
IOErrorType(..)
)
import System.Exit
import System.Process
import Utils
import Data.ByteString.UTF8 (toString)
2016-06-05 14:09:34 +00:00
2016-06-05 12:33:53 +00:00
2016-05-09 14:53:31 +00:00
2016-06-05 12:33:53 +00:00
upTmpDir :: IO ()
upTmpDir = do
setTmpDir "CopyFileSpec"
createTmpDir
2016-05-09 14:53:31 +00:00
setupFiles :: IO ()
setupFiles = do
createRegularFile' "inputFile"
createRegularFile' "alreadyExists"
createSymlink' "inputFileSymL" "inputFile"
createDir' "alreadyExistsD"
createDir' "noPerms"
createRegularFile' "noPerms/inputFile"
createDir' "outputDirNoWrite"
createDir' "wrongInput"
noPerms "noPerms"
noWritableDirPerms "outputDirNoWrite"
writeFile' "inputFile" "Blahfaselgagaga"
cleanupFiles :: IO ()
cleanupFiles = do
normalDirPerms "noPerms"
normalDirPerms "outputDirNoWrite"
deleteFile' "noPerms/inputFile"
deleteFile' "inputFile"
deleteFile' "alreadyExists"
deleteFile' "inputFileSymL"
deleteDir' "alreadyExistsD"
deleteDir' "noPerms"
deleteDir' "outputDirNoWrite"
deleteDir' "wrongInput"
2016-05-09 14:53:31 +00:00
spec :: Spec
2016-06-05 13:25:57 +00:00
spec = beforeAll_ (upTmpDir >> setupFiles) $ afterAll_ cleanupFiles $
2016-05-09 14:53:31 +00:00
describe "HPath.IO.copyFile" $ do
-- successes --
it "copyFile (Strict), everything clear" $ do
copyFile' "inputFile"
"outputFile"
Strict
removeFileIfExists "outputFile"
2016-05-09 14:53:31 +00:00
it "copyFile (Strict), and compare" $ do
2016-06-05 12:33:53 +00:00
tmpDir' <- getRawTmpDir
copyFile' "inputFile"
"outputFile"
Strict
2016-06-05 12:33:53 +00:00
(system $ "cmp -s " ++ toString tmpDir' ++ "inputFile" ++ " "
++ toString tmpDir' ++ "outputFile")
2016-05-09 14:53:31 +00:00
`shouldReturn` ExitSuccess
removeFileIfExists "outputFile"
2016-05-09 14:53:31 +00:00
-- posix failures --
it "copyFile (Strict), input file does not exist" $
copyFile' "noSuchFile"
"outputFile"
Strict
2016-05-09 14:53:31 +00:00
`shouldThrow`
(\e -> ioeGetErrorType e == NoSuchThing)
it "copyFile (Strict), no permission to write to output directory" $
copyFile' "inputFile"
"outputDirNoWrite/outputFile"
Strict
2016-05-09 14:53:31 +00:00
`shouldThrow`
(\e -> ioeGetErrorType e == PermissionDenied)
it "copyFile (Strict), cannot open output directory" $
copyFile' "inputFile"
"noPerms/outputFile"
Strict
2016-05-09 14:53:31 +00:00
`shouldThrow`
(\e -> ioeGetErrorType e == PermissionDenied)
it "copyFile (Strict), cannot open source directory" $
copyFile' "noPerms/inputFile"
"outputFile"
Strict
2016-05-09 14:53:31 +00:00
`shouldThrow`
(\e -> ioeGetErrorType e == PermissionDenied)
it "copyFile (Strict), wrong input type (symlink)" $
copyFile' "inputFileSymL"
"outputFile"
Strict
2016-05-09 14:53:31 +00:00
`shouldThrow`
(\e -> ioeGetErrorType e == InvalidArgument)
it "copyFile (Strict), wrong input type (directory)" $
copyFile' "wrongInput"
"outputFile"
Strict
2016-05-09 14:53:31 +00:00
`shouldThrow`
(\e -> ioeGetErrorType e == InappropriateType)
it "copyFile (Strict), output file already exists" $
copyFile' "inputFile"
"alreadyExists"
Strict
2016-05-09 14:53:31 +00:00
`shouldThrow`
(\e -> ioeGetErrorType e == AlreadyExists)
it "copyFile (Strict), output file already exists and is a dir" $
copyFile' "inputFile"
"alreadyExistsD"
Strict
2016-05-09 14:53:31 +00:00
`shouldThrow`
(\e -> ioeGetErrorType e == AlreadyExists)
-- custom failures --
it "copyFile (Strict), output and input are same file" $
copyFile' "inputFile"
"inputFile"
Strict
2016-05-09 14:53:31 +00:00
`shouldThrow`
isSameFile