Skip to content
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
15 changes: 11 additions & 4 deletions src/slipcover/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,8 @@ def printit(coverage, outfile):

atexit.register(sci_atexit)

return_code = 0

if args.script:
# python 'globals' for the script being executed
script_globals: Dict[Any, Any] = dict()
Expand All @@ -297,20 +299,25 @@ def printit(coverage, outfile):
code = sci.instrument(code)

with sc.ImportManager(sci, file_matcher):
exec(code, script_globals)

try:
exec(code, script_globals)
except SystemExit as e:
return_code = e.code if e.code is not None else 0
else:
import runpy
sys.argv = [*args.module, *args.script_or_module_args]
with sc.ImportManager(sci, file_matcher):
runpy.run_module(*args.module, run_name='__main__', alter_sys=True)
try:
runpy.run_module(*args.module, run_name='__main__', alter_sys=True)
except SystemExit as e:
return_code = e.code if e.code is not None else 0

if args.fail_under:
cov = sci.get_coverage()
if cov['summary']['percent_covered'] < args.fail_under:
return 2

return 0
return return_code


if __name__ == "__main__":
Expand Down
2 changes: 2 additions & 0 deletions tests/branch.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ def foo(x):
if x == 0:
x += 1
foo(0)

raise SystemExit()
12 changes: 9 additions & 3 deletions tests/test_coverage.py
Original file line number Diff line number Diff line change
Expand Up @@ -694,14 +694,20 @@ def test_summary_in_output_zero_lines(do_branch):


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

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

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

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

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


Expand Down