Skip to content

Commit 5dc1802

Browse files
committed
core: move Arbitrary instances near their types
This allows us to use them in remote serialization round trip props. Couple of them are not needed anymore (`NixLike` is the default now) so whole `tests/Arbitrary` is gone.
1 parent 2a6fd96 commit 5dc1802

File tree

5 files changed

+34
-75
lines changed

5 files changed

+34
-75
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ library
7070
, monad-control
7171
, mtl
7272
, nix-derivation >= 1.1.1 && <2
73+
, QuickCheck
7374
, saltine
7475
, time
7576
, text
@@ -114,7 +115,6 @@ test-suite format-tests
114115
type: exitcode-stdio-1.0
115116
main-is: Driver.hs
116117
other-modules:
117-
Arbitrary
118118
Derivation
119119
NarFormat
120120
Hash

hnix-store-core/src/System/Nix/Internal/StorePath.hs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ import Crypto.Hash ( SHA256
4949
, HashAlgorithm
5050
)
5151

52+
import Test.QuickCheck
53+
5254
-- | A path in a Nix store.
5355
--
5456
-- From the Nix thesis: A store path is the full path of a store
@@ -72,6 +74,12 @@ instance Hashable StorePath where
7274
hashWithSalt s StorePath{..} =
7375
s `hashWithSalt` storePathHash `hashWithSalt` storePathName
7476

77+
instance Arbitrary StorePath where
78+
arbitrary =
79+
liftA2 StorePath
80+
arbitrary
81+
arbitrary
82+
7583
-- | The name portion of a Nix path.
7684
--
7785
-- 'unStorePathName' must only contain a-zA-Z0-9+._?=-, can't start
@@ -82,13 +90,23 @@ newtype StorePathName = StorePathName
8290
unStorePathName :: Text
8391
} deriving (Eq, Hashable, Ord, Show)
8492

93+
instance Arbitrary StorePathName where
94+
arbitrary = StorePathName . toText <$> ((:) <$> s1 <*> listOf sn)
95+
where
96+
alphanum = ['a' .. 'z'] <> ['A' .. 'Z'] <> ['0' .. '9']
97+
s1 = elements $ alphanum <> "+-_?="
98+
sn = elements $ alphanum <> "+-._?="
99+
85100
-- | The hash algorithm used for store path hashes.
86101
newtype StorePathHashPart = StorePathHashPart
87102
{ -- | Extract the contents of the hash.
88103
unStorePathHashPart :: ByteString
89104
}
90105
deriving (Eq, Hashable, Ord, Show)
91106

107+
instance Arbitrary StorePathHashPart where
108+
arbitrary = mkStorePathHashPart @SHA256 . Bytes.Char8.pack <$> arbitrary
109+
92110
mkStorePathHashPart
93111
:: forall hashAlgo
94112
. HashAlgorithm hashAlgo
@@ -167,6 +185,9 @@ newtype StoreDir = StoreDir {
167185
unStoreDir :: RawFilePath
168186
} deriving (Eq, Hashable, Ord, Show)
169187

188+
instance Arbitrary StoreDir where
189+
arbitrary = StoreDir . ("/" <>) . Bytes.Char8.pack <$> arbitrary
190+
170191
-- | Render a 'StorePath' as a 'RawFilePath'.
171192
storePathToRawFilePath :: StoreDir -> StorePath -> RawFilePath
172193
storePathToRawFilePath storeDir StorePath{..} =

hnix-store-core/tests/Arbitrary.hs

Lines changed: 0 additions & 58 deletions
This file was deleted.

hnix-store-core/tests/Hash.hs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import Test.Tasty.QuickCheck
1313

1414
import System.Nix.Hash
1515
import System.Nix.StorePath
16-
import Arbitrary
1716
import System.Nix.Internal.Base
1817
import Crypto.Hash ( MD5
1918
, SHA1
@@ -49,6 +48,12 @@ spec_hash = do
4948
prop_nixBase32Roundtrip :: Property
5049
prop_nixBase32Roundtrip = forAllShrink nonEmptyString genericShrink $
5150
\x -> pure (encodeUtf8 x) === (B32.decode . B32.encode . encodeUtf8 $ x)
51+
where
52+
nonEmptyString :: Gen String
53+
nonEmptyString = listOf1 genSafeChar
54+
55+
genSafeChar :: Gen Char
56+
genSafeChar = choose ('\1', '\127') -- ASCII without \NUL
5257

5358
-- | API variants
5459
prop_nixBase16Roundtrip :: StorePathHashPart -> Property

hnix-store-core/tests/StorePath.hs

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,13 @@ import qualified Data.Attoparsec.Text
99
import Test.Tasty.QuickCheck
1010

1111
import System.Nix.StorePath
12-
import Arbitrary
1312

14-
-- | Test that Nix(OS) like paths roundtrip
15-
prop_storePathRoundtrip :: StoreDir -> NixLike -> NixLike -> Property
16-
prop_storePathRoundtrip storeDir (_ :: NixLike) (NixLike x) =
13+
-- | Test @StorePath@ roundtrips using @parsePath@
14+
prop_storePathRoundtrip :: StoreDir -> StorePath -> Property
15+
prop_storePathRoundtrip storeDir x =
1716
parsePath storeDir (storePathToRawFilePath storeDir x) === pure x
1817

19-
-- | Test that any `StorePath` roundtrips
20-
prop_storePathRoundtrip' :: StoreDir -> StorePath -> Property
21-
prop_storePathRoundtrip' storeDir x =
22-
parsePath storeDir (storePathToRawFilePath storeDir x) === pure x
23-
24-
prop_storePathRoundtripParser :: StoreDir -> NixLike -> NixLike -> Property
25-
prop_storePathRoundtripParser storeDir (_ :: NixLike) (NixLike x) =
26-
Data.Attoparsec.Text.parseOnly (pathParser storeDir) (storePathToText storeDir x) === pure x
27-
28-
prop_storePathRoundtripParser' :: StoreDir -> StorePath -> Property
29-
prop_storePathRoundtripParser' storeDir x =
18+
-- | Test @StorePath@ roundtrips using @pathParser@
19+
prop_storePathRoundtripParser :: StoreDir -> StorePath -> Property
20+
prop_storePathRoundtripParser storeDir x =
3021
Data.Attoparsec.Text.parseOnly (pathParser storeDir) (storePathToText storeDir x) === pure x

0 commit comments

Comments
 (0)