Skip to content

Commit 3e26a0d

Browse files
committed
core: add GenericArbitrary derived instances
for `Build` and `Derivation` modules. Switch derivation path parser to use `Nix.Derivation.textParser` like `nix-derivation` does.
1 parent 34e788e commit 3e26a0d

File tree

5 files changed

+78
-28
lines changed

5 files changed

+78
-28
lines changed

hnix-store-core/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@
1414
* `StorePathName`
1515
* `StorePathHashPart`
1616
* `StoreDir`
17+
* Added `Arbitrary` instances for [#231](https://github.com/haskell-nix/hnix-store/pull/231)
18+
* `BuildMode`
19+
* `BuildStatus`
20+
* `BuildResult`
21+
* `Derivation StorePath Text`
22+
* `DerivationOutput StorePath Text`
1723

1824
# [0.7.0.0](https://github.com/haskell-nix/hnix-store/compare/core-0.6.1.0...core-0.7.0.0) 2023-11-15
1925

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ library
6060
, case-insensitive
6161
, cereal
6262
, containers
63+
, generic-arbitrary < 1.1
6364
-- Required for cryptonite low-level type convertion
6465
, memory
6566
, cryptonite
@@ -71,6 +72,7 @@ library
7172
, mtl
7273
, nix-derivation >= 1.1.1 && <2
7374
, QuickCheck
75+
, quickcheck-instances
7476
, saltine
7577
, time
7678
, text
@@ -89,6 +91,8 @@ library
8991
, DeriveFoldable
9092
, DeriveTraversable
9193
, DeriveLift
94+
, DerivingStrategies
95+
, DerivingVia
9296
, FlexibleContexts
9397
, FlexibleInstances
9498
, StandaloneDeriving
@@ -139,6 +143,7 @@ test-suite format-tests
139143
, directory
140144
, filepath
141145
, process
146+
, nix-derivation >= 1.1.1 && <2
142147
, tasty
143148
, tasty-golden
144149
, hspec

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

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
-- due to recent generic-arbitrary
2+
{-# OPTIONS_GHC -fconstraint-solver-iterations=0 #-}
13
{-|
24
Description : Build related types
35
Maintainer : srk <[email protected]>
@@ -7,15 +9,18 @@ module System.Nix.Build
79
, BuildStatus(..)
810
, BuildResult(..)
911
, buildSuccess
10-
)
11-
where
12+
) where
1213

13-
import Data.Time ( UTCTime )
14+
import Data.Time (UTCTime)
15+
import Test.QuickCheck (Arbitrary(..))
16+
import Test.QuickCheck.Arbitrary.Generic (GenericArbitrary(..))
17+
import Test.QuickCheck.Instances ()
1418

1519
-- keep the order of these Enums to match enums from reference implementations
1620
-- src/libstore/store-api.hh
1721
data BuildMode = Normal | Repair | Check
18-
deriving (Eq, Ord, Enum, Show)
22+
deriving (Eq, Generic, Ord, Enum, Show)
23+
deriving Arbitrary via GenericArbitrary BuildMode
1924

2025
data BuildStatus =
2126
Built
@@ -31,8 +36,8 @@ data BuildStatus =
3136
| DependencyFailed
3237
| LogLimitExceeded
3338
| NotDeterministic
34-
deriving (Eq, Ord, Enum, Show)
35-
39+
deriving (Eq, Generic, Ord, Enum, Show)
40+
deriving Arbitrary via GenericArbitrary BuildStatus
3641

3742
-- | Result of the build
3843
data BuildResult = BuildResult
@@ -49,7 +54,8 @@ data BuildResult = BuildResult
4954
, -- Stop time of this build
5055
stopTime :: !UTCTime
5156
}
52-
deriving (Eq, Ord, Show)
57+
deriving (Eq, Generic, Ord, Show)
58+
deriving Arbitrary via GenericArbitrary BuildResult
5359

5460
buildSuccess :: BuildResult -> Bool
5561
buildSuccess BuildResult {..} =
Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,48 @@
1+
-- due to recent generic-arbitrary
2+
{-# OPTIONS_GHC -Wno-orphans -fconstraint-solver-iterations=0 #-}
13

24
module System.Nix.Derivation
35
( parseDerivation
46
, buildDerivation
5-
)
6-
where
7+
) where
78

8-
import qualified Data.Text.Lazy.Builder as Text.Lazy
9-
( Builder )
10-
import qualified Data.Attoparsec.Text.Lazy as Text.Lazy
11-
( Parser )
12-
import Nix.Derivation ( Derivation )
13-
import qualified Nix.Derivation as Derivation
14-
import System.Nix.StorePath ( StoreDir
15-
, StorePath
16-
, storePathToFilePath
17-
)
18-
import qualified System.Nix.StorePath as StorePath
9+
import Data.Attoparsec.Text.Lazy (Parser)
10+
import Data.Text.Lazy.Builder (Builder)
11+
import Test.QuickCheck (Arbitrary(..))
12+
import Test.QuickCheck.Arbitrary.Generic (GenericArbitrary(..))
13+
import Test.QuickCheck.Instances ()
1914

15+
import Nix.Derivation (Derivation, DerivationOutput)
16+
import System.Nix.StorePath (StoreDir, StorePath)
2017

18+
import qualified Data.Attoparsec.Text.Lazy
19+
import qualified Data.Text.Lazy
2120

22-
parseDerivation :: StoreDir -> Text.Lazy.Parser (Derivation StorePath Text)
21+
import qualified Nix.Derivation
22+
import qualified System.Nix.StorePath
23+
24+
deriving via GenericArbitrary (Derivation StorePath Text)
25+
instance Arbitrary (Derivation StorePath Text)
26+
deriving via GenericArbitrary (DerivationOutput StorePath Text)
27+
instance Arbitrary (DerivationOutput StorePath Text)
28+
29+
parseDerivation :: StoreDir -> Parser (Derivation StorePath Text)
2330
parseDerivation expectedRoot =
24-
Derivation.parseDerivationWith
25-
("\"" *> StorePath.pathParser expectedRoot <* "\"")
26-
Derivation.textParser
31+
Nix.Derivation.parseDerivationWith
32+
pathParser
33+
Nix.Derivation.textParser
34+
where
35+
pathParser = do
36+
text <- Nix.Derivation.textParser
37+
case Data.Attoparsec.Text.Lazy.parseOnly
38+
(System.Nix.StorePath.pathParser expectedRoot)
39+
(Data.Text.Lazy.fromStrict text)
40+
of
41+
Right p -> pure p
42+
Left e -> fail e
2743

28-
buildDerivation :: StoreDir -> Derivation StorePath Text -> Text.Lazy.Builder
44+
buildDerivation :: StoreDir -> Derivation StorePath Text -> Builder
2945
buildDerivation storeDir =
30-
Derivation.buildDerivationWith
31-
(show . storePathToFilePath storeDir)
46+
Nix.Derivation.buildDerivationWith
47+
(show . System.Nix.StorePath.storePathToText storeDir)
3248
show

hnix-store-core/tests/Derivation.hs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,17 @@ import Test.Tasty ( TestTree
55
, testGroup
66
)
77
import Test.Tasty.Golden ( goldenVsFile )
8+
import Test.Tasty.QuickCheck
89

9-
import System.Nix.StorePath ( StoreDir(..) )
10+
import Nix.Derivation ( Derivation )
11+
import System.Nix.StorePath ( StoreDir(..), StorePath )
1012
import System.Nix.Derivation ( parseDerivation
1113
, buildDerivation
1214
)
1315

1416
import qualified Data.Attoparsec.Text
1517
import qualified Data.Text.IO
18+
import qualified Data.Text.Lazy
1619
import qualified Data.Text.Lazy.Builder
1720

1821
processDerivation :: FilePath -> FilePath -> IO ()
@@ -46,3 +49,17 @@ test_derivation =
4649
drv = fp <> show n <> ".drv"
4750
act = fp <> show n <> ".actual"
4851
fp = "tests/samples/example"
52+
53+
-- TODO(srk): this won't roundtrip as Arbitrary Text
54+
-- contains wild stuff like control characters and UTF8 sequences.
55+
-- Either fix in nix-derivation or use wrapper type
56+
-- (but we use Nix.Derivation.textParser so we need Text for now)
57+
xprop_derivationRoundTrip :: StoreDir -> Derivation StorePath Text -> Property
58+
xprop_derivationRoundTrip = \sd drv ->
59+
Data.Attoparsec.Text.parseOnly (parseDerivation sd)
60+
( Data.Text.Lazy.toStrict
61+
$ Data.Text.Lazy.Builder.toLazyText
62+
$ buildDerivation sd drv
63+
)
64+
=== pure drv
65+

0 commit comments

Comments
 (0)