hpath/test/HPath/IO/MoveFileSpec.hs
Julian Ospald 395621b27a
Fix tests for sdist
We now create the necessary directories and files
for the tests on-the-fly.
2016-05-29 17:29:13 +02:00

114 lines
2.6 KiB
Haskell

{-# LANGUAGE OverloadedStrings #-}
module HPath.IO.MoveFileSpec where
import Test.Hspec
import HPath.IO.Errors
import System.IO.Error
(
ioeGetErrorType
)
import GHC.IO.Exception
(
IOErrorType(..)
)
import Utils
import qualified Data.ByteString as BS
import Data.ByteString.UTF8 (toString)
setupFiles :: IO ()
setupFiles = do
createRegularFile' "myFile"
createSymlink' "myFileL" "myFile"
createRegularFile' "alreadyExists"
createDir' "alreadyExistsD"
createDir' "dir"
createDir' "noPerms"
createDir' "noWritePerm"
noPerms "noPerms"
noWritableDirPerms "noWritePerm"
writeFile' "myFile" "Blahfaselgagaga"
cleanupFiles :: IO ()
cleanupFiles = do
normalDirPerms "noPerms"
normalDirPerms "noWritePerm"
deleteFile' "myFile"
deleteFile' "myFileL"
deleteFile' "alreadyExists"
deleteDir' "alreadyExistsD"
deleteDir' "dir"
deleteDir' "noPerms"
deleteDir' "noWritePerm"
spec :: Spec
spec = before_ setupFiles $ after_ cleanupFiles $
describe "HPath.IO.moveFile" $ do
-- successes --
it "moveFile, all fine" $
moveFile' "myFile"
"movedFile"
it "moveFile, all fine" $
moveFile' "myFile"
"dir/movedFile"
it "moveFile, all fine on symlink" $
moveFile' "myFileL"
"movedFile"
it "moveFile, all fine on directory" $
moveFile' "dir"
"movedFile"
-- posix failures --
it "moveFile, source file does not exist" $
moveFile' "fileDoesNotExist"
"movedFile"
`shouldThrow`
(\e -> ioeGetErrorType e == NoSuchThing)
it "moveFile, can't write to destination directory" $
moveFile' "myFile"
"noWritePerm/movedFile"
`shouldThrow`
(\e -> ioeGetErrorType e == PermissionDenied)
it "moveFile, can't open destination directory" $
moveFile' "myFile"
"noPerms/movedFile"
`shouldThrow`
(\e -> ioeGetErrorType e == PermissionDenied)
it "moveFile, can't open source directory" $
moveFile' "noPerms/myFile"
"movedFile"
`shouldThrow`
(\e -> ioeGetErrorType e == PermissionDenied)
-- custom failures --
it "moveFile, destination file already exists" $
moveFile' "myFile"
"alreadyExists"
`shouldThrow`
isFileDoesExist
it "moveFile, move from file to dir" $
moveFile' "myFile"
"alreadyExistsD"
`shouldThrow`
isDirDoesExist
it "moveFile, source and dest are same file" $
moveFile' "myFile"
"myFile"
`shouldThrow`
isSameFile