77import qualified Streamly.Prelude as S
88import qualified Streamly.Internal.Data.Fold as FL
99
10- import Data.List (stripPrefix )
1110import Streamly.Coreutils.Common (Switch (.. ))
1211import Streamly.Coreutils.Uniq
1312import Streamly.Coreutils.Rm
14- import Streamly.Coreutils.FileTest
1513import Streamly.Coreutils.Chmod
1614import System.FilePath ((</>) )
15+ import System.IO.Temp (withSystemTempDirectory )
1716
1817import Control.Exception (try , SomeException )
1918import 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
6564processResult :: Either SomeException s -> IO String
6665processResult res = return $
@@ -69,80 +68,109 @@ processResult res = return $
6968 Right _ -> " Passed"
7069
7170testRmDefault :: 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
8589testRmNonExist :: 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
97100testRmROFile :: 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
107110testRmForceFile :: 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
127140testRmNuke :: 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
138167testRmRecursive :: (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
147175testRmRecursiveOn :: IO String
148176testRmRecursiveOn = testRmRecursive (recursive On )
@@ -151,22 +179,26 @@ testRmRecursiveOff :: IO String
151179testRmRecursiveOff = testRmRecursive (recursive Off )
152180
153181printResult :: 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
160188testRm :: IO ()
161189testRm = 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
171203main :: IO ()
172204main = do
0 commit comments