Coverage for python / felis / tests / run_cli.py: 17%
19 statements
« prev ^ index » next coverage.py v7.13.5, created at 2026-04-26 08:49 +0000
« prev ^ index » next coverage.py v7.13.5, created at 2026-04-26 08:49 +0000
1"""Test utility for running cli commands."""
3# This file is part of felis.
4#
5# Developed for the LSST Data Management System.
6# This product includes software developed by the LSST Project
7# (https://www.lsst.org).
8# See the COPYRIGHT file at the top-level directory of this distribution
9# for details of code ownership.
10#
11# This program is free software: you can redistribute it and/or modify
12# it under the terms of the GNU General Public License as published by
13# the Free Software Foundation, either version 3 of the License, or
14# (at your option) any later version.
15#
16# This program is distributed in the hope that it will be useful,
17# but WITHOUT ANY WARRANTY; without even the implied warranty of
18# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19# GNU General Public License for more details.
20#
21# You should have received a copy of the GNU General Public License
22# along with this program. If not, see <https://www.gnu.org/licenses/>.
24import logging
26from click.testing import CliRunner
28from felis.cli import cli
30__all__ = ["run_cli"]
33def run_cli(
34 cmd: list[str],
35 log_level: int = logging.WARNING,
36 catch_exceptions: bool = False,
37 expect_error: bool = False,
38 print_cmd: bool = False,
39 print_output: bool = False,
40 id_generation: bool = False,
41) -> None:
42 """Run a CLI command and check the exit code.
44 Parameters
45 ----------
46 cmd : list[str]
47 The command to run.
48 log_level : int
49 The logging level to use, by default logging.WARNING.
50 catch_exceptions : bool
51 Whether to catch exceptions, by default False.
52 expect_error : bool
53 Whether to expect an error, by default False.
54 print_cmd : bool
55 Whether to print the command, by default False.
56 print_output : bool
57 Whether to print the output, by default False.
58 id_generation : bool
59 Whether to enable id generation, by default False.
60 """
61 if not cmd:
62 raise ValueError("No command provided.")
63 full_cmd = ["--log-level", logging.getLevelName(log_level)] + cmd
64 if id_generation:
65 full_cmd = ["--id-generation"] + full_cmd
66 if print_cmd:
67 print(f"Running command: felis {' '.join(full_cmd)}")
68 runner = CliRunner()
69 result = runner.invoke(
70 cli,
71 full_cmd,
72 catch_exceptions=catch_exceptions,
73 )
74 if print_output:
75 print(result.output)
76 if expect_error:
77 assert result.exit_code != 0
78 else:
79 assert result.exit_code == 0