Skip to content

Commit dab3450

Browse files
mmhathasufell
authored andcommitted
Added tests for ReadDirStream
1 parent 8183e05 commit dab3450

File tree

3 files changed

+63
-0
lines changed

3 files changed

+63
-0
lines changed

tests/ReadDirStream.hs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
module ReadDirStream
2+
( emptyDirStream
3+
, nonEmptyDirStream
4+
) where
5+
6+
import System.Posix.Files
7+
import System.Posix.Directory
8+
import System.Posix.IO
9+
import Control.Exception as E
10+
import Test.Tasty.HUnit
11+
12+
dir :: FilePath
13+
dir = "dir"
14+
15+
emptyDirStream :: IO ()
16+
emptyDirStream = do
17+
cleanup
18+
createDirectory dir ownerReadMode
19+
dir_p <- openDirStream dir
20+
_ <- readDirStreamMaybe dir_p -- Just "."
21+
_ <- readDirStreamMaybe dir_p -- Just ".."
22+
ment <- readDirStreamMaybe dir_p
23+
closeDirStream dir_p
24+
cleanup
25+
ment @?= Nothing
26+
27+
nonEmptyDirStream :: IO ()
28+
nonEmptyDirStream = do
29+
cleanup
30+
createDirectory dir ownerModes
31+
_ <- createFile (dir ++ "/file") ownerReadMode
32+
dir_p <- openDirStream dir
33+
-- We read three entries here since "." and "." are included in the dirstream
34+
one <- readDirStreamMaybe dir_p
35+
two <- readDirStreamMaybe dir_p
36+
three <- readDirStreamMaybe dir_p
37+
let ment = maximum [one, two, three]
38+
closeDirStream dir_p
39+
cleanup
40+
ment @?= Just "file"
41+
42+
cleanup :: IO ()
43+
cleanup = do
44+
ignoreIOExceptions $ removeLink $ dir ++ "/file"
45+
ignoreIOExceptions $ removeDirectory dir
46+
47+
ignoreIOExceptions :: IO () -> IO ()
48+
ignoreIOExceptions io = io `E.catch`
49+
((\_ -> return ()) :: E.IOException -> IO ())

tests/Test.hsc

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import Test.Tasty.QuickCheck
2929
import qualified FileStatus
3030
import qualified FileExtendedStatus
3131
import qualified FileStatusByteString
32+
import qualified ReadDirStream
3233
import qualified Signals001
3334

3435
main :: IO ()
@@ -59,6 +60,9 @@ main = defaultMain $ testGroup "All"
5960
, posix005 -- JS: missing "environ"
6061
, posix006 -- JS: missing "time"
6162
, posix010 -- JS: missing "sysconf"
63+
, emptyDirStream
64+
, nonEmptyDirStream
65+
, dirStreamWithTypes
6266
]
6367
#endif
6468
, testWithFilePath
@@ -275,6 +279,15 @@ testWithFilePath =
275279
(\ptr -> (=== ys) <$> Sh.packCString ptr)
276280
]
277281

282+
emptyDirStream :: TestTree
283+
emptyDirStream = testCase "emptyDirStream" ReadDirStream.emptyDirStream
284+
285+
nonEmptyDirStream :: TestTree
286+
nonEmptyDirStream = testCase "nonEmptyDirStream" ReadDirStream.nonEmptyDirStream
287+
288+
dirStreamWithTypes :: TestTree
289+
dirStreamWithTypes = testCase "dirStreamWithTypes" ReadDirStream.dirStreamWithTypes
290+
278291
-------------------------------------------------------------------------------
279292
-- Utils
280293

unix.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ test-suite unix-tests
179179
FileExtendedStatus
180180
FileStatusByteString
181181
Signals001
182+
ReadDirStream
182183
type: exitcode-stdio-1.0
183184
default-language: Haskell2010
184185
build-depends: base, bytestring, tasty, tasty-hunit, tasty-quickcheck, unix

0 commit comments

Comments
 (0)