-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path__main___test.py
More file actions
42 lines (30 loc) · 1.07 KB
/
__main___test.py
File metadata and controls
42 lines (30 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
from __future__ import annotations
import importlib
import runpy
import sys
import pytest
def test_caramba_module_main_exits_with_cli_exit_code(monkeypatch) -> None:
m = importlib.import_module("__main__")
def fake_cli_main(_argv=None) -> int:
return 7
monkeypatch.setattr(m, "cli_main", fake_cli_main)
with pytest.raises(SystemExit) as e:
m.main(["--help"])
assert e.value.code == 7
def test_caramba_python_m_executes_dunder_main(monkeypatch) -> None:
# Covers the `if __name__ == "__main__": main()` branch.
import cli as cli_mod
monkeypatch.setattr(cli_mod, "main", lambda _argv=None: 5)
# Ensure sys.exit propagates a SystemExit we can assert on.
def raise_system_exit(code=0) -> None:
raise SystemExit(code)
monkeypatch.setattr(
sys,
"exit",
raise_system_exit,
)
# Avoid runpy warning about pre-imported module.
sys.modules.pop("__main__", None)
with pytest.raises(SystemExit) as e:
runpy.run_module("__main__", run_name="__main__")
assert e.value.code == 5