Coverage for tests/unit_tests/utils/test_cmd_util.py: 97%
32 statements
« prev ^ index » next coverage.py v7.17.2, created at 2026-11-07 05:08 +0000
« prev ^ index » next coverage.py v7.17.2, created at 2026-11-07 05:08 +0000
1# This file is part of the Apio project
2# (C) 2016-present FPGAwars
3# Project authors:
4# * Jesús Arroyo (Jesus89) (2016-2019)
5# * Juan Gonzalez (obijuan) (2019-2024)
6# * The Apio Team (2024-present)
7# License: GPLv2
9"""
10Tests of cmd_util.py
11"""
13import sys
14import pytest
15import click
16from tests.conftest import ApioRunner
17from apio.utils.cmd_util import (
18 ApioOption,
19 ApioCmdContext,
20 check_at_most_one_param,
21)
23# TODO: Add more tests.
26# -- A fake command for testing.
27@click.command(name="fake_cmd")
28@click.option("_opt1", "--opt1", is_flag=True, cls=ApioOption)
29@click.option("_opt2", "--opt2", is_flag=True, cls=ApioOption)
30@click.option("_opt3", "--opt3", is_flag=True, cls=ApioOption)
31@click.option("_opt4", "--opt4", is_flag=True, cls=ApioOption)
32def fake_cmd(*, _opt1, _opt2, _opt3, _opt4):
33 """Fake click command for testing."""
34 sys.exit(0)
37def test_check_at_most_one_param(apio_runner: ApioRunner):
38 """Tests the check_at_most_one_param() function."""
40 # -- No option is specified. Should be ok.
41 cmd_ctx = ApioCmdContext(fake_cmd)
42 fake_cmd.parse_args(cmd_ctx, [])
43 check_at_most_one_param(cmd_ctx, ["_opt1", "_opt2", "_opt3"])
45 # -- One option is specified. Should be ok.
46 cmd_ctx = ApioCmdContext(fake_cmd)
47 fake_cmd.parse_args(cmd_ctx, ["--opt1"])
48 check_at_most_one_param(cmd_ctx, ["_opt1", "_opt2", "_opt3"])
50 # -- Another option is specified. Should be ok.
51 cmd_ctx = ApioCmdContext(fake_cmd)
52 fake_cmd.parse_args(cmd_ctx, ["--opt2"])
53 check_at_most_one_param(cmd_ctx, ["_opt1", "_opt2", "_opt3"])
55 # -- Two options but one is non related to the check. Should be ok.
56 cmd_ctx = ApioCmdContext(fake_cmd)
57 fake_cmd.parse_args(cmd_ctx, ["--opt1", "--opt4"])
58 check_at_most_one_param(cmd_ctx, ["_opt1", "_opt2", "_opt3"])
60 # -- Two options are specifies. Should fail.
61 cmd_ctx = ApioCmdContext(fake_cmd)
62 fake_cmd.parse_args(cmd_ctx, ["--opt1", "--opt2"])
63 with apio_runner.with_logger() as log:
64 with pytest.raises(SystemExit) as e:
65 check_at_most_one_param(cmd_ctx, ["_opt1", "_opt2", "_opt3"])
66 assert e.value.code == 1
67 assert "--opt1 and --opt2 cannot be combined together" in log.out