Skip to content

Commit 6194cce

Browse files
committed
Add test cases
1 parent 7e9963e commit 6194cce

3 files changed

Lines changed: 73 additions & 2 deletions

File tree

streamly-coreutils.cabal

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,7 @@ library
105105
, unix >= 2.7.0 && < 2.8
106106
, directory >= 1.2.2 && < 1.4
107107
, filepath >= 1.4 && < 1.5
108-
, directory >= 1.3.6 && < 1.3.7
109-
, temporary >= 1.3 && < 1.4
108+
110109
hs-source-dirs: src
111110
exposed-modules:
112111
Streamly.Coreutils

test/Common.hs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
module Common
2+
where
3+
4+
import Control.Monad (unless)
5+
import System.Directory
6+
( createDirectory
7+
, createDirectoryIfMissing
8+
, createDirectoryLink
9+
, removeFile
10+
, removePathForcibly
11+
, renameDirectory
12+
, renamePath
13+
)
14+
import System.FilePath ((</>), takeDirectory)
15+
import System.IO
16+
( BufferMode(..), hSetBuffering, stdout, IOMode (WriteMode), openFile
17+
, hClose)
18+
import System.IO.Temp (withSystemTempDirectory)
19+
20+
21+
createParent :: FilePath -> FilePath -> IO ()
22+
createParent file parent = do
23+
createDirectoryIfMissing True (parent </> takeDirectory file)
24+
25+
createDirWithParent :: FilePath -> FilePath -> IO ()
26+
createDirWithParent dir parent =
27+
unless (null dir) $ createDirectoryIfMissing True (parent </> dir)
28+
29+
createDir :: FilePath -> FilePath -> IO ()
30+
createDir dir parent =
31+
unless (null dir) $ createDirectory (parent </> dir)
32+
33+
createFileWithParent :: FilePath -> FilePath -> IO ()
34+
createFileWithParent file parent = do
35+
unless (null file) $
36+
createDirectoryIfMissing True (parent </> takeDirectory file)
37+
openFile (parent </> file) WriteMode >>= hClose
38+
39+
createFile :: FilePath -> FilePath -> IO ()
40+
createFile file parent =
41+
openFile (parent </> file) WriteMode >>= hClose

test/Main.hs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
{-# LANGUAGE ScopedTypeVariables #-}
12
module Main
23
(main)
34
where
@@ -7,7 +8,10 @@ import qualified Streamly.Internal.Data.Fold as FL
78

89
import Streamly.Coreutils.Uniq
910
import Streamly.Coreutils.Rm
11+
import Streamly.Coreutils.FileTest
12+
import System.FilePath ((</>))
1013

14+
import Control.Exception (try, SomeException)
1115
import Control.Monad.IO.Class (MonadIO)
1216
import Streamly.Prelude (IsStream)
1317
import Common
@@ -51,8 +55,35 @@ gen c n = S.unfoldr step (0, True)
5155
-- * File parent dirs not having permissions
5256
-- * File owned by someone else
5357

58+
testRm :: IO ()
59+
testRm = do
60+
let dir = "testDir"
61+
file = "file.txt"
62+
path = dir </> file
63+
createFileWithParent file dir
64+
exist <- test path isExisting
65+
rm id path
66+
failed <- test path isExisting
67+
if exist && not failed
68+
then print "Test Rm PASS"
69+
else print "Test Rm Failed"
70+
71+
testRmNonExist :: IO ()
72+
testRmNonExist = do
73+
let dir = "testDir"
74+
file = "file.txt"
75+
fileNE = "fileNE.txt"
76+
pathNE = dir </> fileNE
77+
createFileWithParent file dir
78+
res <- try (rm id pathNE)
79+
case res of
80+
Left (_e :: SomeException) -> print "Test RmNE Passed"
81+
Right _ -> print "Test RmNE Failed"
82+
83+
5484
main :: IO ()
5585
main = do
86+
testRm
5687
let comp = compareUsingOptions opt
5788
S.drain $ S.mapM print $ splitOnNewLine $ S.take 100 $ gen 'a' 6
5889
S.drain $

0 commit comments

Comments
 (0)