Coverage for tests/unit_tests/utils/test_cmd_util.py: 97%

32 statements  

« prev     ^ index     » next       coverage.py v7.16.1, created at 2026-09-23 03:53 +0000

1""" 

2Tests of cmd_util.py 

3""" 

4 

5import sys 

6import pytest 

7import click 

8from tests.conftest import ApioRunner 

9from apio.utils.cmd_util import ( 

10 ApioOption, 

11 ApioCmdContext, 

12 check_at_most_one_param, 

13) 

14 

15# TODO: Add more tests. 

16 

17 

18# -- A fake command for testing. 

19@click.command(name="fake_cmd") 

20@click.option("_opt1", "--opt1", is_flag=True, cls=ApioOption) 

21@click.option("_opt2", "--opt2", is_flag=True, cls=ApioOption) 

22@click.option("_opt3", "--opt3", is_flag=True, cls=ApioOption) 

23@click.option("_opt4", "--opt4", is_flag=True, cls=ApioOption) 

24def fake_cmd(*, _opt1, _opt2, _opt3, _opt4): 

25 """Fake click command for testing.""" 

26 sys.exit(0) 

27 

28 

29def test_check_at_most_one_param(apio_runner: ApioRunner): 

30 """Tests the check_at_most_one_param() function.""" 

31 

32 # -- No option is specified. Should be ok. 

33 cmd_ctx = ApioCmdContext(fake_cmd) 

34 fake_cmd.parse_args(cmd_ctx, []) 

35 check_at_most_one_param(cmd_ctx, ["_opt1", "_opt2", "_opt3"]) 

36 

37 # -- One option is specified. Should be ok. 

38 cmd_ctx = ApioCmdContext(fake_cmd) 

39 fake_cmd.parse_args(cmd_ctx, ["--opt1"]) 

40 check_at_most_one_param(cmd_ctx, ["_opt1", "_opt2", "_opt3"]) 

41 

42 # -- Another option is specified. Should be ok. 

43 cmd_ctx = ApioCmdContext(fake_cmd) 

44 fake_cmd.parse_args(cmd_ctx, ["--opt2"]) 

45 check_at_most_one_param(cmd_ctx, ["_opt1", "_opt2", "_opt3"]) 

46 

47 # -- Two options but one is non related to the check. Should be ok. 

48 cmd_ctx = ApioCmdContext(fake_cmd) 

49 fake_cmd.parse_args(cmd_ctx, ["--opt1", "--opt4"]) 

50 check_at_most_one_param(cmd_ctx, ["_opt1", "_opt2", "_opt3"]) 

51 

52 # -- Two options are specifies. Should fail. 

53 cmd_ctx = ApioCmdContext(fake_cmd) 

54 fake_cmd.parse_args(cmd_ctx, ["--opt1", "--opt2"]) 

55 with apio_runner.with_logger() as log: 

56 with pytest.raises(SystemExit) as e: 

57 check_at_most_one_param(cmd_ctx, ["_opt1", "_opt2", "_opt3"]) 

58 assert e.value.code == 1 

59 assert "--opt1 and --opt2 cannot be combined together" in log.out