|
| 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