-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
These are useful when testing with `fs-sim` errors, in which case we sould check how many open handles and/or files exist when disk faults occur.
- Loading branch information
Showing
4 changed files
with
123 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
{-# OPTIONS_GHC -Wno-orphans #-} | ||
|
||
-- TODO: upstream to fs-sim | ||
module Test.FS (tests) where | ||
|
||
import Control.Monad | ||
import Control.Monad.IOSim (runSimOrThrow) | ||
import Data.Char (isAsciiLower, isAsciiUpper) | ||
import qualified Data.List as List | ||
import qualified Data.Text as Text | ||
import System.FS.API | ||
import qualified System.FS.Sim.MockFS as MockFS | ||
import Test.QuickCheck | ||
import Test.QuickCheck.Instances () | ||
import Test.Tasty | ||
import Test.Tasty.QuickCheck (testProperty) | ||
import Test.Util.FS | ||
|
||
tests :: TestTree | ||
tests = testGroup "Test.FS" [ | ||
-- Simulated file system properties | ||
testProperty "prop_propNumOpenHandles" prop_propNumOpenHandles | ||
, testProperty "prop_propNoOpenHandles" prop_propNoOpenHandles | ||
, testProperty "prop_propNumDirEntries" prop_propNumDirEntries | ||
, testProperty "prop_propNoDirEntries" prop_propNoDirEntries | ||
] | ||
|
||
{------------------------------------------------------------------------------- | ||
Simulated file system properties | ||
-------------------------------------------------------------------------------} | ||
|
||
newtype Path = Path FsPath | ||
deriving stock (Show, Eq) | ||
|
||
newtype UniqueList a = UniqueList [a] | ||
deriving stock Show | ||
|
||
instance (Arbitrary a, Eq a) => Arbitrary (UniqueList a) where | ||
arbitrary = do | ||
xs <- arbitrary | ||
pure (UniqueList (List.nub xs)) | ||
shrink (UniqueList []) = [] | ||
shrink (UniqueList xs) = UniqueList . List.nub <$> shrink xs | ||
|
||
instance Arbitrary Path where | ||
arbitrary = Path . mkFsPath . (:[]) <$> ((:) <$> genChar <*> listOf genChar) | ||
where | ||
genChar = elements (['A'..'Z'] ++ ['a'..'z']) | ||
shrink (Path p) = case fsPathToList p of | ||
[] -> [] | ||
t:_ -> [ | ||
Path p' | ||
| t' <- shrink t | ||
, let t'' = Text.filter (\c -> isAsciiUpper c || isAsciiLower c) t' | ||
, not (Text.null t'') | ||
, let p' = fsPathFromList [t'] | ||
] | ||
|
||
prop_propNumOpenHandles :: UniqueList Path -> Property | ||
prop_propNumOpenHandles (UniqueList paths) = runSimOrThrow $ | ||
withSimHasFS prop MockFS.empty $ \hfs _ -> do | ||
forM_ paths $ \(Path p) -> hOpen hfs p (WriteMode MustBeNew) | ||
where | ||
prop = propNumOpenHandles (length paths) | ||
|
||
prop_propNoOpenHandles :: Property | ||
prop_propNoOpenHandles = once $ runSimOrThrow $ | ||
withSimHasFS propNoOpenHandles MockFS.empty $ \_ _ -> pure () | ||
|
||
prop_propNumDirEntries :: Path -> InfiniteList Bool -> UniqueList Path -> Property | ||
prop_propNumDirEntries (Path dir) isFiles (UniqueList paths) = runSimOrThrow $ | ||
withSimHasFS prop MockFS.empty $ \hfs _ -> do | ||
createDirectoryIfMissing hfs False dir | ||
forM_ (zip (getInfiniteList isFiles) paths) $ \(isFile, Path p) -> | ||
if isFile then | ||
void $ hOpen hfs (dir </> p) (WriteMode MustBeNew) | ||
else | ||
createDirectory hfs (dir </> p) | ||
where | ||
prop = propNumDirEntries dir (length paths) | ||
|
||
prop_propNoDirEntries :: Path -> Property | ||
prop_propNoDirEntries (Path dir) = runSimOrThrow $ | ||
withSimHasFS (propNoDirEntries dir) MockFS.empty $ \hfs _ -> do | ||
createDirectoryIfMissing hfs False dir |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters