Coverage for tests/test_versioning.py: 100.00%

38 statements  

« prev     ^ index     » next       coverage.py v7.4.4, created at 2024-04-02 00:26 +0200

1"""Test version bumping.""" 

2 

3from __future__ import annotations 

4 

5import pytest 

6 

7from git_changelog.versioning import ( 

8 PEP440Strategy, 

9 PEP440Version, 

10 SemVerStrategy, 

11 bump_pep440, 

12 bump_semver, 

13 version_prefix, 

14) 

15 

16 

17@pytest.mark.parametrize( 

18 ("version", "expected"), 

19 [ 

20 ("1", ("1", "")), 

21 ("v1", ("1", "v")), 

22 ("va", ("a", "v")), 

23 ("x1", ("x1", "")), 

24 ], 

25) 

26def test_version_prefix(version: str, expected: tuple[str, str]) -> None: 

27 """Test splitting version and `v` prefix. 

28 

29 Parameters: 

30 version: The version to split. 

31 bumped: The expected version and prefix. 

32 """ 

33 assert expected == version_prefix(version) 

34 

35 

36@pytest.mark.parametrize( 

37 ("part", "version", "bumped"), 

38 [ 

39 ("major", "0.0.1", "1.0.0"), 

40 ("major", "0.1.0", "1.0.0"), 

41 ("major", "0.1.1", "1.0.0"), 

42 ("major", "1.0.0", "2.0.0"), 

43 ("major", "1.0.1", "2.0.0"), 

44 ("major", "1.1.0", "2.0.0"), 

45 ("major", "1.1.1", "2.0.0"), 

46 ("minor", "0.0.1", "0.1.0"), 

47 ("minor", "0.1.0", "0.2.0"), 

48 ("minor", "0.1.1", "0.2.0"), 

49 ("minor", "1.0.0", "1.1.0"), 

50 ("minor", "1.0.1", "1.1.0"), 

51 ("minor", "1.1.0", "1.2.0"), 

52 ("minor", "1.1.1", "1.2.0"), 

53 ("patch", "0.0.1", "0.0.2"), 

54 ("patch", "0.1.0", "0.1.1"), 

55 ("patch", "0.1.1", "0.1.2"), 

56 ("patch", "1.0.0", "1.0.1"), 

57 ("patch", "1.0.1", "1.0.2"), 

58 ("patch", "1.1.0", "1.1.1"), 

59 ("patch", "1.1.1", "1.1.2"), 

60 ("release", "1.1.1", "1.1.1"), 

61 ("release", "1.1.1-alpha", "1.1.1"), 

62 ("release", "1.1.1-alpha+build", "1.1.1"), 

63 ], 

64) 

65def test_semver_bump_patch(part: SemVerStrategy, version: str, bumped: str) -> None: 

66 """Test default and patch version bumping. 

67 

68 Parameters: 

69 part: The version part to bump. 

70 version: The base version. 

71 bumped: The expected, bumped version. 

72 """ 

73 assert bump_semver(version, part, zerover=False) == bumped 

74 

75 

76@pytest.mark.parametrize( 

77 ("version", "bumped"), 

78 [ 

79 ("0.0.1", "0.1.0"), 

80 ("0.1.0", "0.2.0"), 

81 ("0.1.1", "0.2.0"), 

82 ("1.0.0", "2.0.0"), 

83 ("1.0.1", "2.0.0"), 

84 ("1.1.0", "2.0.0"), 

85 ("1.1.1", "2.0.0"), 

86 ], 

87) 

88def test_semver_bump_major_zerover(version: str, bumped: str) -> None: 

89 """Test major version bumping with zerover. 

90 

91 Parameters: 

92 version: The base version. 

93 bumped: The expected, bumped version. 

94 """ 

95 assert bump_semver(version, "major", zerover=True) == bumped 

96 

97 

98def test_semver_bump_unknown_part() -> None: 

99 """Bump unknown part of a SemVer version.""" 

100 with pytest.raises(ValueError, match="Invalid strategy unknown"): 

101 bump_semver("1.0.0", "unknown") # type: ignore[arg-type] 

102 

103 

104@pytest.mark.parametrize( 

105 ("part", "version", "bumped"), 

106 [ 

107 ("epoch", "0!1.0", "1!1.0"), 

108 ("epoch", "1!1.0", "2!1.0"), 

109 ("epoch", "2!1.0a0.dev1", "3!1.0"), 

110 ("release", "1.0a0", "1.0"), 

111 ("release", "1.0.1b1", "1.0.1"), 

112 ("release", "2.1rc2", "2.1"), 

113 ("release", "1.dev0", "1"), 

114 ("major", "0.0.1", "1.0.0"), 

115 ("major", "0.1.0", "1.0.0"), 

116 ("major", "0.1.1", "1.0.0"), 

117 ("major", "1.0.0", "2.0.0"), 

118 ("major", "1.0.1", "2.0.0"), 

119 ("major", "1.1.0", "2.0.0"), 

120 ("major", "1.1.1", "2.0.0"), 

121 ("major", "1", "2"), 

122 ("major", "1.1", "2.0"), 

123 ("major", "1.1.1.1", "2.0.0.0"), 

124 ("major", "1a2.post3", "2"), 

125 ("minor", "0.0.1", "0.1.0"), 

126 ("minor", "0.1.0", "0.2.0"), 

127 ("minor", "0.1.1", "0.2.0"), 

128 ("minor", "1.0.0", "1.1.0"), 

129 ("minor", "1.0.1", "1.1.0"), 

130 ("minor", "1.1.0", "1.2.0"), 

131 ("minor", "1.1.1", "1.2.0"), 

132 ("minor", "1", "1.1"), 

133 ("minor", "1.1", "1.2"), 

134 ("minor", "1.1.1.1", "1.2.0.0"), 

135 ("minor", "1a2.post3", "1.1"), 

136 ("micro", "0.0.1", "0.0.2"), 

137 ("micro", "0.1.0", "0.1.1"), 

138 ("micro", "0.1.1", "0.1.2"), 

139 ("micro", "1.0.0", "1.0.1"), 

140 ("micro", "1.0.1", "1.0.2"), 

141 ("micro", "1.1.0", "1.1.1"), 

142 ("micro", "1.1.1", "1.1.2"), 

143 ("micro", "1", "1.0.1"), 

144 ("micro", "1.1", "1.1.1"), 

145 ("micro", "1.1.1.1", "1.1.2.0"), 

146 ("micro", "1a2.post3", "1.0.1"), 

147 ("pre", "1a0", "1a1"), 

148 ("pre", "1b1", "1b2"), 

149 ("pre", "1c2", "1rc3"), 

150 ("pre", "1.0a2.post3", "1.0a3"), 

151 ("alpha", "1a0", "1a1"), 

152 ("alpha", "1.1a99", "1.1a100"), 

153 ("beta", "1a0", "1b0"), 

154 ("beta", "1.1a99", "1.1b0"), 

155 ("beta", "1b0", "1b1"), 

156 ("beta", "1.1b1", "1.1b2"), 

157 ("candidate", "1a0", "1rc0"), 

158 ("candidate", "1.1a99", "1.1rc0"), 

159 ("candidate", "1b0", "1rc0"), 

160 ("candidate", "1.1b1", "1.1rc0"), 

161 ("candidate", "1c0", "1rc1"), 

162 ("candidate", "1.1rc1", "1.1rc2"), 

163 ("post", "1", "1.post0"), 

164 ("post", "1.post0", "1.post1"), 

165 ("post", "1.0a2.post3", "1.0a2.post4"), 

166 ("dev", "1.dev0", "1.dev1"), 

167 ("dev", "1.post0.dev1", "1.post0.dev2"), 

168 ("dev", "1.0a2.post3.dev2", "1.0a2.post3.dev3"), 

169 ("major+alpha", "1", "2a0"), 

170 ("major+alpha", "1a0", "2a0"), 

171 ("major+alpha", "1b0.dev0", "2a0"), 

172 ("major+beta", "1", "2b0"), 

173 ("major+beta", "1a0", "2b0"), 

174 ("major+candidate", "1", "2rc0"), 

175 ("major+candidate", "1a0", "2rc0"), 

176 ("major+dev", "1", "2.dev0"), 

177 ("major+dev", "1a0.post0", "2.dev0"), 

178 ("major+alpha+dev", "1", "2a0.dev0"), 

179 ("major+alpha+dev", "1a1.dev1", "2a0.dev0"), 

180 ("major+beta+dev", "1", "2b0.dev0"), 

181 ("major+beta+dev", "1a1.dev1", "2b0.dev0"), 

182 ("major+candidate+dev", "1", "2rc0.dev0"), 

183 ("major+candidate+dev", "1a1.dev1", "2rc0.dev0"), 

184 ("minor+alpha", "1", "1.1a0"), 

185 ("minor+alpha", "1a0", "1.1a0"), 

186 ("minor+alpha", "1b0.dev0", "1.1a0"), 

187 ("minor+beta", "1", "1.1b0"), 

188 ("minor+beta", "1a0", "1.1b0"), 

189 ("minor+candidate", "1", "1.1rc0"), 

190 ("minor+candidate", "1a0", "1.1rc0"), 

191 ("minor+dev", "1", "1.1.dev0"), 

192 ("minor+dev", "1a0.post0", "1.1.dev0"), 

193 ("minor+alpha+dev", "1", "1.1a0.dev0"), 

194 ("minor+alpha+dev", "1a1.dev1", "1.1a0.dev0"), 

195 ("minor+beta+dev", "1", "1.1b0.dev0"), 

196 ("minor+beta+dev", "1a1.dev1", "1.1b0.dev0"), 

197 ("minor+candidate+dev", "1", "1.1rc0.dev0"), 

198 ("minor+candidate+dev", "1a1.dev1", "1.1rc0.dev0"), 

199 ("micro+alpha", "1", "1.0.1a0"), 

200 ("micro+alpha", "1a0", "1.0.1a0"), 

201 ("micro+alpha", "1b0.dev0", "1.0.1a0"), 

202 ("micro+beta", "1", "1.0.1b0"), 

203 ("micro+beta", "1a0", "1.0.1b0"), 

204 ("micro+candidate", "1", "1.0.1rc0"), 

205 ("micro+candidate", "1a0", "1.0.1rc0"), 

206 ("micro+dev", "1", "1.0.1.dev0"), 

207 ("micro+dev", "1a0.post0", "1.0.1.dev0"), 

208 ("micro+alpha+dev", "1", "1.0.1a0.dev0"), 

209 ("micro+alpha+dev", "1a1.dev1", "1.0.1a0.dev0"), 

210 ("micro+beta+dev", "1", "1.0.1b0.dev0"), 

211 ("micro+beta+dev", "1a1.dev1", "1.0.1b0.dev0"), 

212 ("micro+candidate+dev", "1", "1.0.1rc0.dev0"), 

213 ("micro+candidate+dev", "1a1.dev1", "1.0.1rc0.dev0"), 

214 ("alpha+dev", "1a0", "1a1.dev0"), 

215 ("beta+dev", "1a0", "1b0.dev0"), 

216 ("beta+dev", "1b0", "1b1.dev0"), 

217 ("candidate+dev", "1a0", "1rc0.dev0"), 

218 ("candidate+dev", "1b0", "1rc0.dev0"), 

219 ("candidate+dev", "1rc0", "1rc1.dev0"), 

220 ], 

221) 

222def test_pep440_bump(part: PEP440Strategy, version: str, bumped: str) -> None: 

223 """Test PEP 440 version bumping. 

224 

225 Parameters: 

226 part: The version part to bump. 

227 version: The base version. 

228 bumped: The expected, bumped version. 

229 """ 

230 assert bump_pep440(version, part, zerover=False) == bumped 

231 

232 

233@pytest.mark.parametrize( 

234 ("version", "bumped"), 

235 [ 

236 ("0.0.1", "0.1.0"), 

237 ("0.1.0", "0.2.0"), 

238 ("0.1.1", "0.2.0"), 

239 ("1.0.0", "2.0.0"), 

240 ("1.0.1", "2.0.0"), 

241 ("1.1.0", "2.0.0"), 

242 ("1.1.1", "2.0.0"), 

243 ("1", "2"), 

244 ("1.1", "2.0"), 

245 ("1a2.post3", "2"), 

246 ], 

247) 

248def test_pep440_bump_major_zerover(version: str, bumped: str) -> None: 

249 """Test major version bumping with zerover. 

250 

251 Parameters: 

252 version: The base version. 

253 bumped: The expected, bumped version. 

254 """ 

255 assert bump_pep440(version, "major", zerover=True) == bumped 

256 

257 

258@pytest.mark.parametrize( 

259 ("part", "version"), 

260 [ 

261 ("pre", "1"), 

262 ("pre", "1.0"), 

263 ("pre", "1.0.post0"), 

264 ("pre", "1.0.dev0"), 

265 ("release", "1.post0"), 

266 ("release", "1.post0.dev0"), 

267 ("alpha", "1b0"), 

268 ("alpha", "1rc0"), 

269 ("beta", "1rc0"), 

270 ("dev", "1"), 

271 ("dev", "1a0"), 

272 ("dev", "1.post0"), 

273 ], 

274) 

275def test_pep440_bump_error(part: PEP440Strategy, version: str) -> None: 

276 """Test bumping errors. 

277 

278 Parameters: 

279 part: The version part to bump. 

280 version: The base version. 

281 """ 

282 with pytest.raises(ValueError, match="Cannot bump"): 

283 bump_pep440(version, part) 

284 

285 

286def test_pep440_bump_unknown_part() -> None: 

287 """Bump unknown part of a PEP 440 version.""" 

288 with pytest.raises(ValueError, match="Invalid strategy unknown"): 

289 bump_pep440("1", "unknown") # type: ignore[arg-type] 

290 

291 

292@pytest.mark.parametrize( 

293 ("part", "version", "bumped"), 

294 [ 

295 ("release", "1.0", "1"), 

296 ("release", "1.0a0", "1"), 

297 ("major", "1.0", "2"), 

298 ("major", "1.0a0", "2"), 

299 ("minor", "1.0", "1.1"), 

300 ("minor", "1.0.0a0", "1.1"), 

301 ("micro", "1.0.0.0", "1.0.1"), 

302 ("micro", "1.0.0.0a0", "1.0.1"), 

303 ], 

304) 

305def test_pep440_bump_trim(part: PEP440Strategy, version: str, bumped: str) -> None: 

306 """Test PEP 440 version bumping and trimming. 

307 

308 Parameters: 

309 part: The version part to bump. 

310 version: The base version. 

311 bumped: The expected, bumped version. 

312 """ 

313 assert bump_pep440(version, part, trim=True) == bumped 

314 

315 

316@pytest.mark.parametrize( 

317 ("part", "version", "dented"), 

318 [ 

319 ("pre", "1", "1a0"), 

320 ("pre", "1.0", "1.0a0"), 

321 ("alpha", "1", "1a0"), 

322 ("alpha", "1.0", "1.0a0"), 

323 ("beta", "1", "1b0"), 

324 ("beta", "1.0", "1.0b0"), 

325 ("candidate", "1", "1rc0"), 

326 ("candidate", "1.0", "1.0rc0"), 

327 ("dev", "1", "1.dev0"), 

328 ("dev", "1.0", "1.0.dev0"), 

329 ], 

330) 

331def test_pep440_dent(part: PEP440Strategy, version: str, dented: str) -> None: 

332 """Test denting versions. 

333 

334 Parameters: 

335 part: The version part to dent. 

336 version: The base version. 

337 dented: The expected, dented version. 

338 """ 

339 assert str(getattr(PEP440Version(version), f"dent_{part}")()) == dented 

340 

341 

342@pytest.mark.parametrize( 

343 ("part", "version"), 

344 [ 

345 ("pre", "1a0"), 

346 ("pre", "1b0"), 

347 ("pre", "1rc0"), 

348 ("alpha", "1a0"), 

349 ("alpha", "1b0"), 

350 ("alpha", "1rc0"), 

351 ("beta", "1a0"), 

352 ("beta", "1b0"), 

353 ("beta", "1rc0"), 

354 ("candidate", "1a0"), 

355 ("candidate", "1b0"), 

356 ("candidate", "1rc0"), 

357 ("dev", "1.dev0"), 

358 ], 

359) 

360def test_pep440_dent_error(part: PEP440Strategy, version: str) -> None: 

361 """Test denting errors. 

362 

363 Parameters: 

364 part: The version part to bump. 

365 version: The base version. 

366 """ 

367 with pytest.raises(ValueError, match="Cannot dent"): 

368 getattr(PEP440Version(version), f"dent_{part}")()