Skip to content

Commit d20fa51

Browse files
committed
Fix --fail-under when SystemExit raised in script
Script can raises SystemExit when it running through exec function and code block with --fail-under never ran before.
1 parent 9e5c4eb commit d20fa51

3 files changed

Lines changed: 9 additions & 5 deletions

File tree

src/slipcover/__main__.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -299,8 +299,10 @@ def printit(coverage, outfile):
299299
code = sci.instrument(code)
300300

301301
with sc.ImportManager(sci, file_matcher):
302-
exec(code, script_globals)
303-
302+
try:
303+
exec(code, script_globals)
304+
except SystemExit as e:
305+
return_code = e.code if e.code is not None else 0
304306
else:
305307
import runpy
306308
sys.argv = [*args.module, *args.script_or_module_args]

tests/branch.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@ def foo(x):
22
if x == 0:
33
x += 1
44
foo(0)
5+
6+
raise SystemExit()

tests/test_coverage.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -694,14 +694,14 @@ def test_summary_in_output_zero_lines(do_branch):
694694

695695

696696
@pytest.mark.parametrize("json_flag", ["", "--json"])
697-
def test_fail_under(json_flag):
697+
def test_fail_under(tmp_path, json_flag):
698698
p = subprocess.run(f"{sys.executable} -m slipcover {json_flag} --fail-under 100 tests/branch.py".split(), check=False)
699699
assert 0 == p.returncode
700700

701-
p = subprocess.run(f"{sys.executable} -m slipcover {json_flag} --branch --fail-under 83 tests/branch.py".split(), check=False)
701+
p = subprocess.run(f"{sys.executable} -m slipcover {json_flag} --branch --fail-under 85 tests/branch.py".split(), check=False)
702702
assert 0 == p.returncode
703703

704-
p = subprocess.run(f"{sys.executable} -m slipcover {json_flag} --branch --fail-under 84 tests/branch.py".split(), check=False)
704+
p = subprocess.run(f"{sys.executable} -m slipcover {json_flag} --branch --fail-under 86 tests/branch.py".split(), check=False)
705705
assert 2 == p.returncode
706706

707707
p = subprocess.run(f"{sys.executable} -m slipcover --branch --fail-under 93 -m pytest tests/pyt.py".split(), check=False)

0 commit comments

Comments
 (0)