Coverage for python / felis / tests / run_cli.py: 17%
19 statements
« prev ^ index » next coverage.py v7.14.0, created at 2026-05-16 00:50 -0700
« prev ^ index » next coverage.py v7.14.0, created at 2026-05-16 00:50 -0700
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.DEBUG,
36 expect_error: bool = False,
37 print_cmd: bool = False,
38 print_output: bool = False,
39 id_generation: bool = False,
40) -> None:
41 """Run a CLI command and check the exit code.
43 Parameters
44 ----------
45 cmd : list[str]
46 The command to run.
47 log_level : int
48 The logging level to use, by default logging.DEBUG so that all messages
49 are emitted.
50 expect_error : bool
51 Whether to expect an error, by default False.
52 print_cmd : bool
53 Whether to print the command, by default False.
54 print_output : bool
55 Whether to print the output, by default False.
56 id_generation : bool
57 Whether to enable id generation, by default False.
58 """
59 if not cmd:
60 raise ValueError("No command provided.")
61 full_cmd = ["--log-level", logging.getLevelName(log_level)] + cmd
62 if id_generation:
63 full_cmd = ["--id-generation"] + full_cmd
64 if print_cmd:
65 print(f"Running command: felis {' '.join(full_cmd)}")
66 runner = CliRunner()
67 result = runner.invoke(
68 cli,
69 full_cmd,
70 catch_exceptions=expect_error,
71 )
72 if print_output:
73 print(result.output)
74 if expect_error:
75 assert result.exit_code != 0
76 else:
77 assert result.exit_code == 0, result.output