Coverage for tests/test_get_requirements.py: 100%

45 statements  

« prev     ^ index     » next       coverage.py v6.4.2, created at 2022-08-01 13:01 +0200

1from typing import Any, Final, Optional, Union 

2from pathlib import Path 

3 

4import pytest 

5 

6from iso_freeze.lib import PyPackage 

7from iso_freeze.get_requirements import ( 

8 read_toml, 

9 read_pip_report, 

10 build_pip_report_command, 

11) 

12 

13 

14TEST_TOML: Final[dict[str, Any]] = { 

15 "project": { 

16 "dependencies": ["tomli"], 

17 "optional-dependencies": { 

18 "dev": ["pytest", "pytest-mock"], 

19 "virtualenv": ["virtualenv"], 

20 }, 

21 } 

22} 

23 

24TEST_EMPTY_TOML: Final[dict[str, Any]] = { 

25 "something": {"no_project_section": "nothing"} 

26} 

27 

28TEST_NO_OPS_TOML: Final[dict[str, Any]] = { 

29 "project": { 

30 "dependencies": ["tomli"], 

31 } 

32} 

33 

34 

35def test_read_toml_base_requirements() -> None: 

36 """Test if base requirements are captured correctly.""" 

37 base_requirements: list[str] = read_toml( 

38 toml_dict=TEST_TOML, optional_dependency=None 

39 ) 

40 assert base_requirements == ["tomli"] 

41 

42 

43def test_read_toml_optional_dependency() -> None: 

44 """Test optional dependency is captured correctly.""" 

45 dev_deps: list[str] = read_toml(toml_dict=TEST_TOML, optional_dependency="dev") 

46 assert dev_deps == [ 

47 "tomli", 

48 "pytest", 

49 "pytest-mock", 

50 ] 

51 

52 

53def test_read_toml_missing_optional_dependency() -> None: 

54 """Test if missing optional dependency causes sys.exit().""" 

55 with pytest.raises(SystemExit): 

56 read_toml(toml_dict=TEST_TOML, optional_dependency="something") 

57 

58 

59def test_read_toml_no_optional_dependencies() -> None: 

60 """Test if missing optional dependency causes sys.exit().""" 

61 with pytest.raises(SystemExit): 

62 read_toml(toml_dict=TEST_NO_OPS_TOML, optional_dependency="something") 

63 

64 

65def test_read_toml_no_project_section() -> None: 

66 """Test if TOML file without 'project' section causes sys.exit().""" 

67 with pytest.raises(SystemExit): 

68 read_toml(toml_dict=TEST_EMPTY_TOML, optional_dependency="something") 

69 

70 

71def test_build_pip_report_command() -> None: 

72 """Test whether pip command is build correctly.""" 

73 # First test: requirements file 

74 pip_report_command_1: list[Union[str, Path]] = build_pip_report_command( 

75 pip_report_input=["-r", Path("requirements.in")], 

76 python_exec=Path("python3"), 

77 pip_args=None, 

78 ) 

79 expected_pip_report_command_1 = [ 

80 "env", 

81 "PIP_REQUIRE_VIRTUALENV=false", 

82 Path("python3"), 

83 "-m", 

84 "pip", 

85 "install", 

86 "-q", 

87 "--dry-run", 

88 "--ignore-installed", 

89 "--report", 

90 "-", 

91 "-r", 

92 Path("requirements.in"), 

93 ] 

94 assert pip_report_command_1 == expected_pip_report_command_1 

95 # Second test: TOML dependencies 

96 pip_report_command_2: list[Union[str, Path]] = build_pip_report_command( 

97 pip_report_input=["tomli", "pytest", "pytest-mock"], 

98 python_exec=Path("python3"), 

99 pip_args=None, 

100 ) 

101 expected_pip_report_command_2: list[Union[str, Path]] = [ 

102 "env", 

103 "PIP_REQUIRE_VIRTUALENV=false", 

104 Path("python3"), 

105 "-m", 

106 "pip", 

107 "install", 

108 "-q", 

109 "--dry-run", 

110 "--ignore-installed", 

111 "--report", 

112 "-", 

113 "tomli", 

114 "pytest", 

115 "pytest-mock", 

116 ] 

117 assert pip_report_command_2 == expected_pip_report_command_2 

118 # Third test: TOML dependencies with pip-args 

119 pip_report_command_3: list[Union[str, Path]] = build_pip_report_command( 

120 pip_report_input=["tomli", "pytest", "pytest-mock"], 

121 python_exec=Path("python3"), 

122 pip_args=["--upgrade-strategy", "eager", "--retries", "10"], 

123 ) 

124 expected_pip_report_command_3: list[Union[str, Path]] = [ 

125 "env", 

126 "PIP_REQUIRE_VIRTUALENV=false", 

127 Path("python3"), 

128 "-m", 

129 "pip", 

130 "install", 

131 "--upgrade-strategy", 

132 "eager", 

133 "--retries", 

134 "10", 

135 "-q", 

136 "--dry-run", 

137 "--ignore-installed", 

138 "--report", 

139 "-", 

140 "tomli", 

141 "pytest", 

142 "pytest-mock", 

143 ] 

144 assert pip_report_command_3 == expected_pip_report_command_3 

145 # Fourth test: requirements file with pip-args 

146 pip_report_command_4: list[Union[str, Path]] = build_pip_report_command( 

147 pip_report_input=["-r", Path("requirements.in")], 

148 python_exec=Path("python3"), 

149 pip_args=["--upgrade-strategy", "eager", "--require-hashes"], 

150 ) 

151 expected_pip_report_command_4: list[Union[str, Path]] = [ 

152 "env", 

153 "PIP_REQUIRE_VIRTUALENV=false", 

154 Path("python3"), 

155 "-m", 

156 "pip", 

157 "install", 

158 "--upgrade-strategy", 

159 "eager", 

160 "--require-hashes", 

161 "-q", 

162 "--dry-run", 

163 "--ignore-installed", 

164 "--report", 

165 "-", 

166 "-r", 

167 Path("requirements.in"), 

168 ] 

169 assert pip_report_command_4 == expected_pip_report_command_4 

170 

171 

172def test_read_pip_report() -> None: 

173 """Test if pip install --report is correctly captured.""" 

174 mocked_pip_report_output: dict[str, Any] = { 

175 "environment": {}, 

176 "install": [ 

177 { 

178 "download_info": { 

179 "archive_info": {"hash": "sha256=some_hash"}, 

180 "url": "some_url", 

181 }, 

182 "is_direct": False, 

183 "metadata": { 

184 "author_email": "Someone" "<someone@email.com>", 

185 "classifier": ["License :: OSI Approved :: MIT "], 

186 "description": "", 

187 "keywords": ["something"], 

188 "metadata_version": "2.1", 

189 "name": "cool_package", 

190 "project_url": [ 

191 "Homepage, " "https://github.com/", 

192 ], 

193 "requires_python": ">=3.7", 

194 "summary": "A cool package", 

195 "version": "2.0.1", 

196 }, 

197 "requested": True, 

198 } 

199 ], 

200 "pip_version": "22.2", 

201 "version": "0", 

202 } 

203 expected_result_1: Optional[list[PyPackage]] = [ 

204 PyPackage( 

205 name="cool_package", 

206 version="2.0.1", 

207 requested=True, 

208 hash="sha256:some_hash", 

209 ) 

210 ] 

211 actual_result_1: Optional[list[PyPackage]] = read_pip_report( 

212 mocked_pip_report_output 

213 ) 

214 assert actual_result_1 == expected_result_1 

215 mocked_pip_report_output_no_install: dict[str, Any] = { 

216 "environment": {}, 

217 "install": [], 

218 "pip_version": "22.2", 

219 "version": "0", 

220 } 

221 expected_result_2: Optional[list[PyPackage]] = None 

222 actual_result_2: Optional[list[PyPackage]] = read_pip_report( 

223 mocked_pip_report_output_no_install 

224 ) 

225 assert actual_result_2 == expected_result_2