TESTS: add deleteFileSpec
This commit is contained in:
parent
9a11e35be0
commit
c84512e3b3
@ -393,6 +393,12 @@ easyCopy from to = do
|
||||
|
||||
-- |Deletes the given file, does not follow symlinks. Raises `eISDIR`
|
||||
-- if run on a directory. Does not follow symbolic links.
|
||||
--
|
||||
-- Throws:
|
||||
--
|
||||
-- - `InappropriateType` for wrong file type (directory)
|
||||
-- - `NoSuchThing` if the file does not exist
|
||||
-- - `PermissionDenied` if the directory cannot be read
|
||||
deleteFile :: Path Abs -> IO ()
|
||||
deleteFile p = P.withAbsPath p removeLink
|
||||
|
||||
|
65
test/Spec.hs
65
test/Spec.hs
@ -29,7 +29,8 @@ import System.Exit
|
||||
import System.Process
|
||||
import System.Posix.Files.ByteString
|
||||
(
|
||||
groupExecuteMode
|
||||
getSymbolicLinkStatus
|
||||
, groupExecuteMode
|
||||
, groupReadMode
|
||||
, nullFileMode
|
||||
, otherExecuteMode
|
||||
@ -59,6 +60,7 @@ main = hspec $ before_ fixPermissions $ after_ revertPermissions $ do
|
||||
, renameFileSpec
|
||||
, moveFileSpec
|
||||
, recreateSymlinkSpec
|
||||
, deleteFileSpec
|
||||
]
|
||||
|
||||
-- run all stateful tests twice to catch missing cleanups or state skew
|
||||
@ -88,6 +90,7 @@ main = hspec $ before_ fixPermissions $ after_ revertPermissions $ do
|
||||
,"test/recreateSymlinkSpec/noPerms"
|
||||
,"test/getFileTypeSpec/noPerms"
|
||||
,"test/getDirsFilesSpec/noPerms"
|
||||
, "test/deleteFileSpec/noPerms"
|
||||
]
|
||||
fixPermissions = do
|
||||
sequence_ $ fmap noWritableDirPerms noWriteDirs
|
||||
@ -585,6 +588,44 @@ getDirsFilesSpec =
|
||||
(\e -> ioeGetErrorType e == PermissionDenied)
|
||||
|
||||
|
||||
deleteFileSpec :: Spec
|
||||
deleteFileSpec =
|
||||
describe "HSFM.FileSystem.FileOperations.deleteFile" $ do
|
||||
|
||||
-- successes --
|
||||
it "deleteFile, regular file, all fine" $ do
|
||||
createRegularFile'' "test/deleteFileSpec/testFile"
|
||||
deleteFile' "test/deleteFileSpec/testFile"
|
||||
getSymbolicLinkStatus "test/deleteFileSpec/testFile"
|
||||
`shouldThrow`
|
||||
(\e -> ioeGetErrorType e == NoSuchThing)
|
||||
|
||||
it "deleteFile, symlink, all fine" $ do
|
||||
recreateSymlink'' "test/deleteFileSpec/syml"
|
||||
"test/deleteFileSpec/testFile"
|
||||
deleteFile' "test/deleteFileSpec/testFile"
|
||||
getSymbolicLinkStatus "test/deleteFileSpec/testFile"
|
||||
`shouldThrow`
|
||||
(\e -> ioeGetErrorType e == NoSuchThing)
|
||||
|
||||
-- posix failures --
|
||||
it "deleteFile, wrong file type (directory)" $
|
||||
deleteFile' "test/deleteFileSpec/dir"
|
||||
`shouldThrow`
|
||||
(\e -> ioeGetErrorType e == InappropriateType)
|
||||
|
||||
it "deleteFile, file does not exist" $
|
||||
deleteFile' "test/deleteFileSpec/doesNotExist"
|
||||
`shouldThrow`
|
||||
(\e -> ioeGetErrorType e == NoSuchThing)
|
||||
|
||||
it "deleteFile, can't read directory" $
|
||||
deleteFile' "test/deleteFileSpec/noPerms/blah"
|
||||
`shouldThrow`
|
||||
(\e -> ioeGetErrorType e == PermissionDenied)
|
||||
|
||||
|
||||
|
||||
|
||||
-----------------
|
||||
--[ Utilities ]--
|
||||
@ -639,6 +680,13 @@ createRegularFile' dest = do
|
||||
whenM (doesFileExist outputFile) (deleteFile outputFile)
|
||||
|
||||
|
||||
createRegularFile'' :: ByteString -> IO ()
|
||||
createRegularFile'' dest = do
|
||||
pwd <- fromJust <$> getEnv "PWD" >>= P.parseAbs
|
||||
outputFile <- (pwd P.</>) <$> P.parseRel dest
|
||||
createRegularFile outputFile
|
||||
|
||||
|
||||
renameFile' :: ByteString -> ByteString -> IO ()
|
||||
renameFile' inputFileP outputFileP = do
|
||||
pwd <- fromJust <$> getEnv "PWD" >>= P.parseAbs
|
||||
@ -666,6 +714,14 @@ recreateSymlink' inputFileP outputFileP = do
|
||||
whenM (doesFileExist outputFile) (deleteFile outputFile)
|
||||
|
||||
|
||||
recreateSymlink'' :: ByteString -> ByteString -> IO ()
|
||||
recreateSymlink'' inputFileP outputFileP = do
|
||||
pwd <- fromJust <$> getEnv "PWD" >>= P.parseAbs
|
||||
inputFile <- (pwd P.</>) <$> P.parseRel inputFileP
|
||||
outputFile <- (pwd P.</>) <$> P.parseRel outputFileP
|
||||
recreateSymlink inputFile outputFile
|
||||
|
||||
|
||||
noWritableDirPerms :: ByteString -> IO ()
|
||||
noWritableDirPerms path = do
|
||||
pwd <- fromJust <$> getEnv "PWD" >>= P.parseAbs
|
||||
@ -707,3 +763,10 @@ getDirsFiles' path = do
|
||||
file <- (pwd P.</>) <$> P.parseRel path
|
||||
getDirsFiles file
|
||||
|
||||
|
||||
deleteFile' :: ByteString -> IO ()
|
||||
deleteFile' p = do
|
||||
pwd <- fromJust <$> getEnv "PWD" >>= P.parseAbs
|
||||
file <- (pwd P.</>) <$> P.parseRel p
|
||||
deleteFile file
|
||||
|
||||
|
0
test/deleteFileSpec/dir/.keep
Normal file
0
test/deleteFileSpec/dir/.keep
Normal file
0
test/deleteFileSpec/foo
Normal file
0
test/deleteFileSpec/foo
Normal file
0
test/deleteFileSpec/noPerms/blah
Normal file
0
test/deleteFileSpec/noPerms/blah
Normal file
1
test/deleteFileSpec/syml
Symbolic link
1
test/deleteFileSpec/syml
Symbolic link
@ -0,0 +1 @@
|
||||
foo
|
Loading…
Reference in New Issue
Block a user