Coverage for tests / cli / test_cli.py: 100%

59 statements  

« prev     ^ index     » next       coverage.py v7.13.0, created at 2026-04-03 18:53 +0000

1"""Tests for CLI module.""" 

2 

3import subprocess 

4import sys 

5from unittest.mock import patch 

6 

7import pytest 

8from assertpy import assert_that 

9from click.testing import CliRunner 

10 

11from lintro.cli import cli 

12 

13 

14def test_cli_help() -> None: 

15 """Test that CLI shows help.""" 

16 runner = CliRunner() 

17 result = runner.invoke(cli, ["--help"]) 

18 assert_that(result.exit_code).is_equal_to(0) 

19 assert_that(result.output).contains("Lintro") 

20 

21 

22def test_cli_version() -> None: 

23 """Test that CLI shows version.""" 

24 runner = CliRunner() 

25 result = runner.invoke(cli, ["--version"]) 

26 assert_that(result.exit_code).is_equal_to(0) 

27 assert_that(result.output.lower()).contains("version") 

28 

29 

30@pytest.mark.parametrize( 

31 "command", 

32 ["check", "format", "list-tools", "test"], 

33 ids=["check", "format", "list-tools", "test"], 

34) 

35def test_cli_commands_registered(command: str) -> None: 

36 """Test that all commands are registered and show help. 

37 

38 Args: 

39 command: CLI command to test. 

40 """ 

41 runner = CliRunner() 

42 result = runner.invoke(cli, [command, "--help"]) 

43 assert_that(result.exit_code).is_equal_to(0) 

44 

45 

46def test_main_function() -> None: 

47 """Test the main function.""" 

48 runner = CliRunner() 

49 result = runner.invoke(cli, ["--help"]) 

50 assert_that(result.exit_code).is_equal_to(0) 

51 assert_that(result.output).contains("Lintro") 

52 

53 

54@pytest.mark.parametrize( 

55 "alias,expected_text", 

56 [ 

57 ("chk", "check"), 

58 ("fmt", "format"), 

59 ("ls", "list all available tools"), 

60 ("tst", "Run tests"), 

61 ], 

62 ids=["chk", "fmt", "ls", "tst"], 

63) 

64def test_cli_command_aliases(alias: str, expected_text: str) -> None: 

65 """Test that command aliases work. 

66 

67 Args: 

68 alias: Command alias to test. 

69 expected_text: Text expected in help output. 

70 """ 

71 runner = CliRunner() 

72 result = runner.invoke(cli, [alias, "--help"]) 

73 assert_that(result.exit_code).is_equal_to(0) 

74 assert_that(result.output.lower()).contains(expected_text.lower()) 

75 

76 

77def test_cli_with_no_args() -> None: 

78 """Test CLI with no arguments.""" 

79 runner = CliRunner() 

80 result = runner.invoke(cli, []) 

81 assert_that(result.exit_code).is_equal_to(0) 

82 assert_that(result.output).is_equal_to("") 

83 

84 

85def test_main_module_execution() -> None: 

86 """Test that __main__.py can be executed directly.""" 

87 with patch.object(sys, "argv", ["lintro", "--help"]): 

88 import lintro.__main__ 

89 

90 assert_that(lintro.__main__).is_not_none() 

91 

92 

93def test_main_module_as_script() -> None: 

94 """Test that __main__.py works when run as a script.""" 

95 result = subprocess.run( 

96 [sys.executable, "-m", "lintro", "--help"], 

97 capture_output=True, 

98 text=True, 

99 timeout=10, 

100 ) 

101 assert_that(result.returncode).is_equal_to(0) 

102 assert_that(result.stdout).contains("Lintro") 

103 

104 

105def test_command_chaining_basic() -> None: 

106 """Test basic command chaining syntax recognition.""" 

107 runner = CliRunner() 

108 # Patch both format and check commands to prevent real tools from executing 

109 with ( 

110 patch("lintro.cli_utils.commands.format.run_lint_tools_simple") as mock_fmt, 

111 patch("lintro.cli_utils.commands.check.run_lint_tools_simple") as mock_chk, 

112 ): 

113 mock_fmt.return_value = 0 

114 mock_chk.return_value = 0 

115 # Test that chaining syntax is accepted (should parse correctly) 

116 result = runner.invoke(cli, ["fmt", ",", "chk"]) 

117 # We expect this to succeed with mocked runners, not parsing errors 

118 assert_that(result.output).does_not_contain("Error: unexpected argument") 

119 

120 

121@pytest.mark.parametrize( 

122 "command", 

123 ["check", "format"], 

124 ids=["check", "format"], 

125) 

126def test_pytest_excluded_from_command_help(command: str) -> None: 

127 """Test that pytest is excluded from available tools in check/format commands. 

128 

129 Args: 

130 command: CLI command to test. 

131 """ 

132 runner = CliRunner() 

133 result = runner.invoke(cli, [command, "--help"]) 

134 assert_that(result.exit_code).is_equal_to(0) 

135 # The help should not mention pytest as an available tool 

136 assert_that(result.output).does_not_contain("pytest")