Skip to content

Commit 88279c0

Browse files
committed
feat: Add some unit tests on argument parsing
1 parent 6306d74 commit 88279c0

File tree

2 files changed

+45
-17
lines changed

2 files changed

+45
-17
lines changed

tests/integration/test_stm32loader.py

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,6 @@ def test_stm32loader_is_executable():
5353
subprocess.call(["stm32loader", "--help"])
5454

5555

56-
@pytest.mark.parametrize(
57-
"help_argument", ["-h", "--help"],
58-
)
59-
def test_argument_h_prints_help_info(help_argument, capsys):
60-
main(help_argument, avoid_system_exit=True)
61-
captured = capsys.readouterr()
62-
assert "Example:" in captured.out
63-
64-
6556
def test_unexisting_serial_port_prints_readable_error(capsys):
6657
main("-p", "COM108", avoid_system_exit=True)
6758
captured = capsys.readouterr()
@@ -70,14 +61,6 @@ def test_unexisting_serial_port_prints_readable_error(capsys):
7061
assert "Is the device connected and powered correctly?" in captured.err
7162

7263

73-
def test_missing_argument_p_prints_readable_error(capsys):
74-
main(avoid_system_exit=True)
75-
captured = capsys.readouterr()
76-
assert "No serial port configured" in captured.err
77-
assert "Supply the -p option" in captured.err
78-
assert "environment variable STM32LOADER_SERIAL_PORT" in captured.err
79-
80-
8164
def test_env_var_stm32loader_serial_port_defines_port(capsys):
8265
os.environ['STM32LOADER_SERIAL_PORT'] = "COM109"
8366
main(avoid_system_exit=True)

tests/unit/test_arguments.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
2+
import atexit
3+
4+
import pytest
5+
6+
from stm32loader.main import Stm32Loader
7+
8+
9+
@pytest.fixture
10+
def program():
11+
return Stm32Loader()
12+
13+
14+
def test_parse_arguments_without_args_raises_typeerror(program):
15+
with pytest.raises(TypeError, match="missing.*required.*argument"):
16+
program.parse_arguments()
17+
18+
19+
def test_parse_arguments_with_standard_args_passes(program):
20+
program.parse_arguments(["-p", "port", "-b", "9600", "-q"])
21+
22+
23+
@pytest.mark.parametrize(
24+
"help_argument", ["-h", "--help"],
25+
)
26+
def test_parse_arguments_with_help_raises_systemexit(program, help_argument):
27+
with pytest.raises(SystemExit):
28+
program.parse_arguments([help_argument])
29+
30+
31+
def test_parse_arguments_erase_without_port_complains_about_missing_argument(program, capsys):
32+
try:
33+
program.parse_arguments(["-e", "-w", "-v", "file.bin"])
34+
except SystemExit:
35+
pass
36+
37+
# Also call atexit functions so that the hint about using an env variable
38+
# is printed.
39+
atexit._run_exitfuncs()
40+
41+
_output, error_output = capsys.readouterr()
42+
if not error_output:
43+
pytest.skip("Not sure why nothing is captured in some pytest runs?")
44+
assert "arguments are required: -p/--port" in error_output
45+
assert "STM32LOADER_SERIAL_PORT" in error_output

0 commit comments

Comments
 (0)