diff --git a/docs/testing.md b/docs/testing.md index 9f6b1a2b..2e77983c 100644 --- a/docs/testing.md +++ b/docs/testing.md @@ -28,6 +28,7 @@ The following example shows how you can use the `Example` class to test what inl ).run_pytest( # run without flags and check the pytest report changed_files=snapshot(), report=snapshot(), + returncode=snapshot(), ).run_pytest( # run with create flag and check the changed files ["--inline-snapshot=create"], changed_files=snapshot(), @@ -37,7 +38,7 @@ The following example shows how you can use the `Example` class to test what inl === "--inline-snapshot=create" - ``` python hl_lines="16 18 19 20 21 22 23 24 27 28 29 30 31 32 33 34 35" + ``` python hl_lines="16 18 19 20 21 22 23 24 25 28 29 30 31 32 33 34 35 36" from inline_snapshot.testing import Example from inline_snapshot import snapshot @@ -62,6 +63,7 @@ The following example shows how you can use the `Example` class to test what inl You can also use --inline-snapshot=review to approve the changes interactively\ """ ), + returncode=snapshot(1), ).run_pytest( # run with create flag and check the changed files ["--inline-snapshot=create"], changed_files=snapshot( diff --git a/src/inline_snapshot/pytest_plugin.py b/src/inline_snapshot/pytest_plugin.py index 56b99084..88f018d0 100644 --- a/src/inline_snapshot/pytest_plugin.py +++ b/src/inline_snapshot/pytest_plugin.py @@ -68,6 +68,27 @@ def xdist_running(config): ) +def is_ci_run(): + ci_env_vars = ( + "CI", + "bamboo.buildKey", + "BUILD_ID", + "BUILD_NUMBER", + "BUILDKITE", + "CIRCLECI", + "CONTINUOUS_INTEGRATION", + "GITHUB_ACTIONS", + "HUDSON_URL", + "JENKINS_URL", + "TEAMCITY_VERSION", + "TRAVIS", + ) + for var in ci_env_vars: + if os.environ.get(var, False): + return var + return False + + def is_implementation_supported(): return sys.implementation.name == "cpython" @@ -98,7 +119,7 @@ def pytest_configure(config): f"--inline-snapshot=disable can not be combined with other flags ({', '.join(flags-{'disable'})})" ) - if xdist_running(config) or not is_implementation_supported(): + if xdist_running(config) or not is_implementation_supported() or is_ci_run(): _inline_snapshot._active = False elif flags & {"review"}: @@ -200,6 +221,15 @@ def pytest_terminal_summary(terminalreporter, exitstatus, config): ) return + if var := is_ci_run(): + if flags != {"disable"}: + terminalreporter.section("inline snapshot") + terminalreporter.write( + f'INFO: CI run was detected because environment variable "{var}" was defined.\n' + + "INFO: inline-snapshot runs with --inline-snapshot=disabled by default in CI.\n" + ) + return + if not is_implementation_supported(): if flags != {"disable"}: terminalreporter.section("inline snapshot") diff --git a/src/inline_snapshot/testing/_example.py b/src/inline_snapshot/testing/_example.py index 72c15f9f..fe8d770a 100644 --- a/src/inline_snapshot/testing/_example.py +++ b/src/inline_snapshot/testing/_example.py @@ -233,7 +233,7 @@ def run_pytest( changed_files: Snapshot[dict[str, str]] | None = None, report: Snapshot[str] | None = None, stderr: Snapshot[str] | None = None, - returncode: Snapshot[int] | None = None, + returncode: Snapshot[int] | None = 0, ) -> Example: """Run pytest with the given args and env variables in an seperate process. @@ -266,6 +266,7 @@ def run_pytest( term_columns + 1 if platform.system() == "Windows" else term_columns ) command_env.pop("CI", None) + command_env.pop("GITHUB_ACTIONS", None) command_env.update(env) @@ -277,8 +278,7 @@ def run_pytest( print("stderr:") print(result.stderr.decode()) - if returncode is not None: - assert result.returncode == returncode + assert result.returncode == returncode if stderr is not None: assert result.stderr.decode() == stderr diff --git a/tests/adapter/test_dataclass.py b/tests/adapter/test_dataclass.py index e386cebe..781a7c2d 100644 --- a/tests/adapter/test_dataclass.py +++ b/tests/adapter/test_dataclass.py @@ -485,6 +485,7 @@ def test_L2(): """ } ), + returncode=snapshot(1), ) diff --git a/tests/conftest.py b/tests/conftest.py index 94eef606..47c1d66c 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -344,6 +344,8 @@ def run(self, *args, stdin=""): if "CI" in os.environ: del os.environ["CI"] # pragma: no cover + os.environ.pop("GITHUB_ACTIONS", None) + try: with mock.patch.dict( os.environ, diff --git a/tests/test_ci.py b/tests/test_ci.py new file mode 100644 index 00000000..67028051 --- /dev/null +++ b/tests/test_ci.py @@ -0,0 +1,25 @@ +from inline_snapshot import snapshot +from inline_snapshot.testing._example import Example + + +def test_ci(): + Example( + """ +from inline_snapshot import snapshot +def test_something(): + assert type(snapshot(5)) is int + + """ + ).run_pytest( + env={"CI": "true"}, + report=snapshot( + """\ +INFO: CI run was detected because environment variable "CI" was defined. +INFO: inline-snapshot runs with --inline-snapshot=disabled by default in CI.\ +""" + ), + ).run_pytest( + ["--inline-snapshot=disable"], + env={"CI": "true"}, + report=snapshot(""), + ) diff --git a/tests/test_config.py b/tests/test_config.py index 98fcb518..078b6a5f 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -34,12 +34,14 @@ def test_config_pyproject(): default-flags = ["trim"] """, } - ).run_pytest(changed_files=trimmed_files) + ).run_pytest(changed_files=trimmed_files, returncode=snapshot(1)) def test_config_env(): Example(file_to_trim).run_pytest( - env={"INLINE_SNAPSHOT_DEFAULT_FLAGS": "trim"}, changed_files=trimmed_files + env={"INLINE_SNAPSHOT_DEFAULT_FLAGS": "trim"}, + changed_files=trimmed_files, + returncode=snapshot(1), ) @@ -53,4 +55,4 @@ def test_shortcuts(): strim=["trim"] """, } - ).run_pytest(["--strim"], changed_files=trimmed_files) + ).run_pytest(["--strim"], changed_files=trimmed_files, returncode=snapshot(1)) diff --git a/tests/test_pypy.py b/tests/test_pypy.py index e76ba960..97f01f44 100644 --- a/tests/test_pypy.py +++ b/tests/test_pypy.py @@ -7,6 +7,9 @@ @pytest.mark.no_rewriting def test_pypy(): + + no_cpython = sys.implementation.name != "cpython" + report = ( snapshot("INFO: inline-snapshot was disabled because pypy is not supported") if sys.implementation.name == "pypy" @@ -35,6 +38,8 @@ def test_example(): assert 1+1==snapshot(3) """ - ).run_pytest(["--inline-snapshot=fix"], report=report).run_pytest( - ["--inline-snapshot=disable"], report="" + ).run_pytest( + ["--inline-snapshot=fix"], report=report, returncode=1 if no_cpython else 0 + ).run_pytest( + ["--inline-snapshot=disable"], report="", returncode=1 if no_cpython else 0 )