Skip to content

Commit 1076926

Browse files
committed
core: add System.Nix.Derivation and tests
1 parent 1127725 commit 1076926

File tree

7 files changed

+80
-0
lines changed

7 files changed

+80
-0
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ cabal-version: >=1.10
1919
library
2020
exposed-modules: System.Nix.Base32
2121
, System.Nix.Build
22+
, System.Nix.Derivation
2223
, System.Nix.Hash
2324
, System.Nix.Internal.Base32
2425
, System.Nix.Internal.Hash
@@ -43,6 +44,7 @@ library
4344
, filepath
4445
, hashable
4546
, mtl
47+
, nix-derivation >= 1.1.1 && <2
4648
, saltine
4749
, time
4850
, text
@@ -64,6 +66,7 @@ test-suite format-tests
6466
main-is: Driver.hs
6567
other-modules:
6668
Arbitrary
69+
Derivation
6770
NarFormat
6871
Hash
6972
StorePath
@@ -83,6 +86,7 @@ test-suite format-tests
8386
, process
8487
, tasty
8588
, tasty-discover
89+
, tasty-golden
8690
, tasty-hspec
8791
, tasty-hunit
8892
, tasty-quickcheck
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{-# LANGUAGE OverloadedStrings #-}
2+
3+
module System.Nix.Derivation (
4+
parseDerivation
5+
, buildDerivation
6+
) where
7+
8+
import Data.Attoparsec.Text.Lazy (Parser)
9+
import Data.ByteString (ByteString)
10+
import Data.Text (Text)
11+
import Data.Text.Lazy.Builder (Builder)
12+
import Nix.Derivation (Derivation)
13+
import System.Nix.StorePath (StorePath, pathParser)
14+
15+
import qualified Data.ByteString.Char8
16+
import qualified Data.Text
17+
import qualified Data.Text.Lazy.Builder
18+
import qualified Data.Attoparsec.Text.Lazy
19+
20+
import qualified Nix.Derivation
21+
import qualified System.Nix.StorePath
22+
23+
parseDerivation :: FilePath -> Parser (Derivation StorePath Text)
24+
parseDerivation expectedRoot =
25+
Nix.Derivation.parseDerivationWith
26+
("\"" *> System.Nix.StorePath.pathParser expectedRoot <* "\"")
27+
Nix.Derivation.textParser
28+
29+
buildDerivation :: Derivation StorePath Text -> Builder
30+
buildDerivation derivation =
31+
Nix.Derivation.buildDerivationWith
32+
(string . Data.Text.pack . show)
33+
string
34+
derivation
35+
where
36+
string = Data.Text.Lazy.Builder.fromText . Data.Text.pack . show
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
2+
module Derivation where
3+
4+
import Test.Tasty (TestTree, testGroup)
5+
import Test.Tasty.Golden (goldenVsFile)
6+
7+
import System.Nix.Derivation (parseDerivation, buildDerivation)
8+
9+
import qualified Data.Attoparsec.Text.Lazy
10+
import qualified Data.Text.IO
11+
import qualified Data.Text.Lazy
12+
import qualified Data.Text.Lazy.Builder
13+
14+
processDerivation source dest = do
15+
contents <- Data.Text.IO.readFile source
16+
case Data.Attoparsec.Text.Lazy.parseOnly (parseDerivation "/nix/store") contents of
17+
Left e -> error e
18+
Right drv ->
19+
Data.Text.IO.writeFile dest
20+
. Data.Text.Lazy.toStrict
21+
. Data.Text.Lazy.Builder.toLazyText
22+
$ buildDerivation drv
23+
24+
test_derivation :: TestTree
25+
test_derivation = testGroup "golden" $ map mk [0..1]
26+
where
27+
mk n =
28+
let
29+
fp = "tests/samples/example"
30+
drv = (fp ++ show n ++ ".drv")
31+
act = (fp ++ show n ++ ".actual")
32+
in
33+
goldenVsFile
34+
("derivation roundtrip of " ++ drv)
35+
drv act (processDerivation drv act)
36+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Derive([("devdoc","/nix/store/15x9ii8c3n5wb5lg80cm8x0yk6zy7rha-perl-MIME-Types-2.13-devdoc","",""),("out","/nix/store/93d75ghjyibmbxgfzwhh4b5zwsxzs44w-perl-MIME-Types-2.13","","")],[("/nix/store/cvdbbvnvg131bz9bwyyk97jpq1crclqr-MIME-Types-2.13.tar.gz.drv",["out"]),("/nix/store/p5g31bc5x92awghx9dlm065d7j773l0r-stdenv.drv",["out"]),("/nix/store/x50y5qihwsn0lfjhrf1s81b5hgb9w632-bash-4.4-p5.drv",["out"]),("/nix/store/57h2hjsdkdiwbzilcjqkn46138n1xb4a-perl-5.22.3.drv",["out"])],["/nix/store/cdips4lakfk1qbf1x68fq18wnn3r5r14-builder.sh"],"x86_64-linux","/nix/store/fi3mbd2ml4pbgzyasrlnp0wyy6qi48fh-bash-4.4-p5/bin/bash",["-e","/nix/store/cdips4lakfk1qbf1x68fq18wnn3r5r14-builder.sh"],[("AUTOMATED_TESTING","1"),("PERL_AUTOINSTALL","--skipdeps"),("buildInputs",""),("builder","/nix/store/fi3mbd2ml4pbgzyasrlnp0wyy6qi48fh-bash-4.4-p5/bin/bash"),("checkTarget","test"),("devdoc","/nix/store/15x9ii8c3n5wb5lg80cm8x0yk6zy7rha-perl-MIME-Types-2.13-devdoc"),("doCheck","1"),("installTargets","pure_install"),("name","perl-MIME-Types-2.13"),("nativeBuildInputs","/nix/store/nsa311yg8h93wfaacjk16c96a98bs09f-perl-5.22.3"),("out","/nix/store/93d75ghjyibmbxgfzwhh4b5zwsxzs44w-perl-MIME-Types-2.13"),("outputs","out devdoc"),("propagatedBuildInputs",""),("propagatedNativeBuildInputs",""),("src","/nix/store/5smhymz7viq8p47mc3jgyvqd003ab732-MIME-Types-2.13.tar.gz"),("stdenv","/nix/store/s3rlr45jzlzx0d6k2azlpxa5zwzr7xyy-stdenv"),("system","x86_64-linux")])
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Derive([("devdoc","/nix/store/15x9ii8c3n5wb5lg80cm8x0yk6zy7rha-perl-MIME-Types-2.13-devdoc","",""),("out","/nix/store/93d75ghjyibmbxgfzwhh4b5zwsxzs44w-perl-MIME-Types-2.13","","")],[("/nix/store/cvdbbvnvg131bz9bwyyk97jpq1crclqr-MIME-Types-2.13.tar.gz.drv",["out"]),("/nix/store/p5g31bc5x92awghx9dlm065d7j773l0r-stdenv.drv",["out"]),("/nix/store/x50y5qihwsn0lfjhrf1s81b5hgb9w632-bash-4.4-p5.drv",["out"]),("/nix/store/57h2hjsdkdiwbzilcjqkn46138n1xb4a-perl-5.22.3.drv",["out"])],["/nix/store/cdips4lakfk1qbf1x68fq18wnn3r5r14-builder.sh"],"x86_64-linux","/nix/store/fi3mbd2ml4pbgzyasrlnp0wyy6qi48fh-bash-4.4-p5/bin/bash",["-e","/nix/store/cdips4lakfk1qbf1x68fq18wnn3r5r14-builder.sh"],[("AUTOMATED_TESTING","1"),("PERL_AUTOINSTALL","--skipdeps"),("buildInputs",""),("builder","/nix/store/fi3mbd2ml4pbgzyasrlnp0wyy6qi48fh-bash-4.4-p5/bin/bash"),("checkTarget","test"),("devdoc","/nix/store/15x9ii8c3n5wb5lg80cm8x0yk6zy7rha-perl-MIME-Types-2.13-devdoc"),("doCheck","1"),("installTargets","pure_install"),("name","perl-MIME-Types-2.13"),("nativeBuildInputs","/nix/store/nsa311yg8h93wfaacjk16c96a98bs09f-perl-5.22.3"),("out","/nix/store/93d75ghjyibmbxgfzwhh4b5zwsxzs44w-perl-MIME-Types-2.13"),("outputs","out devdoc"),("propagatedBuildInputs",""),("propagatedNativeBuildInputs",""),("src","/nix/store/5smhymz7viq8p47mc3jgyvqd003ab732-MIME-Types-2.13.tar.gz"),("stdenv","/nix/store/s3rlr45jzlzx0d6k2azlpxa5zwzr7xyy-stdenv"),("system","x86_64-linux")])

0 commit comments

Comments
 (0)