Skip to content

Commit

Permalink
Add test cases for Rm module
Browse files Browse the repository at this point in the history
Add test cases

Fix dependencies

Add test cases

Add rm test cases

Fix CI

Fix CI

Fix CI
  • Loading branch information
rnjtranjan committed Jul 27, 2022
1 parent cc5348c commit 5f280aa
Show file tree
Hide file tree
Showing 5 changed files with 197 additions and 0 deletions.
2 changes: 2 additions & 0 deletions hie.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ cradle:
cabal:
- path: "./src"
component: "lib:streamly-coreutils"
- path: "./test"
component: "test:coreutils-test"
dependencies:
- streamly-coreutils.cabal
- hie.yaml
1 change: 1 addition & 0 deletions src/Streamly/Coreutils/Chmod.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

module Streamly.Coreutils.Chmod
( chmod
, perm
)
where

Expand Down
6 changes: 6 additions & 0 deletions streamly-coreutils.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ library
, Streamly.Coreutils.Which
, Streamly.Coreutils.Ln


default-language: Haskell2010

-------------------------------------------------------------------------------
Expand All @@ -149,6 +150,7 @@ benchmark coreutils-bench
, base >= 4.8 && < 5
, gauge >= 0.2.4 && < 0.3
, random >= 1.0.0 && < 2

default-language: Haskell2010

-------------------------------------------------------------------------------
Expand All @@ -159,9 +161,13 @@ test-suite coreutils-test
import: compile-options
type: exitcode-stdio-1.0
main-is: Main.hs
other-modules: Common
hs-source-dirs: test
build-depends:
streamly-coreutils
, streamly
, base >= 4.8 && < 5
, filepath >= 1.4.2 && < 1.4.3
, directory >= 1.3.6 && < 1.3.7
, temporary >= 1.3 && < 1.4
default-language: Haskell2010
35 changes: 35 additions & 0 deletions test/Common.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
module Common
( createParent
, createDirWithParent
, createDir
, createFileWithParent
, createFile
)
where

import Control.Monad (unless)
import System.Directory (createDirectory, createDirectoryIfMissing)
import System.FilePath ((</>), takeDirectory)
import System.IO ( IOMode (WriteMode), openFile, hClose)

createParent :: FilePath -> FilePath -> IO ()
createParent file parent = do
createDirectoryIfMissing True (parent </> takeDirectory file)

createDirWithParent :: FilePath -> FilePath -> IO ()
createDirWithParent dir parent =
unless (null dir) $ createDirectoryIfMissing True (parent </> dir)

createDir :: FilePath -> FilePath -> IO ()
createDir dir parent =
unless (null dir) $ createDirectory (parent </> dir)

createFileWithParent :: FilePath -> FilePath -> IO ()
createFileWithParent file parent = do
unless (null file) $
createDirectoryIfMissing True (parent </> takeDirectory file)
openFile (parent </> file) WriteMode >>= hClose

createFile :: FilePath -> FilePath -> IO ()
createFile file parent =
openFile (parent </> file) WriteMode >>= hClose
153 changes: 153 additions & 0 deletions test/Main.hs
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE QuasiQuotes #-}
module Main
(main)
where

import qualified Streamly.Prelude as S
import qualified Streamly.Internal.Data.Fold as FL

import Streamly.Coreutils.Common (Switch(..))
import Streamly.Coreutils.Uniq
import Streamly.Coreutils.Rm
import Streamly.Coreutils.Chmod
import System.FilePath ((</>))
import System.IO.Temp (withSystemTempDirectory)

import Control.Exception (try, SomeException)
import Control.Monad.IO.Class (MonadIO)
import Streamly.Prelude (IsStream)
import Common
import System.Exit (exitFailure)

opt :: UniqOptions
opt = defaultUniqOptions {skipFields = 1, skipChar = 1}
Expand Down Expand Up @@ -49,8 +59,151 @@ gen c n = S.unfoldr step (0, True)
-- * File parent dirs not having permissions
-- * File owned by someone else

rmDir :: FilePath
rmDir = "rmDir"

processResult :: Either SomeException s -> IO String
processResult res = return $
case res of
Left _ -> "Failed"
Right _ -> "Passed"

testRmDefault :: IO String
testRmDefault =
withSystemTempDirectory rmDir $ \fp -> do
let dir = fp </> "testDir"
file = "file.txt"
path = dir </> file
createFileWithParent file dir
try (rm id path) >>= processResult

testRmDefaultFail :: IO String
testRmDefaultFail =
withSystemTempDirectory rmDir $ \fp -> do
let dir = fp </> "testDir"
file = "fileRO.txt"
path = dir </> file
createFileWithParent file dir
chmod [perm|u=r|] path
try (rm id path) >>= processResult

testRmNonExist :: IO String
testRmNonExist =
withSystemTempDirectory rmDir $ \fp -> do
let dir = fp </> "testDir"
fileNE = "fileNE.txt"
pathNE = dir </> fileNE
try (rm id pathNE) >>= processResult

-- make path read-only
-- chmod [perm|u=r|] "path"

testRmROFile :: IO String
testRmROFile =
withSystemTempDirectory rmDir $ \fp -> do
let dir = fp </> "testDir"
file = "fileRO.txt"
path = dir </> file
createFileWithParent file dir
chmod [perm|u=r|] path
try (rm id path) >>= processResult

testRmForceFile :: IO String
testRmForceFile =
withSystemTempDirectory rmDir $ \fp -> do
let dir = fp </> "testDir"
file = "fileRO.txt"
path = dir </> file
createFileWithParent file dir
chmod [perm|u=r|] path
try (rm (force Force) path) >>= processResult

testRmForceFailRO :: IO String
testRmForceFailRO =
withSystemTempDirectory rmDir $ \fp -> do
let dir = fp </> "testDir"
file = "fileRW.txt"
path = dir </> file
createFileWithParent file dir
chmod [perm|u=r|] dir
try (rm (force Force) path) >>= processResult

testRmForceFailNP :: IO String
testRmForceFailNP =
withSystemTempDirectory rmDir $ \fp -> do
let dir = fp </> "testDir"
file = "fileRW.txt"
path = dir </> file
createFileWithParent file dir
chmod [perm|a=|] dir
try (rm (force Force) path) >>= processResult

testRmNuke :: IO String
testRmNuke =
withSystemTempDirectory rmDir $ \fp -> do
let dir = fp </> "testDir"
file = "fileRW.txt"
createFileWithParent file dir
chmod [perm|u=r|] dir
try (rm (force Nuke . recursive On) dir) >>= processResult

testRmNukeNoPerm :: IO String
testRmNukeNoPerm =
withSystemTempDirectory rmDir $ \fp -> do
let dir = fp </> "testDir"
file = "fileRW.txt"
createFileWithParent file dir
chmod [perm|a=|] dir
try (rm (force Nuke . recursive On) dir) >>= processResult

testRmNukeRecOff :: IO String
testRmNukeRecOff =
withSystemTempDirectory rmDir $ \fp -> do
let dir = fp </> "testDir"
file = "fileRW.txt"
createFileWithParent file dir
chmod [perm|a=|] dir
try (rm (force Nuke . recursive Off) dir) >>= processResult

testRmRecursive ::(Rm -> Rm) -> IO String
testRmRecursive f =
withSystemTempDirectory rmDir $ \fp -> do
let dir = fp </> "testDir" </> "testDir"
file = "fileRW.txt"
createFileWithParent file dir
try (rm f dir) >>= processResult

testRmRecursiveOn :: IO String
testRmRecursiveOn = testRmRecursive (recursive On)

testRmRecursiveOff :: IO String
testRmRecursiveOff = testRmRecursive (recursive Off)

describe :: String -> String -> IO String -> IO ()
describe tc expec m = do
res <- m
if res == expec
then print (tc ++ "->" ++ "PASS")
else print (tc ++ "->" ++ "FAILED") >> exitFailure

testRm :: IO ()
testRm = do
describe "default" "Passed" testRmDefault
describe "defaultFail" "Failed" testRmDefaultFail
describe "nonExistant" "Failed" testRmNonExist
describe "readOnly" "Failed" testRmROFile
describe "forcePass" "Passed" testRmForceFile
describe "forceFail ReadOnly" "Failed" testRmForceFailRO
describe "forceFail None Permission" "Failed" testRmForceFailNP
describe "recursiveOn" "Passed" testRmRecursiveOn
describe "recursiveOff" "Failed" testRmRecursiveOff
describe "nuke" "Passed" testRmNuke
describe "nuke None Permission" "Passed" testRmNukeNoPerm
describe "nuke Recursive Off" "Failed" testRmNukeRecOff

main :: IO ()
main = do
testRm
let comp = compareUsingOptions opt
S.drain $ S.mapM print $ splitOnNewLine $ S.take 100 $ gen 'a' 6
S.drain $
Expand Down

0 comments on commit 5f280aa

Please sign in to comment.