| 1 |
import datetime as dt |
| 2 |
import os |
| 3 |
import re |
| 4 |
from contextlib import contextmanager |
| 5 |
from pathlib import Path |
| 6 |
from typing import Callable, Iterator, Optional |
| 7 |
|
| 8 |
try: |
| 9 |
import importlib.metadata as ilm |
| 10 |
except ImportError: |
| 11 |
import importlib_metadata as ilm # type: ignore |
| 12 |
|
| 13 |
import pytest |
| 14 |
|
| 15 |
from dunamai import ( |
| 16 |
bump_version, |
| 17 |
check_version, |
| 18 |
get_version, |
| 19 |
Version, |
| 20 |
serialize_pep440, |
| 21 |
serialize_pvp, |
| 22 |
serialize_semver, |
| 23 |
Pattern, |
| 24 |
Style, |
| 25 |
Vcs, |
| 26 |
_run_cmd, |
| 27 |
VERSION_SOURCE_PATTERN, |
| 28 |
) |
| 29 |
|
| 30 |
|
| 31 |
@contextmanager |
| 32 |
def chdir(where: Path) -> Iterator[None]: |
| 33 |
start = Path.cwd() |
| 34 |
os.chdir(str(where)) |
| 35 |
try: |
| 36 |
yield |
| 37 |
finally: |
| 38 |
os.chdir(str(start)) |
| 39 |
|
| 40 |
|
| 41 |
def make_run_callback(where: Path) -> Callable: |
| 42 |
def inner(command, expected_code: int = 0): |
| 43 |
_, out = _run_cmd(command, where=where, codes=[expected_code]) |
| 44 |
return out |
| 45 |
|
| 46 |
return inner |
| 47 |
|
| 48 |
|
| 49 |
def make_from_callback(function: Callable, mock_commit: Optional[str] = "abc") -> Callable: |
| 50 |
def inner(*args, **kwargs): |
| 51 |
version = function(*args, **kwargs) |
| 52 |
if version.commit and mock_commit: |
| 53 |
version.commit = mock_commit |
| 54 |
return version |
| 55 |
|
| 56 |
return inner |
| 57 |
|
| 58 |
|
| 59 |
from_any_vcs = make_from_callback(Version.from_any_vcs) |
| 60 |
from_any_vcs_unmocked = make_from_callback(Version.from_any_vcs, mock_commit=None) |
| 61 |
from_explicit_vcs = make_from_callback(Version.from_vcs) |
| 62 |
|
| 63 |
|
| 64 |
def test__pattern__regex() -> None: |
| 65 |
assert Pattern.Default.regex() == VERSION_SOURCE_PATTERN |
| 66 |
assert Pattern.DefaultUnprefixed.regex() == VERSION_SOURCE_PATTERN.replace("^v", "^v?", 1) |
| 67 |
assert Pattern.Default.regex("foo-") == VERSION_SOURCE_PATTERN.replace("^", "^foo-", 1) |
| 68 |
|
| 69 |
|
| 70 |
def test__pattern__parse() -> None: |
| 71 |
assert Pattern.parse(r"(?P<base>\d+)") == r"(?P<base>\d+)" |
| 72 |
assert Pattern.parse(r"(?P<base>\d+)", "foo-") == r"(?P<base>\d+)" |
| 73 |
assert Pattern.parse(r"^(?P<base>\d+)", "foo-") == r"^foo-(?P<base>\d+)" |
| 74 |
|
| 75 |
assert Pattern.parse("default") == Pattern.Default.regex() |
| 76 |
assert Pattern.parse("default-unprefixed") == Pattern.DefaultUnprefixed.regex() |
| 77 |
assert Pattern.parse("default", "foo-") == Pattern.Default.regex("foo-") |
| 78 |
|
| 79 |
with pytest.raises(ValueError): |
| 80 |
Pattern.parse(r"foo") |
| 81 |
|
| 82 |
|
| 83 |
def test__version__init() -> None: |
| 84 |
v = Version("1", stage=("a", 2), distance=3, commit="abc", dirty=True, tagged_metadata="def", epoch=4) |
| 85 |
assert v.base == "1" |
| 86 |
assert v.stage == "a" |
| 87 |
assert v.revision == 2 |
| 88 |
assert v.distance == 3 |
| 89 |
assert v.commit == "abc" |
| 90 |
assert v.dirty |
| 91 |
assert v.tagged_metadata == "def" |
| 92 |
assert v.epoch == 4 |
| 93 |
|
| 94 |
|
| 95 |
def test__version__str() -> None: |
| 96 |
v = Version("1", stage=("a", 2), distance=3, commit="abc", dirty=True) |
| 97 |
assert str(v) == v.serialize() |
| 98 |
|
| 99 |
|
| 100 |
def test__version__repr() -> None: |
| 101 |
v = Version( |
| 102 |
"1", |
| 103 |
stage=("a", 2), |
| 104 |
distance=3, |
| 105 |
commit="abc", |
| 106 |
dirty=True, |
| 107 |
tagged_metadata="tagged", |
| 108 |
epoch=4, |
| 109 |
branch="master", |
| 110 |
timestamp=dt.datetime(2000, 1, 2, 3, 4, 5).replace(tzinfo=dt.timezone.utc), |
| 111 |
) |
| 112 |
assert repr(v) == ( |
| 113 |
"Version(base='1', stage='a', revision=2, distance=3, commit='abc'," |
| 114 |
" dirty=True, tagged_metadata='tagged', epoch=4, branch='master'," |
| 115 |
" timestamp=datetime.datetime(2000, 1, 2, 3, 4, 5, tzinfo=datetime.timezone.utc))" |
| 116 |
) |
| 117 |
|
| 118 |
|
| 119 |
def test__version__ordering() -> None: |
| 120 |
assert Version("0.1.0", distance=2) == Version("0.1.0", distance=2) |
| 121 |
assert Version("0.2.0") > Version("0.1.0") |
| 122 |
assert Version("0.1.0", distance=2) > Version("0.1.0", distance=1) |
| 123 |
with pytest.raises(TypeError): |
| 124 |
Version("0.1.0") == "0.1.0" |
| 125 |
with pytest.raises(TypeError): |
| 126 |
Version("0.1.0") < "0.2.0" |
| 127 |
assert Version("0.1.0", commit="a") != Version("0.1.0", commit="b") |
| 128 |
assert Version("0.1.0", dirty=True) == Version("0.1.0", dirty=True) |
| 129 |
assert Version("0.1.0", dirty=False) != Version("0.1.0", dirty=True) |
| 130 |
assert Version("0.1.0") != Version("0.1.0", dirty=True) |
| 131 |
assert Version("0.1.0") != Version("0.1.0", dirty=False) |
| 132 |
|
| 133 |
assert Version("0.2.0") < Version("0.2.1") |
| 134 |
assert Version("1.1.0") < Version("1.2.0") |
| 135 |
assert Version("1.0.0", stage=("rc", 1)) < Version("1.0.0") |
| 136 |
assert Version("1.0.0", stage=("a", 1)) < Version("1.0.0", stage=("b", 1)) |
| 137 |
assert Version("1.0.0") < Version("1.0.0", stage=("post", 1)) |
| 138 |
assert Version("1.0.0", epoch=1) < Version("1.0.0", epoch=2) |
| 139 |
assert Version("1.0.0", stage=("bar", 1)) < Version("1.0.0", stage=("foo", 1)) |
| 140 |
|
| 141 |
|
| 142 |
def test__version__serialize__pep440() -> None: |
| 143 |
assert Version("0.1.0").serialize() == "0.1.0" |
| 144 |
|
| 145 |
assert Version("1", stage=("a", 2), distance=3, commit="abc", dirty=True).serialize() == "1a2.post3.dev0+abc" |
| 146 |
assert ( |
| 147 |
Version("1", stage=("a", 2), distance=3, commit="abc", dirty=True).serialize(dirty=True) |
| 148 |
== "1a2.post3.dev0+abc.dirty" |
| 149 |
) |
| 150 |
assert ( |
| 151 |
Version("1", stage=("a", 2), distance=3, commit="abc", dirty=True).serialize(metadata=False) == "1a2.post3.dev0" |
| 152 |
) |
| 153 |
assert ( |
| 154 |
Version("1", stage=("a", 2), distance=3, commit="abc", dirty=True).serialize(metadata=False, dirty=True) |
| 155 |
== "1a2.post3.dev0" |
| 156 |
) |
| 157 |
|
| 158 |
assert Version("1", stage=("a", 0), distance=3, commit="abc", dirty=False).serialize() == "1a0.post3.dev0+abc" |
| 159 |
assert Version("1", stage=("a", 2), distance=0, commit="abc", dirty=False).serialize() == "1a2" |
| 160 |
assert Version("1", stage=("a", 2), distance=3, commit="000", dirty=False).serialize() == "1a2.post3.dev0+000" |
| 161 |
assert ( |
| 162 |
Version("1", stage=("a", 0), distance=3, commit="abc", dirty=False).serialize(commit_prefix="x") |
| 163 |
== "1a0.post3.dev0+xabc" |
| 164 |
) |
| 165 |
|
| 166 |
assert Version("1", stage=("a", None)).serialize() == "1a0" |
| 167 |
assert Version("1", stage=("b", 2)).serialize() == "1b2" |
| 168 |
assert Version("1", stage=("rc", 2)).serialize() == "1rc2" |
| 169 |
|
| 170 |
assert Version("0.1.0").serialize(bump=True) == "0.1.0" |
| 171 |
assert Version("0.1.0", distance=3).serialize(bump=True) == "0.1.1.dev3" |
| 172 |
assert Version("1", distance=3).serialize(bump=True) == "2.dev3" |
| 173 |
assert Version("0.1.0", stage=("a", None), distance=3).serialize(bump=True) == "0.1.0a2.dev3" |
| 174 |
assert Version("0.1.0", stage=("b", 2), distance=3).serialize(bump=True) == "0.1.0b3.dev3" |
| 175 |
|
| 176 |
assert Version("0.1.0", epoch=2).serialize() == "2!0.1.0" |
| 177 |
|
| 178 |
assert Version("0.1.0", stage=("post", 1)).serialize() == "0.1.0.post1" |
| 179 |
assert Version("0.1.0", stage=("post", 1), distance=3).serialize() == "0.1.0.post1.dev3" |
| 180 |
assert Version("0.1.0", stage=("post", 1), distance=3).serialize(bump=True) == "0.1.0.post2.dev3" |
| 181 |
assert Version("0.1.0", stage=("dev", 1)).serialize() == "0.1.0.dev1" |
| 182 |
assert Version("0.1.0", stage=("dev", 1), distance=3).serialize() == "0.1.0.dev4" |
| 183 |
assert Version("0.1.0", stage=("dev", 1), distance=3).serialize(bump=True) == "0.1.0.dev5" |
| 184 |
|
| 185 |
|
| 186 |
def test__version__serialize__semver() -> None: |
| 187 |
style = Style.SemVer |
| 188 |
assert Version("0.1.0").serialize(style=style) == "0.1.0" |
| 189 |
|
| 190 |
assert ( |
| 191 |
Version("0.1.0", stage=("alpha", 2), distance=3, commit="abc", dirty=True).serialize(style=style) |
| 192 |
== "0.1.0-alpha.2.post.3+abc" |
| 193 |
) |
| 194 |
assert ( |
| 195 |
Version("0.1.0", stage=("alpha", 2), distance=3, commit="abc", dirty=True).serialize(dirty=True, style=style) |
| 196 |
== "0.1.0-alpha.2.post.3+abc.dirty" |
| 197 |
) |
| 198 |
assert ( |
| 199 |
Version("0.1.0", stage=("alpha", 2), distance=3, commit="abc", dirty=True).serialize( |
| 200 |
metadata=False, style=style |
| 201 |
) |
| 202 |
== "0.1.0-alpha.2.post.3" |
| 203 |
) |
| 204 |
assert ( |
| 205 |
Version("0.1.0", stage=("alpha", 2), distance=3, commit="abc", dirty=True).serialize( |
| 206 |
metadata=False, dirty=True, style=style |
| 207 |
) |
| 208 |
== "0.1.0-alpha.2.post.3" |
| 209 |
) |
| 210 |
|
| 211 |
assert ( |
| 212 |
Version("0.1.0", stage=("alpha", 0), distance=3, commit="abc", dirty=False).serialize(style=style) |
| 213 |
== "0.1.0-alpha.0.post.3+abc" |
| 214 |
) |
| 215 |
assert ( |
| 216 |
Version("0.1.0", stage=("alpha", 2), distance=0, commit="abc", dirty=False).serialize(style=style) |
| 217 |
== "0.1.0-alpha.2" |
| 218 |
) |
| 219 |
assert ( |
| 220 |
Version("0.1.0", stage=("alpha", 2), distance=3, commit="000", dirty=False).serialize(style=style) |
| 221 |
== "0.1.0-alpha.2.post.3+000" |
| 222 |
) |
| 223 |
|
| 224 |
assert Version("0.1.0", stage=("alpha", None)).serialize(style=style) == "0.1.0-alpha" |
| 225 |
assert Version("0.1.0", stage=("beta", 2)).serialize(style=style) == "0.1.0-beta.2" |
| 226 |
assert Version("0.1.0", stage=("rc", 2)).serialize(style=style) == "0.1.0-rc.2" |
| 227 |
|
| 228 |
assert Version("0.1.0").serialize(style=style, bump=True) == "0.1.0" |
| 229 |
assert Version("0.1.0", distance=3).serialize(style=style, bump=True) == "0.1.1-pre.3" |
| 230 |
assert ( |
| 231 |
Version("0.1.0", stage=("alpha", None), distance=3).serialize(style=style, bump=True) == "0.1.0-alpha.2.pre.3" |
| 232 |
) |
| 233 |
assert Version("0.1.0", stage=("beta", 2), distance=4).serialize(style=style, bump=True) == "0.1.0-beta.3.pre.4" |
| 234 |
|
| 235 |
assert Version("0.1.0", epoch=2).serialize(style=style) == "0.1.0" |
| 236 |
|
| 237 |
|
| 238 |
def test__version__serialize__pvp() -> None: |
| 239 |
style = Style.Pvp |
| 240 |
assert Version("0.1.0").serialize(style=style) == "0.1.0" |
| 241 |
|
| 242 |
assert ( |
| 243 |
Version("0.1.0", stage=("alpha", 2), distance=3, commit="abc", dirty=True).serialize(style=style) |
| 244 |
== "0.1.0-alpha-2-post-3-abc" |
| 245 |
) |
| 246 |
assert ( |
| 247 |
Version("0.1.0", stage=("alpha", 2), distance=3, commit="abc", dirty=True).serialize(dirty=True, style=style) |
| 248 |
== "0.1.0-alpha-2-post-3-abc-dirty" |
| 249 |
) |
| 250 |
assert ( |
| 251 |
Version("0.1.0", stage=("alpha", 2), distance=3, commit="abc", dirty=True).serialize( |
| 252 |
metadata=False, style=style |
| 253 |
) |
| 254 |
== "0.1.0-alpha-2-post-3" |
| 255 |
) |
| 256 |
assert ( |
| 257 |
Version("0.1.0", stage=("alpha", 2), distance=3, commit="abc", dirty=True).serialize( |
| 258 |
metadata=False, dirty=True, style=style |
| 259 |
) |
| 260 |
== "0.1.0-alpha-2-post-3" |
| 261 |
) |
| 262 |
|
| 263 |
assert ( |
| 264 |
Version("0.1.0", stage=("alpha", 0), distance=3, commit="abc", dirty=False).serialize(style=style) |
| 265 |
== "0.1.0-alpha-0-post-3-abc" |
| 266 |
) |
| 267 |
assert ( |
| 268 |
Version("0.1.0", stage=("alpha", 2), distance=0, commit="abc", dirty=False).serialize(style=style) |
| 269 |
== "0.1.0-alpha-2" |
| 270 |
) |
| 271 |
assert ( |
| 272 |
Version("0.1.0", stage=("alpha", 2), distance=3, commit="000", dirty=False).serialize(style=style) |
| 273 |
== "0.1.0-alpha-2-post-3-000" |
| 274 |
) |
| 275 |
|
| 276 |
assert Version("0.1.0", stage=("alpha", None)).serialize(style=style) == "0.1.0-alpha" |
| 277 |
assert Version("0.1.0", stage=("beta", 2)).serialize(style=style) == "0.1.0-beta-2" |
| 278 |
assert Version("0.1.0", stage=("rc", 2)).serialize(style=style) == "0.1.0-rc-2" |
| 279 |
|
| 280 |
assert Version("0.1.0").serialize(style=style, bump=True) == "0.1.0" |
| 281 |
assert Version("0.1.0", distance=3).serialize(style=style, bump=True) == "0.1.1-pre-3" |
| 282 |
assert ( |
| 283 |
Version("0.1.0", stage=("alpha", None), distance=3).serialize(style=style, bump=True) == "0.1.0-alpha-2-pre-3" |
| 284 |
) |
| 285 |
assert Version("0.1.0", stage=("beta", 2), distance=4).serialize(style=style, bump=True) == "0.1.0-beta-3-pre-4" |
| 286 |
|
| 287 |
assert Version("0.1.0", epoch=2).serialize(style=style) == "0.1.0" |
| 288 |
|
| 289 |
|
| 290 |
def test__version__serialize__pep440_metadata() -> None: |
| 291 |
assert Version("0.1.0").serialize() == "0.1.0" |
| 292 |
assert Version("0.1.0").serialize(metadata=True) == "0.1.0" |
| 293 |
assert Version("0.1.0").serialize(metadata=False) == "0.1.0" |
| 294 |
|
| 295 |
assert Version("0.1.0", stage=("a", 1), commit="abc").serialize() == "0.1.0a1" |
| 296 |
assert Version("0.1.0", stage=("a", 1), commit="abc").serialize(metadata=True) == "0.1.0a1+abc" |
| 297 |
assert Version("0.1.0", stage=("a", 1), commit="abc").serialize(metadata=False) == "0.1.0a1" |
| 298 |
|
| 299 |
assert Version("0.1.0", distance=1, commit="abc").serialize() == "0.1.0.post1.dev0+abc" |
| 300 |
assert Version("0.1.0", distance=1, commit="abc").serialize(metadata=True) == "0.1.0.post1.dev0+abc" |
| 301 |
assert Version("0.1.0", distance=1, commit="abc").serialize(metadata=False) == "0.1.0.post1.dev0" |
| 302 |
|
| 303 |
assert ( |
| 304 |
Version("0.1.0", distance=1, commit="abc", tagged_metadata="def").serialize(tagged_metadata=True) |
| 305 |
== "0.1.0.post1.dev0+def.abc" |
| 306 |
) |
| 307 |
|
| 308 |
|
| 309 |
def test__version__serialize__semver_with_metadata() -> None: |
| 310 |
style = Style.SemVer |
| 311 |
assert Version("0.1.0").serialize(style=style) == "0.1.0" |
| 312 |
assert Version("0.1.0").serialize(metadata=True, style=style) == "0.1.0" |
| 313 |
assert Version("0.1.0").serialize(metadata=False, style=style) == "0.1.0" |
| 314 |
|
| 315 |
assert Version("0.1.0", stage=("a", 1), commit="abc").serialize(style=style) == "0.1.0-a.1" |
| 316 |
assert Version("0.1.0", stage=("a", 1), commit="abc").serialize(metadata=True, style=style) == "0.1.0-a.1+abc" |
| 317 |
assert Version("0.1.0", stage=("a", 1), commit="abc").serialize(metadata=False, style=style) == "0.1.0-a.1" |
| 318 |
|
| 319 |
assert Version("0.1.0", distance=1, commit="abc").serialize(style=style) == "0.1.0-post.1+abc" |
| 320 |
assert Version("0.1.0", distance=1, commit="abc").serialize(metadata=True, style=style) == "0.1.0-post.1+abc" |
| 321 |
assert Version("0.1.0", distance=1, commit="abc").serialize(metadata=False, style=style) == "0.1.0-post.1" |
| 322 |
|
| 323 |
assert ( |
| 324 |
Version("0.1.0", distance=1, commit="abc", tagged_metadata="def").serialize(style=style, tagged_metadata=True) |
| 325 |
== "0.1.0-post.1+def.abc" |
| 326 |
) |
| 327 |
|
| 328 |
|
| 329 |
def test__version__serialize__pvp_with_metadata() -> None: |
| 330 |
style = Style.Pvp |
| 331 |
assert Version("0.1.0").serialize(style=style) == "0.1.0" |
| 332 |
assert Version("0.1.0").serialize(metadata=True, style=style) == "0.1.0" |
| 333 |
assert Version("0.1.0").serialize(metadata=False, style=style) == "0.1.0" |
| 334 |
|
| 335 |
assert Version("0.1.0", stage=("a", 1), commit="abc").serialize(style=style) == "0.1.0-a-1" |
| 336 |
assert Version("0.1.0", stage=("a", 1), commit="abc").serialize(metadata=True, style=style) == "0.1.0-a-1-abc" |
| 337 |
assert Version("0.1.0", stage=("a", 1), commit="abc").serialize(metadata=False, style=style) == "0.1.0-a-1" |
| 338 |
|
| 339 |
assert Version("0.1.0", distance=1, commit="abc").serialize(style=style) == "0.1.0-post-1-abc" |
| 340 |
assert Version("0.1.0", distance=1, commit="abc").serialize(metadata=True, style=style) == "0.1.0-post-1-abc" |
| 341 |
assert Version("0.1.0", distance=1, commit="abc").serialize(metadata=False, style=style) == "0.1.0-post-1" |
| 342 |
|
| 343 |
assert ( |
| 344 |
Version("0.1.0", distance=1, commit="abc", tagged_metadata="def").serialize(style=style, tagged_metadata=True) |
| 345 |
== "0.1.0-post-1-def-abc" |
| 346 |
) |
| 347 |
|
| 348 |
|
| 349 |
def test__version__serialize__pep440_with_dirty() -> None: |
| 350 |
assert Version("0.1.0", dirty=True).serialize() == "0.1.0" |
| 351 |
assert Version("0.1.0", dirty=True).serialize(dirty=True) == "0.1.0+dirty" |
| 352 |
|
| 353 |
assert Version("0.1.0", dirty=False).serialize() == "0.1.0" |
| 354 |
assert Version("0.1.0", dirty=False).serialize(dirty=True) == "0.1.0" |
| 355 |
|
| 356 |
assert Version("0.1.0", dirty=True).serialize(metadata=True) == "0.1.0" |
| 357 |
assert Version("0.1.0", dirty=True).serialize(metadata=True, dirty=True) == "0.1.0+dirty" |
| 358 |
|
| 359 |
assert Version("0.1.0", dirty=True).serialize(metadata=False) == "0.1.0" |
| 360 |
assert Version("0.1.0", dirty=True).serialize(metadata=False, dirty=True) == "0.1.0" |
| 361 |
|
| 362 |
|
| 363 |
def test__version__serialize__semver_with_dirty() -> None: |
| 364 |
style = Style.SemVer |
| 365 |
assert Version("0.1.0", dirty=True).serialize(style=style) == "0.1.0" |
| 366 |
assert Version("0.1.0", dirty=True).serialize(dirty=True, style=style) == "0.1.0+dirty" |
| 367 |
|
| 368 |
assert Version("0.1.0", dirty=False).serialize(style=style) == "0.1.0" |
| 369 |
assert Version("0.1.0", dirty=False).serialize(dirty=True, style=style) == "0.1.0" |
| 370 |
|
| 371 |
assert Version("0.1.0", dirty=True).serialize(metadata=True, style=style) == "0.1.0" |
| 372 |
assert Version("0.1.0", dirty=True).serialize(metadata=True, dirty=True, style=style) == "0.1.0+dirty" |
| 373 |
|
| 374 |
assert Version("0.1.0", dirty=True).serialize(metadata=False, style=style) == "0.1.0" |
| 375 |
assert Version("0.1.0", dirty=True).serialize(metadata=False, dirty=True, style=style) == "0.1.0" |
| 376 |
|
| 377 |
|
| 378 |
def test__version__serialize__pvp_with_dirty() -> None: |
| 379 |
style = Style.Pvp |
| 380 |
assert Version("0.1.0", dirty=True).serialize(style=style) == "0.1.0" |
| 381 |
assert Version("0.1.0", dirty=True).serialize(dirty=True, style=style) == "0.1.0-dirty" |
| 382 |
|
| 383 |
assert Version("0.1.0", dirty=False).serialize(style=style) == "0.1.0" |
| 384 |
assert Version("0.1.0", dirty=False).serialize(dirty=True, style=style) == "0.1.0" |
| 385 |
|
| 386 |
assert Version("0.1.0", dirty=True).serialize(metadata=True, style=style) == "0.1.0" |
| 387 |
assert Version("0.1.0", dirty=True).serialize(metadata=True, dirty=True, style=style) == "0.1.0-dirty" |
| 388 |
|
| 389 |
assert Version("0.1.0", dirty=True).serialize(metadata=False, style=style) == "0.1.0" |
| 390 |
assert Version("0.1.0", dirty=True).serialize(metadata=False, dirty=True, style=style) == "0.1.0" |
| 391 |
|
| 392 |
|
| 393 |
def test__version__serialize__format_as_str() -> None: |
| 394 |
format = "{base},{stage},{revision},{distance},{commit},{dirty},{branch},{branch_escaped},{timestamp},{major},{minor},{patch}" |
| 395 |
assert Version("0.1.0").serialize(format=format) == "0.1.0,,,0,,clean,,,,0,1,0" |
| 396 |
assert ( |
| 397 |
Version( |
| 398 |
"1", |
| 399 |
stage=("a", 2), |
| 400 |
distance=3, |
| 401 |
commit="abc", |
| 402 |
dirty=True, |
| 403 |
branch="a/b", |
| 404 |
timestamp=dt.datetime(2001, 2, 3, 4, 5, 6, tzinfo=dt.timezone.utc), |
| 405 |
).serialize(format=format) |
| 406 |
== "1,a,2,3,abc,dirty,a/b,ab,20010203040506,1,0,0" |
| 407 |
) |
| 408 |
with pytest.raises(ValueError): |
| 409 |
Version("0.1.0").serialize(format="v{base}", style=Style.Pep440) |
| 410 |
|
| 411 |
assert Version("1", commit="abc").serialize(format="{commit}", commit_prefix="x") == "xabc" |
| 412 |
assert Version("1", branch="a/b").serialize(format="{branch_escaped}", escape_with=".") == "a.b" |
| 413 |
|
| 414 |
|
| 415 |
def test__version__serialize__format_as_callable() -> None: |
| 416 |
def format(v: Version) -> str: |
| 417 |
return "{},{},{}".format(v.base, v.stage, v.revision) |
| 418 |
|
| 419 |
assert Version("0.1.0").serialize(format=format) == "0.1.0,None,None" |
| 420 |
assert Version("1", stage=("a", 2)).serialize(format=format) == "1,a,2" |
| 421 |
with pytest.raises(ValueError): |
| 422 |
Version("0.1.0").serialize(format=lambda v: "v{}".format(v.base), style=Style.Pep440) |
| 423 |
|
| 424 |
def immutable(v: Version) -> str: |
| 425 |
v.distance += 100 |
| 426 |
return v.serialize() |
| 427 |
|
| 428 |
version = Version("0.1.0") |
| 429 |
version.serialize(format=immutable) |
| 430 |
assert version.distance == 0 |
| 431 |
|
| 432 |
|
| 433 |
def test__version__bump() -> None: |
| 434 |
assert Version("1.2.3").bump().serialize() == "1.2.4" |
| 435 |
assert Version("1.2.3").bump(-2).serialize() == "1.3.0" |
| 436 |
assert Version("1.2.3").bump(0).serialize() == "2.0.0" |
| 437 |
assert Version("1.2.3", stage=("a", None)).bump().serialize() == "1.2.3a2" |
| 438 |
assert Version("1.2.3", stage=("a", 4)).bump().serialize() == "1.2.3a5" |
| 439 |
|
| 440 |
assert Version("1.2.3", distance=0).bump(smart=False).serialize() == "1.2.4" |
| 441 |
assert Version("1.2.3", distance=0).bump(smart=True).serialize() == "1.2.3" |
| 442 |
assert Version("1.2.3", distance=0).serialize(bump=True) == "1.2.3" |
| 443 |
|
| 444 |
assert Version("1.2.3", distance=5).bump(smart=False).serialize() == "1.2.4.post5.dev0" |
| 445 |
assert Version("1.2.3", distance=5).bump(smart=True).serialize() == "1.2.4.dev5" |
| 446 |
assert Version("1.2.3", distance=5).serialize(bump=True) == "1.2.4.dev5" |
| 447 |
|
| 448 |
|
| 449 |
def test__version__parse(): |
| 450 |
assert Version.parse("1.2.3") == Version("1.2.3") |
| 451 |
assert Version.parse("1.2.3a") == Version("1.2.3", stage=("a", None)) |
| 452 |
assert Version.parse("1.2.3a3") == Version("1.2.3", stage=("a", 3)) |
| 453 |
assert Version.parse("1.2.3+7") == Version("1.2.3", distance=7) |
| 454 |
assert Version.parse("1.2.3+d7") == Version("1.2.3", distance=7) |
| 455 |
assert Version.parse("1.2.3+b6a9020") == Version("1.2.3", commit="b6a9020") |
| 456 |
assert Version.parse("1.2.3+gb6a9020") == Version("1.2.3", commit="b6a9020") |
| 457 |
assert Version.parse("1.2.3+dirty") == Version("1.2.3", dirty=True) |
| 458 |
assert Version.parse("1.2.3+clean") == Version("1.2.3", dirty=False) |
| 459 |
assert Version.parse("1.2.3a3+7.b6a9020.dirty") == Version( |
| 460 |
"1.2.3", stage=("a", 3), distance=7, commit="b6a9020", dirty=True |
| 461 |
) |
| 462 |
assert Version.parse("1.2.3a3+7.b6a9020.dirty.linux") == Version( |
| 463 |
"1.2.3", stage=("a", 3), distance=7, commit="b6a9020", dirty=True, tagged_metadata="linux" |
| 464 |
) |
| 465 |
assert Version.parse("2!1.2.3") == Version("1.2.3", epoch=2) |
| 466 |
assert Version.parse("2!1.2.3a3+d7.gb6a9020.dirty.linux") == Version( |
| 467 |
"1.2.3", |
| 468 |
stage=("a", 3), |
| 469 |
distance=7, |
| 470 |
commit="b6a9020", |
| 471 |
dirty=True, |
| 472 |
tagged_metadata="linux", |
| 473 |
epoch=2, |
| 474 |
) |
| 475 |
|
| 476 |
assert Version.parse("foo") == Version("foo") |
| 477 |
|
| 478 |
assert Version.parse("1.2.3.dev5") == Version("1.2.3", distance=5) |
| 479 |
assert Version.parse("1.2.3.post4") == Version("1.2.3", stage=("post", 4)) |
| 480 |
assert Version.parse("1.2.3.post4+d6") == Version("1.2.3", stage=("post", 4), distance=6) |
| 481 |
assert Version.parse("1.2.3.post4.dev5") == Version("1.2.3", distance=4) |
| 482 |
assert Version.parse("1.2.3.post4.dev5+d6") == Version("1.2.3", distance=10) |
| 483 |
assert Version.parse("1.2.3.post4.dev5.blah6") == Version("1.2.3.post4.dev5.blah6") |
| 484 |
|
| 485 |
|
| 486 |
def test__get_version__from_name() -> None: |
| 487 |
assert get_version("dunamai") == Version(ilm.version("dunamai")) |
| 488 |
|
| 489 |
|
| 490 |
def test__get_version__first_choice() -> None: |
| 491 |
assert get_version("dunamai", first_choice=lambda: Version("1")) == Version("1") |
| 492 |
|
| 493 |
|
| 494 |
def test__get_version__third_choice() -> None: |
| 495 |
assert get_version("dunamai_nonexistent_test", third_choice=lambda: Version("3")) == Version("3") |
| 496 |
|
| 497 |
|
| 498 |
def test__get_version__fallback() -> None: |
| 499 |
assert get_version("dunamai_nonexistent_test") == Version("0.0.0") |
| 500 |
|
| 501 |
|
| 502 |
def test__get_version__from_name__ignore() -> None: |
| 503 |
assert get_version( |
| 504 |
"dunamai", |
| 505 |
ignore=[Version(ilm.version("dunamai"))], |
| 506 |
fallback=Version("2"), |
| 507 |
) == Version("2") |
| 508 |
|
| 509 |
|
| 510 |
def test__get_version__first_choice__ignore() -> None: |
| 511 |
assert get_version( |
| 512 |
"dunamai_nonexistent_test", |
| 513 |
first_choice=lambda: Version("1"), |
| 514 |
ignore=[Version("1")], |
| 515 |
fallback=Version("2"), |
| 516 |
) == Version("2") |
| 517 |
|
| 518 |
|
| 519 |
def test__get_version__first_choice__ignore_with_distance() -> None: |
| 520 |
assert get_version( |
| 521 |
"dunamai_nonexistent_test", |
| 522 |
first_choice=lambda: Version("1", distance=2), |
| 523 |
ignore=[Version("1")], |
| 524 |
fallback=Version("2"), |
| 525 |
) == Version("2") |
| 526 |
assert get_version( |
| 527 |
"dunamai_nonexistent_test", |
| 528 |
first_choice=lambda: Version("1"), |
| 529 |
ignore=[Version("1", distance=2)], |
| 530 |
fallback=Version("2"), |
| 531 |
) != Version("2") |
| 532 |
|
| 533 |
|
| 534 |
def test__get_version__first_choice__ignore__with_commit() -> None: |
| 535 |
assert get_version( |
| 536 |
"dunamai_nonexistent_test", |
| 537 |
first_choice=lambda: Version("1", commit="aaaa"), |
| 538 |
ignore=[Version("1")], |
| 539 |
fallback=Version("2"), |
| 540 |
) == Version("2") |
| 541 |
|
| 542 |
|
| 543 |
def test__get_version__first_choice__ignore__without_commit() -> None: |
| 544 |
assert get_version( |
| 545 |
"dunamai_nonexistent_test", |
| 546 |
first_choice=lambda: Version("1"), |
| 547 |
ignore=[Version("1", commit="aaaa")], |
| 548 |
fallback=Version("2"), |
| 549 |
) == Version("1") |
| 550 |
|
| 551 |
|
| 552 |
def test__get_version__third_choice__ignore() -> None: |
| 553 |
assert get_version( |
| 554 |
"dunamai_nonexistent_test", |
| 555 |
third_choice=lambda: Version("3"), |
| 556 |
ignore=[Version("3")], |
| 557 |
fallback=Version("2"), |
| 558 |
) == Version("2") |
| 559 |
|
| 560 |
|
| 561 |
def test__version__from_any_vcs(tmp_path) -> None: |
| 562 |
with chdir(tmp_path): |
| 563 |
with pytest.raises(RuntimeError): |
| 564 |
Version.from_any_vcs() |
| 565 |
with pytest.raises(RuntimeError): |
| 566 |
Version.from_vcs(Vcs.Any) |
| 567 |
|
| 568 |
|
| 569 |
def test__check_version__pep440() -> None: |
| 570 |
check_version("0.1.0") |
| 571 |
check_version("0.01.0") |
| 572 |
|
| 573 |
check_version("2!0.1.0") |
| 574 |
check_version("23!0.1.0") |
| 575 |
check_version("0.1.0a1") |
| 576 |
check_version("0.1.0b1") |
| 577 |
check_version("0.1.0rc1") |
| 578 |
with pytest.raises(ValueError): |
| 579 |
check_version("0.1.0x1") |
| 580 |
|
| 581 |
check_version("0.1.0.post0") |
| 582 |
check_version("0.1.0.dev0") |
| 583 |
check_version("0.1.0.post0.dev0") |
| 584 |
with pytest.raises(ValueError): |
| 585 |
check_version("0.1.0.other0") |
| 586 |
|
| 587 |
check_version("0.1.0+abc.dirty") |
| 588 |
check_version("0.1.0+abc..dirty") |
| 589 |
with pytest.raises(ValueError): |
| 590 |
check_version("0.1.0+abc_dirty") |
| 591 |
with pytest.raises(ValueError): |
| 592 |
check_version("0.1.0+.abc") |
| 593 |
with pytest.raises(ValueError): |
| 594 |
check_version("0.1.0+abc.") |
| 595 |
|
| 596 |
check_version("2!0.1.0a1.post0.dev0+abc.dirty") |
| 597 |
|
| 598 |
|
| 599 |
def test__check_version__semver() -> None: |
| 600 |
style = Style.SemVer |
| 601 |
|
| 602 |
check_version("0.1.0", style=style) |
| 603 |
check_version("0.1.0-alpha.1", style=style) |
| 604 |
check_version("0.1.0+abc", style=style) |
| 605 |
check_version("0.1.0+a.b.c", style=style) |
| 606 |
check_version("0.1.0-alpha.1.beta.2+abc.dirty", style=style) |
| 607 |
|
| 608 |
with pytest.raises(ValueError): |
| 609 |
check_version("1", style=style) |
| 610 |
with pytest.raises(ValueError): |
| 611 |
check_version("0.1", style=style) |
| 612 |
with pytest.raises(ValueError): |
| 613 |
check_version("0.0.0.1", style=style) |
| 614 |
|
| 615 |
# "-" is a valid identifier. |
| 616 |
Version("0.1.0--").serialize(style=style) |
| 617 |
Version("0.1.0--.-").serialize(style=style) |
| 618 |
|
| 619 |
with pytest.raises(ValueError): |
| 620 |
check_version("0.1.0+abc_dirty", style=style) |
| 621 |
|
| 622 |
# No leading zeroes in numeric segments: |
| 623 |
with pytest.raises(ValueError): |
| 624 |
Version("00.0.0").serialize(style=style) |
| 625 |
with pytest.raises(ValueError): |
| 626 |
Version("0.01.0").serialize(style=style) |
| 627 |
with pytest.raises(ValueError): |
| 628 |
Version("0.1.0-alpha.02").serialize(style=style) |
| 629 |
# But leading zeroes are fine for non-numeric parts: |
| 630 |
Version("0.1.0-alpha.02a").serialize(style=style) |
| 631 |
|
| 632 |
# Identifiers can't be empty: |
| 633 |
with pytest.raises(ValueError): |
| 634 |
Version("0.1.0-.").serialize(style=style) |
| 635 |
with pytest.raises(ValueError): |
| 636 |
Version("0.1.0-a.").serialize(style=style) |
| 637 |
with pytest.raises(ValueError): |
| 638 |
Version("0.1.0-.a").serialize(style=style) |
| 639 |
|
| 640 |
|
| 641 |
def test__check_version__pvp() -> None: |
| 642 |
style = Style.Pvp |
| 643 |
|
| 644 |
check_version("1", style=style) |
| 645 |
check_version("0.1", style=style) |
| 646 |
check_version("0.0.1", style=style) |
| 647 |
check_version("0.0.0.1", style=style) |
| 648 |
check_version("0.1.0-alpha-1", style=style) |
| 649 |
|
| 650 |
with pytest.raises(ValueError): |
| 651 |
check_version("0.1.0-a.1", style=style) |
| 652 |
with pytest.raises(ValueError): |
| 653 |
check_version("0.1.0-abc_dirty", style=style) |
| 654 |
|
| 655 |
|
| 656 |
def test__default_version_pattern() -> None: |
| 657 |
def check_re( |
| 658 |
tag: str, |
| 659 |
base: Optional[str] = None, |
| 660 |
stage: Optional[str] = None, |
| 661 |
revision: Optional[str] = None, |
| 662 |
tagged_metadata: Optional[str] = None, |
| 663 |
epoch: Optional[str] = None, |
| 664 |
) -> None: |
| 665 |
result = re.search(VERSION_SOURCE_PATTERN, tag) |
| 666 |
if result is None: |
| 667 |
if any(x is not None for x in [base, stage, revision]): |
| 668 |
raise ValueError("Pattern did not match, {tag}".format(tag=tag)) |
| 669 |
else: |
| 670 |
assert result.group("base") == base |
| 671 |
assert result.group("stage") == stage |
| 672 |
assert result.group("revision") == revision |
| 673 |
assert result.group("tagged_metadata") == tagged_metadata |
| 674 |
assert result.group("epoch") == epoch |
| 675 |
|
| 676 |
check_re("v0.1.0", "0.1.0") |
| 677 |
check_re("av0.1.0") |
| 678 |
|
| 679 |
check_re("v0.1.0a", "0.1.0", "a") |
| 680 |
check_re("v0.1.0a1", "0.1.0", "a", "1") |
| 681 |
check_re("v0.1.0a1b", None) |
| 682 |
check_re("v0.1.0-1.a", None) |
| 683 |
|
| 684 |
check_re("v0.1.0-alpha.123", "0.1.0", "alpha", "123") |
| 685 |
check_re("v0.1.0-1.alpha", None) |
| 686 |
check_re("v0.1.0-alpha.1.post.4", None) |
| 687 |
|
| 688 |
check_re("v0.1.0rc.4", "0.1.0", "rc", "4") |
| 689 |
check_re("v0.1.0-beta", "0.1.0", "beta") |
| 690 |
|
| 691 |
check_re("v0.1.0a2", "0.1.0", "a", "2") |
| 692 |
check_re("v0.1.0-a-2", "0.1.0", "a", "2") |
| 693 |
check_re("v0.1.0.a.2", "0.1.0", "a", "2") |
| 694 |
check_re("v0.1.0_a_2", "0.1.0", "a", "2") |
| 695 |
|
| 696 |
check_re("v0.1.0rc.4+specifier", "0.1.0", "rc", "4", tagged_metadata="specifier") |
| 697 |
|
| 698 |
check_re("v1", "1") |
| 699 |
check_re("v1b2", "1", "b", "2") |
| 700 |
|
| 701 |
check_re("v1!2", "2", epoch="1") |
| 702 |
|
| 703 |
|
| 704 |
def test__serialize_pep440(): |
| 705 |
assert serialize_pep440("1.2.3") == "1.2.3" |
| 706 |
assert serialize_pep440("1.2.3", epoch=0) == "0!1.2.3" |
| 707 |
assert serialize_pep440("1.2.3", stage="a") == "1.2.3a0" |
| 708 |
assert serialize_pep440("1.2.3", stage="a", revision=4) == "1.2.3a4" |
| 709 |
assert serialize_pep440("1.2.3", post=4) == "1.2.3.post4" |
| 710 |
assert serialize_pep440("1.2.3", dev=4) == "1.2.3.dev4" |
| 711 |
assert serialize_pep440("1.2.3", metadata=[]) == "1.2.3" |
| 712 |
assert serialize_pep440("1.2.3", metadata=["foo"]) == "1.2.3+foo" |
| 713 |
assert serialize_pep440("1.2.3", metadata=["foo", "bar"]) == "1.2.3+foo.bar" |
| 714 |
assert serialize_pep440("1.2.3", metadata=[4]) == "1.2.3+4" |
| 715 |
|
| 716 |
assert ( |
| 717 |
serialize_pep440("1.2.3", epoch=0, stage="a", revision=4, post=5, dev=6, metadata=["foo", "bar"]) |
| 718 |
== "0!1.2.3a4.post5.dev6+foo.bar" |
| 719 |
) |
| 720 |
|
| 721 |
assert serialize_pep440("1.2.3", stage="alpha", revision=4) == "1.2.3a4" |
| 722 |
assert serialize_pep440("1.2.3", stage="ALphA", revision=4) == "1.2.3a4" |
| 723 |
assert serialize_pep440("1.2.3", stage="beta", revision=4) == "1.2.3b4" |
| 724 |
assert serialize_pep440("1.2.3", stage="c", revision=4) == "1.2.3rc4" |
| 725 |
assert serialize_pep440("1.2.3", stage="pre", revision=4) == "1.2.3rc4" |
| 726 |
assert serialize_pep440("1.2.3", stage="preview", revision=4) == "1.2.3rc4" |
| 727 |
|
| 728 |
with pytest.raises(ValueError): |
| 729 |
serialize_pep440("foo") |
| 730 |
|
| 731 |
|
| 732 |
def test__serialize_semver(): |
| 733 |
assert serialize_semver("1.2.3") == "1.2.3" |
| 734 |
assert serialize_semver("1.2.3", pre=["alpha"]) == "1.2.3-alpha" |
| 735 |
assert serialize_semver("1.2.3", pre=["alpha", 4]) == "1.2.3-alpha.4" |
| 736 |
assert serialize_semver("1.2.3", metadata=["foo"]) == "1.2.3+foo" |
| 737 |
assert serialize_semver("1.2.3", metadata=["foo", "bar"]) == "1.2.3+foo.bar" |
| 738 |
assert serialize_semver("1.2.3", metadata=[4]) == "1.2.3+4" |
| 739 |
|
| 740 |
assert serialize_semver("1.2.3", pre=["alpha", 4], metadata=["foo", "bar"]) == "1.2.3-alpha.4+foo.bar" |
| 741 |
|
| 742 |
with pytest.raises(ValueError): |
| 743 |
serialize_semver("foo") |
| 744 |
|
| 745 |
|
| 746 |
def test__serialize_pvp(): |
| 747 |
assert serialize_pvp("1") == "1" |
| 748 |
assert serialize_pvp("1.2") == "1.2" |
| 749 |
assert serialize_pvp("1.2.3") == "1.2.3" |
| 750 |
assert serialize_pvp("1.2.3.4") == "1.2.3.4" |
| 751 |
assert serialize_pvp("1.2.3", metadata=["foo"]) == "1.2.3-foo" |
| 752 |
assert serialize_pvp("1.2.3", metadata=["foo", "bar"]) == "1.2.3-foo-bar" |
| 753 |
assert serialize_pvp("1.2.3", metadata=[4]) == "1.2.3-4" |
| 754 |
|
| 755 |
with pytest.raises(ValueError): |
| 756 |
serialize_pvp("foo") |
| 757 |
|
| 758 |
|
| 759 |
def test__bump_version(): |
| 760 |
# default bump=1 |
| 761 |
assert bump_version("1.2.3") == "1.2.4" |
| 762 |
|
| 763 |
assert bump_version("1.2.3", 0) == "2.0.0" |
| 764 |
assert bump_version("1.2.3", 1) == "1.3.0" |
| 765 |
assert bump_version("1.2.3", 2) == "1.2.4" |
| 766 |
|
| 767 |
assert bump_version("1.2.3", -1) == "1.2.4" |
| 768 |
assert bump_version("1.2.3", -2) == "1.3.0" |
| 769 |
assert bump_version("1.2.3", -3) == "2.0.0" |
| 770 |
|
| 771 |
# expicit bump increment |
| 772 |
assert bump_version("1.2.3", increment=3) == "1.2.6" |
| 773 |
|
| 774 |
assert bump_version("1.2.3", 0, increment=3) == "4.0.0" |
| 775 |
assert bump_version("1.2.3", 1, increment=3) == "1.5.0" |
| 776 |
assert bump_version("1.2.3", 2, increment=3) == "1.2.6" |
| 777 |
|
| 778 |
assert bump_version("1.2.3", -1, increment=3) == "1.2.6" |
| 779 |
assert bump_version("1.2.3", -2, increment=3) == "1.5.0" |
| 780 |
assert bump_version("1.2.3", -3, increment=3) == "4.0.0" |
| 781 |
|
| 782 |
# check if incorrect index raises issues |
| 783 |
with pytest.raises(IndexError): |
| 784 |
bump_version("1.2.3", 3) |
| 785 |
|
| 786 |
with pytest.raises(IndexError): |
| 787 |
bump_version("1.2.3", -4) |
| 788 |
|
| 789 |
with pytest.raises(ValueError): |
| 790 |
bump_version("foo", 0) |