Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: disable inline-snapshot in CI runs #153

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.

-->
8 changes: 8 additions & 0 deletions docs/pytest.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ def test_something():
```bash exec="1" title="something" result="ansi"
set -e
cd $(mktemp -d)
export -n CI
export -n GITHUB_ACTIONS

export FORCE_COLOR=256
export COLUMNS=80
Expand Down Expand Up @@ -72,6 +74,8 @@ give a short report over which changes can be made to the snapshots

```bash exec="1" title="something" result="ansi"
cd $(mktemp -d)
export -n CI
export -n GITHUB_ACTIONS

export FORCE_COLOR=256
export COLUMNS=80
Expand Down Expand Up @@ -105,6 +109,8 @@ Shows a diff report over which changes can be made to the snapshots

```bash exec="1" title="something" result="ansi"
cd $(mktemp -d)
export -n CI
export -n GITHUB_ACTIONS

export FORCE_COLOR=256
export COLUMNS=80
Expand Down Expand Up @@ -135,6 +141,8 @@ Shows a diff report for each category and ask if you want to apply the changes
```bash exec="1" title="something" result="ansi"
set -e
cd $(mktemp -d)
export -n CI
export -n GITHUB_ACTIONS

export FORCE_COLOR=256
export COLUMNS=80
Expand Down
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 @@ -72,6 +72,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 @@ -107,7 +128,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():
state().active = False

elif flags & {"review"}:
Expand Down Expand Up @@ -211,6 +232,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 @@ -213,7 +213,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 @@ -244,6 +244,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 @@ -255,8 +256,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
9 changes: 6 additions & 3 deletions tests/adapter/test_dataclass.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,8 @@ class container:
a: int
b: int = attrs.field(default=5,repr=False)

assert container(a=1,b=5) == snapshot()
def test():
assert container(a=1,b=5) == snapshot()
"""
).run_pytest(
["--inline-snapshot=create"],
Expand All @@ -249,7 +250,8 @@ class container:
a: int
b: int = attrs.field(default=5,repr=False)

assert container(a=1,b=5) == snapshot(container(a=1))
def test():
assert container(a=1,b=5) == snapshot(container(a=1))
"""
}
),
Expand Down Expand Up @@ -452,7 +454,7 @@ def test_L3():
for _ in [1,2]:
assert L(1,2) == snapshot(L(1, 2)), "not equal"
"""
).run_pytest().run_pytest(
).run_pytest(returncode=snapshot(1)).run_pytest(
["--inline-snapshot=fix"],
changed_files=snapshot(
{
Expand Down Expand Up @@ -499,6 +501,7 @@ def test_L3():
"""
}
),
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 @@ -348,6 +348,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,7 +55,7 @@ def test_shortcuts():
strim=["trim"]
""",
}
).run_pytest(["--strim"], changed_files=trimmed_files)
).run_pytest(["--strim"], changed_files=trimmed_files, returncode=snapshot(1))


def test_default_shortcuts():
Expand Down
6 changes: 4 additions & 2 deletions tests/test_pydantic.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ class container(BaseModel):
a: int
b: int = Field(default=5,repr=False)

assert container(a=1,b=5) == snapshot()
def test():
assert container(a=1,b=5) == snapshot()
"""
).run_pytest(
["--inline-snapshot=create"],
Expand All @@ -73,7 +74,8 @@ class container(BaseModel):
a: int
b: int = Field(default=5,repr=False)

assert container(a=1,b=5) == snapshot(container(a=1))
def test():
assert container(a=1,b=5) == snapshot(container(a=1))
"""
}
),
Expand Down
9 changes: 7 additions & 2 deletions tests/test_pypy.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,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 @@ -36,6 +39,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
)
Loading