TESTS: add getFileTypeSpec
This commit is contained in:
parent
ac41b053e3
commit
5670b160d8
@ -184,7 +184,7 @@ data FileType = Directory
|
||||
| CharacterDevice
|
||||
| NamedPipe
|
||||
| Socket
|
||||
deriving (Show)
|
||||
deriving (Eq, Show)
|
||||
|
||||
|
||||
|
||||
@ -614,6 +614,13 @@ getDirsFiles p =
|
||||
---------------------------
|
||||
|
||||
|
||||
-- |Get the file type of the file located at the given path. Does
|
||||
-- not follow symbolic links.
|
||||
--
|
||||
-- Throws:
|
||||
--
|
||||
-- - `NoSuchThing` if the file does not exist
|
||||
-- - `PermissionDenied` if any part of the path is not accessible
|
||||
getFileType :: Path Abs -> IO FileType
|
||||
getFileType p = do
|
||||
fs <- PF.getSymbolicLinkStatus (P.fromAbs p)
|
||||
|
54
test/Spec.hs
54
test/Spec.hs
@ -42,6 +42,7 @@ import System.Posix.Files.ByteString
|
||||
|
||||
|
||||
|
||||
|
||||
-- TODO: chardev, blockdev, namedpipe, socket
|
||||
|
||||
|
||||
@ -54,9 +55,10 @@ main = hspec $ before_ fixPermissions $ after_ revertPermissions $ do
|
||||
, renameFileSpec
|
||||
, moveFileSpec
|
||||
, recreateSymlinkSpec
|
||||
, getFileTypeSpec
|
||||
]
|
||||
|
||||
-- run all tests twice to catch missing cleanups or state skew
|
||||
-- run all stateful tests twice to catch missing cleanups or state skew
|
||||
sequence_ tests
|
||||
sequence_ tests
|
||||
|
||||
@ -77,6 +79,7 @@ main = hspec $ before_ fixPermissions $ after_ revertPermissions $ do
|
||||
,"test/renameFile/noPerms"
|
||||
,"test/moveFile/noPerms"
|
||||
,"test/recreateSymlinkSpec/noPerms"
|
||||
,"test/getFileTypeSpec/noPerms"
|
||||
]
|
||||
fixPermissions = do
|
||||
sequence_ $ fmap noWritableDirPerms noWriteDirs
|
||||
@ -489,6 +492,49 @@ recreateSymlinkSpec =
|
||||
(\e -> ioeGetErrorType e == AlreadyExists)
|
||||
|
||||
|
||||
getFileTypeSpec :: Spec
|
||||
getFileTypeSpec =
|
||||
describe "HSFM.FileSystem.FileOperations.getFileType" $ do
|
||||
|
||||
-- successes --
|
||||
it "getFileType, regular file" $
|
||||
getFileType' "test/getFileTypeSpec/regularfile"
|
||||
`shouldReturn` RegularFile
|
||||
|
||||
it "getFileType, directory" $
|
||||
getFileType' "test/getFileTypeSpec/directory"
|
||||
`shouldReturn` Directory
|
||||
|
||||
it "getFileType, directory with null permissions" $
|
||||
getFileType' "test/getFileTypeSpec/noPerms"
|
||||
`shouldReturn` Directory
|
||||
|
||||
it "getFileType, symlink to file" $
|
||||
getFileType' "test/getFileTypeSpec/symlink"
|
||||
`shouldReturn` SymbolicLink
|
||||
|
||||
it "getFileType, symlink to directory" $
|
||||
getFileType' "test/getFileTypeSpec/symlinkD"
|
||||
`shouldReturn` SymbolicLink
|
||||
|
||||
it "getFileType, broken symlink" $
|
||||
getFileType' "test/getFileTypeSpec/brokenSymlink"
|
||||
`shouldReturn` SymbolicLink
|
||||
|
||||
-- posix failures --
|
||||
it "getFileType, file does not exist" $
|
||||
getFileType' "test/getFileTypeSpec/nothingHere"
|
||||
`shouldThrow`
|
||||
(\e -> ioeGetErrorType e == NoSuchThing)
|
||||
|
||||
it "getFileType, can't open directory" $
|
||||
getFileType' "test/getFileTypeSpec/noPerms/forz"
|
||||
`shouldThrow`
|
||||
(\e -> ioeGetErrorType e == PermissionDenied)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
-----------------
|
||||
--[ Utilities ]--
|
||||
@ -597,3 +643,9 @@ normalDirPerms path = do
|
||||
file <- (pwd P.</>) <$> P.parseRel path
|
||||
setFileMode (P.fromAbs file) newDirPerms
|
||||
|
||||
|
||||
getFileType' :: ByteString -> IO FileType
|
||||
getFileType' path = do
|
||||
pwd <- fromJust <$> getEnv "PWD" >>= P.parseAbs
|
||||
file <- (pwd P.</>) <$> P.parseRel path
|
||||
getFileType file
|
||||
|
1
test/getFileTypeSpec/brokenSymlink
Symbolic link
1
test/getFileTypeSpec/brokenSymlink
Symbolic link
@ -0,0 +1 @@
|
||||
broken
|
0
test/getFileTypeSpec/directory/.keep
Normal file
0
test/getFileTypeSpec/directory/.keep
Normal file
0
test/getFileTypeSpec/noPerms/.keep
Normal file
0
test/getFileTypeSpec/noPerms/.keep
Normal file
0
test/getFileTypeSpec/regularfile
Normal file
0
test/getFileTypeSpec/regularfile
Normal file
1
test/getFileTypeSpec/symlink
Symbolic link
1
test/getFileTypeSpec/symlink
Symbolic link
@ -0,0 +1 @@
|
||||
regularfile
|
1
test/getFileTypeSpec/symlinkD
Symbolic link
1
test/getFileTypeSpec/symlinkD
Symbolic link
@ -0,0 +1 @@
|
||||
directory
|
Loading…
Reference in New Issue
Block a user