Last active
April 12, 2018 23:18
-
-
Save Profpatsch/fcc5931305997ca18fee73f2ede886b7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ lib, pkgs, stdenv }: | |
{ libPath ? <nixpkgs/lib> }: | |
# TODO: Do we need all of lib? | |
let | |
trans = pkgs.writeScript "nix-json-trans" '' | |
#!${stdenv.shell} | |
DIR=$(mktemp -d) | |
cat >$DIR/script-input.nix <<EOF | |
with import ${libPath}; | |
$(cat "$1") | |
EOF | |
JSON="$(realpath "$2")" | |
cat >$DIR/script.nix <<EOF | |
let | |
f = import $DIR/script-input.nix; | |
json = builtins.fromJSON (builtins.readFile $JSON); | |
in | |
assert builtins.typeOf $JSON == "path"; | |
assert builtins.typeOf f == "lambda"; | |
f json | |
EOF | |
source ${pkgs.setupLocalNixStore} | |
${pkgs.nix}/bin/nix-instantiate \ | |
--eval --json --strict \ | |
--show-trace \ | |
$DIR/script.nix | |
''; | |
tests = | |
let | |
call = nixFile: jsonFile: "${trans} ${nixFile} ${jsonFile}"; | |
eq = nixFile: jsonFile: resultFile: '' | |
${pkgs.diffutils}/bin/diff <(${call nixFile jsonFile}) ${resultFile} | |
''; | |
mkjson = json: pkgs.writeText "test.json" (builtins.toJSON json); | |
in { | |
idJson = | |
let | |
json = mkjson { | |
foo = [ 1 2 3 null "bar" ]; | |
}; | |
idScr = pkgs.writeText "id-converter" "id"; | |
in eq idScr json json; | |
replaceAttrsJson = | |
eq (pkgs.writeText "maybe-its-neuer" | |
''mapAttrs (_: _: "manuel neuer")'') | |
(mkjson { foo = "richard stallman"; bar = "linus torvalds"; }) | |
(mkjson { foo = "manuel neuer"; bar = "manuel neuer"; }); | |
}; | |
in pkgs.withTests tests trans |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment