Coverage for tests/test_cli.py: 100%

1 import os

2 import shutil

3

4 from typer.testing import CliRunner

5

6 from src.su6.cli import _check_tool, app

7 from src.su6.core import EXIT_CODE_COMMAND_NOT_FOUND

8

9 runner = CliRunner()

10

11 from ._shared import BAD_CODE, EXAMPLES_PATH, FIXABLE_CODE, GOOD_CODE

12

13

14 def test_ruff_good():

15 result = runner.invoke(app, ["ruff", GOOD_CODE])

16 assert result.exit_code == 0

17

18

19 def test_ruff_bad():

20 result = runner.invoke(app, ["ruff", BAD_CODE])

21 assert result.exit_code == 1

22

23

24 def test_black_good():

25 result = runner.invoke(app, ["black", GOOD_CODE])

26 assert result.exit_code == 0

27

28

29 def test_black_bad():

30 result = runner.invoke(app, ["black", BAD_CODE])

31 assert result.exit_code == 1

32

33

34 def test_black_fix():

35 shutil.copyfile(BAD_CODE, FIXABLE_CODE)

36 try:

37 # 1. assert error

38 result = runner.invoke(app, ["black", FIXABLE_CODE])

39 assert result.exit_code == 1

40

41 # 2. fix

42 result = runner.invoke(app, ["--verbosity", "3", "black", FIXABLE_CODE, "--fix"])

43 assert result.exit_code == 0

44

45 # 3. assert success

46 result = runner.invoke(app, ["black", FIXABLE_CODE])

47 assert result.exit_code == 0

48 finally:

49 # cleanup

50 os.unlink(FIXABLE_CODE)

51

52

53 def test_mypy_good():

54 result = runner.invoke(app, ["mypy", GOOD_CODE])

55 assert result.exit_code == 0

56

57

58 def test_mypy_bad():

59 result = runner.invoke(app, ["mypy", BAD_CODE])

60 assert result.exit_code == 1

61

62

63 def test_bandit_good():

64 result = runner.invoke(app, ["bandit", GOOD_CODE])

65 assert result.exit_code == 0

66

67

68 def test_bandit_bad():

69 result = runner.invoke(app, ["bandit", BAD_CODE])

70 assert result.exit_code == 1

71

72

73 def test_isort_good():

74 result = runner.invoke(app, ["isort", GOOD_CODE])

75 assert result.exit_code == 0

76

77

78 def test_isort_bad():

79 result = runner.invoke(app, ["isort", BAD_CODE])

80 assert result.exit_code == 1

81

82

83 def test_isort_fix():

84 shutil.copyfile(BAD_CODE, FIXABLE_CODE)

85 try:

86 # 1. assert error

87 result = runner.invoke(app, ["isort", FIXABLE_CODE])

88 assert result.exit_code == 1

89

90 # 2. fix

91 result = runner.invoke(app, ["--verbosity", "3", "isort", FIXABLE_CODE, "--fix"])

92 assert result.exit_code == 0

93

94 # 3. assert success

95 result = runner.invoke(app, ["isort", FIXABLE_CODE])

96 assert result.exit_code == 0

97 finally:

98 # cleanup

99 os.unlink(FIXABLE_CODE)

100

101

102 def test_all_fix():

103 shutil.copyfile(BAD_CODE, FIXABLE_CODE)

104 try:

105 # 1. assert error

106 result = runner.invoke(app, ["isort", FIXABLE_CODE])

107 assert result.exit_code == 1

108

109 result = runner.invoke(app, ["black", FIXABLE_CODE])

110 assert result.exit_code == 1

111

112 # 2. fix

113 result = runner.invoke(app, ["--verbosity", "3", "fix", FIXABLE_CODE, "--ignore-uninstalled"])

114 assert result.exit_code == 0

115

116 # 3. assert success

117 result = runner.invoke(app, ["isort", FIXABLE_CODE])

118 assert result.exit_code == 0

119

120 result = runner.invoke(app, ["black", FIXABLE_CODE])

121 assert result.exit_code == 0

122 finally:

123 # cleanup

124 os.unlink(FIXABLE_CODE)

125

126

127 def test_pydocstyle_good():

128 result = runner.invoke(app, ["pydocstyle", GOOD_CODE])

129 assert result.exit_code == 0

130

131

132 def test_pydocstyle_bad():

133 result = runner.invoke(app, ["pydocstyle", BAD_CODE])

134 assert result.exit_code == 1

135

136

137 def test_all_good():

138 result = runner.invoke(app, ["all", GOOD_CODE])

139 assert result.exit_code == 0

140

141 result = runner.invoke(app, ["all", GOOD_CODE, "--ignore-uninstalled"])

142 # can't really test without having everything installed,

143 # but at least we can make sure the flag doen't crash anything!

144 assert result.exit_code == 0

145

146

147 def test_all_bad():

148 result = runner.invoke(app, ["all", BAD_CODE])

149 assert result.exit_code == 1

150

151

152### test_pytest is kind of an issue since this seems to hang the first running pytest session

153

154

155 def test_command_not_found():

156 fake_tool = _check_tool("xxx-should-never-exist-xxx")

157

158 assert fake_tool == EXIT_CODE_COMMAND_NOT_FOUND

159

160

161 def test_custom_include_exclude():

162 code_file = str(EXAMPLES_PATH / "black_good_mypy_bad.py")

163

164 result = runner.invoke(app, ["--config", str(EXAMPLES_PATH / "only_black.toml"), "all", code_file])

165 assert result.exit_code == 0

166

167 result = runner.invoke(app, ["--config", str(EXAMPLES_PATH / "only_mypy.toml"), "all", code_file])

168 assert result.exit_code == 1