Skip to content

Commit d47e1ae

Browse files
Support compression for the reports (#34)
Fix #17 --------- Co-authored-by: Bruno Oliveira <[email protected]>
1 parent 10b4a40 commit d47e1ae

File tree

6 files changed

+63
-5
lines changed

6 files changed

+63
-5
lines changed

.pre-commit-config.yaml

+6
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ repos:
99
hooks:
1010
- id: trailing-whitespace
1111
- id: end-of-file-fixer
12+
13+
- repo: https://github.com/pre-commit/mirrors-mypy
14+
rev: 'v1.2.0'
15+
hooks:
16+
- id: mypy
17+
1218
- repo: local
1319
hooks:
1420
- id: rst

CHANGELOG.rst

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
next (tbd)
1+
Next (tbd)
22
-------------------
33

44
* `#16 <https://github.com/pytest-dev/pytest-reportlog/issues/15>`_: switch recommended file extension to `jsonl <https://jsonlines.org/>`__.
5+
* `#17 <https://github.com/pytest-dev/pytest-reportlog/issues/17>`_: add compression support for ``.bz2``/``.gz``/``.xz`` log file extensions.
56

67

78
0.3.0 (2023-04-26)

README.rst

+5
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ that kind of report object. For future compatibility, consumers of the file shou
4343
they don't recognize, as well as ignore unknown properties/keys in JSON objects that they do know,
4444
as future pytest versions might enrich the objects with more properties/keys.
4545

46+
Compression
47+
===========
48+
49+
Common compression suffixes like `.gz`, `.bz2` and `.xz` will automatically use the requested compression format.
50+
The implementations from the python stdlib are used and must be enabled in the python builds.
4651

4752
Example
4853
-------

src/pytest_reportlog/plugin.py

+20-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import json
2-
from typing import Dict, Any
2+
from typing import Dict, Any, TextIO
33

44
from _pytest.pathlib import Path
55

@@ -31,13 +31,31 @@ def pytest_unconfigure(config):
3131
del config._report_log_plugin
3232

3333

34+
def _open_filtered_writer(log_path: Path) -> TextIO:
35+
if log_path.suffix == ".gz":
36+
import gzip
37+
38+
return gzip.open(log_path, "wt", encoding="UTF-8")
39+
elif log_path.suffix == ".bz2":
40+
import bz2
41+
42+
return bz2.open(log_path, "wt", encoding="UTF-8")
43+
elif log_path.suffix == ".xz":
44+
import lzma
45+
46+
return lzma.open(log_path, "wt", encoding="UTF-8")
47+
else:
48+
# line buffer for text mode to ease tail -f
49+
return log_path.open("wt", buffering=1, encoding="UTF-8")
50+
51+
3452
class ReportLogPlugin:
3553
def __init__(self, config, log_path: Path):
3654
self._config = config
3755
self._log_path = log_path
3856

3957
log_path.parent.mkdir(parents=True, exist_ok=True)
40-
self._file = log_path.open("w", buffering=1, encoding="UTF-8")
58+
self._file = _open_filtered_writer(log_path)
4159

4260
def close(self):
4361
if self._file is not None:

tests/test_reportlog.py

+29-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,37 @@
11
import json
22
from collections import defaultdict
3-
3+
from typing import TextIO
4+
import bz2, gzip, lzma, io
45
import pytest
6+
from pathlib import Path
57
from _pytest.reports import BaseReport
68

7-
from pytest_reportlog.plugin import cleanup_unserializable
9+
from pytest_reportlog.plugin import cleanup_unserializable, _open_filtered_writer
10+
11+
from typing_extensions import Protocol, Literal
12+
13+
14+
class OpenerModule(Protocol):
15+
def open(self, path: Path, mode: Literal["rt"]) -> TextIO:
16+
...
17+
18+
19+
@pytest.mark.parametrize(
20+
"filename, opener_module",
21+
[
22+
("test.jsonl", io),
23+
("test.unknown", io),
24+
("test.jsonl.gz", gzip),
25+
("test.jsonl.bz2", bz2),
26+
("test.jsonl.xz", lzma),
27+
],
28+
)
29+
def test_open_filtered(filename: str, opener_module: OpenerModule, tmp_path: Path):
30+
path = tmp_path / filename
31+
with _open_filtered_writer(path) as fp:
32+
fp.write("test\n")
33+
with opener_module.open(path, "rt") as fp2:
34+
assert fp2.read() == "test\n"
835

936

1037
def test_basics(testdir, tmp_path, pytestconfig):

tox.ini

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ envlist = py{37,38,39,310,311},linting
44
[testenv]
55
deps =
66
pytest-xdist
7+
typing-extensions # for the python3.7 compat imports in the tests
78
commands =
89
pytest -n1 tests
910

0 commit comments

Comments
 (0)