Skip to content

Commit

Permalink
feat: disable inline-snapshot in CI runs
Browse files Browse the repository at this point in the history
  • Loading branch information
15r10nk committed Dec 18, 2024
1 parent 4a202ff commit f52931f
Show file tree
Hide file tree
Showing 9 changed files with 125 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<!--
A new scriv changelog fragment.
Uncomment the section that is right (remove the HTML comment wrapper).
-->

<!--
### Removed
- A bullet item for the Removed category.
-->
<!--
### Added
- A bullet item for the Added category.
-->
### Changed

- inline-snapshot uses now `--inline-snapshot=disable` during CI runs by default.
This improves performance because `snapshot()` is then equal to:
```python
def snapshot(x):
return x
```




<!--
### Deprecated

- A bullet item for the Deprecated category.

-->
<!--
### Fixed

- A bullet item for the Fixed category.

-->
<!--
### Security

- A bullet item for the Security category.

-->
4 changes: 3 additions & 1 deletion docs/testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -37,7 +38,7 @@ The following example shows how you can use the `Example` class to test what inl
=== "--inline-snapshot=create"

<!-- inline-snapshot: create outcome-passed=1 -->
``` 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

Expand All @@ -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(
Expand Down
32 changes: 31 additions & 1 deletion src/inline_snapshot/pytest_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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"}:
Expand Down Expand Up @@ -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")
Expand Down
6 changes: 3 additions & 3 deletions src/inline_snapshot/testing/_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)

Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions tests/adapter/test_dataclass.py
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,7 @@ def test_L2():
"""
}
),
returncode=snapshot(1),
)


Expand Down
2 changes: 2 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
25 changes: 25 additions & 0 deletions tests/test_ci.py
Original file line number Diff line number Diff line change
@@ -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(""),
)
8 changes: 5 additions & 3 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
)


Expand All @@ -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))
9 changes: 7 additions & 2 deletions tests/test_pypy.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
)

0 comments on commit f52931f

Please sign in to comment.