From d49c144a963588517078f82cda167f4819e89038 Mon Sep 17 00:00:00 2001 From: Isaiah Odhner Date: Fri, 8 Sep 2023 11:04:28 -0400 Subject: [PATCH] Create script for merging ambr files --- cspell.json | 1 + tests/merge_ambr.py | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 tests/merge_ambr.py diff --git a/cspell.json b/cspell.json index 2fbc9c8..c271670 100644 --- a/cspell.json +++ b/cspell.json @@ -23,6 +23,7 @@ ], "words": [ "afterstep", + "ambr", "Andale", "APNG", "appcode", diff --git a/tests/merge_ambr.py b/tests/merge_ambr.py new file mode 100644 index 0000000..9c744e1 --- /dev/null +++ b/tests/merge_ambr.py @@ -0,0 +1,39 @@ +"""Merge Amber snapshot files to include test results from both, and warn if shared results don't match. + +This allows applying the TDD principle to snapshot tests, in the specific case that +tests can pass in isolation, but fail when run together. +""" + +import re +from pathlib import Path + +DIR = Path(__file__).parent / "__snapshots__" + +files = [ + DIR / "test_snapshots.ambr", + DIR / "test_snapshots_ascii.ambr", +] +output_file = DIR / "test_snapshots_merged.ambr" + +re_snapshot = re.compile(r"^# name: (test_.*)\n '''\n((?:.|\n )*)\n '''$", re.MULTILINE) + +snapshots: dict[str, str] = {} +names: set[str] = set() +for file in files: + with open(file, "r", encoding="utf-8") as f: + for match in re_snapshot.finditer(f.read()): + name = match.group(1) + snapshot = match.group(2) + if name in snapshots: + if snapshots[name] != snapshot: + print(f"Snapshots don't match for {name}; preferring result from {files[0]}") + else: + snapshots[name] = snapshot + names.add(name) + +with open(output_file, "w", encoding="utf-8") as f: + f.write(f"# serializer version: 1\n") + for name, snapshot in snapshots.items(): + f.write(f"# name: {name}\n '''\n{snapshot}\n '''\n# ---\n") + +print(f"Merged {len(names)} snapshots from {len(files)} files into {output_file}")