Skip to content

Commit e8474ee

Browse files
committed
core: add roundtrip prop for DerivedPath
1 parent d832d9a commit e8474ee

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

hnix-store-core/hnix-store-core.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ test-suite format-tests
125125
main-is: Driver.hs
126126
other-modules:
127127
Derivation
128+
DerivedPath
128129
ContentAddress
129130
NarFormat
130131
Hash

hnix-store-core/src/System/Nix/DerivedPath.hs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import GHC.Generics (Generic)
1414
import Data.Set (Set)
1515
import Data.Text (Text)
1616
import System.Nix.StorePath (StoreDir, StorePath, StorePathName, InvalidPathError)
17+
import Test.QuickCheck (Arbitrary)
18+
import Test.QuickCheck.Arbitrary.Generic (GenericArbitrary(..))
1719

1820
import qualified Data.Set
1921
import qualified Data.Text
@@ -24,11 +26,17 @@ data OutputsSpec =
2426
| OutputsSpec_Names (Set StorePathName)
2527
deriving (Eq, Generic, Ord, Show)
2628

29+
deriving via GenericArbitrary OutputsSpec
30+
instance Arbitrary OutputsSpec
31+
2732
data DerivedPath =
2833
DerivedPath_Opaque StorePath
2934
| DerivedPath_Built StorePath OutputsSpec
3035
deriving (Eq, Generic, Ord, Show)
3136

37+
deriving via GenericArbitrary DerivedPath
38+
instance Arbitrary DerivedPath
39+
3240
data ParseOutputsError =
3341
ParseOutputsError_InvalidPath InvalidPathError
3442
| ParseOutputsError_NoNames
@@ -62,13 +70,13 @@ parseDerivedPath
6270
-> Either ParseOutputsError DerivedPath
6371
parseDerivedPath root p =
6472
case Data.Text.breakOn "!" p of
65-
(s,r) ->
73+
(s, r) ->
6674
if Data.Text.null r
6775
then DerivedPath_Opaque
6876
<$> (convertError $ System.Nix.StorePath.parsePathFromText root s)
6977
else DerivedPath_Built
7078
<$> (convertError $ System.Nix.StorePath.parsePathFromText root s)
71-
<*> parseOutputsSpec r
79+
<*> parseOutputsSpec (Data.Text.tail r)
7280

7381
derivedPathToText :: StoreDir -> DerivedPath -> Text
7482
derivedPathToText root = \case
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
module DerivedPath where
2+
3+
import Data.Default.Class (Default(def))
4+
import Test.Tasty.QuickCheck
5+
import System.Nix.DerivedPath (DerivedPath(..), OutputsSpec(..))
6+
7+
import qualified Data.Set
8+
import qualified System.Nix.DerivedPath
9+
10+
prop_derivedPathRoundTrip :: Property
11+
prop_derivedPathRoundTrip = forAll (arbitrary `suchThat` nonEmptyOutputsSpec_Names) $ \p ->
12+
System.Nix.DerivedPath.parseDerivedPath def
13+
(System.Nix.DerivedPath.derivedPathToText def p)
14+
=== pure p
15+
where
16+
nonEmptyOutputsSpec_Names :: DerivedPath -> Bool
17+
nonEmptyOutputsSpec_Names (DerivedPath_Built _ (OutputsSpec_Names set)) =
18+
not $ Data.Set.null set
19+
nonEmptyOutputsSpec_Names _ = True

0 commit comments

Comments
 (0)