File: test_algorithm.py

package info (click to toggle)
python-semantic-release 10.3.1-2
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 3,236 kB
  • sloc: python: 36,523; sh: 340; makefile: 156
file content (378 lines) | stat: -rw-r--r-- 10,718 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
from __future__ import annotations

from typing import TYPE_CHECKING
from unittest import mock

import pytest
from git import Commit, Repo, TagReference

from semantic_release.enums import LevelBump
from semantic_release.version.algorithm import (
    _increment_version,
    _traverse_graph_for_commits,
    tags_and_versions,
)
from semantic_release.version.translator import VersionTranslator
from semantic_release.version.version import Version

from tests.fixtures.repos import repo_w_initial_commit

if TYPE_CHECKING:
    from typing import Sequence


@pytest.mark.usefixtures(repo_w_initial_commit.__name__)
def test_traverse_graph_for_commits():
    # Setup fake git graph
    """
    * merge commit 6 (start) [3636363]
    |\
    | * commit 5 [3535353]
    | * commit 4 [3434343]
    |/
    * commit 3 [3333333]
    * commit 2 [3232323]
    * commit 1 [3131313]
    * v1.0.0 [3030303]
    """
    repo = Repo()
    v1_commit = Commit(repo, binsha=b"0" * 20, parents=[])

    class TagReferenceOverride(TagReference):
        commit = v1_commit  # mocking the commit property

    v1_tag = TagReferenceOverride(repo, "refs/tags/v1.0.0", check_path=False)

    trunk = Commit(
        repo,
        binsha=b"3" * 20,
        parents=[
            Commit(
                repo,
                binsha=b"2" * 20,
                parents=[
                    Commit(repo, binsha=b"1" * 20, parents=[v1_commit]),
                ],
            ),
        ],
    )
    start_commit = Commit(
        repo,
        binsha=b"6" * 20,
        parents=[
            trunk,
            Commit(
                repo,
                binsha=b"5" * 20,
                parents=[
                    Commit(repo, binsha=b"4" * 20, parents=[trunk]),
                ],
            ),
        ],
    )

    commit_1 = trunk.parents[0].parents[0]
    commit_2 = trunk.parents[0]
    commit_3 = trunk
    commit_4 = start_commit.parents[1].parents[0]
    commit_5 = start_commit.parents[1]
    commit_6 = start_commit

    expected_commit_order = [
        commit_6.hexsha,
        commit_5.hexsha,
        commit_4.hexsha,
        commit_3.hexsha,
        commit_2.hexsha,
        commit_1.hexsha,
    ]

    # Execute
    with mock.patch.object(
        repo, repo.iter_commits.__name__, return_value=iter([v1_commit])
    ):
        actual_commit_order = [
            commit.hexsha
            for commit in _traverse_graph_for_commits(
                head_commit=start_commit,
                latest_release_tag_str=v1_tag.name,
            )
        ]

    # Verify
    assert expected_commit_order == actual_commit_order


@pytest.mark.parametrize(
    "tags, sorted_tags",
    [
        (
            ["v1.0.0", "v1.1.0", "v1.1.1"],
            ["v1.1.1", "v1.1.0", "v1.0.0"],
        ),
        (
            ["v1.1.0", "v1.0.0", "v1.1.1"],
            ["v1.1.1", "v1.1.0", "v1.0.0"],
        ),
        (
            ["v1.1.1", "v1.1.0", "v1.0.0"],
            ["v1.1.1", "v1.1.0", "v1.0.0"],
        ),
        # Examples from https://semver.org/#spec-item-11 (or inspired, where not all
        # version structures are supported)
        (
            ["v1.0.0", "v2.0.0", "v2.1.1", "v2.1.0"],
            ["v2.1.1", "v2.1.0", "v2.0.0", "v1.0.0"],
        ),
        (
            [
                "v1.0.0-rc.1",
                "v1.0.0-beta.2",
                "v1.0.0-beta.11",
                "v1.0.0-alpha.1",
                "v1.0.0-alpha.beta.1",
                "v1.0.0",
            ],
            [
                "v1.0.0",
                "v1.0.0-rc.1",
                "v1.0.0-beta.11",
                "v1.0.0-beta.2",
                "v1.0.0-alpha.beta.1",
                "v1.0.0-alpha.1",
            ],
        ),
    ],
)
def test_sorted_repo_tags_and_versions(tags: list[str], sorted_tags: list[str]):
    repo = Repo()
    translator = VersionTranslator()
    tagrefs = [repo.tag(tag) for tag in tags]
    actual = [t.name for t, _ in tags_and_versions(tagrefs, translator)]
    assert sorted_tags == actual


@pytest.mark.parametrize(
    "tag_format, invalid_tags, valid_tags",
    [
        (
            "v{version}",
            ("test-v1.1.0", "v1.1.0-test-test"),
            [
                "v1.0.0-rc.1",
                "v1.0.0-beta.2",
                "v1.0.0-beta.11",
                "v1.0.0-alpha.1",
                "v1.0.0-alpha.beta.1",
                "v1.0.0",
            ],
        ),
        (
            "v{version}",
            ("0.3", "0.4"),
            [
                "v1.0.0-rc.1",
                "v1.0.0-beta.2",
                "v1.0.0-beta.11",
                "v1.0.0-alpha.1",
                "v1.0.0-alpha.beta.1",
                "v1.0.0",
            ],
        ),
        (
            r"(\w+--)?v{version}",
            ("v1.1.0-test-test", "test_v1.1.0"),
            [
                "v1.0.0-rc.1",
                "test--v1.1.0",
                "v1.0.0-beta.2",
                "v1.0.0-beta.11",
                "v1.0.0-alpha.1",
                "v1.0.0-alpha.beta.1",
                "v1.0.0",
            ],
        ),
        (
            r"(?P<type>feature|fix)/v{version}--(?P<env>dev|stg|prod)",
            ("v1.1.0--test", "test_v1.1.0", "docs/v1.2.0--dev"),
            [
                "feature/v1.0.0-rc.1--dev",
                "fix/v1.1.0--stg",
                "feature/v1.0.0-beta.2--stg",
                "fix/v1.0.0-beta.11--dev",
                "fix/v1.0.0-alpha.1--dev",
                "feature/v1.0.0-alpha.beta.1--dev",
                "feature/v1.0.0--prod",
            ],
        ),
    ],
)
def test_tags_and_versions_ignores_invalid_tags_as_versions(
    tag_format: str,
    invalid_tags: Sequence[str],
    valid_tags: Sequence[str],
):
    repo = Repo()
    translator = VersionTranslator(tag_format=tag_format)
    tagrefs = [repo.tag(tag) for tag in (*valid_tags, *invalid_tags)]
    actual = [t.name for t, _ in tags_and_versions(tagrefs, translator)]
    assert set(valid_tags) == set(actual)


@pytest.mark.parametrize(
    str.join(
        ", ",
        [
            "latest_version",
            "latest_full_version",
            "level_bump",
            "prerelease",
            "prerelease_token",
            "expected_version",
        ],
    ),
    [
        # NOTE: level_bump != LevelBump.NO_RELEASE, we return early in the
        # algorithm to discount this case
        # NOTE: you can only perform a PRERELEASE_REVISION bump on a previously
        # prerelease version and if you are requesting a prerelease
        (
            "1.0.1-rc.1",
            "1.0.0",
            LevelBump.PRERELEASE_REVISION,
            True,
            "rc",
            "1.0.1-rc.2",
        ),
        *[
            (
                "1.0.0",
                "1.0.0",
                bump_level,
                prerelease,
                "rc",
                expected_version,
            )
            for bump_level, prerelease, expected_version in [
                (LevelBump.PATCH, False, "1.0.1"),
                (LevelBump.PATCH, True, "1.0.1-rc.1"),
                (LevelBump.MINOR, False, "1.1.0"),
                (LevelBump.MINOR, True, "1.1.0-rc.1"),
                (LevelBump.MAJOR, False, "2.0.0"),
                (LevelBump.MAJOR, True, "2.0.0-rc.1"),
            ]
        ],
        (
            "1.2.4-rc.1",
            "1.2.3",
            LevelBump.PRERELEASE_REVISION,
            True,
            "rc",
            "1.2.4-rc.2",
        ),
        *[
            (
                "1.2.4-rc.1",
                "1.2.3",
                bump_level,
                prerelease,
                "rc",
                expected_version,
            )
            for bump_level, prerelease, expected_version in [
                (LevelBump.PATCH, False, "1.2.4"),
                (LevelBump.PATCH, True, "1.2.4-rc.2"),
                (LevelBump.MINOR, False, "1.3.0"),
                (LevelBump.MINOR, True, "1.3.0-rc.1"),
                (LevelBump.MAJOR, False, "2.0.0"),
                (LevelBump.MAJOR, True, "2.0.0-rc.1"),
            ]
        ],
        (
            "2.0.0-rc.1",
            "1.19.3",
            LevelBump.PRERELEASE_REVISION,
            True,
            "rc",
            "2.0.0-rc.2",
        ),
        *[
            (
                "2.0.0-rc.1",
                "1.22.0",
                bump_level,
                prerelease,
                "rc",
                expected_version,
            )
            for bump_level, prerelease, expected_version in [
                (LevelBump.PATCH, False, "2.0.0"),
                (LevelBump.PATCH, True, "2.0.0-rc.2"),
                (LevelBump.MINOR, False, "2.0.0"),
                (LevelBump.MINOR, True, "2.0.0-rc.2"),
                (LevelBump.MAJOR, False, "2.0.0"),
                (LevelBump.MAJOR, True, "2.0.0-rc.2"),
            ]
        ],
    ],
)
def test_increment_version_no_major_on_zero(
    latest_version: str,
    latest_full_version: str,
    level_bump: LevelBump,
    prerelease: bool,
    prerelease_token: str,
    expected_version: str,
):
    actual = _increment_version(
        latest_version=Version.parse(latest_version),
        latest_full_version=Version.parse(latest_full_version),
        level_bump=level_bump,
        prerelease=prerelease,
        prerelease_token=prerelease_token,
        major_on_zero=False,
        allow_zero_version=True,
    )
    assert expected_version == str(actual)


@pytest.mark.parametrize(
    "latest_version, latest_full_version, level_bump, prerelease, prerelease_token",
    [
        # NOTE: level_bump != LevelBump.NO_RELEASE, we return early in the
        # algorithm to discount this case
        # NOTE: you can only perform a PRERELEASE_REVISION bump on a previously
        # prerelease version and if you are requesting a prerelease
        (
            "1.0.0",
            "1.0.0",
            LevelBump.PRERELEASE_REVISION,
            False,
            "rc",
        ),
        (
            "1.0.0",
            "1.0.0",
            LevelBump.PRERELEASE_REVISION,
            True,
            "rc",
        ),
    ],
)
def test_increment_version_invalid_operation(
    latest_version: str,
    latest_full_version: str,
    level_bump: LevelBump,
    prerelease: bool,
    prerelease_token: str,
):
    with pytest.raises(ValueError):
        _increment_version(
            latest_version=Version.parse(latest_version),
            latest_full_version=Version.parse(latest_full_version),
            level_bump=level_bump,
            prerelease=prerelease,
            prerelease_token=prerelease_token,
            major_on_zero=False,
            allow_zero_version=True,
        )