From a6171c9a620ee042ac25337346a59e9f768f6c00 Mon Sep 17 00:00:00 2001 From: adisbladis Date: Wed, 25 Aug 2021 12:43:35 -0500 Subject: [PATCH] Switch to streaming output (using line delimited JSON) --- src/nix-eval-jobs.cc | 21 +++++++-------------- tests/test_eval.py | 14 ++++++++++---- 2 files changed, 17 insertions(+), 18 deletions(-) diff --git a/src/nix-eval-jobs.cc b/src/nix-eval-jobs.cc index 8e35234..2840194 100644 --- a/src/nix-eval-jobs.cc +++ b/src/nix-eval-jobs.cc @@ -156,6 +156,7 @@ static void worker( /* Evaluate it and send info back to the master. */ nlohmann::json reply; + reply["attr"] = attrPath; try { auto vTmp = findAlongAttrPath(state, attrPath, autoArgs, *vRoot).first; @@ -215,7 +216,8 @@ static void worker( reply["job"] = std::move(job); } - else if (v->type() == nAttrs) { + else if (v->type() == nAttrs) + { auto attrs = nlohmann::json::array(); StringSet ss; for (auto & i : v->attrs->lexicographicOrder()) { @@ -287,7 +289,6 @@ int main(int argc, char * * argv) { std::set todo{""}; std::set active; - nlohmann::json jobs; std::exception_ptr exc; }; @@ -369,26 +370,19 @@ int main(int argc, char * * argv) writeLine(to.get(), "do " + attrPath); /* Wait for the response. */ - auto response = nlohmann::json::parse(readLine(from.get())); + auto respString = readLine(from.get()); + auto response = nlohmann::json::parse(respString); /* Handle the response. */ StringSet newAttrs; - - if (response.find("job") != response.end()) { - auto state(state_.lock()); - state->jobs[attrPath] = response["job"]; - } - if (response.find("attrs") != response.end()) { for (auto & i : response["attrs"]) { auto s = (attrPath.empty() ? "" : attrPath + ".") + (std::string) i; newAttrs.insert(s); } - } - - if (response.find("error") != response.end()) { + } else { auto state(state_.lock()); - state->jobs[attrPath]["error"] = response["error"]; + std::cout << respString << "\n"; } /* Add newly discovered job names to the queue. */ @@ -419,6 +413,5 @@ int main(int argc, char * * argv) if (state->exc) std::rethrow_exception(state->exc); - std::cout << state->jobs.dump(2) << "\n"; }); } diff --git a/tests/test_eval.py b/tests/test_eval.py index fe5a826..7f5e2bf 100644 --- a/tests/test_eval.py +++ b/tests/test_eval.py @@ -21,12 +21,18 @@ def common_test(extra_args: List[str]) -> None: check=True, stdout=subprocess.PIPE, ) - data = json.loads(res.stdout) - assert data["builtJob"]["nixName"] == "job1" - assert "out" in data["builtJob"]["outputs"] + results = [json.loads(r) for r in res.stdout.split("\n") if r] + assert len(results) == 2 - assert data["substitutedJob"]["nixName"].startswith("hello-") + built_job = results[0] + assert built_job["attr"] == "builtJob" + assert built_job["job"]["nixName"] == "job1" + + + substituted_job = results[1] + assert substituted_job["attr"] == "substitutedJob" + assert substituted_job["job"]["nixName"].startswith("hello-") def test_flake() -> None: