Skip to content

Commit 6f8a9f7

Browse files
test: Refactor make test with test.py for easier debugging
1 parent ab73edb commit 6f8a9f7

File tree

3 files changed

+97
-11
lines changed

3 files changed

+97
-11
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,7 @@ scripts/*
301301
!scripts/*.sh
302302
!scripts/*.zsh
303303
!scripts/*.ps1
304+
!scripts/*.py
304305

305306
.DS_Store
306307

Makefile

+2-11
Original file line numberDiff line numberDiff line change
@@ -59,17 +59,8 @@ clean-all: clean clean-dist FORCE_MAKE
5959

6060
.PHONY: test
6161
test: doc copy FORCE_MAKE
62-
cd $(SCAFFOLDDIR)/undergraduate-thesis && latexmk && cd ..
63-
cd $(SCAFFOLDDIR)/paper-translation && latexmk && cd ..
64-
cd $(SCAFFOLDDIR)/undergraduate-thesis-en && latexmk && cd ..
65-
cd $(SCAFFOLDDIR)/graduate-thesis && latexmk && cd ..
66-
cd $(SCAFFOLDDIR)/reading-report && latexmk && cd ..
67-
cd $(SCAFFOLDDIR)/lab-report && latexmk && cd ..
68-
cd $(SCAFFOLDDIR)/presentation-slide && latexmk && cd ..
69-
cd $(TESTDIR)/doctor-thesis && latexmk && cd ..
70-
cd $(TESTDIR)/autorefs && latexmk && cd ..
71-
cd ./handbook && latexmk \
72-
&& GRADUATE=true latexmk -gg && cd ..
62+
SCAFFOLDDIR=$(SCAFFOLDDIR) TESTDIR=$(TESTDIR) \
63+
python scripts/test.py
7364

7465
.PHONY: regression-test
7566
regression-test: cls

scripts/test.py

+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
"""Compile all projects, used by `make test`."""
2+
3+
from dataclasses import dataclass
4+
from os import environ
5+
from pathlib import Path
6+
from subprocess import CalledProcessError, run
7+
from sys import stderr
8+
from typing import Any
9+
10+
ROOT_DIR = Path(__file__).parent.parent
11+
assert ROOT_DIR.exists()
12+
SCAFFOLD_DIR = Path(environ["SCAFFOLDDIR"])
13+
assert SCAFFOLD_DIR.exists()
14+
TEST_DIR = Path(environ["TESTDIR"])
15+
assert TEST_DIR.exists()
16+
17+
# https://docs.github.com/en/actions/writing-workflows/choosing-what-your-workflow-does/workflow-commands-for-github-actions#adding-a-job-summary
18+
SUMMARY = Path(environ.get("GITHUB_STEP_SUMMARY", ROOT_DIR / "scripts/test-result.md"))
19+
20+
21+
def log(message: str | Any) -> None:
22+
"""Write to stdout and the SUMMARY file."""
23+
if not isinstance(message, str):
24+
message = str(message)
25+
26+
print(message)
27+
with SUMMARY.open("a", encoding="utf-8") as f:
28+
f.write(message + "\n")
29+
30+
31+
@dataclass
32+
class TestCase:
33+
"""A test case for compiling a LaTeX project."""
34+
35+
icon: str
36+
directory: Path
37+
name: str
38+
env: dict[str, str] | None = None
39+
40+
def __init__(
41+
self,
42+
icon: str,
43+
directory: Path,
44+
/,
45+
*,
46+
name: str | None = None,
47+
env: dict[str, str] | None = None,
48+
) -> None:
49+
self.icon = icon
50+
self.directory = directory
51+
self.name = name or directory.name
52+
self.env = env
53+
54+
def execute(self) -> CalledProcessError | None:
55+
"""Execute the test case."""
56+
print(f"🟡 Compiling `{self}`", file=stderr)
57+
assert self.directory.exists()
58+
try:
59+
run(
60+
["latexmk"],
61+
cwd=self.directory,
62+
# Append to the original env
63+
env=None if self.env is None else {**environ, **self.env},
64+
check=True,
65+
)
66+
log(f"✅{self.icon} 可正常编译 {self.name}。")
67+
except CalledProcessError as error:
68+
log(f"💥{self.icon} 无法编译 {self.name}。")
69+
return error
70+
71+
72+
TESTS = [
73+
TestCase("📁", SCAFFOLD_DIR / "undergraduate-thesis"),
74+
TestCase("📁", SCAFFOLD_DIR / "paper-translation"),
75+
TestCase("📁", SCAFFOLD_DIR / "graduate-thesis"),
76+
TestCase("📁", SCAFFOLD_DIR / "reading-report"),
77+
TestCase("📁", SCAFFOLD_DIR / "lab-report"),
78+
TestCase("📁", SCAFFOLD_DIR / "presentation-slide"),
79+
TestCase("🧪", TEST_DIR / "doctor-thesis"),
80+
TestCase("🧪", TEST_DIR / "autorefs"),
81+
TestCase("📖", ROOT_DIR / "handbook", name="undergraduate-handbook"),
82+
TestCase(
83+
"📖", ROOT_DIR / "handbook", name="graduate-handbook", env={"GRADUATE": "true"}
84+
),
85+
]
86+
87+
# Execute all test cases, and raise the first error.
88+
first_error = None
89+
for t in TESTS:
90+
result = t.execute()
91+
if result is not None and first_error is None:
92+
first_error = result
93+
if first_error is not None:
94+
raise first_error

0 commit comments

Comments
 (0)