Coverage for tests / unit / cli / test_cli_programmatic.py: 100%
38 statements
« prev ^ index » next coverage.py v7.13.0, created at 2026-04-03 18:53 +0000
« prev ^ index » next coverage.py v7.13.0, created at 2026-04-03 18:53 +0000
1"""Programmatic invocation tests for CLI command functions."""
3from __future__ import annotations
5from unittest.mock import MagicMock
7import pytest
8from assertpy import assert_that
10from lintro.cli_utils.commands.check import check as check_prog
11from lintro.cli_utils.commands.format import format_code
12from lintro.enums.action import Action
15def test_check_programmatic_success(monkeypatch: pytest.MonkeyPatch) -> None:
16 """Programmatic check returns None on success.
18 Args:
19 monkeypatch: Pytest monkeypatch fixture to stub executor return.
20 """
21 import lintro.cli_utils.commands.check as check_mod
23 mock_run = MagicMock(return_value=0)
24 monkeypatch.setattr(check_mod, "run_lint_tools_simple", mock_run, raising=True)
26 # Function returns None on success (no exception raised)
27 check_prog(
28 paths=(".",),
29 tools="ruff",
30 tool_options=None,
31 exclude=None,
32 include_venv=False,
33 output=None,
34 output_format="grid",
35 group_by="auto",
36 ignore_conflicts=False,
37 verbose=False,
38 no_log=False,
39 )
41 mock_run.assert_called_once()
42 call_kwargs = mock_run.call_args.kwargs
43 assert_that(call_kwargs["paths"]).is_equal_to(["."])
44 assert_that(call_kwargs["tools"]).is_equal_to("ruff")
45 assert_that(call_kwargs["action"]).is_equal_to(Action.CHECK)
48def test_check_programmatic_failure_raises(monkeypatch: pytest.MonkeyPatch) -> None:
49 """Programmatic check raises SystemExit when executor returns non-zero.
51 Args:
52 monkeypatch: Pytest fixture for patching modules and attributes.
53 """
54 import lintro.cli_utils.commands.check as check_mod
56 monkeypatch.setattr(
57 check_mod,
58 "run_lint_tools_simple",
59 lambda **k: 1,
60 raising=True,
61 )
62 with pytest.raises(SystemExit) as exc_info:
63 check_prog(
64 paths=(".",),
65 tools="ruff",
66 tool_options=None,
67 exclude=None,
68 include_venv=False,
69 output=None,
70 output_format="grid",
71 group_by="auto",
72 ignore_conflicts=False,
73 verbose=False,
74 no_log=False,
75 )
76 assert_that(exc_info.value.code).is_equal_to(1)
79def test_format_programmatic_success(monkeypatch: pytest.MonkeyPatch) -> None:
80 """Programmatic format returns None on success.
82 Args:
83 monkeypatch: Pytest fixture for patching modules and attributes.
84 """
85 import lintro.cli_utils.commands.format as format_mod
87 mock_run = MagicMock(return_value=0)
88 monkeypatch.setattr(
89 format_mod,
90 "run_lint_tools_simple",
91 mock_run,
92 raising=True,
93 )
95 # Function returns None on success (no exception raised)
96 format_code(
97 paths=["."],
98 tools="prettier",
99 tool_options=None,
100 exclude=None,
101 include_venv=False,
102 group_by="auto",
103 output_format="grid",
104 verbose=False,
105 )
107 mock_run.assert_called_once()
108 call_kwargs = mock_run.call_args.kwargs
109 assert_that(call_kwargs["paths"]).is_equal_to(["."])
110 assert_that(call_kwargs["tools"]).is_equal_to("prettier")
111 assert_that(call_kwargs["action"]).is_equal_to("fmt")
114def test_format_programmatic_failure_raises(monkeypatch: pytest.MonkeyPatch) -> None:
115 """Programmatic format raises when executor returns non-zero.
117 Args:
118 monkeypatch: Pytest fixture for patching modules and attributes.
119 """
120 import lintro.cli_utils.commands.format as format_mod
122 monkeypatch.setattr(
123 format_mod,
124 "run_lint_tools_simple",
125 lambda **k: 1,
126 raising=True,
127 )
128 with pytest.raises(RuntimeError):
129 format_code(
130 paths=["."],
131 tools="prettier",
132 tool_options=None,
133 exclude=None,
134 include_venv=False,
135 group_by="auto",
136 output_format="grid",
137 verbose=False,
138 )