Skip to content

Commit 6e23060

Browse files
test: Collect key errors from log files
1 parent c76efbb commit 6e23060

File tree

1 file changed

+30
-5
lines changed

1 file changed

+30
-5
lines changed

scripts/test.py

+30-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""Compile all projects, used by `make test`."""
22

3+
from collections.abc import Generator
34
from dataclasses import dataclass
45
from os import environ
56
from pathlib import Path
@@ -20,13 +21,27 @@
2021

2122

2223
def log(message: str | Any) -> None:
23-
"""Write to stdout and the SUMMARY file."""
24+
"""Write markdown `message` to stdout and the SUMMARY file."""
2425
if not isinstance(message, str):
2526
message = str(message)
2627

27-
print(message)
28+
print(message, end="\n\n")
2829
with SUMMARY.open("a", encoding="utf-8") as f:
29-
f.write(message + "\n")
30+
f.write(message + "\n\n")
31+
32+
33+
def parse_log(file: Path) -> Generator[str]:
34+
"""Collect key errors from a log file."""
35+
with file.open(encoding="utf-8") as f:
36+
for line in f:
37+
if line.startswith("!"):
38+
yield line.strip()
39+
40+
41+
def collect_errors(directory: Path) -> Generator[tuple[Path, Generator[str]]]:
42+
"""Read all log files in the `directory` and collect key errors."""
43+
for file in directory.glob("*.log"):
44+
yield file.relative_to(directory), parse_log(file)
3045

3146

3247
@dataclass
@@ -68,9 +83,19 @@ def execute(self) -> CalledProcessError | None:
6883
env=None if self.env is None else {**environ, **self.env},
6984
check=True,
7085
)
71-
except CalledProcessError as error:
86+
except CalledProcessError as running_error:
7287
log(f"💥{self.icon} 无法编译 {self.name}。")
73-
return error
88+
for file, errors in collect_errors(self.directory):
89+
log(
90+
"\n".join(
91+
[
92+
f"- `{file}`:",
93+
# LaTeX 习惯写 `file.sty',需避免 markdown 解析
94+
*(f" - ```{e}```" for e in errors),
95+
]
96+
)
97+
)
98+
return running_error
7499

75100
duration = perf_counter() - start
76101
log(f"✅{self.icon} 可正常编译 {self.name}:⌛ {duration:.1f} 秒。")

0 commit comments

Comments
 (0)