File tree 3 files changed +59
-0
lines changed
3 files changed +59
-0
lines changed Original file line number Diff line number Diff line change
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 () )
Original file line number Diff line number Diff line change @@ -19,6 +19,7 @@ import Test.Tasty.HUnit
19
19
20
20
import qualified FileStatus
21
21
import qualified FileStatusByteString
22
+ import qualified ReadDirStream
22
23
import qualified Signals001
23
24
24
25
main :: IO ()
@@ -42,6 +43,8 @@ main = defaultMain $ testGroup "All"
42
43
, posix005
43
44
, posix006
44
45
, posix010
46
+ , emptyDirStream
47
+ , nonEmptyDirStream
45
48
]
46
49
47
50
executeFile001 :: TestTree
@@ -225,6 +228,12 @@ posix010 = testCase "posix010" $ do
225
228
226
229
homeDirectory root @?= homeDirectory root'
227
230
231
+ emptyDirStream :: TestTree
232
+ emptyDirStream = testCase " emptyDirStream" ReadDirStream. emptyDirStream
233
+
234
+ nonEmptyDirStream :: TestTree
235
+ nonEmptyDirStream = testCase " nonEmptyDirStream" ReadDirStream. nonEmptyDirStream
236
+
228
237
-------------------------------------------------------------------------------
229
238
-- Utils
230
239
Original file line number Diff line number Diff line change @@ -162,6 +162,7 @@ test-suite unix-tests
162
162
FileStatus
163
163
FileStatusByteString
164
164
Signals001
165
+ ReadDirStream
165
166
type : exitcode-stdio-1.0
166
167
default-language : Haskell2010
167
168
build-depends : base, tasty, tasty-hunit, unix
You can’t perform that action at this time.
0 commit comments