Coverage for rust2rpm/tests/test_end_to_end.py: 100%

76 statements  

« prev     ^ index     » next       coverage.py v7.6.7, created at 2024-11-26 13:52 +0100

1import os 

2import re 

3from importlib import resources 

4from pathlib import Path 

5 

6import pytest 

7 

8from rust2rpm import log 

9from rust2rpm.__main__ import main 

10 

11 

12@pytest.mark.parametrize( 

13 ("filename", "compat", "spec_name"), 

14 [ 

15 ("a-lib-0.1.0.crate", False, "rust-a-lib.spec"), 

16 ("a-lib-0.1.0.crate", True, "rust-a-lib0.1.spec"), 

17 ("a-bin-0.1.0.crate", False, "rust-a-bin.spec"), 

18 ], 

19 ids=repr, 

20) 

21def test_cli_crate(tmp_path: Path, filename: str, compat: bool, spec_name: str): # noqa: FBT001 

22 crate_path = Path(str(resources.files("rust2rpm.tests.data.fixtures").joinpath(filename))) 

23 spec_path = Path(str(resources.files("rust2rpm.tests.data.specs").joinpath(spec_name))) 

24 

25 cli_args: list[str] = [ 

26 "--target", 

27 "fedora", 

28 "--offline", 

29 "--path", 

30 str(crate_path), 

31 "--output-directory", 

32 str(tmp_path), 

33 ] 

34 if compat: 

35 cli_args.append("--compat") 

36 

37 main(cli_args) 

38 

39 rendered = (tmp_path / spec_name).read_text() 

40 rendered = re.sub("(# Generated by rust2rpm) .*", r"\1 NNN", rendered) 

41 

42 if os.getenv("UPDATE_FIXTURES") == "1": # pragma nocover 

43 # helper mode to create test data 

44 spec_path.write_text(rendered) 

45 

46 else: 

47 expected = spec_path.read_text() 

48 assert rendered == expected 

49 

50 

51@pytest.mark.parametrize( 

52 ("filename", "out_name", "spec_name"), 

53 [ 

54 ("a-bin-0.1.0.crate", "rust-a-bin.spec", "rust-a-bin.epel8.spec"), 

55 ], 

56 ids=repr, 

57) 

58def test_cli_crate_epel8(tmp_path: Path, filename: str, out_name: str, spec_name: str): 

59 crate_path = Path(str(resources.files("rust2rpm.tests.data.fixtures").joinpath(filename))) 

60 spec_path = Path(str(resources.files("rust2rpm.tests.data.specs").joinpath(spec_name))) 

61 

62 cli_args: list[str] = [ 

63 "--target", 

64 "epel8", 

65 "--offline", 

66 "--path", 

67 str(crate_path), 

68 "--output-directory", 

69 str(tmp_path), 

70 ] 

71 

72 main(cli_args) 

73 

74 rendered = (tmp_path / out_name).read_text() 

75 rendered = re.sub("(# Generated by rust2rpm) .*", r"\1 NNN", rendered) 

76 

77 if os.getenv("UPDATE_FIXTURES") == "1": # pragma nocover 

78 # helper mode to create test data 

79 spec_path.write_text(rendered) 

80 

81 else: 

82 expected = spec_path.read_text() 

83 assert rendered == expected 

84 

85 

86@pytest.mark.parametrize( 

87 ("directory", "version", "spec_name"), 

88 [ 

89 ("a-bin", None, "a-bin.spec"), 

90 ("a-workspace", "0.1.0", "a-workspace.spec"), 

91 ], 

92 ids=repr, 

93) 

94def test_cli_path_toml(tmp_path: Path, directory: str, version: str | None, spec_name: str): 

95 project_path = Path(str(resources.files("rust2rpm.tests.data.fixtures").joinpath(directory))) 

96 spec_path = Path(str(resources.files("rust2rpm.tests.data.specs").joinpath(spec_name))) 

97 

98 cli_args: list[str] = [ 

99 "--target", 

100 "fedora", 

101 "--offline", 

102 "--path", 

103 str(project_path / "Cargo.toml"), 

104 "--output-directory", 

105 str(tmp_path), 

106 ] 

107 

108 if version: 

109 cli_args.append(f"@{version}") 

110 

111 main(cli_args) 

112 

113 rendered = (tmp_path / spec_name).read_text() 

114 rendered = re.sub("(# Generated by rust2rpm) .*", r"\1 NNN", rendered) 

115 

116 if os.getenv("UPDATE_FIXTURES") == "1": # pragma nocover 

117 # helper mode to create test data 

118 spec_path.write_text(rendered) 

119 

120 else: 

121 expected = spec_path.read_text() 

122 assert rendered == expected 

123 

124 

125@pytest.mark.parametrize( 

126 ("directory", "version", "spec_name"), 

127 [ 

128 ("a-bin", None, "a-bin.spec"), 

129 ("a-workspace", "0.1.0", "a-workspace.spec"), 

130 ], 

131 ids=repr, 

132) 

133def test_cli_path_dir(tmp_path: Path, directory: str, version: str | None, spec_name: str): 

134 project_path = Path(str(resources.files("rust2rpm.tests.data.fixtures").joinpath(directory))) 

135 spec_path = Path(str(resources.files("rust2rpm.tests.data.specs").joinpath(spec_name))) 

136 

137 cli_args: list[str] = [ 

138 "--target", 

139 "fedora", 

140 "--offline", 

141 "--path", 

142 str(project_path), 

143 "--output-directory", 

144 str(tmp_path), 

145 ] 

146 

147 if version: 

148 cli_args.append(f"@{version}") 

149 

150 main(cli_args) 

151 

152 rendered = (tmp_path / spec_name).read_text() 

153 rendered = re.sub("(# Generated by rust2rpm) .*", r"\1 NNN", rendered) 

154 

155 if os.getenv("UPDATE_FIXTURES") == "1": # pragma nocover 

156 # helper mode to create test data 

157 spec_path.write_text(rendered) 

158 

159 else: 

160 expected = spec_path.read_text() 

161 assert rendered == expected 

162 

163 

164@pytest.mark.parametrize( 

165 ("filename", "conf_name", "messages"), 

166 [ 

167 ( 

168 "a-lib-0.1.0.crate", 

169 "a-lib-1.rust2rpm.toml", 

170 [ 

171 "Extra source 'foo' might conflict with other files in the future. " 

172 "Using source numbers >= 2 is recommended.", 

173 "Extra patch 'bar' might conflict with other patches in the future. " 

174 "Using patch numbers >= 2 is recommended.", 

175 ], 

176 ), 

177 ], 

178 ids=repr, 

179) 

180def test_cli_messages( 

181 capsys: pytest.CaptureFixture[str], 

182 tmp_path: Path, 

183 filename: str, 

184 conf_name: str, 

185 messages: list[str], 

186): 

187 crate_path = Path(str(resources.files("rust2rpm.tests.data.fixtures").joinpath(filename))) 

188 conf_path = Path(str(resources.files("rust2rpm.tests.data.config").joinpath(conf_name))) 

189 

190 cli_args: list[str] = [ 

191 "--config-file", 

192 str(conf_path), 

193 "--target", 

194 "fedora", 

195 "--offline", 

196 "--path", 

197 str(crate_path), 

198 "--output-directory", 

199 str(tmp_path), 

200 ] 

201 

202 log.NOWRAP = True 

203 

204 main(cli_args) 

205 

206 stderr = capsys.readouterr().err 

207 for message in messages: 

208 assert message in stderr 

209 

210 

211@pytest.mark.parametrize( 

212 ("filename", "conf_name", "messages"), 

213 [ 

214 ( 

215 "a-lib-0.1.0.crate", 

216 "a-lib-2.rust2rpm.toml", 

217 [ 

218 "Extra source 'foo' conflicts with the primary tarball. Use a number larger than 1 instead.", 

219 ], 

220 ), 

221 ( 

222 "does-not-exist", 

223 None, 

224 [ 

225 "Invalid path argument:", 

226 "is not a local file or directory", 

227 ], 

228 ), 

229 ], 

230 ids=repr, 

231) 

232def test_cli_errors( 

233 capsys: pytest.CaptureFixture[str], 

234 tmp_path: Path, 

235 filename: str, 

236 conf_name: str | None, 

237 messages: list[str], 

238): 

239 crate_path = Path(str(resources.files("rust2rpm.tests.data.fixtures").joinpath(filename))) 

240 conf_path = Path(str(resources.files("rust2rpm.tests.data.config").joinpath(conf_name))) if conf_name else None 

241 

242 cli_args: list[str] = [ 

243 "--target", 

244 "fedora", 

245 "--offline", 

246 "--path", 

247 str(crate_path), 

248 "--output-directory", 

249 str(tmp_path), 

250 ] 

251 if conf_name: 

252 cli_args.extend(["--config-file", str(conf_path)]) 

253 

254 log.NOWRAP = True 

255 

256 with pytest.raises(SystemExit): 

257 main(cli_args) 

258 

259 stderr = capsys.readouterr().err 

260 for message in messages: 

261 assert message in stderr