Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tests for FsTree.find #9

Merged
merged 1 commit into from
Mar 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion fs-sim/fs-sim.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ test-suite fs-sim-test
type: exitcode-stdio-1.0
hs-source-dirs: test
main-is: Main.hs
other-modules: Test.System.FS.StateMachine
other-modules: Test.System.FS.Sim.FsTree
, Test.System.FS.StateMachine
, Test.Util.RefEnv
default-language: Haskell2010
build-depends: base >=4.14 && <4.17
Expand All @@ -81,6 +82,7 @@ test-suite fs-sim-test
, quickcheck-state-machine
, random
, tasty
, tasty-hunit
, tasty-quickcheck
, temporary
, text
Expand Down
7 changes: 4 additions & 3 deletions fs-sim/test/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import System.IO.Temp (withSystemTempDirectory)

import Test.Tasty

import qualified Test.System.FS.Sim.FsTree
import qualified Test.System.FS.StateMachine

main :: IO ()
Expand All @@ -15,9 +16,9 @@ main = withSystemTempDirectory "fs-sim-test" $ \tmpDir ->
testGroup "System" [
-- TODO: The FS tests fail for darwin on CI, see #532. So, they are
-- disabled for now, but should be enabled once #532 is resolved.
testGroup "FS" [
Test.System.FS.StateMachine.tests tmpDir
| not darwin
testGroup "FS" $
[ Test.System.FS.StateMachine.tests tmpDir | not darwin] <>
[ Test.System.FS.Sim.FsTree.tests
]
]
]
Expand Down
125 changes: 125 additions & 0 deletions fs-sim/test/Test/System/FS/Sim/FsTree.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
{-# LANGUAGE OverloadedStrings #-}

module Test.System.FS.Sim.FsTree (tests) where

import Data.List ((\\))
import qualified Data.Map.Strict as M
import Data.Text (Text)

import Test.Tasty (TestTree, testGroup)
import Test.Tasty.HUnit (Assertion, assertFailure, testCase, (@?))
import Text.Show.Pretty (ppShow, ppShowList)

import System.FS.API.Types (FsPath, fsPathFromList)
import System.FS.Sim.FsTree (FsTree (File, Folder),
FsTreeError (FsMissing), find)

tests :: TestTree
tests =
testGroup "FsTree"
[ testGroup "find command returns exactly what's expected"
[ testCase "usr" $ checkResultsOfFind ["usr"] example findUsr
, testCase "var" $ checkResultsOfFind ["var"] example findVar
, testCase "var/log" $ checkResultsOfFind ["var", "log"] example findVarLog
, testCase "root" $ checkResultsOfFind [] example findRoot

-- Bad weather
, testCase "boom" $ ["boom"] `shouldReportMissingFileIn` example
]
]

checkResultsOfFind :: [Text] -> FsTree () -> [FsPath] -> Assertion
checkResultsOfFind fp fs expectedResult = do
(expectedResult \\ filePathsFound)
`shouldBeEmptyOtherwise` "Not all expected paths were found"
(filePathsFound \\ expectedResult)
`shouldBeEmptyOtherwise` "Find returned unexpected paths"
where
filePathsFound = either (error . ppShow) id
$ find (fsPathFromList fp) fs
shouldBeEmptyOtherwise x msg =
null x @? msg ++ ":\n" ++ ppShowList x

shouldReportMissingFileIn :: [Text] -> FsTree () -> Assertion
shouldReportMissingFileIn fp fs =
case find (fsPathFromList fp) fs of
Left FsMissing {} -> pure ()
Left err -> assertFailure $ "Unexpected error: " ++ ppShow err
Right _ -> assertFailure $ ppShow fp
++ " was found on this filesystem:\n"
++ ppShow fs

{-------------------------------------------------------------------------------
Examples and expected results
-------------------------------------------------------------------------------}

example :: FsTree ()
example =
Folder $ M.fromList [
("usr", Folder $ M.fromList [
("local", Folder $ M.fromList [
("bin", Folder mempty)
])
])
, ("var", Folder $ M.fromList [
("log", Folder $ M.fromList [
("some.log", File mempty)
, ("apt", Folder mempty)
, ("cups", Folder $ M.fromList [
("bar.txt", File mempty)
, ("baz.txt", File mempty)
, ("buz", Folder $ M.fromList [
("sample.log", File mempty)
])
, ("biz", Folder mempty)
])
])
, ("mail", Folder mempty)
, ("run", Folder mempty)
, ("tmp", Folder $ M.fromList [
("foo.txt", File mempty)
])
])
]

findUsr :: [FsPath]
findUsr =
fmap fsPathFromList [ ["usr"]
, ["usr", "local"]
, ["usr", "local", "bin"]
]


findVar :: [FsPath]
findVar =
fmap fsPathFromList [ ["var"]
, ["var", "log"]
, ["var", "log", "some.log"]
, ["var", "log", "apt"]
, ["var", "log", "cups"]
, ["var", "log", "cups", "bar.txt"]
, ["var", "log", "cups", "baz.txt"]
, ["var", "log", "cups", "buz"]
, ["var", "log", "cups", "buz", "sample.log"]
, ["var", "log", "cups", "biz"]
, ["var", "mail"]
, ["var", "run"]
, ["var", "tmp"]
, ["var", "tmp", "foo.txt"]
]

findVarLog :: [FsPath]
findVarLog =
fmap fsPathFromList [ ["var", "log"]
, ["var", "log", "some.log"]
, ["var", "log", "apt"]
, ["var", "log", "cups"]
, ["var", "log", "cups", "bar.txt"]
, ["var", "log", "cups", "baz.txt"]
, ["var", "log", "cups", "buz"]
, ["var", "log", "cups", "buz", "sample.log"]
, ["var", "log", "cups", "biz"]
]

findRoot :: [FsPath]
findRoot = [fsPathFromList []] <> findUsr <> findVar