Skip to content

Commit a7014ec

Browse files
committed
Added tests for ReadDirStream
1 parent 42ecbf5 commit a7014ec

File tree

3 files changed

+59
-0
lines changed

3 files changed

+59
-0
lines changed

tests/ReadDirStream.hs

+49
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.hs

+9
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import Test.Tasty.HUnit
1919

2020
import qualified FileStatus
2121
import qualified FileStatusByteString
22+
import qualified ReadDirStream
2223
import qualified Signals001
2324

2425
main :: IO ()
@@ -42,6 +43,8 @@ main = defaultMain $ testGroup "All"
4243
, posix005
4344
, posix006
4445
, posix010
46+
, emptyDirStream
47+
, nonEmptyDirStream
4548
]
4649

4750
executeFile001 :: TestTree
@@ -225,6 +228,12 @@ posix010 = testCase "posix010" $ do
225228

226229
homeDirectory root @?= homeDirectory root'
227230

231+
emptyDirStream :: TestTree
232+
emptyDirStream = testCase "emptyDirStream" ReadDirStream.emptyDirStream
233+
234+
nonEmptyDirStream :: TestTree
235+
nonEmptyDirStream = testCase "nonEmptyDirStream" ReadDirStream.nonEmptyDirStream
236+
228237
-------------------------------------------------------------------------------
229238
-- Utils
230239

unix.cabal

+1
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ test-suite unix-tests
162162
FileStatus
163163
FileStatusByteString
164164
Signals001
165+
ReadDirStream
165166
type: exitcode-stdio-1.0
166167
default-language: Haskell2010
167168
build-depends: base, tasty, tasty-hunit, unix

0 commit comments

Comments
 (0)