Skip to content

Commit 92f593d

Browse files
committed
Add rm test cases
1 parent c2f8138 commit 92f593d

4 files changed

Lines changed: 117 additions & 89 deletions

File tree

hie.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ cradle:
66
cabal:
77
- path: "./src"
88
component: "lib:streamly-coreutils"
9+
- path: "./test"
10+
component: "test:coreutils-test"
911
dependencies:
1012
- streamly-coreutils.cabal
1113
- hie.yaml

src/Streamly/Coreutils/StringQ.hs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import Control.Applicative (Alternative(..))
2222
import Control.Monad.Catch (MonadCatch)
2323
import Control.Monad.IO.Class (liftIO, MonadIO)
2424
import Data.Char (chr)
25-
import Data.Data (Data, Typeable)
25+
import Data.Data (Data)
2626
import Data.Default.Class (Default(..))
2727
import Language.Haskell.TH (Exp, Q, Pat)
2828
import Language.Haskell.TH.Quote (QuasiQuoter(..), dataToExpQ, dataToPatQ)
@@ -61,20 +61,20 @@ data Permissions = Permissions
6161
{ readable :: Bool
6262
, writable :: Bool
6363
, executable :: Bool
64-
} deriving (Eq, Ord, Read, Show, Typeable, Data)
64+
} deriving (Eq, Ord, Read, Show, Data)
6565

6666
data UserType =
6767
Owner
6868
| Group
6969
| Others
7070
| All
71-
deriving (Eq, Ord, Read, Show, Typeable, Data)
71+
deriving (Eq, Ord, Read, Show, Data)
7272

7373
data UserTypePerm =
7474
UserTypePerm
7575
{ utype :: UserType
7676
, permssions :: Permissions
77-
} deriving (Eq, Ord, Read, Show, Typeable, Data)
77+
} deriving (Eq, Ord, Read, Show, Data)
7878

7979
instance Default Permissions where
8080
def = Permissions

test/Common.hs

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,16 @@
11
module Common
2+
( createParent
3+
, createDirWithParent
4+
, createDir
5+
, createFileWithParent
6+
, createFile
7+
)
28
where
39

410
import Control.Monad (unless)
5-
import System.Directory
6-
( createDirectory
7-
, createDirectoryIfMissing
8-
, createDirectoryLink
9-
, removeFile
10-
, removePathForcibly
11-
, renameDirectory
12-
, renamePath
13-
)
11+
import System.Directory (createDirectory, createDirectoryIfMissing)
1412
import System.FilePath ((</>), takeDirectory)
15-
import System.IO
16-
( BufferMode(..), hSetBuffering, stdout, IOMode (WriteMode), openFile
17-
, hClose)
18-
import System.IO.Temp (withSystemTempDirectory)
19-
13+
import System.IO ( IOMode (WriteMode), openFile, hClose)
2014

2115
createParent :: FilePath -> FilePath -> IO ()
2216
createParent file parent = do

test/Main.hs

Lines changed: 103 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,12 @@ where
77
import qualified Streamly.Prelude as S
88
import qualified Streamly.Internal.Data.Fold as FL
99

10-
import Data.List (stripPrefix)
1110
import Streamly.Coreutils.Common (Switch(..))
1211
import Streamly.Coreutils.Uniq
1312
import Streamly.Coreutils.Rm
14-
import Streamly.Coreutils.FileTest
1513
import Streamly.Coreutils.Chmod
1614
import System.FilePath ((</>))
15+
import System.IO.Temp (withSystemTempDirectory)
1716

1817
import Control.Exception (try, SomeException)
1918
import Control.Monad.IO.Class (MonadIO)
@@ -59,8 +58,8 @@ gen c n = S.unfoldr step (0, True)
5958
-- * File parent dirs not having permissions
6059
-- * File owned by someone else
6160

62-
cleanup :: String -> IO ()
63-
cleanup = rm (force Nuke . recursive On)
61+
rmDir :: FilePath
62+
rmDir = "rmDir"
6463

6564
processResult :: Either SomeException s -> IO String
6665
processResult res = return $
@@ -69,80 +68,109 @@ processResult res = return $
6968
Right _ -> "Passed"
7069

7170
testRmDefault :: IO String
72-
testRmDefault = do
73-
let dir = "testDir1"
74-
file = "file.txt"
75-
path = dir </> file
76-
cleanup dir
77-
createFileWithParent file dir
78-
exist <- test path isExisting
79-
rm id path
80-
failed <- test path isExisting
81-
if exist && not failed
82-
then return "Passed"
83-
else return "Failed"
71+
testRmDefault =
72+
withSystemTempDirectory rmDir $ \fp -> do
73+
let dir = fp </> "testDir"
74+
file = "file.txt"
75+
path = dir </> file
76+
createFileWithParent file dir
77+
try (rm id path) >>= processResult
78+
79+
testRmDefaultFail :: IO String
80+
testRmDefaultFail =
81+
withSystemTempDirectory rmDir $ \fp -> do
82+
let dir = fp </> "testDir"
83+
file = "fileRO.txt"
84+
path = dir </> file
85+
createFileWithParent file dir
86+
chmod [perm|u=r|] path
87+
try (rm id path) >>= processResult
8488

8589
testRmNonExist :: IO String
86-
testRmNonExist = do
87-
let dir = "testDir2"
88-
file = "file.txt"
89-
fileNE = "fileNE.txt"
90-
pathNE = dir </> fileNE
91-
cleanup dir
92-
createFileWithParent file dir
93-
try (rm id pathNE) >>= processResult
90+
testRmNonExist =
91+
withSystemTempDirectory rmDir $ \fp -> do
92+
let dir = fp </> "testDir"
93+
fileNE = "fileNE.txt"
94+
pathNE = dir </> fileNE
95+
try (rm id pathNE) >>= processResult
9496

95-
--chmod [perm|a=rwx|] "path"
97+
-- make path read-only
98+
-- chmod [perm|u=r|] "path"
9699

97100
testRmROFile :: IO String
98-
testRmROFile = do
99-
let dir = "testDir3"
100-
file = "fileRO.txt"
101-
path = dir </> file
102-
cleanup dir
103-
createFileWithParent file dir
104-
chmod [perm|u=r|] path
105-
try (rm id path) >>= processResult
101+
testRmROFile =
102+
withSystemTempDirectory rmDir $ \fp -> do
103+
let dir = fp </> "testDir"
104+
file = "fileRO.txt"
105+
path = dir </> file
106+
createFileWithParent file dir
107+
chmod [perm|u=r|] path
108+
try (rm id path) >>= processResult
106109

107110
testRmForceFile :: IO String
108-
testRmForceFile = do
109-
let dir = "testDir4"
110-
file = "fileRO.txt"
111-
path = dir </> file
112-
cleanup dir
113-
createFileWithParent file dir
114-
chmod [perm|u=r|] path
115-
try (rm (force Force) path) >>= processResult
116-
117-
testRmForceFail :: IO String
118-
testRmForceFail = do
119-
let dir = "testDirRO1"
120-
file = "fileRW.txt"
121-
path = dir </> file
122-
cleanup dir
123-
createFileWithParent file dir
124-
chmod [perm|u=r|] dir
125-
try (rm (force Force) path) >>= processResult
111+
testRmForceFile =
112+
withSystemTempDirectory rmDir $ \fp -> do
113+
let dir = fp </> "testDir"
114+
file = "fileRO.txt"
115+
path = dir </> file
116+
createFileWithParent file dir
117+
chmod [perm|u=r|] path
118+
try (rm (force Force) path) >>= processResult
119+
120+
testRmForceFailRO :: IO String
121+
testRmForceFailRO =
122+
withSystemTempDirectory rmDir $ \fp -> do
123+
let dir = fp </> "testDir"
124+
file = "fileRW.txt"
125+
path = dir </> file
126+
createFileWithParent file dir
127+
chmod [perm|u=r|] dir
128+
try (rm (force Force) path) >>= processResult
129+
130+
testRmForceFailNP :: IO String
131+
testRmForceFailNP =
132+
withSystemTempDirectory rmDir $ \fp -> do
133+
let dir = fp </> "testDir"
134+
file = "fileRW.txt"
135+
path = dir </> file
136+
createFileWithParent file dir
137+
chmod [perm|a=|] dir
138+
try (rm (force Force) path) >>= processResult
126139

127140
testRmNuke :: IO String
128-
testRmNuke = do
129-
let dir = "testDirNuke"
130-
file = "fileRW.txt"
131-
path = dir </> file
132-
cleanup dir
133-
createFileWithParent file dir
134-
chmod [perm|u=r|] dir
135-
try (rm (force Nuke . recursive On) dir) >>= processResult
136-
141+
testRmNuke =
142+
withSystemTempDirectory rmDir $ \fp -> do
143+
let dir = fp </> "testDir"
144+
file = "fileRW.txt"
145+
createFileWithParent file dir
146+
chmod [perm|u=r|] dir
147+
try (rm (force Nuke . recursive On) dir) >>= processResult
148+
149+
testRmNukeNoPerm :: IO String
150+
testRmNukeNoPerm =
151+
withSystemTempDirectory rmDir $ \fp -> do
152+
let dir = fp </> "testDir"
153+
file = "fileRW.txt"
154+
createFileWithParent file dir
155+
chmod [perm|a=|] dir
156+
try (rm (force Nuke . recursive On) dir) >>= processResult
157+
158+
testRmNukeRecOff :: IO String
159+
testRmNukeRecOff =
160+
withSystemTempDirectory rmDir $ \fp -> do
161+
let dir = fp </> "testDir"
162+
file = "fileRW.txt"
163+
createFileWithParent file dir
164+
chmod [perm|a=|] dir
165+
try (rm (force Nuke . recursive Off) dir) >>= processResult
137166

138167
testRmRecursive ::(Rm -> Rm) -> IO String
139-
testRmRecursive f = do
140-
let dir = "testDirRec"
141-
file = "fileRW.txt"
142-
path = dir </> file
143-
cleanup dir
144-
createFileWithParent file dir
145-
try (rm f dir) >>= processResult
168+
testRmRecursive f =
169+
withSystemTempDirectory rmDir $ \fp -> do
170+
let dir = fp </> "testDir" </> "testDir"
171+
file = "fileRW.txt"
172+
createFileWithParent file dir
173+
try (rm f dir) >>= processResult
146174

147175
testRmRecursiveOn :: IO String
148176
testRmRecursiveOn = testRmRecursive (recursive On)
@@ -151,22 +179,26 @@ testRmRecursiveOff :: IO String
151179
testRmRecursiveOff = testRmRecursive (recursive Off)
152180

153181
printResult :: String -> String -> IO String -> IO ()
154-
printResult tc exp m = do
182+
printResult tc expec m = do
155183
res <- m
156-
if res == exp
184+
if res == expec
157185
then print $ tc ++ "->" ++ "PASS"
158186
else print $ tc ++ "->" ++ "FAILED"
159187

160188
testRm :: IO ()
161189
testRm = do
162190
printResult "default" "Passed" testRmDefault
191+
printResult "defaultFail" "Failed" testRmDefaultFail
163192
printResult "nonExistant" "Failed" testRmNonExist
164193
printResult "readOnly" "Failed" testRmROFile
165194
printResult "forcePass" "Passed" testRmForceFile
166-
printResult "forceFail" "Failed" testRmForceFail
167-
printResult "nuke" "Passed" testRmNuke
195+
printResult "forceFail ReadOnly" "Failed" testRmForceFailRO
196+
printResult "forceFail None Permission" "Failed" testRmForceFailNP
168197
printResult "recursiveOn" "Passed" testRmRecursiveOn
169198
printResult "recursiveOff" "Failed" testRmRecursiveOff
199+
printResult "nuke" "Passed" testRmNuke
200+
printResult "nuke None Permission" "Passed" testRmNukeNoPerm
201+
printResult "nuke Recursive Off" "Passed" testRmNukeRecOff
170202

171203
main :: IO ()
172204
main = do

0 commit comments

Comments
 (0)