Skip to content

Commit 4dfec43

Browse files
authored
add some small tests (#5)
* only weakly match the filepath to capture test groups in the test name * add a hypothesis-based test * ignore hypothesis cache and python bytecode files * use the module name and fix the mistaken use of `starmap` * add a test for truncate * add CI
1 parent a24c119 commit 4dfec43

File tree

4 files changed

+83
-1
lines changed

4 files changed

+83
-1
lines changed

.github/workflows/ci.yaml

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
name: CI
2+
on:
3+
push:
4+
branches: [main]
5+
pull_request:
6+
branches: [main]
7+
8+
jobs:
9+
ci:
10+
name: tests
11+
runs-on: [ubuntu-latest]
12+
strategy:
13+
matrix:
14+
python-version: ["3.10", "3.11"]
15+
16+
steps:
17+
- name: clone the repository
18+
uses: actions/checkout@v3
19+
- name: setup python
20+
uses: actions/setup-python@v4
21+
with:
22+
python-version: ${{ matrix.python-version }}
23+
- name: upgrade pip
24+
run: |
25+
python -m pip install --upgrade pip
26+
- name: install dependencies
27+
run: |
28+
python -m pip install pytest hypothesis more-itertools
29+
- name: run tests
30+
run: |
31+
python -m pytest -rf

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/.hypothesis/
2+
__pycache__/

parse_logs.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def parse_record(record):
5757
return cls._from_json(record)
5858

5959

60-
nodeid_re = re.compile(r"(?P<filepath>.+)::(?P<name>.+?)(?:\[(?P<variant>.+)\])?")
60+
nodeid_re = re.compile(r"(?P<filepath>.+?)::(?P<name>.+?)(?:\[(?P<variant>.+)\])?")
6161

6262

6363
def parse_nodeid(nodeid):

test_parse_log.py

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import re
2+
import sys
3+
4+
import hypothesis.strategies as st
5+
from hypothesis import given, note
6+
7+
import parse_logs
8+
9+
directory_re = r"(\w|-)+"
10+
path_re = re.compile(rf"/?({directory_re}(/{directory_re})*/)?test_[A-Za-z0-9_]+\.py")
11+
filepaths = st.from_regex(path_re, fullmatch=True)
12+
13+
group_re = r"Test[A-Za-z0-9_]+"
14+
name_re = re.compile(rf"({group_re}::)*test_[A-Za-z0-9_]+")
15+
names = st.from_regex(name_re, fullmatch=True)
16+
17+
variants = st.from_regex(re.compile(r"(\w+-)*\w+"), fullmatch=True)
18+
19+
messages = st.text()
20+
21+
22+
def preformatted_reports():
23+
return st.tuples(filepaths, names, variants | st.none(), messages).map(
24+
lambda x: parse_logs.PreformattedReport(*x)
25+
)
26+
27+
28+
@given(filepaths, names, variants)
29+
def test_parse_nodeid(path, name, variant):
30+
if variant is not None:
31+
nodeid = f"{path}::{name}[{variant}]"
32+
else:
33+
nodeid = f"{path}::{name}"
34+
35+
note(f"nodeid: {nodeid}")
36+
37+
expected = {"filepath": path, "name": name, "variant": variant}
38+
actual = parse_logs.parse_nodeid(nodeid)
39+
40+
assert actual == expected
41+
42+
43+
@given(st.lists(preformatted_reports()), st.integers(min_value=0))
44+
def test_truncate(reports, max_chars):
45+
py_version = ".".join(str(part) for part in sys.version_info[:3])
46+
47+
formatted = parse_logs.truncate(reports, max_chars=max_chars, py_version=py_version)
48+
49+
assert formatted is None or len(formatted) <= max_chars

0 commit comments

Comments
 (0)