From bac07c31f0d70d31facf8ce3fa5ac913c4a411d3 Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Thu, 16 Apr 2020 17:53:17 -0300 Subject: [PATCH 1/2] Fix integration with xdist Fix #5 --- .pre-commit-config.yaml | 2 +- CHANGELOG.rst | 7 +++++- src/pytest_reportlog/plugin.py | 21 +++++++++++++++++- tests/test_reportlog.py | 40 ++++++++++++++++++++++++++++++++++ tox.ini | 2 ++ 5 files changed, 69 insertions(+), 3 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 68d50e6..2b31bb2 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -5,7 +5,7 @@ repos: hooks: - id: black args: [--safe, --quiet] - language_version: python3.7 + language_version: python3.8 - repo: https://github.com/pre-commit/pre-commit-hooks rev: v2.2.3 hooks: diff --git a/CHANGELOG.rst b/CHANGELOG.rst index e6ca943..6725d33 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -1,4 +1,9 @@ -0.1.0 (unreleased) +0.1.1 (2020-04-16) +------------------ + +* `#5 `_: Fix support for `pytest-xdist `_. + +0.1.0 (2019-11-18) ------------------ * First version. diff --git a/src/pytest_reportlog/plugin.py b/src/pytest_reportlog/plugin.py index 109444a..a5e9351 100644 --- a/src/pytest_reportlog/plugin.py +++ b/src/pytest_reportlog/plugin.py @@ -1,4 +1,6 @@ import json +from typing import Dict, Any + from _pytest.pathlib import Path import pytest @@ -43,7 +45,12 @@ def close(self): self._file = None def _write_json_data(self, data): - self._file.write(json.dumps(data) + "\n") + try: + json_data = json.dumps(data) + except TypeError: + data = cleanup_unserializable(data) + json_data = json.dumps(data) + self._file.write(json_data + "\n") self._file.flush() def pytest_sessionstart(self): @@ -70,3 +77,15 @@ def pytest_terminal_summary(self, terminalreporter): terminalreporter.write_sep( "-", "generated report log file: {}".format(self._log_path) ) + + +def cleanup_unserializable(d: Dict[str, Any]) -> Dict[str, Any]: + """Return new dict with entries that are not json serializable by their str().""" + result = {} + for k, v in d.items(): + try: + json.dumps({k: v}) + except TypeError: + v = str(v) + result[k] = v + return result diff --git a/tests/test_reportlog.py b/tests/test_reportlog.py index cc2a431..659a35a 100644 --- a/tests/test_reportlog.py +++ b/tests/test_reportlog.py @@ -3,6 +3,8 @@ import pytest from _pytest.reports import BaseReport +from pytest_reportlog.plugin import cleanup_unserializable + def test_basics(testdir, tmp_path, pytestconfig): """Basic testing of the report log functionality. @@ -52,3 +54,41 @@ def test_fail(): config=pytestconfig, data=json_obj ) assert isinstance(rep, BaseReport) + + +def test_xdist_integration(testdir, tmp_path): + pytest.importorskip("xdist") + testdir.makepyfile( + """ + def test_ok(): + pass + + def test_fail(): + assert 0 + """ + ) + fn = tmp_path / "result.log" + result = testdir.runpytest("-n2", "--report-log={}".format(fn)) + result.stdout.fnmatch_lines("*1 failed, 1 passed*") + + lines = fn.read_text("UTF-8").splitlines() + data = json.loads(lines[0]) + assert data == { + "pytest_version": pytest.__version__, + "$report_type": "SessionStart", + } + + +def test_cleanup_unserializable(): + """Unittest for the cleanup_unserializable function""" + good = {"x": 1, "y": ["a", "b"]} + new = cleanup_unserializable(good) + assert new == good + + class C: + def __str__(self): + return "C instance" + + bad = {"x": 1, "y": ["a", "b"], "c": C()} + new = cleanup_unserializable(bad) + assert new == {"x": 1, "c": "C instance", "y": ["a", "b"]} diff --git a/tox.ini b/tox.ini index 4952939..c14daa4 100644 --- a/tox.ini +++ b/tox.ini @@ -2,6 +2,8 @@ envlist = py{35,36,37,38}, linting [testenv] +deps = + pytest-xdist commands = pytest tests From 63907f9b0049a96bc3a5e163b14281fd4652e25a Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Thu, 16 Apr 2020 18:01:08 -0300 Subject: [PATCH 2/2] Remove language_version for black Let's see if getting the default helps. 'linting' on Linux is failing with: RuntimeError: failed to find interpreter for Builtin discover of python_spec='python3.8' --- .github/workflows/main.yml | 2 +- .pre-commit-config.yaml | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index dd2ea1a..8dd16af 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -31,7 +31,7 @@ jobs: - name: Install tox run: | python -m pip install --upgrade pip - pip install tox + pip install --upgrade virtualenv tox - name: Test run: | tox -e ${{ matrix.tox_env }} diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 2b31bb2..54feacc 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -5,7 +5,6 @@ repos: hooks: - id: black args: [--safe, --quiet] - language_version: python3.8 - repo: https://github.com/pre-commit/pre-commit-hooks rev: v2.2.3 hooks: