Skip to content

Commit 79c1e5d

Browse files
authored
Merge pull request #299 from haskell-nix/test-json
Follow up to #296
2 parents c0cd4fc + 1466b05 commit 79c1e5d

File tree

8 files changed

+79
-58
lines changed

8 files changed

+79
-58
lines changed

hnix-store-core/CHANGELOG.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
# Unreleased
2+
3+
* Additions:
4+
* `System.Nix.Placeholder` module with `DownstreamPlaceholder`, `createPlaceholder`, and `renderPlaceholder`
5+
* `System.Nix.StorePath.ContentAddressed` module, for creating content-addressed store paths
6+
* `textToMethod`, `methodToText` in `System.Nix.ContentAddress`
7+
* `parseBasePath`, `parseBasePathFromText` in `System.Nix.StorePath`
8+
* `textToBaseEncoding`, `baseEncodingToText` in `System.Nix.Base`
9+
* `outputStoreObjectName` in `System.Nix.OutputName`
10+
* `SingleDerivedPath` type with `parseSingleDerivedPath`, `singleDerivedPathToText`
11+
12+
* Changes:
13+
* `OutputName.unOutputName` now returns `StorePathName` instead of `Text`
14+
* `DerivedPath_Built` constructor now takes `SingleDerivedPath` instead of `StorePath`
15+
116
# [0.8.0.0](https://github.com/haskell-nix/hnix-store/compare/core-0.7.0.0...core-0.8.0.0) 2024-07-31
217

318
* Changes:

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

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ module System.Nix.StorePath
2727
storePathToFilePath
2828
, storePathToRawFilePath
2929
, storePathToText
30+
, storePathBaseToText
31+
, storePathBaseToRawFilePath
3032
, storePathToNarInfo
3133
, storePathHashPartToText
3234
, -- * Parsing 'StorePath's
@@ -184,14 +186,25 @@ instance Default StoreDir where
184186
class HasStoreDir r where
185187
hasStoreDir :: r -> StoreDir
186188

187-
-- | Render a 'StorePath' as a 'RawFilePath'.
188-
storePathToRawFilePath :: StoreDir -> StorePath -> RawFilePath
189-
storePathToRawFilePath storeDir StorePath{..} =
190-
unStoreDir storeDir <> "/" <> hashPart <> "-" <> name
189+
-- | Render a 'StorePath' as a base path (without store directory).
190+
-- This is the relative path format: @hashPart-name@
191+
storePathBaseToRawFilePath :: StorePath -> RawFilePath
192+
storePathBaseToRawFilePath StorePath{..} =
193+
hashPart <> "-" <> name
191194
where
192195
hashPart = Data.Text.Encoding.encodeUtf8 $ storePathHashPartToText storePathHash
193196
name = Data.Text.Encoding.encodeUtf8 $ unStorePathName storePathName
194197

198+
-- | Render a 'StorePath' as a base path 'Text' (without store directory).
199+
-- This is the relative path format: @hashPart-name@
200+
storePathBaseToText :: StorePath -> Text
201+
storePathBaseToText = Data.Text.Encoding.decodeUtf8 . storePathBaseToRawFilePath
202+
203+
-- | Render a 'StorePath' as a 'RawFilePath'.
204+
storePathToRawFilePath :: StoreDir -> StorePath -> RawFilePath
205+
storePathToRawFilePath storeDir sp =
206+
unStoreDir storeDir <> "/" <> storePathBaseToRawFilePath sp
207+
195208
-- | Render a 'StorePath' as a 'FilePath'.
196209
storePathToFilePath :: StoreDir -> StorePath -> FilePath
197210
storePathToFilePath storeDir = Data.ByteString.Char8.unpack . storePathToRawFilePath storeDir

hnix-store-json/CHANGELOG.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
# Unreleased
2+
3+
The test suite is now much more comprenesive, uses test data from upstream Nix to make sure the JSON formats match.
4+
5+
* JSON instance for `StorePath` now doesn't include the store dir, matching upstream.
6+
(This keeps it canonical.)
7+
8+
* New JSON instances for:
9+
* `Hash`,
10+
* `ContentAddress`
11+
* `OutputsSpec`
12+
* `SingleDerivedPath`
13+
* `DerivedPath`
14+
115
# 0.1.0.0 2024-07-31
216

317
* Initial release

hnix-store-json/hnix-store-json.cabal

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,5 +100,4 @@ test-suite json
100100
, aeson
101101
, bytestring
102102
, containers
103-
, data-default-class
104103
, hspec

hnix-store-json/src/System/Nix/JSON.hs

Lines changed: 12 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import Data.Foldable (toList)
2525
import Data.Set qualified
2626
import Data.Some
2727
import Data.Text (Text)
28-
import Data.Text qualified
2928
import Data.Text.Lazy qualified
3029
import Data.Text.Lazy.Builder qualified
3130
import Deriving.Aeson
@@ -41,7 +40,7 @@ import System.Nix.Realisation (DerivationOutput(..), Realisation, RealisationWit
4140
import System.Nix.Realisation qualified
4241
import System.Nix.Signature (Signature)
4342
import System.Nix.Signature qualified
44-
import System.Nix.StorePath (StorePath, StorePathName, StorePathHashPart, storePathHash, storePathName, mkStorePathName, unStorePathName, parseBasePathFromText)
43+
import System.Nix.StorePath (StorePath, StorePathName, StorePathHashPart, mkStorePathName, unStorePathName, parseBasePathFromText)
4544
import System.Nix.StorePath qualified
4645

4746
instance ToJSON StorePathName where
@@ -68,19 +67,8 @@ instance FromJSON StorePathHashPart where
6867
)
6968

7069
instance ToJSON StorePath where
71-
toJSON sp =
72-
toJSON $ Data.Text.concat
73-
[ System.Nix.StorePath.storePathHashPartToText (storePathHash sp)
74-
, "-"
75-
, System.Nix.StorePath.unStorePathName (storePathName sp)
76-
]
77-
78-
toEncoding sp =
79-
toEncoding $ Data.Text.concat
80-
[ System.Nix.StorePath.storePathHashPartToText (storePathHash sp)
81-
, "-"
82-
, System.Nix.StorePath.unStorePathName (storePathName sp)
83-
]
70+
toJSON = toJSON . System.Nix.StorePath.storePathBaseToText
71+
toEncoding = toEncoding . System.Nix.StorePath.storePathBaseToText
8472

8573
instance FromJSON StorePath where
8674
parseJSON =
@@ -92,12 +80,7 @@ instance FromJSON StorePath where
9280
)
9381

9482
instance ToJSONKey StorePath where
95-
toJSONKey = Data.Aeson.Types.toJSONKeyText $ \sp ->
96-
Data.Text.concat
97-
[ System.Nix.StorePath.storePathHashPartToText (storePathHash sp)
98-
, "-"
99-
, System.Nix.StorePath.unStorePathName (storePathName sp)
100-
]
83+
toJSONKey = Data.Aeson.Types.toJSONKeyText System.Nix.StorePath.storePathBaseToText
10184

10285
instance FromJSONKey StorePath where
10386
fromJSONKey = FromJSONKeyTextParser $
@@ -243,33 +226,19 @@ instance FromJSON SingleDerivedPath where
243226
instance ToJSON DerivedPath where
244227
toJSON (DerivedPath_Opaque path) = toJSON path
245228
toJSON (DerivedPath_Built drvPath outputs) =
246-
case outputs of
247-
OutputsSpec_Names names | Data.Set.size names == 1 ->
248-
object
249-
[ "drvPath" .= toJSON drvPath
250-
, "output" .= Data.Set.elemAt 0 names
251-
]
252-
_ ->
253-
object
254-
[ "drvPath" .= toJSON drvPath
255-
, "outputs" .= outputs
256-
]
229+
object
230+
[ "drvPath" .= toJSON drvPath
231+
, "outputs" .= outputs
232+
]
257233

258234
instance FromJSON DerivedPath where
259235
parseJSON v = parseOpaque v <|> parseBuilt v
260236
where
261237
parseOpaque = fmap DerivedPath_Opaque . parseJSON
262-
parseBuilt = withObject "DerivedPath_Built" $ \obj -> do
263-
drvPath <- obj .: "drvPath"
264-
-- Try single output first, then multiple outputs
265-
mOutput <- obj .:? "output"
266-
mOutputs <- obj .:? "outputs"
267-
case (mOutput, mOutputs) of
268-
(Just output, Nothing) ->
269-
pure $ DerivedPath_Built drvPath (OutputsSpec_Names (Data.Set.singleton output))
270-
(Nothing, Just outputs) ->
271-
pure $ DerivedPath_Built drvPath outputs
272-
_ -> fail "Expected either 'output' or 'outputs' field"
238+
parseBuilt = withObject "DerivedPath_Built" $ \obj ->
239+
DerivedPath_Built
240+
<$> obj .: "drvPath"
241+
<*> obj .: "outputs"
273242

274243
data LowerLeading
275244
instance StringModifier LowerLeading where

hnix-store-json/tests/RealisationSpec.hs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ module RealisationSpec where
44

55
import Data.Aeson (eitherDecode, encode)
66
import Data.ByteString.Lazy qualified as BSL
7-
import Data.Default.Class (Default(def))
87
import Data.List (isInfixOf)
98
import Data.Map qualified
109
import Data.Set qualified
@@ -35,9 +34,8 @@ sampleRealisation0 :: Realisation
3534
sampleRealisation0 = Realisation
3635
{ realisationOutPath =
3736
forceRight
38-
$ System.Nix.StorePath.parsePath
39-
def
40-
"/nix/store/cdips4lakfk1qbf1x68fq18wnn3r5r14-builder.sh"
37+
$ System.Nix.StorePath.parseBasePath
38+
"cdips4lakfk1qbf1x68fq18wnn3r5r14-builder.sh"
4139
, realisationSignatures = mempty
4240
, realisationDependencies = mempty
4341
}
@@ -46,9 +44,8 @@ sampleRealisation1 :: Realisation
4644
sampleRealisation1 = Realisation
4745
{ realisationOutPath =
4846
forceRight
49-
$ System.Nix.StorePath.parsePath
50-
def
51-
"/nix/store/5rwxzi7pal3qhpsyfc16gzkh939q1np6-curl-7.82.0.drv"
47+
$ System.Nix.StorePath.parseBasePath
48+
"5rwxzi7pal3qhpsyfc16gzkh939q1np6-curl-7.82.0.drv"
5249
, realisationSignatures =
5350
Data.Set.fromList
5451
$ forceRight
@@ -60,9 +57,8 @@ sampleRealisation1 = Realisation
6057
Data.Map.fromList
6158
[ ( sampleDerivationOutput
6259
, forceRight
63-
$ System.Nix.StorePath.parsePathFromText
64-
def
65-
"/nix/store/9472ijanf79nlkb5n1yh57s7867p1930-testFixed"
60+
$ System.Nix.StorePath.parseBasePathFromText
61+
"9472ijanf79nlkb5n1yh57s7867p1930-testFixed"
6662
)
6763
]
6864
}

hnix-store-tests/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
# Unreleased
2+
3+
* Additions:
4+
* `Arbitrary` instance for `SingleDerivedPath`
5+
16
# 0.1.0.0 2024-07-31
27

38
* Initial release

hnix-store-tests/tests/StorePathSpec.hs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,13 @@ spec = do
2929
roundtrips
3030
(storePathToText storeDir)
3131
(Data.Attoparsec.Text.parseOnly $ pathParser storeDir)
32+
33+
prop "roundtrips using parseBasePath . storePathBaseToRawFilePath" $
34+
roundtrips
35+
storePathBaseToRawFilePath
36+
parseBasePath
37+
38+
prop "roundtrips using parseBasePathFromText . storePathBaseToText" $
39+
roundtrips
40+
storePathBaseToText
41+
parseBasePathFromText

0 commit comments

Comments
 (0)