2016-05-09 14:53:31 +00:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
|
2016-05-29 15:29:13 +00:00
|
|
|
|
2016-05-09 14:53:31 +00:00
|
|
|
module HPath.IO.CopyDirRecursiveSpec where
|
|
|
|
|
|
|
|
|
|
|
|
import Test.Hspec
|
|
|
|
import HPath.IO.Errors
|
|
|
|
import System.IO.Error
|
|
|
|
(
|
|
|
|
ioeGetErrorType
|
|
|
|
)
|
|
|
|
import GHC.IO.Exception
|
|
|
|
(
|
|
|
|
IOErrorType(..)
|
|
|
|
)
|
|
|
|
import System.Exit
|
|
|
|
import System.Process
|
|
|
|
import Utils
|
|
|
|
import qualified Data.ByteString as BS
|
|
|
|
import Data.ByteString.UTF8 (toString)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-05-29 15:29:13 +00:00
|
|
|
|
|
|
|
setupFiles :: IO ()
|
|
|
|
setupFiles = do
|
|
|
|
createRegularFile' "alreadyExists"
|
|
|
|
createRegularFile' "wrongInput"
|
|
|
|
createSymlink' "wrongInputSymL" "inputDir/"
|
|
|
|
createDir' "alreadyExistsD"
|
|
|
|
createDir' "noPerms"
|
|
|
|
createDir' "noWritePerm"
|
|
|
|
|
|
|
|
createDir' "inputDir"
|
|
|
|
createDir' "inputDir/bar"
|
|
|
|
createDir' "inputDir/foo"
|
|
|
|
createRegularFile' "inputDir/foo/inputFile1"
|
|
|
|
createRegularFile' "inputDir/inputFile2"
|
|
|
|
createRegularFile' "inputDir/bar/inputFile3"
|
|
|
|
writeFile' "inputDir/foo/inputFile1" "SDAADSdsada"
|
|
|
|
writeFile' "inputDir/inputFile2" "Blahfaselgagaga"
|
|
|
|
writeFile' "inputDir/bar/inputFile3"
|
|
|
|
"fdfdssdffsd3223sasdasdasdadasasddasdasdasasd4"
|
|
|
|
|
|
|
|
noPerms "noPerms"
|
|
|
|
noWritableDirPerms "noWritePerm"
|
|
|
|
|
|
|
|
|
|
|
|
cleanupFiles :: IO ()
|
|
|
|
cleanupFiles = do
|
|
|
|
normalDirPerms "noPerms"
|
|
|
|
normalDirPerms "noWritePerm"
|
|
|
|
deleteFile' "alreadyExists"
|
|
|
|
deleteFile' "wrongInput"
|
|
|
|
deleteFile' "wrongInputSymL"
|
|
|
|
deleteDir' "alreadyExistsD"
|
|
|
|
deleteDir' "noPerms"
|
|
|
|
deleteDir' "noWritePerm"
|
|
|
|
deleteFile' "inputDir/foo/inputFile1"
|
|
|
|
deleteFile' "inputDir/inputFile2"
|
|
|
|
deleteFile' "inputDir/bar/inputFile3"
|
|
|
|
deleteDir' "inputDir/foo"
|
|
|
|
deleteDir' "inputDir/bar"
|
|
|
|
deleteDir' "inputDir"
|
|
|
|
|
|
|
|
|
2016-05-09 14:53:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
spec :: Spec
|
2016-05-29 15:29:13 +00:00
|
|
|
spec = before_ setupFiles $ after_ cleanupFiles $
|
2016-05-09 14:53:31 +00:00
|
|
|
describe "HPath.IO.copyDirRecursive" $ do
|
|
|
|
|
|
|
|
-- successes --
|
|
|
|
it "copyDirRecursive, all fine" $ do
|
2016-05-29 15:29:13 +00:00
|
|
|
copyDirRecursive' "inputDir"
|
|
|
|
"outputDir"
|
|
|
|
removeDirIfExists "outputDir"
|
2016-05-09 14:53:31 +00:00
|
|
|
|
|
|
|
it "copyDirRecursive, all fine and compare" $ do
|
2016-05-29 15:29:13 +00:00
|
|
|
copyDirRecursive' "inputDir"
|
|
|
|
"outputDir"
|
2016-05-09 14:53:31 +00:00
|
|
|
(system $ "diff -r --no-dereference "
|
2016-05-29 15:29:13 +00:00
|
|
|
++ toString tmpDir ++ "inputDir" ++ " "
|
|
|
|
++ toString tmpDir ++ "outputDir")
|
2016-05-09 14:53:31 +00:00
|
|
|
`shouldReturn` ExitSuccess
|
2016-05-29 15:29:13 +00:00
|
|
|
removeDirIfExists "outputDir"
|
2016-05-09 14:53:31 +00:00
|
|
|
|
|
|
|
-- posix failures --
|
|
|
|
it "copyDirRecursive, source directory does not exist" $
|
2016-05-29 15:29:13 +00:00
|
|
|
copyDirRecursive' "doesNotExist"
|
|
|
|
"outputDir"
|
2016-05-09 14:53:31 +00:00
|
|
|
`shouldThrow`
|
|
|
|
(\e -> ioeGetErrorType e == NoSuchThing)
|
|
|
|
|
|
|
|
it "copyDirRecursive, no write permission on output dir" $
|
2016-05-29 15:29:13 +00:00
|
|
|
copyDirRecursive' "inputDir"
|
|
|
|
"noWritePerm/foo"
|
2016-05-09 14:53:31 +00:00
|
|
|
`shouldThrow`
|
|
|
|
(\e -> ioeGetErrorType e == PermissionDenied)
|
|
|
|
|
|
|
|
it "copyDirRecursive, cannot open output dir" $
|
2016-05-29 15:29:13 +00:00
|
|
|
copyDirRecursive' "inputDir"
|
|
|
|
"noPerms/foo"
|
2016-05-09 14:53:31 +00:00
|
|
|
`shouldThrow`
|
|
|
|
(\e -> ioeGetErrorType e == PermissionDenied)
|
|
|
|
|
|
|
|
it "copyDirRecursive, cannot open source dir" $
|
2016-05-29 15:29:13 +00:00
|
|
|
copyDirRecursive' "noPerms/inputDir"
|
|
|
|
"foo"
|
2016-05-09 14:53:31 +00:00
|
|
|
`shouldThrow`
|
|
|
|
(\e -> ioeGetErrorType e == PermissionDenied)
|
|
|
|
|
|
|
|
it "copyDirRecursive, destination dir already exists" $
|
2016-05-29 15:29:13 +00:00
|
|
|
copyDirRecursive' "inputDir"
|
|
|
|
"alreadyExistsD"
|
2016-05-09 14:53:31 +00:00
|
|
|
`shouldThrow`
|
|
|
|
(\e -> ioeGetErrorType e == AlreadyExists)
|
|
|
|
|
|
|
|
it "copyDirRecursive, destination already exists and is a file" $
|
2016-05-29 15:29:13 +00:00
|
|
|
copyDirRecursive' "inputDir"
|
|
|
|
"alreadyExists"
|
2016-05-09 14:53:31 +00:00
|
|
|
`shouldThrow`
|
|
|
|
(\e -> ioeGetErrorType e == AlreadyExists)
|
|
|
|
|
|
|
|
it "copyDirRecursive, wrong input (regular file)" $
|
2016-05-29 15:29:13 +00:00
|
|
|
copyDirRecursive' "wrongInput"
|
|
|
|
"outputDir"
|
2016-05-09 14:53:31 +00:00
|
|
|
`shouldThrow`
|
|
|
|
(\e -> ioeGetErrorType e == InappropriateType)
|
|
|
|
|
|
|
|
it "copyDirRecursive, wrong input (symlink to directory)" $
|
2016-05-29 15:29:13 +00:00
|
|
|
copyDirRecursive' "wrongInputSymL"
|
|
|
|
"outputDir"
|
2016-05-09 14:53:31 +00:00
|
|
|
`shouldThrow`
|
|
|
|
(\e -> ioeGetErrorType e == InvalidArgument)
|
|
|
|
|
|
|
|
-- custom failures
|
|
|
|
it "copyDirRecursive, destination in source" $
|
2016-05-29 15:29:13 +00:00
|
|
|
copyDirRecursive' "inputDir"
|
|
|
|
"inputDir/foo"
|
2016-05-09 14:53:31 +00:00
|
|
|
`shouldThrow`
|
|
|
|
isDestinationInSource
|
|
|
|
|
|
|
|
it "copyDirRecursive, destination and source same directory" $
|
2016-05-29 15:29:13 +00:00
|
|
|
copyDirRecursive' "inputDir"
|
|
|
|
"inputDir"
|
2016-05-09 14:53:31 +00:00
|
|
|
`shouldThrow`
|
|
|
|
isSameFile
|