hpath/test/HPath/IO/MoveFileSpec.hs

114 lines
2.6 KiB
Haskell
Raw Normal View History

2016-05-09 14:53:31 +00:00
{-# 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"
2016-05-09 14:53:31 +00:00
spec :: Spec
spec = before_ setupFiles $ after_ cleanupFiles $
2016-05-09 14:53:31 +00:00
describe "HPath.IO.moveFile" $ do
-- successes --
it "moveFile, all fine" $
moveFile' "myFile"
"movedFile"
2016-05-09 14:53:31 +00:00
it "moveFile, all fine" $
moveFile' "myFile"
"dir/movedFile"
2016-05-09 14:53:31 +00:00
it "moveFile, all fine on symlink" $
moveFile' "myFileL"
"movedFile"
2016-05-09 14:53:31 +00:00
it "moveFile, all fine on directory" $
moveFile' "dir"
"movedFile"
2016-05-09 14:53:31 +00:00
-- posix failures --
it "moveFile, source file does not exist" $
moveFile' "fileDoesNotExist"
"movedFile"
2016-05-09 14:53:31 +00:00
`shouldThrow`
(\e -> ioeGetErrorType e == NoSuchThing)
it "moveFile, can't write to destination directory" $
moveFile' "myFile"
"noWritePerm/movedFile"
2016-05-09 14:53:31 +00:00
`shouldThrow`
(\e -> ioeGetErrorType e == PermissionDenied)
it "moveFile, can't open destination directory" $
moveFile' "myFile"
"noPerms/movedFile"
2016-05-09 14:53:31 +00:00
`shouldThrow`
(\e -> ioeGetErrorType e == PermissionDenied)
it "moveFile, can't open source directory" $
moveFile' "noPerms/myFile"
"movedFile"
2016-05-09 14:53:31 +00:00
`shouldThrow`
(\e -> ioeGetErrorType e == PermissionDenied)
-- custom failures --
it "moveFile, destination file already exists" $
moveFile' "myFile"
"alreadyExists"
2016-05-09 14:53:31 +00:00
`shouldThrow`
isFileDoesExist
it "moveFile, move from file to dir" $
moveFile' "myFile"
"alreadyExistsD"
2016-05-09 14:53:31 +00:00
`shouldThrow`
isDirDoesExist
it "moveFile, source and dest are same file" $
moveFile' "myFile"
"myFile"
2016-05-09 14:53:31 +00:00
`shouldThrow`
isSameFile