nix-eval-jobs/tests/test_eval.py
adisbladis 61c9f4cfcc Replace internal attr string representation with an array
This ensures correct handling of attrnames with a dot in them and will
not throw errors about illegal attrnames.

Additionally this escapes any attributes containing dots in the JSON
output and adds another field called `attrPath` which contains the
attribute path as a list.

Example output:
```
{
  "attr": "hello",
  "attrPath": [
    "hello"
  ],
  "drvPath": "/nix/store/n204jib73z55cp9s0rmw1c5v5q528j7v-hello-2.12.drv",
  "name": "hello-2.12",
  "outputs": {
    "out": "/nix/store/h59dfk7dwrn7d2csykh9z9xm2miqmrnz-hello-2.12"
  },
  "system": "x86_64-linux"
}
```
2022-04-26 19:43:56 +12:00

58 lines
1.7 KiB
Python

#!/usr/bin/env python3
import subprocess
import json
from tempfile import TemporaryDirectory
from pathlib import Path
from typing import List
TEST_ROOT = Path(__file__).parent.resolve()
PROJECT_ROOT = TEST_ROOT.parent
BIN = PROJECT_ROOT.joinpath("build", "src", "nix-eval-jobs")
def common_test(extra_args: List[str]) -> None:
with TemporaryDirectory() as tempdir:
cmd = [str(BIN), "--gc-roots-dir", tempdir, "--meta"] + extra_args
res = subprocess.run(
cmd,
cwd=TEST_ROOT.joinpath("assets"),
text=True,
check=True,
stdout=subprocess.PIPE,
)
results = [json.loads(r) for r in res.stdout.split("\n") if r]
assert len(results) == 5
built_job = results[0]
assert built_job["attr"] == "builtJob"
assert built_job["name"] == "job1"
assert built_job["outputs"]["out"].startswith("/nix/store")
assert built_job["drvPath"].endswith(".drv")
assert built_job["meta"]['broken'] is False
dotted_job = results[1]
assert dotted_job["attr"] == "\"dotted.attr\""
assert dotted_job["attrPath"] == [ "dotted.attr" ]
recurse_drv = results[2]
assert recurse_drv["attr"] == "recurse.drvB"
assert recurse_drv["name"] == "drvB"
recurse_recurse_bool = results[3]
assert "error" in recurse_recurse_bool
substituted_job = results[4]
assert substituted_job["attr"] == "substitutedJob"
assert substituted_job["name"].startswith("hello-")
assert substituted_job["meta"]['broken'] is False
def test_flake() -> None:
common_test(["--flake", ".#hydraJobs"])
def test_expression() -> None:
common_test(["ci.nix"])