2016-05-09 14:53:31 +00:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
|
|
|
|
module HPath.IO.GetDirsFilesSpec where
|
|
|
|
|
|
|
|
|
2016-05-09 16:53:26 +00:00
|
|
|
import Control.Applicative
|
|
|
|
(
|
|
|
|
(<$>)
|
|
|
|
)
|
2016-05-09 14:53:31 +00:00
|
|
|
import Data.List
|
|
|
|
(
|
|
|
|
sort
|
|
|
|
)
|
|
|
|
import Data.Maybe
|
|
|
|
(
|
|
|
|
fromJust
|
|
|
|
)
|
|
|
|
import qualified HPath as P
|
2016-05-29 15:29:13 +00:00
|
|
|
import HPath.IO
|
2016-05-09 14:53:31 +00:00
|
|
|
import Test.Hspec
|
|
|
|
import System.IO.Error
|
|
|
|
(
|
|
|
|
ioeGetErrorType
|
|
|
|
)
|
|
|
|
import System.Posix.Env.ByteString
|
|
|
|
(
|
|
|
|
getEnv
|
|
|
|
)
|
|
|
|
import GHC.IO.Exception
|
|
|
|
(
|
|
|
|
IOErrorType(..)
|
|
|
|
)
|
|
|
|
import Utils
|
|
|
|
import qualified Data.ByteString as BS
|
|
|
|
import Data.ByteString.UTF8 (toString)
|
|
|
|
|
|
|
|
|
2016-05-29 15:29:13 +00:00
|
|
|
setupFiles :: IO ()
|
|
|
|
setupFiles = do
|
|
|
|
createRegularFile' "file"
|
|
|
|
createRegularFile' "Lala"
|
|
|
|
createRegularFile' ".hidden"
|
|
|
|
createSymlink' "syml" "Lala"
|
|
|
|
createDir' "dir"
|
|
|
|
createSymlink' "dirsym" "dir"
|
|
|
|
createDir' "noPerms"
|
|
|
|
noPerms "noPerms"
|
2016-05-09 14:53:31 +00:00
|
|
|
|
|
|
|
|
2016-05-29 15:29:13 +00:00
|
|
|
cleanupFiles :: IO ()
|
|
|
|
cleanupFiles = do
|
|
|
|
normalDirPerms "noPerms"
|
|
|
|
deleteFile' "file"
|
|
|
|
deleteFile' "Lala"
|
|
|
|
deleteFile' ".hidden"
|
|
|
|
deleteFile' "syml"
|
|
|
|
deleteDir' "dir"
|
|
|
|
deleteFile' "dirsym"
|
|
|
|
deleteDir' "noPerms"
|
|
|
|
|
2016-05-09 14:53:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
spec :: Spec
|
2016-05-29 15:29:13 +00:00
|
|
|
spec = before_ setupFiles $ after_ cleanupFiles $
|
2016-05-09 14:53:31 +00:00
|
|
|
describe "HPath.IO.getDirsFiles" $ do
|
|
|
|
|
|
|
|
-- successes --
|
2016-05-29 15:29:13 +00:00
|
|
|
it "getDirsFiles, all fine" $
|
|
|
|
withRawTmpDir $ \p -> do
|
|
|
|
expectedFiles <- mapM P.parseRel [".hidden"
|
|
|
|
,"Lala"
|
|
|
|
,"dir"
|
|
|
|
,"dirsym"
|
|
|
|
,"file"
|
|
|
|
,"noPerms"
|
|
|
|
,"syml"]
|
|
|
|
(fmap sort $ getDirsFiles p)
|
|
|
|
`shouldReturn` fmap (p P.</>) expectedFiles
|
2016-05-09 14:53:31 +00:00
|
|
|
|
|
|
|
-- posix failures --
|
|
|
|
it "getDirsFiles, nonexistent directory" $
|
2016-05-29 15:29:13 +00:00
|
|
|
getDirsFiles' "nothingHere"
|
2016-05-09 14:53:31 +00:00
|
|
|
`shouldThrow`
|
|
|
|
(\e -> ioeGetErrorType e == NoSuchThing)
|
|
|
|
|
|
|
|
it "getDirsFiles, wrong file type (file)" $
|
2016-05-29 15:29:13 +00:00
|
|
|
getDirsFiles' "file"
|
2016-05-09 14:53:31 +00:00
|
|
|
`shouldThrow`
|
|
|
|
(\e -> ioeGetErrorType e == InappropriateType)
|
|
|
|
|
|
|
|
it "getDirsFiles, wrong file type (symlink to file)" $
|
2016-05-29 15:29:13 +00:00
|
|
|
getDirsFiles' "syml"
|
2016-05-09 14:53:31 +00:00
|
|
|
`shouldThrow`
|
|
|
|
(\e -> ioeGetErrorType e == InvalidArgument)
|
|
|
|
|
|
|
|
it "getDirsFiles, wrong file type (symlink to dir)" $
|
2016-05-29 15:29:13 +00:00
|
|
|
getDirsFiles' "dirsym"
|
2016-05-09 14:53:31 +00:00
|
|
|
`shouldThrow`
|
|
|
|
(\e -> ioeGetErrorType e == InvalidArgument)
|
|
|
|
|
|
|
|
it "getDirsFiles, can't open directory" $
|
2016-05-29 15:29:13 +00:00
|
|
|
getDirsFiles' "noPerms"
|
2016-05-09 14:53:31 +00:00
|
|
|
`shouldThrow`
|
|
|
|
(\e -> ioeGetErrorType e == PermissionDenied)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|