2016-05-03 11:27:10 +00:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
|
|
|
|
module FileSystem.FileOperations.DeleteDirSpec where
|
|
|
|
|
|
|
|
|
|
|
|
import Test.Hspec
|
|
|
|
import System.IO.Error
|
|
|
|
(
|
|
|
|
ioeGetErrorType
|
|
|
|
)
|
|
|
|
import System.Posix.Files.ByteString
|
|
|
|
(
|
|
|
|
getSymbolicLinkStatus
|
|
|
|
)
|
|
|
|
import GHC.IO.Exception
|
|
|
|
(
|
|
|
|
IOErrorType(..)
|
|
|
|
)
|
|
|
|
import Utils
|
2016-05-08 22:16:26 +00:00
|
|
|
import qualified Data.ByteString as BS
|
|
|
|
import Data.ByteString.UTF8 (toString)
|
|
|
|
|
|
|
|
|
|
|
|
ba :: BS.ByteString -> BS.ByteString -> BS.ByteString
|
|
|
|
ba = BS.append
|
|
|
|
|
|
|
|
specDir :: BS.ByteString
|
|
|
|
specDir = "test/FileSystem/FileOperations/deleteDirSpec/"
|
|
|
|
|
|
|
|
specDir' :: String
|
|
|
|
specDir' = toString specDir
|
2016-05-03 11:27:10 +00:00
|
|
|
|
|
|
|
|
2016-05-08 21:45:51 +00:00
|
|
|
spec :: Spec
|
|
|
|
spec =
|
2016-05-03 11:27:10 +00:00
|
|
|
describe "HSFM.FileSystem.FileOperations.deleteDir" $ do
|
|
|
|
|
|
|
|
-- successes --
|
|
|
|
it "deleteDir, empty directory, all fine" $ do
|
2016-05-08 22:16:26 +00:00
|
|
|
createDir' (specDir `ba` "testDir")
|
|
|
|
deleteDir' (specDir `ba` "testDir")
|
|
|
|
getSymbolicLinkStatus (specDir `ba` "testDir")
|
2016-05-03 11:27:10 +00:00
|
|
|
`shouldThrow`
|
|
|
|
(\e -> ioeGetErrorType e == NoSuchThing)
|
|
|
|
|
|
|
|
it "deleteDir, directory with null permissions, all fine" $ do
|
2016-05-08 22:16:26 +00:00
|
|
|
createDir' (specDir `ba` "noPerms/testDir")
|
|
|
|
noPerms (specDir `ba` "noPerms/testDir")
|
|
|
|
deleteDir' (specDir `ba` "noPerms/testDir")
|
|
|
|
getSymbolicLinkStatus (specDir `ba` "testDir")
|
2016-05-03 11:27:10 +00:00
|
|
|
`shouldThrow`
|
|
|
|
(\e -> ioeGetErrorType e == NoSuchThing)
|
|
|
|
|
|
|
|
-- posix failures --
|
|
|
|
it "deleteDir, wrong file type (symlink to directory)" $
|
2016-05-08 22:16:26 +00:00
|
|
|
deleteDir' (specDir `ba` "dirSym")
|
2016-05-03 11:27:10 +00:00
|
|
|
`shouldThrow`
|
|
|
|
(\e -> ioeGetErrorType e == InappropriateType)
|
|
|
|
|
|
|
|
it "deleteDir, wrong file type (regular file)" $
|
2016-05-08 22:16:26 +00:00
|
|
|
deleteDir' (specDir `ba` "file")
|
2016-05-03 11:27:10 +00:00
|
|
|
`shouldThrow`
|
|
|
|
(\e -> ioeGetErrorType e == InappropriateType)
|
|
|
|
|
|
|
|
it "deleteDir, directory does not exist" $
|
2016-05-08 22:16:26 +00:00
|
|
|
deleteDir' (specDir `ba` "doesNotExist")
|
2016-05-03 11:27:10 +00:00
|
|
|
`shouldThrow`
|
|
|
|
(\e -> ioeGetErrorType e == NoSuchThing)
|
|
|
|
|
|
|
|
it "deleteDir, directory not empty" $
|
2016-05-08 22:16:26 +00:00
|
|
|
deleteDir' (specDir `ba` "dir")
|
2016-05-03 11:27:10 +00:00
|
|
|
`shouldThrow`
|
|
|
|
(\e -> ioeGetErrorType e == UnsatisfiedConstraints)
|
|
|
|
|
|
|
|
it "deleteDir, can't open parent directory" $ do
|
2016-05-08 22:16:26 +00:00
|
|
|
createDir' (specDir `ba` "noPerms/foo")
|
|
|
|
noPerms (specDir `ba` "noPerms")
|
|
|
|
(deleteDir' (specDir `ba` "noPerms/foo")
|
2016-05-03 11:27:10 +00:00
|
|
|
`shouldThrow`
|
|
|
|
(\e -> ioeGetErrorType e == PermissionDenied))
|
2016-05-08 22:16:26 +00:00
|
|
|
>> normalDirPerms (specDir `ba` "noPerms")
|
|
|
|
>> deleteDir' (specDir `ba` "noPerms/foo")
|
2016-05-03 11:27:10 +00:00
|
|
|
|
|
|
|
it "deleteDir, can't write to parent directory, still fine" $ do
|
2016-05-08 22:16:26 +00:00
|
|
|
createDir' (specDir `ba` "noWritable/foo")
|
|
|
|
noWritableDirPerms (specDir `ba` "noWritable")
|
|
|
|
(deleteDir' (specDir `ba` "noWritable/foo")
|
2016-05-03 11:27:10 +00:00
|
|
|
`shouldThrow`
|
|
|
|
(\e -> ioeGetErrorType e == PermissionDenied))
|
2016-05-08 22:16:26 +00:00
|
|
|
normalDirPerms (specDir `ba` "noWritable")
|
|
|
|
deleteDir' (specDir `ba` "noWritable/foo")
|
2016-05-03 11:27:10 +00:00
|
|
|
|
|
|
|
|
|
|
|
|