From 68458e9d8f7d819a36ea013b8bedac30149b3c9e Mon Sep 17 00:00:00 2001 From: Frank Hoffmann <15r10nk-git@polarbit.de> Date: Sun, 10 Nov 2024 20:12:25 +0100 Subject: [PATCH] fix: pypy --- .github/workflows/ci.yml | 2 +- docs/limitations.md | 4 +++ pyproject.toml | 4 +-- src/inline_snapshot/pytest_plugin.py | 14 ++++++++- src/inline_snapshot/testing/_example.py | 2 +- tests/conftest.py | 16 ++++++++-- tests/test_inline_snapshot.py | 2 ++ tests/test_pypy.py | 40 +++++++++++++++++++++++++ tests/test_pytest_plugin.py | 3 ++ 9 files changed, 80 insertions(+), 7 deletions(-) create mode 100644 tests/test_pypy.py diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index c95b554e..56f7b797 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -26,7 +26,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - python-version: ['3.8', '3.9', '3.10', '3.11', '3.12', '3.13'] + python-version: ['3.8', '3.9', '3.10', '3.11', '3.12', '3.13', pypy3.8, pypy3.9, pypy3.10] os: [ubuntu-latest, windows-latest, macos-13] steps: - uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7 diff --git a/docs/limitations.md b/docs/limitations.md index 1435e501..9015621b 100644 --- a/docs/limitations.md +++ b/docs/limitations.md @@ -6,3 +6,7 @@ inline-snapshot must disable pytest assert-rewriting if you use report/review/cr ## xdist is not supported You can not use inline-snapshot in combination with `pytest-xdist`. The use of `-n=...` implies `--inline-snapshot=disable`. + +## works only with cpython + +inline-snapshot works currently only with cpython. `--inline-snapshot=disable` is enforced for every other implementation. diff --git a/pyproject.toml b/pyproject.toml index fdf30424..d4f5c94d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -105,7 +105,7 @@ dependencies=["cogapp","lxml","requests"] scripts.update="cog -r docs/**.md" [[tool.hatch.envs.hatch-test.matrix]] -python = ["3.13", "3.12", "3.11", "3.10", "3.9", "3.8"] +python = ["3.13", "3.12", "3.11", "3.10", "3.9", "3.8","pypy3.8","pypy3.9","pypy3.10"] [tool.hatch.envs.hatch-test] extra-dependencies = [ @@ -114,7 +114,7 @@ extra-dependencies = [ "mypy>=1.2.0", "pyright>=1.1.359", "pytest-subtests>=0.11.0", - "time-machine>=2.10.0", + "pytest-freezer>=0.4.8", "pydantic" ] env-vars.TOP = "{root}" diff --git a/src/inline_snapshot/pytest_plugin.py b/src/inline_snapshot/pytest_plugin.py index ae8235a2..c62413c7 100644 --- a/src/inline_snapshot/pytest_plugin.py +++ b/src/inline_snapshot/pytest_plugin.py @@ -64,6 +64,10 @@ def xdist_running(config): ) +def is_implementation_supported(): + return sys.implementation.name == "cpython" + + def pytest_configure(config): global flags @@ -84,7 +88,7 @@ def pytest_configure(config): f"--inline-snapshot=disable can not be combined with other flags ({', '.join(flags-{'disable'})})" ) - if xdist_running(config): + if xdist_running(config) or not is_implementation_supported(): _inline_snapshot._active = False elif flags & {"review"}: @@ -186,6 +190,14 @@ def pytest_terminal_summary(terminalreporter, exitstatus, config): ) return + if not is_implementation_supported(): + if flags != {"disable"}: + terminalreporter.section("inline snapshot") + terminalreporter.write( + f"INFO: inline-snapshot was disabled because {sys.implementation.name} is not supported\n" + ) + return + if not _inline_snapshot._active: return diff --git a/src/inline_snapshot/testing/_example.py b/src/inline_snapshot/testing/_example.py index 6ed4e7a3..41c52445 100644 --- a/src/inline_snapshot/testing/_example.py +++ b/src/inline_snapshot/testing/_example.py @@ -271,7 +271,7 @@ def run_pytest( report_str = report_str.replace("\r", "") report_str = report_str.replace(" \n", " ⏎\n") - assert report_str == report + assert report_str == report, repr(report_str) if changed_files is not None: current_files = {} diff --git a/tests/conftest.py b/tests/conftest.py index 5ef37fc2..11aaaeeb 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -2,6 +2,7 @@ import platform import re import shutil +import sys import textwrap import traceback from dataclasses import dataclass @@ -30,6 +31,17 @@ black.files.find_project_root = black.files.find_project_root.__wrapped__ # type: ignore +@pytest.fixture(autouse=True) +def check_pypy(request): + implementation = sys.implementation.name + node = request.node + + if implementation != "cpython" and node.get_closest_marker("no_rewriting") is None: + pytest.skip(f"{implementation} is not supported") + + yield + + @pytest.fixture() def check_update(source): def w(source_code, *, flags="", reported_flags=None, number=1): @@ -268,8 +280,8 @@ def setup(self, source: str): import pytest @pytest.fixture(autouse=True) -def set_time(time_machine): - time_machine.move_to(datetime.datetime(2024, 3, 14, 0, 0, 0, 0),tick=False) +def set_time(freezer): + freezer.move_to(datetime.datetime(2024, 3, 14, 0, 0, 0, 0)) yield """ ) diff --git a/tests/test_inline_snapshot.py b/tests/test_inline_snapshot.py index ef7ffbde..82947e59 100644 --- a/tests/test_inline_snapshot.py +++ b/tests/test_inline_snapshot.py @@ -18,12 +18,14 @@ from inline_snapshot.testing._example import snapshot_env +@pytest.mark.no_rewriting def test_snapshot_eq(): with snapshot_env(): assert 1 == snapshot(1) assert snapshot(1) == 1 +@pytest.mark.no_rewriting def test_disabled(): with snapshot_env(): _inline_snapshot._active = False diff --git a/tests/test_pypy.py b/tests/test_pypy.py new file mode 100644 index 00000000..ba9cac1b --- /dev/null +++ b/tests/test_pypy.py @@ -0,0 +1,40 @@ +import sys + +import pytest +from inline_snapshot import snapshot +from inline_snapshot.testing import Example + + +@pytest.mark.no_rewriting +def test_pypy(): + report = ( + snapshot("INFO: inline-snapshot was disabled because pypy is not supported") + if sys.implementation.name == "pypy" + else snapshot( + """\ +-------------------------------- Fix snapshots --------------------------------- ++----------------------------- test_something.py ------------------------------+ +| @@ -1,6 +1,6 @@ | +| | +| from inline_snapshot import snapshot | +| | +| def test_example(): | +| - assert 1+1==snapshot(3) | +| + assert 1+1==snapshot(2) | ++------------------------------------------------------------------------------+ +These changes will be applied, because you used --inline-snapshot=fix\ +""" + ) + ) + + Example( + """\ +from inline_snapshot import snapshot + +def test_example(): + assert 1+1==snapshot(3) + + """ + ).run_pytest(["--inline-snapshot=fix"], report=report).run_pytest( + ["--inline-snapshot=disabled"], report="" + ) diff --git a/tests/test_pytest_plugin.py b/tests/test_pytest_plugin.py index 896eec95..d850fb65 100644 --- a/tests/test_pytest_plugin.py +++ b/tests/test_pytest_plugin.py @@ -1,3 +1,4 @@ +import pytest from inline_snapshot import snapshot from inline_snapshot.testing import Example @@ -249,6 +250,7 @@ def test_a(): ) +@pytest.mark.no_rewriting def test_disable_option(project): project.setup( """\ @@ -474,6 +476,7 @@ def test_assertion_error(project): ) +@pytest.mark.no_rewriting def test_run_without_pytest(pytester): # snapshots are deactivated by default pytester.makepyfile(