File: test_scm.py

package info (click to toggle)
pdm-backend 2.4.4%2Bds1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 1,124 kB
  • sloc: python: 2,807; ansic: 21; makefile: 5; sh: 1
file content (327 lines) | stat: -rw-r--r-- 8,693 bytes parent folder | download
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
from __future__ import annotations

import re
import shutil
import subprocess
import tempfile
from abc import ABC, abstractmethod
from collections.abc import Iterable
from datetime import datetime, timezone
from pathlib import Path
from typing import cast

import pytest

from packaging.version import Version
from pdm.backend.hooks.version.scm import (
    SCMVersion,
    default_version_formatter,
    get_version_from_scm,
)

# Copied from https://semver.org/
# fmt: off
_SEMVER_REGEX = re.compile(
    r"^(?P<major>0|[1-9]\d*)"
    r"\.(?P<minor>0|[1-9]\d*)"
    r"\.(?P<patch>0|[1-9]\d*)"
    r"(?:-(?P<prerelease>(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)"
      r"(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?"
    r"(?:\+(?P<buildmetadata>[0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$"
)
# fmt: on


def increment_patch(version: str) -> str:
    m = _SEMVER_REGEX.match(version)
    assert m is not None, "Version provided doesn't match semver regex"

    result = [m["major"], ".", m["minor"], ".", str(int(m["patch"]) + 1)]
    if "prerelease" in m.groups():
        result.append("-")
        result.append(m["prerelease"])

    if "buildmetadata" in m.groups():
        result.append("+")
        result.append(m["buildmetadata"])

    return "".join(result)


def compare_version(
    version: SCMVersion,
    main_version: str,
    distance: int | None = None,
    dirty: bool = False,
    node: str | None = None,
) -> None:
    assert (version.version, version.distance, version.dirty, version.node) == (
        Version(main_version),
        distance,
        dirty,
        node,
    )


class Scm(ABC):
    """Common interface for different source code management solutions"""

    __slots__ = (
        "_cmd",
        "_cwd",
    )

    def __init__(self, cmd: Path, cwd: Path) -> None:
        """Creates a new Scm

        Args:
            cmd: The base command to use for the scm
            cwd: The working directory to use when running these commands
        """
        self._cmd = cmd
        self._cwd = cwd

        self._init()

    def run(self, *args: str | Path):
        result = subprocess.run(
            [self._cmd, *args],
            capture_output=True,
            encoding="utf-8",
            check=True,
            cwd=self._cwd,
        )
        return result.stdout

    @abstractmethod
    def _init(self):
        """Initializes the SCM system in the provided directory"""
        ...

    @abstractmethod
    def commit(self, commit_message: str, files: list[Path]):
        """Creates a commit

        Args:
            commit_message: The message to store for the commit
            files: The files to include when creating the commit
        """
        ...

    @abstractmethod
    def tag(self, name: str):
        """Create a tag

        Args:
            name: The name for the provided tag
        """
        ...

    @property
    @abstractmethod
    def current_hash(self) -> str: ...


class GitScm(Scm):
    def __init__(self, cmd: Path, cwd: Path) -> None:
        super().__init__(cmd, cwd)
        self.run("config", "commit.gpgsign", "false")

    def _init(self):
        self.run("init")

    def commit(self, commit_message: str, files: list[Path]):
        self.run("add", *files)
        self.run("commit", "-m", commit_message)

    def tag(self, name: str):
        self.run("tag", "-am", "some tag", name)

    @property
    def current_hash(self) -> str:
        return "g" + self.run("rev-parse", "--short", "HEAD").strip()


class HgScm(Scm):
    def _init(self):
        self.run("init")

    def commit(self, commit_message: str, files: list[Path]):
        self.run("add", *files)
        self.run("commit", "-m", commit_message, *files)

    def tag(self, name: str):
        self.run("tag", name)

    @property
    def current_hash(self) -> str:
        return self.run("id", "-i").strip().rstrip("+")


@pytest.fixture
def scm_dir() -> Iterable:
    with tempfile.TemporaryDirectory() as d:
        yield Path(d)


@pytest.fixture
def git(scm_dir: Path) -> GitScm:
    git = shutil.which("git")
    if git is None:
        pytest.skip("Cannot find git in path")

    scm = GitScm(Path(git), scm_dir)

    return scm


@pytest.fixture
def hg(scm_dir: Path) -> HgScm:
    hg = shutil.which("hg")
    if hg is None:
        pytest.skip("Cannot find hg in path")

    scm = HgScm(Path(hg), scm_dir)

    return scm


@pytest.fixture(params=["git", "hg"])
def scm(request: pytest.FixtureRequest, scm_dir: Path) -> Scm:
    scm = cast(Scm, request.getfixturevalue(request.param))

    file_path = scm_dir / "test.txt"
    with open(file_path, "w") as f:
        f.write("a\n")

    scm.commit("Add a", [file_path])

    return scm


def test__get_version_from_scm__returns_tag_if_method_unspecified(
    scm_dir: Path, scm: Scm
):
    expected_tag = "0.2.52"
    scm.tag(expected_tag)

    version = get_version_from_scm(scm_dir)

    assert version is not None
    compare_version(version, expected_tag, node=scm.current_hash)


def test__get_version_from_scm__adds_details_if_project_is_dirty(
    scm_dir: Path, scm: Scm
):
    expected_tag = "0.2.52"
    file_path = scm_dir / "some_file.txt"

    with open(file_path, "w") as f:
        f.write("having fun\n")
    scm.commit("some commit", [file_path])
    scm.tag(expected_tag)
    with open(file_path, "a") as f:
        f.write("having fun 2\n")

    version = get_version_from_scm(scm_dir)

    assert version is not None
    compare_version(version, expected_tag, dirty=True, node=scm.current_hash)


def test__get_version_from_scm__returns_version_if_tag_has_v(scm_dir: Path, scm: Scm):
    expected_tag = "0.2.52"
    scm.tag(f"v{expected_tag}")

    version = get_version_from_scm(scm_dir)

    assert version is not None
    compare_version(version, expected_tag, node=scm.current_hash)


def test__get_version_from_scm__returns_default_if_tag_cannot_be_parsed(
    scm_dir: Path, scm: Scm
):
    scm.tag("some-tag-without-numbers")

    version = get_version_from_scm(scm_dir)

    assert version is not None
    compare_version(version, "0.0", distance=1, node=scm.current_hash)


def test__get_version_from_scm__tag_regex(scm_dir: Path, scm: Scm):
    expected_version = "7.2.9"
    tag_regex = "foo/bar-v(?P<version>.*)"
    scm.tag(f"foo/bar-v{expected_version}")

    version = get_version_from_scm(scm_dir, tag_regex=tag_regex)

    assert version is not None
    compare_version(version, expected_version, node=scm.current_hash)


@pytest.mark.parametrize("index", [0, 1])
def test__get_version_from_scm__selects_by_tag_filter_on_same_commit(
    scm_dir: Path, scm: Scm, index: int
):
    expected_versions = ["4.8.2", "2.4.9"]
    tag_regex = r"tag-\d/(?P<version>.*)"
    for i, version in enumerate(expected_versions):
        scm.tag(f"tag-{i}/v{version}")

    version = get_version_from_scm(
        scm_dir, tag_regex=tag_regex, tag_filter=f"tag-{index}/v*"
    )

    assert version is not None
    compare_version(version, expected_versions[index], node=scm.current_hash)


@pytest.mark.parametrize("index", [0, 1])
def test__get_version_from_scm__selects_by_tag_filter_on_different_commits(
    scm_dir: Path, scm: Scm, index: int
):
    expected_versions = ["4.8.2", "2.4.9"]
    tag_regex = r"tag-\d/(?P<version>.*)"
    for i, version in enumerate(expected_versions):
        file_path = scm_dir / "test_{i}.txt"
        with open(file_path, "w") as f:
            f.write(f"Testing {i}\n")
        scm.commit(f"Add {i}", [file_path])
        scm.tag(f"tag-{i}/v{version}")

    file_path = scm_dir / "test_end.txt"
    with open(file_path, "w") as f:
        f.write("Testing end\n")
    scm.commit("Add end", [file_path])

    version = get_version_from_scm(
        scm_dir, tag_regex=tag_regex, tag_filter=f"tag-{index}/v*"
    )

    num_patches = len(expected_versions) - index
    compare_version(
        version, expected_versions[index], num_patches, node=scm.current_hash
    )


@pytest.mark.parametrize(
    "distance,expected", [(None, "1.0.0"), (1, "1.0.1.dev1+g1234567")]
)
def test_default_version_formatter_distance(distance: int | None, expected: str):
    version = SCMVersion(Version("1.0.0"), distance, False, "g1234567", branch="main")
    assert default_version_formatter(version) == expected


TODAY = datetime.now(timezone.utc).strftime("%Y%m%d")


@pytest.mark.parametrize(
    "distance,expected",
    [(None, f"1.0.0+d{TODAY}"), (1, f"1.0.1.dev1+g1234567.d{TODAY}")],
)
def test_default_version_formatter_dirty(distance: int | None, expected: str):
    version = SCMVersion(Version("1.0.0"), distance, True, "g1234567", branch="main")
    assert default_version_formatter(version) == expected