Skip to content

Commit d1faca5

Browse files
authored
Refactor the pytest test summary info parsing script (#4619)
* refactor the short test summary info parsing script * update the call of the test summary parse script * move the I/O to the topmost function * preprocess the lines before passing them to the extraction function
1 parent 1a4f7bd commit d1faca5

File tree

2 files changed

+47
-14
lines changed

2 files changed

+47
-14
lines changed

.github/workflows/parse_logs.py

Lines changed: 45 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,53 @@
11
# type: ignore
2+
import argparse
3+
import itertools
24
import pathlib
5+
import textwrap
36

4-
files = pathlib.Path("logs").rglob("**/*-log")
5-
files = sorted(filter(lambda x: x.is_file(), files))
7+
parser = argparse.ArgumentParser()
8+
parser.add_argument("filepaths", nargs="+", type=pathlib.Path)
9+
args = parser.parse_args()
10+
11+
filepaths = sorted(p for p in args.filepaths if p.is_file())
12+
13+
14+
def extract_short_test_summary_info(lines):
15+
up_to_start_of_section = itertools.dropwhile(
16+
lambda l: "=== short test summary info ===" not in l,
17+
lines,
18+
)
19+
up_to_section_content = itertools.islice(up_to_start_of_section, 1, None)
20+
section_content = itertools.takewhile(
21+
lambda l: l.startswith("FAILED"), up_to_section_content
22+
)
23+
content = "\n".join(section_content)
24+
25+
return content
26+
27+
28+
def format_log_message(path):
29+
py_version = path.name.split("-")[1]
30+
summary = f"Python {py_version} Test Summary Info"
31+
with open(path) as f:
32+
data = extract_short_test_summary_info(line.rstrip() for line in f)
33+
message = textwrap.dedent(
34+
f"""\
35+
<details><summary>{summary}</summary>
36+
37+
```
38+
{data}
39+
```
40+
41+
</details>
42+
"""
43+
)
44+
45+
return message
646

7-
message = "\n"
847

948
print("Parsing logs ...")
10-
for file in files:
11-
with open(file) as fpt:
12-
print(f"Parsing {file.absolute()}")
13-
data = fpt.read().split("test summary info")[-1].splitlines()[1:-1]
14-
data = "\n".join(data)
15-
py_version = file.name.split("-")[1]
16-
message = f"{message}\n<details>\n<summary>\nPython {py_version} Test Summary Info\n</summary>\n\n```bash\n{data}\n```\n</details>\n"
49+
message = "\n\n".join(format_log_message(path) for path in filepaths)
1750

1851
output_file = pathlib.Path("pytest-logs.txt")
19-
with open(output_file, "w") as fpt:
20-
print(f"Writing output file to: {output_file.absolute()} ")
21-
fpt.write(message)
52+
print(f"Writing output file to: {output_file.absolute()}")
53+
output_file.write_text(message)

.github/workflows/upstream-dev-ci.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ jobs:
7474
ls -R ./logs
7575
- name: Parse logs
7676
run: |
77-
python .github/workflows/parse_logs.py
77+
shopt -s globstar
78+
python .github/workflows/parse_logs.py logs/**/*-log
7879
- name: Report failures
7980
uses: actions/github-script@v3
8081
with:

0 commit comments

Comments
 (0)