Skip to content

Commit b8a753d

Browse files
committed
feat: disable inline-snapshot in CI runs
1 parent 216e205 commit b8a753d

File tree

11 files changed

+142
-15
lines changed

11 files changed

+142
-15
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<!--
2+
A new scriv changelog fragment.
3+
4+
Uncomment the section that is right (remove the HTML comment wrapper).
5+
-->
6+
7+
<!--
8+
### Removed
9+
10+
- A bullet item for the Removed category.
11+
12+
-->
13+
<!--
14+
### Added
15+
16+
- A bullet item for the Added category.
17+
18+
-->
19+
### Changed
20+
21+
- inline-snapshot uses now `--inline-snapshot=disable` during CI runs by default.
22+
This improves performance because `snapshot()` is then equal to:
23+
```python
24+
def snapshot(x):
25+
return x
26+
```
27+
28+
29+
30+
31+
<!--
32+
### Deprecated
33+
34+
- A bullet item for the Deprecated category.
35+
36+
-->
37+
<!--
38+
### Fixed
39+
40+
- A bullet item for the Fixed category.
41+
42+
-->
43+
<!--
44+
### Security
45+
46+
- A bullet item for the Security category.
47+
48+
-->

docs/pytest.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ def test_something():
4242
```bash exec="1" title="something" result="ansi"
4343
set -e
4444
cd $(mktemp -d)
45+
export -n CI
46+
export -n GITHUB_ACTIONS
4547

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

7375
```bash exec="1" title="something" result="ansi"
7476
cd $(mktemp -d)
77+
export -n CI
78+
export -n GITHUB_ACTIONS
7579

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

106110
```bash exec="1" title="something" result="ansi"
107111
cd $(mktemp -d)
112+
export -n CI
113+
export -n GITHUB_ACTIONS
108114

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

139147
export FORCE_COLOR=256
140148
export COLUMNS=80

docs/testing.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ The following example shows how you can use the `Example` class to test what inl
2828
).run_pytest( # run without flags and check the pytest report
2929
changed_files=snapshot(),
3030
report=snapshot(),
31+
returncode=snapshot(),
3132
).run_pytest( # run with create flag and check the changed files
3233
["--inline-snapshot=create"],
3334
changed_files=snapshot(),
@@ -37,7 +38,7 @@ The following example shows how you can use the `Example` class to test what inl
3738
=== "--inline-snapshot=create"
3839

3940
<!-- inline-snapshot: create outcome-passed=1 -->
40-
``` python hl_lines="16 18 19 20 21 22 23 24 27 28 29 30 31 32 33 34 35"
41+
``` python hl_lines="16 18 19 20 21 22 23 24 25 28 29 30 31 32 33 34 35 36"
4142
from inline_snapshot.testing import Example
4243
from inline_snapshot import snapshot
4344

@@ -62,6 +63,7 @@ The following example shows how you can use the `Example` class to test what inl
6263
You can also use --inline-snapshot=review to approve the changes interactively\
6364
"""
6465
),
66+
returncode=snapshot(1),
6567
).run_pytest( # run with create flag and check the changed files
6668
["--inline-snapshot=create"],
6769
changed_files=snapshot(

src/inline_snapshot/pytest_plugin.py

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,27 @@ def xdist_running(config):
7272
)
7373

7474

75+
def is_ci_run():
76+
ci_env_vars = (
77+
"CI",
78+
"bamboo.buildKey",
79+
"BUILD_ID",
80+
"BUILD_NUMBER",
81+
"BUILDKITE",
82+
"CIRCLECI",
83+
"CONTINUOUS_INTEGRATION",
84+
"GITHUB_ACTIONS",
85+
"HUDSON_URL",
86+
"JENKINS_URL",
87+
"TEAMCITY_VERSION",
88+
"TRAVIS",
89+
)
90+
for var in ci_env_vars:
91+
if os.environ.get(var, False):
92+
return var
93+
return False
94+
95+
7596
def is_implementation_supported():
7697
return sys.implementation.name == "cpython"
7798

@@ -107,7 +128,7 @@ def pytest_configure(config):
107128
f"--inline-snapshot=disable can not be combined with other flags ({', '.join(flags-{'disable'})})"
108129
)
109130

110-
if xdist_running(config) or not is_implementation_supported():
131+
if xdist_running(config) or not is_implementation_supported() or is_ci_run():
111132
state().active = False
112133

113134
elif flags & {"review"}:
@@ -211,6 +232,15 @@ def pytest_terminal_summary(terminalreporter, exitstatus, config):
211232
)
212233
return
213234

235+
if var := is_ci_run():
236+
if flags != {"disable"}:
237+
terminalreporter.section("inline snapshot")
238+
terminalreporter.write(
239+
f'INFO: CI run was detected because environment variable "{var}" was defined.\n'
240+
+ "INFO: inline-snapshot runs with --inline-snapshot=disabled by default in CI.\n"
241+
)
242+
return
243+
214244
if not is_implementation_supported():
215245
if flags != {"disable"}:
216246
terminalreporter.section("inline snapshot")

src/inline_snapshot/testing/_example.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ def run_pytest(
213213
changed_files: Snapshot[dict[str, str]] | None = None,
214214
report: Snapshot[str] | None = None,
215215
stderr: Snapshot[str] | None = None,
216-
returncode: Snapshot[int] | None = None,
216+
returncode: Snapshot[int] | None = 0,
217217
) -> Example:
218218
"""Run pytest with the given args and env variables in an seperate
219219
process.
@@ -244,6 +244,7 @@ def run_pytest(
244244
term_columns + 1 if platform.system() == "Windows" else term_columns
245245
)
246246
command_env.pop("CI", None)
247+
command_env.pop("GITHUB_ACTIONS", None)
247248

248249
command_env.update(env)
249250

@@ -255,8 +256,7 @@ def run_pytest(
255256
print("stderr:")
256257
print(result.stderr.decode())
257258

258-
if returncode is not None:
259-
assert result.returncode == returncode
259+
assert result.returncode == returncode
260260

261261
if stderr is not None:
262262
assert result.stderr.decode() == stderr

tests/adapter/test_dataclass.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,8 @@ class container:
234234
a: int
235235
b: int = attrs.field(default=5,repr=False)
236236
237-
assert container(a=1,b=5) == snapshot()
237+
def test():
238+
assert container(a=1,b=5) == snapshot()
238239
"""
239240
).run_pytest(
240241
["--inline-snapshot=create"],
@@ -249,7 +250,8 @@ class container:
249250
a: int
250251
b: int = attrs.field(default=5,repr=False)
251252
252-
assert container(a=1,b=5) == snapshot(container(a=1))
253+
def test():
254+
assert container(a=1,b=5) == snapshot(container(a=1))
253255
"""
254256
}
255257
),
@@ -452,7 +454,7 @@ def test_L3():
452454
for _ in [1,2]:
453455
assert L(1,2) == snapshot(L(1, 2)), "not equal"
454456
"""
455-
).run_pytest().run_pytest(
457+
).run_pytest(returncode=snapshot(1)).run_pytest(
456458
["--inline-snapshot=fix"],
457459
changed_files=snapshot(
458460
{
@@ -499,6 +501,7 @@ def test_L3():
499501
"""
500502
}
501503
),
504+
returncode=snapshot(1),
502505
)
503506

504507

tests/conftest.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,8 @@ def run(self, *args, stdin=""):
348348
if "CI" in os.environ:
349349
del os.environ["CI"] # pragma: no cover
350350

351+
os.environ.pop("GITHUB_ACTIONS", None)
352+
351353
try:
352354
with mock.patch.dict(
353355
os.environ,

tests/test_ci.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from inline_snapshot import snapshot
2+
from inline_snapshot.testing._example import Example
3+
4+
5+
def test_ci():
6+
Example(
7+
"""
8+
from inline_snapshot import snapshot
9+
def test_something():
10+
assert type(snapshot(5)) is int
11+
12+
"""
13+
).run_pytest(
14+
env={"CI": "true"},
15+
report=snapshot(
16+
"""\
17+
INFO: CI run was detected because environment variable "CI" was defined.
18+
INFO: inline-snapshot runs with --inline-snapshot=disabled by default in CI.\
19+
"""
20+
),
21+
).run_pytest(
22+
["--inline-snapshot=disable"],
23+
env={"CI": "true"},
24+
report=snapshot(""),
25+
)

tests/test_config.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,14 @@ def test_config_pyproject():
3434
default-flags = ["trim"]
3535
""",
3636
}
37-
).run_pytest(changed_files=trimmed_files)
37+
).run_pytest(changed_files=trimmed_files, returncode=snapshot(1))
3838

3939

4040
def test_config_env():
4141
Example(file_to_trim).run_pytest(
42-
env={"INLINE_SNAPSHOT_DEFAULT_FLAGS": "trim"}, changed_files=trimmed_files
42+
env={"INLINE_SNAPSHOT_DEFAULT_FLAGS": "trim"},
43+
changed_files=trimmed_files,
44+
returncode=snapshot(1),
4345
)
4446

4547

@@ -53,7 +55,7 @@ def test_shortcuts():
5355
strim=["trim"]
5456
""",
5557
}
56-
).run_pytest(["--strim"], changed_files=trimmed_files)
58+
).run_pytest(["--strim"], changed_files=trimmed_files,returncode=snapshot(1))
5759

5860

5961
def test_default_shortcuts():

tests/test_pydantic.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ class container(BaseModel):
5959
a: int
6060
b: int = Field(default=5,repr=False)
6161
62-
assert container(a=1,b=5) == snapshot()
62+
def test():
63+
assert container(a=1,b=5) == snapshot()
6364
"""
6465
).run_pytest(
6566
["--inline-snapshot=create"],
@@ -73,7 +74,8 @@ class container(BaseModel):
7374
a: int
7475
b: int = Field(default=5,repr=False)
7576
76-
assert container(a=1,b=5) == snapshot(container(a=1))
77+
def test():
78+
assert container(a=1,b=5) == snapshot(container(a=1))
7779
"""
7880
}
7981
),

tests/test_pypy.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88

99
@pytest.mark.no_rewriting
1010
def test_pypy():
11+
12+
no_cpython = sys.implementation.name != "cpython"
13+
1114
report = (
1215
snapshot("INFO: inline-snapshot was disabled because pypy is not supported")
1316
if sys.implementation.name == "pypy"
@@ -36,6 +39,8 @@ def test_example():
3639
assert 1+1==snapshot(3)
3740
3841
"""
39-
).run_pytest(["--inline-snapshot=fix"], report=report).run_pytest(
40-
["--inline-snapshot=disable"], report=""
42+
).run_pytest(
43+
["--inline-snapshot=fix"], report=report, returncode=1 if no_cpython else 0
44+
).run_pytest(
45+
["--inline-snapshot=disable"], report="", returncode=1 if no_cpython else 0
4146
)

0 commit comments

Comments
 (0)