diff --git a/app/nullius/importers/atif.py b/app/nullius/importers/atif.py index bc04b45f..e858882a 100644 --- a/app/nullius/importers/atif.py +++ b/app/nullius/importers/atif.py @@ -345,13 +345,6 @@ def _write_job(docs: list, root: str, out_root: str, job_name: str, command: str summaries = [convert_document(path, doc, data, name, job_dir) for (path, doc, data), name in zip(docs, names)] - # Derived output this importer wholly owns: a trial the tree no longer - # holds is stale and would keep being counted. - produced = {s["trial_dir"] for s in summaries} - for name in os.listdir(job_dir): - sub = os.path.join(job_dir, name) - if os.path.isdir(sub) and name not in produced: - shutil.rmtree(sub) # ordered as instants, not as text: one stamp with a zone and one without sort apart as strings starts = sorted((s["started_at"] for s in summaries if s["started_at"]), key=_instant) @@ -383,6 +376,14 @@ def _write_job(docs: list, root: str, out_root: str, job_name: str, command: str job_dir, ((name, os.path.realpath(path)) for (path, _doc, _data), name in zip(docs, names)), *existing), **importers.recorded_as(sid, root), }) + # Derived output this importer wholly owns: a trial the tree no longer + # holds is stale and would keep being counted. Pruned once + # config.json reserves its name: a refresh cut short before then keeps the folder. + produced = {s["trial_dir"] for s in summaries} + for name in os.listdir(job_dir): + sub = os.path.join(job_dir, name) + if os.path.isdir(sub) and name not in produced: + shutil.rmtree(sub) return job_dir diff --git a/app/nullius/importers/otel.py b/app/nullius/importers/otel.py index 9ceb077f..e044b32d 100644 --- a/app/nullius/importers/otel.py +++ b/app/nullius/importers/otel.py @@ -232,14 +232,6 @@ def _write_job(documents: list, out_root: str, job_name: str, command: str, os.makedirs(job_dir, exist_ok=True) summaries = [write_trial(doc, job_dir, name) for doc, name in zip(documents, names)] - # Derived output this importer wholly owns: a trial the export no longer - # produces is stale and would keep being counted. - produced = {s["trial_dir"] for s in summaries} - for name in os.listdir(job_dir): - sub = os.path.join(job_dir, name) - if os.path.isdir(sub) and name not in produced: - shutil.rmtree(sub) - starts = sorted(s["started_at"] for s in summaries if s["started_at"]) ends = sorted(s["finished_at"] for s in summaries if s["finished_at"]) _dump(os.path.join(job_dir, "result.json"), { @@ -266,6 +258,14 @@ def _write_job(documents: list, out_root: str, job_name: str, command: str, "trial_documents": importers.reservations(job_dir, zip(names, ids), *existing), **importers.recorded_as(sid, root), }) + # Derived output this importer wholly owns: a trial the export no longer + # produces is stale and would keep being counted. Pruned once + # config.json reserves its name: a refresh cut short before then keeps the folder. + produced = {s["trial_dir"] for s in summaries} + for name in os.listdir(job_dir): + sub = os.path.join(job_dir, name) + if os.path.isdir(sub) and name not in produced: + shutil.rmtree(sub) return job_dir diff --git a/app/tests/harnesses/test_atif.py b/app/tests/harnesses/test_atif.py index 1ef8f1cb..3cc876a0 100644 --- a/app/tests/harnesses/test_atif.py +++ b/app/tests/harnesses/test_atif.py @@ -16,6 +16,7 @@ import tempfile import unittest from pathlib import Path +from unittest import mock APP = Path(__file__).resolve().parents[2] sys.path.insert(0, str(APP)) @@ -82,6 +83,15 @@ def test_a_broken_file_that_says_it_is_atif_is_claimed_so_it_can_be_refused(self self.assertFalse(atif.detect(other)) +def cut_at_config(dump): + """*dump*, failing on the job's ``config.json``: a refresh cut short before it publishes.""" + def cut(path, data): + if os.path.basename(path) == "config.json": + raise OSError("cut short") + return dump(path, data) + return cut + + class Conversion(unittest.TestCase): def setUp(self): self.out = tempfile.mkdtemp() @@ -149,18 +159,23 @@ def test_one_tree_through_a_link_or_with_a_folder_deleted_by_hand_keeps_its_tria self.assertEqual(held["a__b"], "s-first") def test_a_gone_document_keeps_its_name_from_a_job_without_reservations_and_from_a_longer_name(self): - src = tempfile.mkdtemp() - write_doc(src, "a/b.json", minimal(session_id="s-gone")) - write_doc(src, "c.json", minimal()) - job_dir, _ = self.convert(src) - config = json.loads(Path(job_dir, "config.json").read_text()) - del config["trial_documents"] # as a conversion before them wrote it - Path(job_dir, "config.json").write_text(json.dumps(config)) - os.remove(os.path.join(src, "a", "b.json")) - self.convert(src) # its folder is pruned, its name kept - write_doc(src, "a__b.json", minimal(session_id="s-new")) - _, job = self.convert(src) - self.assertNotIn("a__b", {t.trial_name for t in job.trials}) + for cut in (False, True): + with self.subTest(cut_before_config=cut): + src = tempfile.mkdtemp() + write_doc(src, "a/b.json", minimal(session_id="s-gone")) + write_doc(src, "c.json", minimal()) + job_dir, _ = self.convert(src, job_name=f"cut-{cut}") + config = json.loads(Path(job_dir, "config.json").read_text()) + del config["trial_documents"] # as a conversion before them wrote it + Path(job_dir, "config.json").write_text(json.dumps(config)) + os.remove(os.path.join(src, "a", "b.json")) + if cut: # a first refresh cut short before its config + with mock.patch.object(atif, "_dump", cut_at_config(atif._dump)), self.assertRaises(OSError): + self.convert(src, job_name=f"cut-{cut}") + self.convert(src, job_name=f"cut-{cut}") # its folder is pruned, its name kept + write_doc(src, "a__b.json", minimal(session_id="s-new")) + _, job = self.convert(src, job_name=f"cut-{cut}") + self.assertNotIn("a__b", {t.trial_name for t in job.trials}) src = tempfile.mkdtemp() # a gone file named like another's longer name longer = "a__b__" + hashlib.sha256(b"a/b.json").hexdigest()[:8] diff --git a/app/tests/harnesses/test_otel_import.py b/app/tests/harnesses/test_otel_import.py index 1f68d467..565c2101 100644 --- a/app/tests/harnesses/test_otel_import.py +++ b/app/tests/harnesses/test_otel_import.py @@ -10,6 +10,7 @@ import tempfile import unittest from pathlib import Path +from unittest import mock APP = Path(__file__).resolve().parents[2] sys.path.insert(0, str(APP)) @@ -17,6 +18,15 @@ from nullius.importers import otel as importer +def cut_at_config(dump): + """*dump*, failing on the job's ``config.json``: a refresh cut short before it publishes.""" + def cut(path, data): + if os.path.basename(path) == "config.json": + raise OSError("cut short") + return dump(path, data) + return cut + + class Detect(unittest.TestCase): def test_a_directory_of_only_refused_files_is_not_claimed(self): @@ -108,18 +118,29 @@ def export(name, tid, span="agent run"): def trials(): return sorted(n for n in os.listdir(job_dir) if os.path.isdir(os.path.join(job_dir, n))) - export("a.json", gone) - export("x.json", other, "other") - job_dir = importer.convert(src, self.out)[0] - os.remove(os.path.join(src, "a.json")) - importer.convert(src, self.out) # its folder is pruned, its name kept - self.assertEqual(trials(), ["other__1234567"]) - export("b.json", newcomer) - importer.convert(src, self.out) - self.assertEqual(trials(), ["agent_run__" + newcomer, "other__1234567"]) - export("a.json", gone) # back: its own name again - importer.convert(src, self.out) - self.assertEqual(trials(), ["agent_run__abcdef1", "agent_run__" + newcomer, "other__1234567"]) + for cut in (False, True): + with self.subTest(cut_before_config=cut): + for name in os.listdir(src): + os.remove(os.path.join(src, name)) + export("a.json", gone) + export("x.json", other, "other") + job_dir = importer.convert(src, self.out, job_name=f"cut-{cut}")[0] + os.remove(os.path.join(src, "a.json")) + if cut: # a job without reservations, its first refresh cut short + config = json.loads(Path(job_dir, "config.json").read_text()) + del config["trial_documents"] + Path(job_dir, "config.json").write_text(json.dumps(config)) + with mock.patch.object(importer, "_dump", cut_at_config(importer._dump)), \ + self.assertRaises(OSError): + importer.convert(src, self.out, job_name=f"cut-{cut}") + importer.convert(src, self.out, job_name=f"cut-{cut}") # its folder is pruned, its name kept + self.assertEqual(trials(), ["other__1234567"]) + export("b.json", newcomer) + importer.convert(src, self.out, job_name=f"cut-{cut}") + self.assertEqual(trials(), ["agent_run__" + newcomer, "other__1234567"]) + export("a.json", gone) # back: its own name again + importer.convert(src, self.out, job_name=f"cut-{cut}") + self.assertEqual(trials(), ["agent_run__abcdef1", "agent_run__" + newcomer, "other__1234567"]) def test_a_refused_file_deeper_down_is_named_by_its_path_under_the_root_once(self): src = tempfile.mkdtemp()