Skip to content

Commit

Permalink
Only generate valid names for FsPath tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jorisdral committed Jun 6, 2024
1 parent 47879aa commit 26d31c3
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
11 changes: 11 additions & 0 deletions fs-api/src/System/FS/API/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,17 @@ allowExisting openMode = case openMode of
-------------------------------------------------------------------------------}

-- | A relative path.
--
-- === Invariant
--
-- The user of this library is tasked with picking sensible names of
-- directories/files on a path, such that the resulting 'FsPath' remains
-- relative to the HasFS instance root. For example, an @'FsPath' ["/"]@ would
-- try to access the root folder, which is most likely outside of the scope of
-- the HasFS instance. Amongst others, the following should hold:
-- * Names are non-empty
-- * Names are monotonic, i.e., they are not equal to @..@
-- * Names should not contain path separators or drive letters
newtype FsPath = UnsafeFsPath { fsPathToList :: [Strict.Text] }
deriving (Eq, Ord, Generic)
deriving newtype NFData
Expand Down
14 changes: 11 additions & 3 deletions fs-api/test/Test/System/FS/API/FsPath.hs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,16 @@ tests = testGroup "Test.System.FS.API.FsPath" [

-- | Orphan instance that generates a __non-empty__ text!
instance Arbitrary Text where
arbitrary = Text.pack <$> (arbitrary `suchThat` (not . null))
shrink x = [Text.pack x'' | let x' = Text.unpack x, x'' <- shrink x']
arbitrary = Text.pack <$> QC.listOf (QC.elements validChars) `suchThat` (not . null)
shrink x = [ x''' | let x' = Text.unpack x
, x'' <- shrink x'
, not (null x'')
, let x''' = Text.pack x'' ]

-- | We pick a small subset of characters to use in directory/file names, so
-- that we don't break the invariant of 'FsPath'.
validChars :: [Char]
validChars = concat [['a'..'z'], ['A'..'Z'], ['0'..'9']]

-- | Commutativity property for 'FS.</>' and 'FilePath.</>'.
--
Expand Down Expand Up @@ -65,7 +73,7 @@ prop_addExtensionCommutes mnt path ext =
.&&. FilePath.makeValid lhs === FilePath.makeValid rhs
where
mnt' = filePathFromList mnt
mnt'' = FS.MountPoint (filePathFromList mnt)
mnt'' = FS.MountPoint mnt'
fsp = FS.fsPathFromList path FS.<.> ext
lhs = FS.fsToFilePath mnt'' fsp
rhs = mnt' FilePath.</> filePathFromList path FilePath.<.> ext
Expand Down

0 comments on commit 26d31c3

Please sign in to comment.