hpath/test/HPath/IO/RenameFileSpec.hs

113 lines
2.7 KiB
Haskell
Raw Normal View History

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