2016-05-09 14:53:31 +00:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
|
|
|
|
module HPath.IO.RecreateSymlinkSpec 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)
|
|
|
|
|
|
|
|
|
2016-05-29 15:29:13 +00:00
|
|
|
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
|
2016-05-29 15:29:13 +00:00
|
|
|
spec = before_ setupFiles $ after_ cleanupFiles $
|
2016-05-09 14:53:31 +00:00
|
|
|
describe "HPath.IO.recreateSymlink" $ do
|
|
|
|
|
|
|
|
-- successes --
|
|
|
|
it "recreateSymLink, all fine" $ do
|
2016-05-29 15:29:13 +00:00
|
|
|
recreateSymlink' "myFileL"
|
|
|
|
"movedFile"
|
|
|
|
removeFileIfExists "movedFile"
|
2016-05-09 14:53:31 +00:00
|
|
|
|
|
|
|
it "recreateSymLink, all fine" $ do
|
2016-05-29 15:29:13 +00:00
|
|
|
recreateSymlink' "myFileL"
|
|
|
|
"dir/movedFile"
|
|
|
|
removeFileIfExists "dir/movedFile"
|
2016-05-09 14:53:31 +00:00
|
|
|
|
|
|
|
-- posix failures --
|
|
|
|
it "recreateSymLink, wrong input type (file)" $
|
2016-05-29 15:29:13 +00:00
|
|
|
recreateSymlink' "myFile"
|
|
|
|
"movedFile"
|
2016-05-09 14:53:31 +00:00
|
|
|
`shouldThrow`
|
|
|
|
(\e -> ioeGetErrorType e == InvalidArgument)
|
|
|
|
|
|
|
|
it "recreateSymLink, wrong input type (directory)" $
|
2016-05-29 15:29:13 +00:00
|
|
|
recreateSymlink' "dir"
|
|
|
|
"movedFile"
|
2016-05-09 14:53:31 +00:00
|
|
|
`shouldThrow`
|
|
|
|
(\e -> ioeGetErrorType e == InvalidArgument)
|
|
|
|
|
|
|
|
it "recreateSymLink, can't write to destination directory" $
|
2016-05-29 15:29:13 +00:00
|
|
|
recreateSymlink' "myFileL"
|
|
|
|
"noWritePerm/movedFile"
|
2016-05-09 14:53:31 +00:00
|
|
|
`shouldThrow`
|
|
|
|
(\e -> ioeGetErrorType e == PermissionDenied)
|
|
|
|
|
|
|
|
it "recreateSymLink, can't open destination directory" $
|
2016-05-29 15:29:13 +00:00
|
|
|
recreateSymlink' "myFileL"
|
|
|
|
"noPerms/movedFile"
|
2016-05-09 14:53:31 +00:00
|
|
|
`shouldThrow`
|
|
|
|
(\e -> ioeGetErrorType e == PermissionDenied)
|
|
|
|
|
|
|
|
it "recreateSymLink, can't open source directory" $
|
2016-05-29 15:29:13 +00:00
|
|
|
recreateSymlink' "noPerms/myFileL"
|
|
|
|
"movedFile"
|
2016-05-09 14:53:31 +00:00
|
|
|
`shouldThrow`
|
|
|
|
(\e -> ioeGetErrorType e == PermissionDenied)
|
|
|
|
|
|
|
|
it "recreateSymLink, destination file already exists" $
|
2016-05-29 15:29:13 +00:00
|
|
|
recreateSymlink' "myFileL"
|
|
|
|
"alreadyExists"
|
2016-05-09 14:53:31 +00:00
|
|
|
`shouldThrow`
|
|
|
|
(\e -> ioeGetErrorType e == AlreadyExists)
|
|
|
|
|
|
|
|
it "recreateSymLink, destination already exists and is a dir" $
|
2016-05-29 15:29:13 +00:00
|
|
|
recreateSymlink' "myFileL"
|
|
|
|
"alreadyExistsD"
|
2016-05-09 14:53:31 +00:00
|
|
|
`shouldThrow`
|
|
|
|
(\e -> ioeGetErrorType e == AlreadyExists)
|
|
|
|
|
|
|
|
-- custom failures --
|
|
|
|
it "recreateSymLink, source and destination are the same file" $
|
2016-05-29 15:29:13 +00:00
|
|
|
recreateSymlink' "myFileL"
|
|
|
|
"myFileL"
|
2016-05-09 14:53:31 +00:00
|
|
|
`shouldThrow`
|
|
|
|
isSameFile
|
|
|
|
|