2021-03-21 18:05:11 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
import json
|
2023-12-16 09:10:49 +00:00
|
|
|
import subprocess
|
2023-12-16 10:17:40 +00:00
|
|
|
import pytest
|
2021-03-21 18:05:11 +00:00
|
|
|
from pathlib import Path
|
2023-12-16 09:10:49 +00:00
|
|
|
from tempfile import TemporaryDirectory
|
|
|
|
from typing import Any, Dict, List
|
2021-03-21 18:05:11 +00:00
|
|
|
|
|
|
|
TEST_ROOT = Path(__file__).parent.resolve()
|
|
|
|
PROJECT_ROOT = TEST_ROOT.parent
|
2021-08-24 18:13:04 +00:00
|
|
|
BIN = PROJECT_ROOT.joinpath("build", "src", "nix-eval-jobs")
|
2021-03-21 18:05:11 +00:00
|
|
|
|
|
|
|
|
2022-09-09 11:24:53 +00:00
|
|
|
def common_test(extra_args: List[str]) -> List[Dict[str, Any]]:
|
2021-03-21 18:05:11 +00:00
|
|
|
with TemporaryDirectory() as tempdir:
|
2022-01-06 00:19:15 +00:00
|
|
|
cmd = [str(BIN), "--gc-roots-dir", tempdir, "--meta"] + extra_args
|
2021-03-21 18:05:11 +00:00
|
|
|
res = subprocess.run(
|
|
|
|
cmd,
|
|
|
|
cwd=TEST_ROOT.joinpath("assets"),
|
|
|
|
text=True,
|
|
|
|
check=True,
|
|
|
|
stdout=subprocess.PIPE,
|
|
|
|
)
|
2021-08-25 08:36:32 +00:00
|
|
|
|
2021-08-25 17:43:35 +00:00
|
|
|
results = [json.loads(r) for r in res.stdout.split("\n") if r]
|
2022-09-09 11:26:15 +00:00
|
|
|
assert len(results) == 4
|
2021-08-25 08:36:32 +00:00
|
|
|
|
2021-08-25 17:43:35 +00:00
|
|
|
built_job = results[0]
|
|
|
|
assert built_job["attr"] == "builtJob"
|
2021-08-26 21:56:23 +00:00
|
|
|
assert built_job["name"] == "job1"
|
2022-01-04 08:43:06 +00:00
|
|
|
assert built_job["outputs"]["out"].startswith("/nix/store")
|
2021-12-31 11:00:04 +00:00
|
|
|
assert built_job["drvPath"].endswith(".drv")
|
2022-04-26 09:36:48 +00:00
|
|
|
assert built_job["meta"]["broken"] is False
|
2021-08-25 17:43:35 +00:00
|
|
|
|
2022-04-26 05:11:17 +00:00
|
|
|
dotted_job = results[1]
|
2022-04-26 09:36:48 +00:00
|
|
|
assert dotted_job["attr"] == '"dotted.attr"'
|
|
|
|
assert dotted_job["attrPath"] == ["dotted.attr"]
|
2022-04-26 05:11:17 +00:00
|
|
|
|
|
|
|
recurse_drv = results[2]
|
2022-04-25 09:23:06 +00:00
|
|
|
assert recurse_drv["attr"] == "recurse.drvB"
|
|
|
|
assert recurse_drv["name"] == "drvB"
|
2022-04-22 08:36:52 +00:00
|
|
|
|
2022-09-09 11:26:15 +00:00
|
|
|
substituted_job = results[3]
|
2021-08-25 17:43:35 +00:00
|
|
|
assert substituted_job["attr"] == "substitutedJob"
|
2021-08-26 21:56:23 +00:00
|
|
|
assert substituted_job["name"].startswith("hello-")
|
2022-04-26 09:36:48 +00:00
|
|
|
assert substituted_job["meta"]["broken"] is False
|
2022-09-09 11:24:53 +00:00
|
|
|
return results
|
2021-03-21 18:05:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_flake() -> None:
|
2022-09-09 11:24:53 +00:00
|
|
|
results = common_test(["--flake", ".#hydraJobs"])
|
|
|
|
for result in results:
|
|
|
|
assert "isCached" not in result
|
|
|
|
|
|
|
|
|
|
|
|
def test_query_cache_status() -> None:
|
|
|
|
results = common_test(["--flake", ".#hydraJobs", "--check-cache-status"])
|
2023-07-21 01:25:44 +00:00
|
|
|
# FIXME in the nix sandbox we cannot query binary caches
|
|
|
|
# this would need some local one
|
2022-09-09 11:24:53 +00:00
|
|
|
for result in results:
|
|
|
|
assert "isCached" in result
|
2021-03-21 18:05:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_expression() -> None:
|
2022-09-09 11:24:53 +00:00
|
|
|
results = common_test(["ci.nix"])
|
|
|
|
for result in results:
|
|
|
|
assert "isCached" not in result
|
2022-08-17 06:38:37 +00:00
|
|
|
|
|
|
|
with open(TEST_ROOT.joinpath("assets/ci.nix"), "r") as ci_nix:
|
|
|
|
common_test(["-E", ci_nix.read()])
|
2023-12-10 15:58:22 +00:00
|
|
|
|
2023-12-10 16:00:31 +00:00
|
|
|
|
2023-12-10 15:58:22 +00:00
|
|
|
def test_eval_error() -> None:
|
|
|
|
with TemporaryDirectory() as tempdir:
|
2023-12-10 16:00:31 +00:00
|
|
|
cmd = [
|
|
|
|
str(BIN),
|
|
|
|
"--gc-roots-dir",
|
|
|
|
tempdir,
|
|
|
|
"--meta",
|
2023-12-16 09:10:49 +00:00
|
|
|
"--workers",
|
|
|
|
"1",
|
2023-12-10 16:00:31 +00:00
|
|
|
"--flake",
|
2023-12-16 09:10:49 +00:00
|
|
|
".#legacyPackages.x86_64-linux.brokenPkgs",
|
2023-12-10 16:00:31 +00:00
|
|
|
]
|
2023-12-10 15:58:22 +00:00
|
|
|
res = subprocess.run(
|
|
|
|
cmd,
|
|
|
|
cwd=TEST_ROOT.joinpath("assets"),
|
|
|
|
text=True,
|
|
|
|
stdout=subprocess.PIPE,
|
|
|
|
)
|
2023-12-16 09:10:49 +00:00
|
|
|
print(res.stdout)
|
2023-12-10 15:58:22 +00:00
|
|
|
attrs = json.loads(res.stdout)
|
|
|
|
assert attrs["attr"] == "brokenPackage"
|
2023-12-10 16:00:31 +00:00
|
|
|
assert "this is an evaluation error" in attrs["error"]
|
2023-12-16 09:10:49 +00:00
|
|
|
|
|
|
|
|
2023-12-16 10:17:40 +00:00
|
|
|
@pytest.mark.infiniterecursion
|
2023-12-16 09:10:49 +00:00
|
|
|
def test_recursion_error() -> None:
|
|
|
|
with TemporaryDirectory() as tempdir:
|
|
|
|
cmd = [
|
|
|
|
str(BIN),
|
|
|
|
"--gc-roots-dir",
|
|
|
|
tempdir,
|
|
|
|
"--meta",
|
|
|
|
"--workers",
|
|
|
|
"1",
|
|
|
|
"--flake",
|
|
|
|
".#legacyPackages.x86_64-linux.infiniteRecursionPkgs",
|
|
|
|
]
|
|
|
|
res = subprocess.run(
|
|
|
|
cmd,
|
|
|
|
cwd=TEST_ROOT.joinpath("assets"),
|
|
|
|
text=True,
|
|
|
|
stderr=subprocess.PIPE,
|
|
|
|
)
|
|
|
|
assert res.returncode == 1
|
|
|
|
print(res.stderr)
|
2023-12-16 10:44:02 +00:00
|
|
|
assert "packageWithInfiniteRecursion" in res.stderr
|
2023-12-16 09:10:49 +00:00
|
|
|
assert "possible infinite recursion" in res.stderr
|