|
12 | 12 | tags (a diff whose from tag published no catalog is recomputed offline; one |
13 | 13 | whose from tag published a catalog is rebuilt by running the diff backwards; |
14 | 14 | a hidden removal, a forged null from and required tags fail), the flags a |
15 | | -sibling catalog uses, and this tree's own checked-in diff. |
| 15 | +sibling catalog uses, and this tree's own checked-in diff. What a diff says of |
| 16 | +a code follows the diff's own start (from.version against the code's since), |
| 17 | +so those tests hold before and after the reset a release brings. |
16 | 18 |
|
17 | 19 | The documentation page lists every class, code and argument kind of |
18 | 20 | docker/run_failure_codes.json between two markers; test_docs_tables holds |
@@ -111,6 +113,17 @@ def catalog_bytes(codes: dict) -> bytes: |
111 | 113 | return (json.dumps(catalog, indent=1, sort_keys=True) + "\n").encode() |
112 | 114 |
|
113 | 115 |
|
| 116 | +def field_at(entry: dict, path: str) -> tuple: |
| 117 | + """Whether `path` (object keys joined by '.', as a diff change spells a |
| 118 | + field) names a member of `entry`, and that member (None when it does not).""" |
| 119 | + value = entry |
| 120 | + for key in path.split("."): |
| 121 | + if not isinstance(value, dict) or key not in value: |
| 122 | + return False, None |
| 123 | + value = value[key] |
| 124 | + return True, value |
| 125 | + |
| 126 | + |
114 | 127 | class Repo: |
115 | 128 | def __init__(self, path: Path): |
116 | 129 | self.path = path |
@@ -187,6 +200,57 @@ def release(self, tag: str) -> None: |
187 | 200 | self.repo.tag(tag) |
188 | 201 | self.ok(self.repo.run("--check")) |
189 | 202 |
|
| 203 | + def assert_diff_states_code(self, diff: dict, name: str, entry: dict) -> None: |
| 204 | + """What `diff` must say of the code `name`, whose catalog entry is `entry`. |
| 205 | +
|
| 206 | + The diff's own start decides, so one expectation holds before and after |
| 207 | + the reset a release brings (--reset: from that release, nothing added). |
| 208 | + `since` is the release that first ships the code, from.version the |
| 209 | + release whose catalog the diff is against; the generator's version_key |
| 210 | + compares them as numbers, a candidate X.Y.Z-rc.N below its release X.Y.Z. |
| 211 | + """ |
| 212 | + start, since = diff["from"]["version"], entry["since"] |
| 213 | + start_key, since_key = generator.version_key(start), generator.version_key(since) |
| 214 | + self.assertIsNotNone(start_key, f"from.version {start!r} is no release version") |
| 215 | + self.assertIsNotNone(since_key, f"{name}: since {since!r} is no release version") |
| 216 | + added = [item for item in diff["added"] if item["code"] == name] |
| 217 | + self.assertNotIn(name, [item["code"] for item in diff["removed"]], |
| 218 | + f"{name} is removed: a published code is deprecated, never removed") |
| 219 | + # since is X.Y.Z, which a release shares with its candidates: from a candidate of |
| 220 | + # its own release (since X.Y.Z, the diff from X.Y.Z-rc.N) it cannot tell whether |
| 221 | + # the candidate shipped the code, so the diff says, and is held to what it says. |
| 222 | + after_start = bool(added) if start.startswith(since + "-rc.") else since_key > start_key |
| 223 | + if after_start: |
| 224 | + # It has not shipped in the diff's published base: corrections to its |
| 225 | + # class/args are part of the whole addition, not a breaking change. |
| 226 | + self.assertEqual(added, [{"code": name, "collection": "codes", "entry": entry}], |
| 227 | + f"{name} first ships in {since}, after {start} where the diff " |
| 228 | + "starts: the diff must add it, whole") |
| 229 | + self.assertNotIn(name, [item["code"] for item in diff["changed"]], |
| 230 | + f"{name} first ships in {since}, after {start} where the diff " |
| 231 | + "starts: it is an addition, not a change") |
| 232 | + return |
| 233 | + # The start's catalog carries it: the diff does not add it. It may list it as |
| 234 | + # changed -- a correction made since the release -- but for real changes only, |
| 235 | + # and as this tree's catalog holds them. |
| 236 | + self.assertEqual(added, [], |
| 237 | + f"{name} first ships in {since}, not after {start} where the diff " |
| 238 | + "starts: it is no addition") |
| 239 | + changed = [item for item in diff["changed"] if item["code"] == name] |
| 240 | + self.assertLessEqual(len(changed), 1, f"{name} is changed twice") |
| 241 | + for item in changed: |
| 242 | + self.assertEqual(item["collection"], "codes") |
| 243 | + self.assertTrue(item["fields"], f"{name} is changed in no field") |
| 244 | + for change in item["fields"]: |
| 245 | + sides = [(change[f"{side}Present"], json.dumps(change[side], sort_keys=True)) |
| 246 | + for side in ("before", "after")] |
| 247 | + where = f"{name} {change['path']}" |
| 248 | + self.assertNotEqual(sides[0], sides[1], |
| 249 | + f"{where} is listed as changed, and did not change") |
| 250 | + held, value = field_at(entry, change["path"]) |
| 251 | + self.assertEqual(sides[1], (held, json.dumps(value, sort_keys=True)), |
| 252 | + f"{where}: the after side is not this tree's catalog") |
| 253 | + |
190 | 254 | def test_self_test(self) -> None: |
191 | 255 | result = subprocess.run([sys.executable, str(SCRIPT), "--self-test"], |
192 | 256 | capture_output=True, text=True) |
@@ -455,14 +519,69 @@ def test_report_sink_failed_is_engine_fault(self) -> None: |
455 | 519 | "report sink write failed", |
456 | 520 | "report sink write made no progress", |
457 | 521 | ]) |
458 | | - # It has not shipped in the diff's published base: corrections to its |
459 | | - # class/args are part of the whole addition, not a breaking change. |
| 522 | + # What the diff says of the code follows the diff's own start: an addition |
| 523 | + # while its release is in progress (the diff starts at an older release), |
| 524 | + # part of the base once a release reset has moved the start to its release. |
460 | 525 | diff = json.loads((ROOT / DIFF).read_text(encoding="utf-8")) |
461 | | - additions = [item for item in diff["added"] if item["code"] == "report_sink_failed"] |
462 | | - self.assertEqual(additions, [ |
463 | | - {"code": "report_sink_failed", "collection": "codes", "entry": entry}, |
464 | | - ]) |
465 | | - self.assertNotIn("report_sink_failed", [item["code"] for item in diff["changed"]]) |
| 526 | + self.assert_diff_states_code(diff, "report_sink_failed", entry) |
| 527 | + |
| 528 | + def test_a_codes_diff_holds_before_and_after_its_release_reset(self) -> None: |
| 529 | + # alpha ships in 1.1.0. While that release is in progress the diff, from |
| 530 | + # v1.0.0, adds it; after the reset the release brings it is in the diff's |
| 531 | + # base and the diff adds nothing. One expectation holds on both sides. |
| 532 | + alpha = code("1.1.0") |
| 533 | + self.start({"alpha": alpha}) |
| 534 | + before = self.repo.diff() |
| 535 | + self.assertEqual([item["code"] for item in before["added"]], ["alpha"]) |
| 536 | + self.assert_diff_states_code(before, "alpha", alpha) |
| 537 | + self.release("v1.1.0") |
| 538 | + after = self.repo.diff() |
| 539 | + self.assertEqual(after["added"], []) |
| 540 | + self.assert_diff_states_code(after, "alpha", alpha) |
| 541 | + |
| 542 | + def refused(diff: dict, needle: str) -> None: |
| 543 | + with self.assertRaises(AssertionError) as raised: |
| 544 | + self.assert_diff_states_code(diff, "alpha", alpha) |
| 545 | + self.assertIn(needle, str(raised.exception)) |
| 546 | + |
| 547 | + def starting_at(diff: dict, version: str) -> dict: |
| 548 | + moved = json.loads(json.dumps(diff)) |
| 549 | + moved["from"]["version"] = version |
| 550 | + return moved |
| 551 | + |
| 552 | + # The expectation can fail: each diff below says the other side's thing. |
| 553 | + refused(starting_at(before, "1.1.0"), "is no addition") |
| 554 | + refused(starting_at(after, "1.0.0"), "the diff must add it") |
| 555 | + # A candidate sorts below its release: 1.0.9-rc.2 starts before 1.1.0, |
| 556 | + # 1.2.0-rc.1 after it. |
| 557 | + for version, state in (("1.0.9-rc.2", before), ("1.2.0-rc.1", after)): |
| 558 | + self.assert_diff_states_code(starting_at(state, version), "alpha", alpha) |
| 559 | + refused(starting_at(after, "1.0.9-rc.2"), "the diff must add it") |
| 560 | + refused(starting_at(before, "1.2.0-rc.1"), "is no addition") |
| 561 | + # 1.1.0-rc.1 shares alpha's since: the diff says whether the candidate |
| 562 | + # shipped alpha, and either way is held to its rules. |
| 563 | + for state in (before, after): |
| 564 | + self.assert_diff_states_code(starting_at(state, "1.1.0-rc.1"), "alpha", alpha) |
| 565 | + # A correction made after the release is a change of a published code: the |
| 566 | + # diff may say so, truthfully, and nothing else. |
| 567 | + said = {"path": "description", "beforePresent": True, "afterPresent": True, |
| 568 | + "before": "older", "after": "d"} |
| 569 | + corrected = starting_at(after, "1.1.0") |
| 570 | + corrected["changed"] = [{"code": "alpha", "collection": "codes", "fields": [said]}] |
| 571 | + self.assert_diff_states_code(corrected, "alpha", alpha) |
| 572 | + for lie, needle in ((dict(said, before="d"), "did not change"), |
| 573 | + (dict(said, after="e"), "not this tree's catalog")): |
| 574 | + lying = starting_at(corrected, "1.1.0") |
| 575 | + lying["changed"][0]["fields"] = [lie] |
| 576 | + refused(lying, needle) |
| 577 | + # Before its release the code is an addition, whole: no change beside it. |
| 578 | + unreleased = starting_at(before, "1.0.0") |
| 579 | + unreleased["changed"] = corrected["changed"] |
| 580 | + refused(unreleased, "not a change") |
| 581 | + # A published code is never removed. |
| 582 | + removed = starting_at(after, "1.1.0") |
| 583 | + removed["removed"] = [{"code": "alpha", "collection": "codes", "entry": alpha}] |
| 584 | + refused(removed, "is removed") |
466 | 585 |
|
467 | 586 | def test_docs_tables(self) -> None: |
468 | 587 | catalog = json.loads((ROOT / CATALOG).read_text(encoding="utf-8")) |
|
0 commit comments