2016-05-09 14:53:31 +00:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
|
|
|
|
module HPath.IO.DeleteDirRecursiveSpec where
|
|
|
|
|
|
|
|
|
|
|
|
import Test.Hspec
|
|
|
|
import System.IO.Error
|
|
|
|
(
|
|
|
|
ioeGetErrorType
|
|
|
|
)
|
|
|
|
import System.Posix.Files.ByteString
|
|
|
|
(
|
|
|
|
getSymbolicLinkStatus
|
|
|
|
)
|
|
|
|
import GHC.IO.Exception
|
|
|
|
(
|
|
|
|
IOErrorType(..)
|
|
|
|
)
|
|
|
|
import Utils
|
|
|
|
|
|
|
|
|
2016-06-05 12:33:53 +00:00
|
|
|
|
|
|
|
upTmpDir :: IO ()
|
|
|
|
upTmpDir = do
|
|
|
|
setTmpDir "DeleteDirRecursiveSpec"
|
|
|
|
createTmpDir
|
|
|
|
|
|
|
|
|
2016-05-29 15:29:13 +00:00
|
|
|
setupFiles :: IO ()
|
|
|
|
setupFiles = do
|
|
|
|
createRegularFile' "file"
|
|
|
|
createDir' "dir"
|
|
|
|
createRegularFile' "dir/.keep"
|
|
|
|
createSymlink' "dirSym" "dir/"
|
|
|
|
createDir' "noPerms"
|
|
|
|
createRegularFile' "noPerms/.keep"
|
|
|
|
createDir' "noWritable"
|
|
|
|
createRegularFile' "noWritable/.keep"
|
|
|
|
|
|
|
|
|
|
|
|
cleanupFiles :: IO ()
|
|
|
|
cleanupFiles = do
|
|
|
|
deleteFile' "file"
|
|
|
|
deleteFile' "dir/.keep"
|
|
|
|
deleteDir' "dir"
|
|
|
|
deleteFile' "dirSym"
|
|
|
|
deleteFile' "noPerms/.keep"
|
|
|
|
deleteDir' "noPerms"
|
|
|
|
deleteFile' "noWritable/.keep"
|
|
|
|
deleteDir' "noWritable"
|
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.deleteDirRecursive" $ do
|
|
|
|
|
|
|
|
-- successes --
|
|
|
|
it "deleteDirRecursive, empty directory, all fine" $ do
|
2016-05-29 15:29:13 +00:00
|
|
|
createDir' "testDir"
|
|
|
|
deleteDirRecursive' "testDir"
|
|
|
|
getSymbolicLinkStatus "testDir"
|
2016-05-09 14:53:31 +00:00
|
|
|
`shouldThrow`
|
|
|
|
(\e -> ioeGetErrorType e == NoSuchThing)
|
|
|
|
|
|
|
|
it "deleteDirRecursive, empty directory with null permissions, all fine" $ do
|
2016-05-29 15:29:13 +00:00
|
|
|
createDir' "noPerms/testDir"
|
|
|
|
noPerms "noPerms/testDir"
|
|
|
|
deleteDirRecursive' "noPerms/testDir"
|
2016-05-09 14:53:31 +00:00
|
|
|
|
|
|
|
it "deleteDirRecursive, non-empty directory, all fine" $ do
|
2016-05-29 15:29:13 +00:00
|
|
|
createDir' "nonEmpty"
|
|
|
|
createDir' "nonEmpty/dir1"
|
|
|
|
createDir' "nonEmpty/dir2"
|
|
|
|
createDir' "nonEmpty/dir2/dir3"
|
|
|
|
createRegularFile' "nonEmpty/file1"
|
|
|
|
createRegularFile' "nonEmpty/dir1/file2"
|
|
|
|
deleteDirRecursive' "nonEmpty"
|
|
|
|
getSymbolicLinkStatus "nonEmpty"
|
2016-05-09 14:53:31 +00:00
|
|
|
`shouldThrow`
|
|
|
|
(\e -> ioeGetErrorType e == NoSuchThing)
|
|
|
|
|
|
|
|
-- posix failures --
|
|
|
|
it "deleteDirRecursive, can't open parent directory" $ do
|
2016-05-29 15:29:13 +00:00
|
|
|
createDir' "noPerms/foo"
|
|
|
|
noPerms "noPerms"
|
|
|
|
(deleteDirRecursive' "noPerms/foo")
|
2016-05-09 14:53:31 +00:00
|
|
|
`shouldThrow`
|
2016-05-29 15:29:13 +00:00
|
|
|
(\e -> ioeGetErrorType e == PermissionDenied)
|
|
|
|
normalDirPerms "noPerms"
|
|
|
|
deleteDir' "noPerms/foo"
|
2016-05-09 14:53:31 +00:00
|
|
|
|
|
|
|
it "deleteDirRecursive, can't write to parent directory" $ do
|
2016-05-29 15:29:13 +00:00
|
|
|
createDir' "noWritable/foo"
|
|
|
|
noWritableDirPerms "noWritable"
|
|
|
|
(deleteDirRecursive' "noWritable/foo")
|
2016-05-09 14:53:31 +00:00
|
|
|
`shouldThrow`
|
2016-05-29 15:29:13 +00:00
|
|
|
(\e -> ioeGetErrorType e == PermissionDenied)
|
|
|
|
normalDirPerms "noWritable"
|
|
|
|
deleteDir' "noWritable/foo"
|
2016-05-09 14:53:31 +00:00
|
|
|
|
|
|
|
it "deleteDirRecursive, wrong file type (symlink to directory)" $
|
2016-05-29 15:29:13 +00:00
|
|
|
deleteDirRecursive' "dirSym"
|
2016-05-09 14:53:31 +00:00
|
|
|
`shouldThrow`
|
|
|
|
(\e -> ioeGetErrorType e == InappropriateType)
|
|
|
|
|
|
|
|
it "deleteDirRecursive, wrong file type (regular file)" $
|
2016-05-29 15:29:13 +00:00
|
|
|
deleteDirRecursive' "file"
|
2016-05-09 14:53:31 +00:00
|
|
|
`shouldThrow`
|
|
|
|
(\e -> ioeGetErrorType e == InappropriateType)
|
|
|
|
|
|
|
|
it "deleteDirRecursive, directory does not exist" $
|
2016-05-29 15:29:13 +00:00
|
|
|
deleteDirRecursive' "doesNotExist"
|
2016-05-09 14:53:31 +00:00
|
|
|
`shouldThrow`
|
|
|
|
(\e -> ioeGetErrorType e == NoSuchThing)
|
|
|
|
|
|
|
|
|