|
10 | 10 | import tempfile |
11 | 11 | import unittest |
12 | 12 | from pathlib import Path |
| 13 | +from unittest import mock |
13 | 14 |
|
14 | 15 | APP = Path(__file__).resolve().parents[2] |
15 | 16 | sys.path.insert(0, str(APP)) |
16 | 17 |
|
17 | 18 | from nullius.importers import otel as importer |
18 | 19 |
|
19 | 20 |
|
| 21 | +def cut_at_config(dump): |
| 22 | + """*dump*, failing on the job's ``config.json``: a refresh cut short before it publishes.""" |
| 23 | + def cut(path, data): |
| 24 | + if os.path.basename(path) == "config.json": |
| 25 | + raise OSError("cut short") |
| 26 | + return dump(path, data) |
| 27 | + return cut |
| 28 | + |
| 29 | + |
20 | 30 | class Detect(unittest.TestCase): |
21 | 31 |
|
22 | 32 | def test_a_directory_of_only_refused_files_is_not_claimed(self): |
@@ -108,18 +118,29 @@ def export(name, tid, span="agent run"): |
108 | 118 | def trials(): |
109 | 119 | return sorted(n for n in os.listdir(job_dir) if os.path.isdir(os.path.join(job_dir, n))) |
110 | 120 |
|
111 | | - export("a.json", gone) |
112 | | - export("x.json", other, "other") |
113 | | - job_dir = importer.convert(src, self.out)[0] |
114 | | - os.remove(os.path.join(src, "a.json")) |
115 | | - importer.convert(src, self.out) # its folder is pruned, its name kept |
116 | | - self.assertEqual(trials(), ["other__1234567"]) |
117 | | - export("b.json", newcomer) |
118 | | - importer.convert(src, self.out) |
119 | | - self.assertEqual(trials(), ["agent_run__" + newcomer, "other__1234567"]) |
120 | | - export("a.json", gone) # back: its own name again |
121 | | - importer.convert(src, self.out) |
122 | | - self.assertEqual(trials(), ["agent_run__abcdef1", "agent_run__" + newcomer, "other__1234567"]) |
| 121 | + for cut in (False, True): |
| 122 | + with self.subTest(cut_before_config=cut): |
| 123 | + for name in os.listdir(src): |
| 124 | + os.remove(os.path.join(src, name)) |
| 125 | + export("a.json", gone) |
| 126 | + export("x.json", other, "other") |
| 127 | + job_dir = importer.convert(src, self.out, job_name=f"cut-{cut}")[0] |
| 128 | + os.remove(os.path.join(src, "a.json")) |
| 129 | + if cut: # a job without reservations, its first refresh cut short |
| 130 | + config = json.loads(Path(job_dir, "config.json").read_text()) |
| 131 | + del config["trial_documents"] |
| 132 | + Path(job_dir, "config.json").write_text(json.dumps(config)) |
| 133 | + with mock.patch.object(importer, "_dump", cut_at_config(importer._dump)), \ |
| 134 | + self.assertRaises(OSError): |
| 135 | + importer.convert(src, self.out, job_name=f"cut-{cut}") |
| 136 | + importer.convert(src, self.out, job_name=f"cut-{cut}") # its folder is pruned, its name kept |
| 137 | + self.assertEqual(trials(), ["other__1234567"]) |
| 138 | + export("b.json", newcomer) |
| 139 | + importer.convert(src, self.out, job_name=f"cut-{cut}") |
| 140 | + self.assertEqual(trials(), ["agent_run__" + newcomer, "other__1234567"]) |
| 141 | + export("a.json", gone) # back: its own name again |
| 142 | + importer.convert(src, self.out, job_name=f"cut-{cut}") |
| 143 | + self.assertEqual(trials(), ["agent_run__abcdef1", "agent_run__" + newcomer, "other__1234567"]) |
123 | 144 |
|
124 | 145 | def test_a_refused_file_deeper_down_is_named_by_its_path_under_the_root_once(self): |
125 | 146 | src = tempfile.mkdtemp() |
|
0 commit comments