2016-05-09 14:53:31 +00:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
|
|
|
|
module HPath.IO.RecreateSymlinkSpec where
|
|
|
|
|
|
|
|
|
2016-06-05 01:10:28 +00:00
|
|
|
|
|
|
|
|
2016-05-09 14:53:31 +00:00
|
|
|
import Test.Hspec
|
2016-06-05 01:10:28 +00:00
|
|
|
import HPath.IO
|
2016-05-09 14:53:31 +00:00
|
|
|
import HPath.IO.Errors
|
|
|
|
import System.IO.Error
|
|
|
|
(
|
|
|
|
ioeGetErrorType
|
|
|
|
)
|
|
|
|
import GHC.IO.Exception
|
|
|
|
(
|
|
|
|
IOErrorType(..)
|
|
|
|
)
|
|
|
|
import Utils
|
|
|
|
|
|
|
|
|
2016-06-05 12:33:53 +00:00
|
|
|
|
|
|
|
upTmpDir :: IO ()
|
|
|
|
upTmpDir = do
|
|
|
|
setTmpDir "RecreateSymlinkSpec"
|
|
|
|
createTmpDir
|
|
|
|
|
|
|
|
|
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-06-05 13:25:57 +00:00
|
|
|
spec = beforeAll_ (upTmpDir >> setupFiles) $ afterAll_ cleanupFiles $
|
2016-05-09 14:53:31 +00:00
|
|
|
describe "HPath.IO.recreateSymlink" $ do
|
|
|
|
|
|
|
|
-- successes --
|
2016-06-05 01:10:28 +00:00
|
|
|
it "recreateSymLink (Strict), all fine" $ do
|
2016-05-29 15:29:13 +00:00
|
|
|
recreateSymlink' "myFileL"
|
|
|
|
"movedFile"
|
2016-06-05 01:10:28 +00:00
|
|
|
Strict
|
2016-05-29 15:29:13 +00:00
|
|
|
removeFileIfExists "movedFile"
|
2016-05-09 14:53:31 +00:00
|
|
|
|
2016-06-05 01:10:28 +00:00
|
|
|
it "recreateSymLink (Strict), all fine" $ do
|
2016-05-29 15:29:13 +00:00
|
|
|
recreateSymlink' "myFileL"
|
|
|
|
"dir/movedFile"
|
2016-06-05 01:10:28 +00:00
|
|
|
Strict
|
2016-05-29 15:29:13 +00:00
|
|
|
removeFileIfExists "dir/movedFile"
|
2016-05-09 14:53:31 +00:00
|
|
|
|
|
|
|
-- posix failures --
|
2016-06-05 01:10:28 +00:00
|
|
|
it "recreateSymLink (Strict), wrong input type (file)" $
|
2016-05-29 15:29:13 +00:00
|
|
|
recreateSymlink' "myFile"
|
|
|
|
"movedFile"
|
2016-06-05 01:10:28 +00:00
|
|
|
Strict
|
2016-05-09 14:53:31 +00:00
|
|
|
`shouldThrow`
|
|
|
|
(\e -> ioeGetErrorType e == InvalidArgument)
|
|
|
|
|
2016-06-05 01:10:28 +00:00
|
|
|
it "recreateSymLink (Strict), wrong input type (directory)" $
|
2016-05-29 15:29:13 +00:00
|
|
|
recreateSymlink' "dir"
|
|
|
|
"movedFile"
|
2016-06-05 01:10:28 +00:00
|
|
|
Strict
|
2016-05-09 14:53:31 +00:00
|
|
|
`shouldThrow`
|
|
|
|
(\e -> ioeGetErrorType e == InvalidArgument)
|
|
|
|
|
2016-06-05 01:10:28 +00:00
|
|
|
it "recreateSymLink (Strict), can't write to destination directory" $
|
2016-05-29 15:29:13 +00:00
|
|
|
recreateSymlink' "myFileL"
|
|
|
|
"noWritePerm/movedFile"
|
2016-06-05 01:10:28 +00:00
|
|
|
Strict
|
2016-05-09 14:53:31 +00:00
|
|
|
`shouldThrow`
|
|
|
|
(\e -> ioeGetErrorType e == PermissionDenied)
|
|
|
|
|
2016-06-05 01:10:28 +00:00
|
|
|
it "recreateSymLink (Strict), can't open destination directory" $
|
2016-05-29 15:29:13 +00:00
|
|
|
recreateSymlink' "myFileL"
|
|
|
|
"noPerms/movedFile"
|
2016-06-05 01:10:28 +00:00
|
|
|
Strict
|
2016-05-09 14:53:31 +00:00
|
|
|
`shouldThrow`
|
|
|
|
(\e -> ioeGetErrorType e == PermissionDenied)
|
|
|
|
|
2016-06-05 01:10:28 +00:00
|
|
|
it "recreateSymLink (Strict), can't open source directory" $
|
2016-05-29 15:29:13 +00:00
|
|
|
recreateSymlink' "noPerms/myFileL"
|
|
|
|
"movedFile"
|
2016-06-05 01:10:28 +00:00
|
|
|
Strict
|
2016-05-09 14:53:31 +00:00
|
|
|
`shouldThrow`
|
|
|
|
(\e -> ioeGetErrorType e == PermissionDenied)
|
|
|
|
|
2016-06-05 01:10:28 +00:00
|
|
|
it "recreateSymLink (Strict), destination file already exists" $
|
2016-05-29 15:29:13 +00:00
|
|
|
recreateSymlink' "myFileL"
|
|
|
|
"alreadyExists"
|
2016-06-05 01:10:28 +00:00
|
|
|
Strict
|
2016-05-09 14:53:31 +00:00
|
|
|
`shouldThrow`
|
|
|
|
(\e -> ioeGetErrorType e == AlreadyExists)
|
|
|
|
|
2016-06-05 01:10:28 +00:00
|
|
|
it "recreateSymLink (Strict), destination already exists and is a dir" $
|
2016-05-29 15:29:13 +00:00
|
|
|
recreateSymlink' "myFileL"
|
|
|
|
"alreadyExistsD"
|
2016-06-05 01:10:28 +00:00
|
|
|
Strict
|
2016-05-09 14:53:31 +00:00
|
|
|
`shouldThrow`
|
|
|
|
(\e -> ioeGetErrorType e == AlreadyExists)
|
|
|
|
|
|
|
|
-- custom failures --
|
2016-06-05 01:10:28 +00:00
|
|
|
it "recreateSymLink (Strict), source and destination are the same file" $
|
2016-05-29 15:29:13 +00:00
|
|
|
recreateSymlink' "myFileL"
|
|
|
|
"myFileL"
|
2016-06-05 01:10:28 +00:00
|
|
|
Strict
|
2016-05-09 14:53:31 +00:00
|
|
|
`shouldThrow`
|
|
|
|
isSameFile
|
|
|
|
|