-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Combinators for FsPaths and extensions
- Loading branch information
Showing
7 changed files
with
165 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package fs-api | ||
ghc-options: -fno-ignore-asserts | ||
|
||
package fs-sim | ||
ghc-options: -fno-ignore-asserts | ||
|
||
-- Enable -fcheck-prim-bounds | ||
-- https://gitlab.haskell.org/ghc/ghc/-/issues/21054 | ||
if impl(ghc >=9.4.6 && <9.5 || >=9.6.3) | ||
package primitive | ||
ghc-options: -fcheck-prim-bounds | ||
|
||
package vector | ||
ghc-options: -fcheck-prim-bounds | ||
|
||
package fs-api | ||
ghc-options: -fcheck-prim-bounds | ||
|
||
package fs-sim | ||
ghc-options: -fcheck-prim-bounds |
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
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 |
---|---|---|
@@ -1,9 +1,11 @@ | ||
module Main (main) where | ||
|
||
import Test.System.FS.IO | ||
import qualified Test.System.FS.API.FsPath | ||
import qualified Test.System.FS.IO | ||
import Test.Tasty | ||
|
||
main :: IO () | ||
main = defaultMain $ testGroup "fs-api-test" [ | ||
Test.System.FS.IO.tests | ||
Test.System.FS.API.FsPath.tests | ||
, Test.System.FS.IO.tests | ||
] |
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,76 @@ | ||
{-# OPTIONS_GHC -Wno-orphans #-} | ||
|
||
module Test.System.FS.API.FsPath (tests) where | ||
|
||
import Data.Text (Text) | ||
import qualified Data.Text as Text | ||
import Prelude hiding (read) | ||
import qualified System.FilePath as FilePath | ||
import qualified System.FS.API as FS | ||
import Test.Tasty | ||
import qualified Test.Tasty.QuickCheck as QC | ||
import Test.Tasty.QuickCheck | ||
|
||
tests :: TestTree | ||
tests = testGroup "Test.System.FS.API.FsPath" [ | ||
testProperty "prop_combineCommutes" prop_combineCommutes | ||
, testProperty "prop_addExtensionCommutes" prop_addExtensionCommutes | ||
] | ||
|
||
-- | 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'] | ||
|
||
-- | Commutativity property for 'FS.</>' and 'FilePath.</>'. | ||
-- | ||
-- TODO: commutativity might not be the right name for this type of property. | ||
-- | ||
-- @ | ||
-- \x y -> toFilePath (x </> y) == toFilePath x </> toFilePath y | ||
-- @ | ||
-- | ||
-- The first argument is used to create a mount point, which makes the property | ||
-- more useful because we are testing more cases. Also, for 'FS.fsToFilePath' to | ||
-- work, we need at least the empty mountpoint. | ||
prop_combineCommutes :: [Text] -> [Text] -> [Text] -> Property | ||
prop_combineCommutes mnt path1 path2 = | ||
QC.classify (FilePath.isValid rhs) "Valid file path" | ||
$ lhs === rhs | ||
.&&. FilePath.makeValid lhs === FilePath.makeValid rhs | ||
where | ||
mnt' = filePathFromList mnt | ||
mnt'' = FS.MountPoint mnt' | ||
fsp = FS.fsPathFromList path1 FS.</> FS.fsPathFromList path2 | ||
lhs = FS.fsToFilePath mnt'' fsp | ||
rhs = mnt' FilePath.</> filePathFromList path1 FilePath.</> filePathFromList path2 | ||
|
||
-- | Commutativity property for 'FS.<.>' and 'FilePath.<.>'. | ||
-- | ||
-- TODO: commutativity might not be the right name for this type of property. | ||
-- | ||
-- @ | ||
-- \path ext -> toFilePath (path <.> ext) == toFilePath path <.> ext | ||
-- @ | ||
-- | ||
-- The first argument is used to create a mount point, which makes the property | ||
-- more useful because we are testing more cases. Also, for 'FS.fsToFilePath' to | ||
-- work, we need at least the empty mountpoint. | ||
prop_addExtensionCommutes :: [Text] -> [Text] -> String -> Property | ||
prop_addExtensionCommutes mnt path ext = | ||
QC.classify (FilePath.isValid rhs) "Valid file path" | ||
$ QC.classify (case ext of '.':_ -> True; _ -> False) | ||
"Extension to add starts with an extension separator (.)" | ||
$ lhs === rhs | ||
.&&. FilePath.makeValid lhs === FilePath.makeValid rhs | ||
where | ||
mnt' = filePathFromList mnt | ||
mnt'' = FS.MountPoint (filePathFromList mnt) | ||
fsp = FS.fsPathFromList path FS.<.> ext | ||
lhs = FS.fsToFilePath mnt'' fsp | ||
rhs = mnt' FilePath.</> filePathFromList path FilePath.<.> ext | ||
|
||
-- | Build a 'FilePath' by 'FilePath.combine'ing the directory/file names. | ||
filePathFromList :: [Text] -> FilePath | ||
filePathFromList [] = [] | ||
filePathFromList xs = foldr (\y ys -> Text.unpack y FilePath.</> ys) (Text.unpack (last xs)) (init xs) |