Repository navigation
Expand file tree
/
Copy pathverify.py
More file actions
89 lines (73 loc) · 3.56 KB
/
Copy pathverify.py
File metadata and controls
89 lines (73 loc) · 3.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
"""
Smoke test for 09-transactional. See SUITE.md "09 -- transactional
(capstone)" for the required markers this checks. Runs run.py TWICE as a
subprocess (mirroring the 06-validate-gate pattern) so the second
invocation observes a warm EmailRenderer cache, even on a clean checkout
where the first-ever invocation (here or from run_all.py) was a miss.
"""
import glob
import os
import re
import subprocess
import sys
sys.path.insert(0, os.path.normpath(os.path.join(os.path.dirname(__file__), "..", "..")))
import bootstrap # noqa: E402
THIS_DIR = os.path.dirname(os.path.abspath(__file__))
dist = bootstrap.inky_example("09-transactional")
failures = 0
# EmailRenderer.render()'s cache-hit path returns an inky.BuildResult with
# an empty warnings list unconditionally (see src/email_renderer.py) -- it
# never re-runs inky.build() on a hit, so it has no warnings to report,
# not because the template is actually warning-clean. Asserting "zero
# warnings" against a run that's all cache hits would therefore pass even
# if the templates were full of warnings, as long as a prior run had ever
# cached them. So: clear the cache first, run once COLD and assert zero
# warnings from THAT run (the only run that actually asks inky.build() to
# check), then run again and confirm the second run is all cache hits.
cache_dir = os.path.join(THIS_DIR, "cache")
for cached_file in glob.glob(os.path.join(cache_dir, "*.json")):
os.remove(cached_file)
def run_capstone():
proc = subprocess.run(
[sys.executable, os.path.join(THIS_DIR, "run.py")],
capture_output=True,
text=True,
)
return proc.returncode, proc.stdout + proc.stderr
first_exit, first_output = run_capstone()
second_exit, second_output = run_capstone()
if first_exit != 0 or second_exit != 0:
print(f"09-transactional: run.py exited non-zero (first={first_exit}, second={second_exit})\n{second_output}", file=sys.stderr)
failures += 1
hit_count = second_output.count("hit (served from cache)")
if hit_count != 3:
print(f"09-transactional: expected 3 cache hits on the second run.py invocation, found {hit_count}\n{second_output}", file=sys.stderr)
failures += 1
# Checked against the FIRST (cold) run's output, not the second (warm) run
# -- a warm run always reports zero warnings regardless of template health
# (see the comment above), so it can't tell us anything about whether the
# templates are actually warning-clean.
m = re.search(r"total warnings:\s*(\d+)", first_output)
if not m or int(m.group(1)) != 0:
print(f"09-transactional: expected zero warnings across all three templates on the cold run, output:\n{first_output}", file=sys.stderr)
failures += 1
# Six output files: three emails, each .html + .txt.
for name in ("welcome", "receipt", "password-reset"):
for ext in ("html", "txt"):
path = os.path.join(dist, f"{name}.{ext}")
if not os.path.isfile(path):
print(f"09-transactional: missing {path}", file=sys.stderr)
failures += 1
# Receipt totals row: a "$"-amount inside the totals row specifically.
receipt_path = os.path.join(dist, "receipt.html")
receipt_html = ""
if os.path.isfile(receipt_path):
with open(receipt_path, encoding="utf-8") as f:
receipt_html = f.read()
totals_match = re.search(r'<tr class="totals-row"[^>]*>.*?</tr>', receipt_html, re.S)
if not totals_match or not re.search(r"\$[0-9]", totals_match.group(0)):
print('09-transactional: expected a $-amount inside <tr class="totals-row">...</tr> in receipt.html', file=sys.stderr)
failures += 1
if failures > 0:
sys.exit(1)
print("09-transactional: ok")