|
3 | 3 | -- TODO: upstream to fs-sim |
4 | 4 | module Test.FS (tests) where |
5 | 5 |
|
| 6 | +import Control.Concurrent.Class.MonadSTM (MonadSTM (atomically)) |
| 7 | +import Control.Concurrent.Class.MonadSTM.Strict.TMVar |
| 8 | +import Control.Monad |
| 9 | +import Control.Monad.IOSim (runSimOrThrow) |
| 10 | +import Data.Char (isAsciiLower, isAsciiUpper) |
| 11 | +import qualified Data.List as List |
| 12 | +import qualified Data.Text as Text |
6 | 13 | import GHC.Generics (Generic) |
7 | 14 | import System.FS.API |
8 | 15 | import System.FS.Sim.Error |
| 16 | +import qualified System.FS.Sim.MockFS as MockFS |
9 | 17 | import qualified System.FS.Sim.Stream as S |
10 | 18 | import System.FS.Sim.Stream (InternalInfo (..), Stream (..)) |
11 | 19 | import Test.QuickCheck |
12 | 20 | import Test.QuickCheck.Classes (eqLaws) |
13 | 21 | import Test.QuickCheck.Instances () |
14 | 22 | import Test.Tasty |
| 23 | +import Test.Tasty.QuickCheck (testProperty) |
15 | 24 | import Test.Util.FS |
16 | 25 | import Test.Util.QC |
17 | 26 |
|
18 | 27 | tests :: TestTree |
19 | 28 | tests = testGroup "Test.FS" [ |
20 | | - testClassLaws "Stream" $ |
| 29 | + -- * Simulated file system properties |
| 30 | + testProperty "prop_numOpenHandles" prop_numOpenHandles |
| 31 | + , testProperty "prop_numDirEntries" prop_numDirEntries |
| 32 | + -- * Equality |
| 33 | + , testClassLaws "Stream" $ |
21 | 34 | eqLaws (Proxy @(Stream Int)) |
22 | 35 | , testClassLaws "Errors" $ |
23 | 36 | eqLaws (Proxy @Errors) |
24 | 37 | ] |
25 | 38 |
|
| 39 | +{------------------------------------------------------------------------------- |
| 40 | + Simulated file system properties |
| 41 | +-------------------------------------------------------------------------------} |
| 42 | + |
| 43 | +newtype Path = Path FsPath |
| 44 | + deriving stock (Show, Eq) |
| 45 | + |
| 46 | +newtype UniqueList a = UniqueList [a] |
| 47 | + deriving stock Show |
| 48 | + |
| 49 | +instance (Arbitrary a, Eq a) => Arbitrary (UniqueList a) where |
| 50 | + arbitrary = do |
| 51 | + xs <- arbitrary |
| 52 | + pure (UniqueList (List.nub xs)) |
| 53 | + shrink (UniqueList []) = [] |
| 54 | + shrink (UniqueList xs) = UniqueList . List.nub <$> shrink xs |
| 55 | + |
| 56 | +instance Arbitrary Path where |
| 57 | + arbitrary = Path . mkFsPath . (:[]) <$> ((:) <$> genChar <*> listOf genChar) |
| 58 | + where |
| 59 | + genChar = elements (['A'..'Z'] ++ ['a'..'z']) |
| 60 | + shrink (Path p) = case fsPathToList p of |
| 61 | + [] -> [] |
| 62 | + t:_ -> [ |
| 63 | + Path p' |
| 64 | + | t' <- shrink t |
| 65 | + , let t'' = Text.filter (\c -> isAsciiUpper c || isAsciiLower c) t' |
| 66 | + , not (Text.null t'') |
| 67 | + , let p' = fsPathFromList [t'] |
| 68 | + ] |
| 69 | + |
| 70 | +-- | Sanity check for 'propNoOpenHandles' and 'propNumOpenHandles' |
| 71 | +prop_numOpenHandles :: UniqueList Path -> Property |
| 72 | +prop_numOpenHandles (UniqueList paths) = runSimOrThrow $ |
| 73 | + withSimHasFS propTrivial MockFS.empty $ \hfs fsVar -> do |
| 74 | + -- No open handles initially |
| 75 | + fs <- atomically $ readTMVar fsVar |
| 76 | + let prop = propNoOpenHandles fs |
| 77 | + |
| 78 | + -- Open n handles |
| 79 | + hs <- forM paths $ \(Path p) -> hOpen hfs p (WriteMode MustBeNew) |
| 80 | + |
| 81 | + -- Now there should be precisely n open handles |
| 82 | + fs' <- atomically $ readTMVar fsVar |
| 83 | + let prop' = propNumOpenHandles n fs' |
| 84 | + |
| 85 | + -- Close all previously opened handles |
| 86 | + forM_ hs $ hClose hfs |
| 87 | + |
| 88 | + -- No open handles again |
| 89 | + fs'' <- atomically $ readTMVar fsVar |
| 90 | + let prop'' = propNoOpenHandles fs'' |
| 91 | + |
| 92 | + pure (prop .&&. prop' .&&. prop'') |
| 93 | + where |
| 94 | + n = length paths |
| 95 | + |
| 96 | +-- | Sanity check for 'propNoDirEntries' and 'propNumDirEntries' |
| 97 | +prop_numDirEntries :: Path -> InfiniteList Bool -> UniqueList Path -> Property |
| 98 | +prop_numDirEntries (Path dir) isFiles (UniqueList paths) = runSimOrThrow $ |
| 99 | + withSimHasFS propTrivial MockFS.empty $ \hfs fsVar -> do |
| 100 | + createDirectoryIfMissing hfs False dir |
| 101 | + |
| 102 | + -- No entries initially |
| 103 | + fs <- atomically $ readTMVar fsVar |
| 104 | + let prop = propNoDirEntries dir fs |
| 105 | + |
| 106 | + -- Create n entries |
| 107 | + forM_ xs $ \(isFile, Path p) -> |
| 108 | + if isFile |
| 109 | + then withFile hfs (dir </> p) (WriteMode MustBeNew) $ \_ -> pure () |
| 110 | + else createDirectory hfs (dir </> p) |
| 111 | + |
| 112 | + -- Now there should be precisely n entries |
| 113 | + fs' <- atomically $ readTMVar fsVar |
| 114 | + let prop' = propNumDirEntries dir n fs' |
| 115 | + |
| 116 | + -- Remove n entries |
| 117 | + forM_ xs $ \(isFile, Path p) -> |
| 118 | + if isFile |
| 119 | + then removeFile hfs (dir </> p) |
| 120 | + else removeDirectoryRecursive hfs (dir </> p) |
| 121 | + |
| 122 | + -- No entries again |
| 123 | + fs'' <- atomically $ readTMVar fsVar |
| 124 | + let prop'' = propNoDirEntries dir fs'' |
| 125 | + |
| 126 | + pure (prop .&&. prop' .&&. prop'') |
| 127 | + where |
| 128 | + n = length paths |
| 129 | + xs = zip (getInfiniteList isFiles) paths |
| 130 | + |
| 131 | +{------------------------------------------------------------------------------- |
| 132 | + Equality |
| 133 | +-------------------------------------------------------------------------------} |
| 134 | + |
26 | 135 | -- | This is not a fully lawful instance, because it uses 'approximateEqStream'. |
27 | 136 | instance Eq a => Eq (Stream a) where |
28 | 137 | (==) = approximateEqStream |
|
0 commit comments