Skip to content

Fix --fail-under with SystemExit raising from pytest or script#73

Open
ownik wants to merge 2 commits intoplasma-umass:mainfrom
ownik:pytest_fail-under
Open

Fix --fail-under with SystemExit raising from pytest or script#73
ownik wants to merge 2 commits intoplasma-umass:mainfrom
ownik:pytest_fail-under

Conversation

@ownik
Copy link

@ownik ownik commented Jan 16, 2026

pytest or python script could raise SystemExit and code block with --fail-under never ran before.

@ownik ownik force-pushed the pytest_fail-under branch 3 times, most recently from e6b45f6 to 1fad013 Compare January 16, 2026 14:39
@emeryberger emeryberger requested a review from Copilot January 16, 2026 17:15
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes the --fail-under flag when running pytest as a module with slipcover. Previously, pytest would raise a SystemExit exception through runpy.run_module, preventing the --fail-under code from executing. The fix catches the SystemExit exception and preserves the exit code to return it after the coverage check completes.

Changes:

  • Added exception handling to catch SystemExit from runpy.run_module() and preserve the exit code
  • Modified return logic to return the captured exit code instead of always returning 0
  • Added test cases to verify --fail-under works correctly when running pytest as a module

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
src/slipcover/main.py Added try-except block to catch SystemExit from pytest and preserve its exit code for proper propagation
tests/test_coverage.py Added test cases verifying --fail-under works with pytest module execution
Comments suppressed due to low confidence (1)

src/slipcover/main.py:302

  • The script execution path (lines 277-302) doesn't catch SystemExit exceptions like the module execution path does (lines 308-311). This inconsistency means scripts that call sys.exit() would still terminate slipcover prematurely, preventing --fail-under checks. Consider wrapping the exec() call with similar exception handling for consistency.
            exec(code, script_globals)

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

ownik added 2 commits January 16, 2026 20:56
pytest raises SystemExit when it running as runpy.run_module and code block with --fail-under never ran before.
Script can raises SystemExit when it running through exec function and code block with --fail-under never ran before.
@ownik ownik force-pushed the pytest_fail-under branch from 1fad013 to d20fa51 Compare January 16, 2026 17:56
@ownik ownik changed the title Fix --fail-under for pytest Fix --fail-under with SystemExit raising from pytest or script Jan 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant