File: test_integration.py

package info (click to toggle)
python-poetry-dynamic-versioning 1.8.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 464 kB
  • sloc: python: 1,474; makefile: 4
file content (321 lines) | stat: -rw-r--r-- 11,897 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
import os
import re
import shlex
import shutil
import subprocess
import tarfile
from pathlib import Path
from typing import Optional, Sequence, Tuple

import dunamai
import pytest
import tomlkit

ROOT = Path(__file__).parent.parent
DIST = ROOT / "dist"
DUMMY = ROOT / "tests" / "project"
DUMMY_DIST = DUMMY / "dist"
DUMMY_PYPROJECT = DUMMY / "pyproject.toml"

DUMMY_PEP621 = ROOT / "tests" / "project-pep621"
DUMMY_PEP621_DIST = DUMMY_PEP621 / "dist"
DUMMY_PEP621_PYPROJECT = DUMMY_PEP621 / "pyproject.toml"

DUMMY_VERSION = "0.0.999"
DEPENDENCY_DYNAMIC_VERSION = "0.0.888"


def run(
    command: str,
    codes: Sequence[int] = (0,),
    where: Optional[Path] = None,
    shell: bool = False,
    env: Optional[dict] = None,
) -> Tuple[int, str]:
    split = shlex.split(command)

    if split[0] == "poetry":
        split[0] = os.environ.get("POETRY", "poetry")

    result = subprocess.run(
        split,
        stdout=subprocess.PIPE,
        stderr=subprocess.STDOUT,
        cwd=str(where) if where is not None else None,
        shell=shell,
        env={**os.environ, **env} if env else None,
    )
    output = result.stdout.decode("utf-8", errors="ignore").strip()
    if codes and result.returncode not in codes:
        raise RuntimeError("The command '{}' returned code {}. Output:\n{}".format(command, result.returncode, output))
    return (result.returncode, output)


def delete(path: Path) -> None:
    if path.is_dir():
        shutil.rmtree(path)
    elif path.is_file():
        path.unlink()


def install_plugin(artifact: str) -> None:
    pipx = os.environ.get("POETRY_DYNAMIC_VERSIONING_TEST_INSTALLATION") == "pipx"

    if pipx:
        run(f'pipx inject poetry "{artifact}"')
    else:
        run(f'poetry self add "{artifact}"')


def uninstall_plugin() -> None:
    pipx = os.environ.get("POETRY_DYNAMIC_VERSIONING_TEST_INSTALLATION") == "pipx"

    if pipx:
        run("pipx uninject poetry poetry-dynamic-versioning", codes=[0, 1])
    else:
        run("poetry self remove poetry-dynamic-versioning", codes=[0, 1])


@pytest.fixture(scope="module", autouse=True)
def before_all():
    uninstall_plugin()
    delete(DIST)
    delete(DUMMY / ".venv")
    run("poetry build", where=ROOT)
    artifact = next(DIST.glob("*.whl"))
    install_plugin(artifact)

    yield

    run(f'git checkout -- "{DUMMY.as_posix()}" "{ROOT.as_posix()}/tests/dependency-*"')
    uninstall_plugin()


@pytest.fixture(autouse=True)
def before_each():
    for project in [DUMMY, DUMMY_PEP621]:
        run(f"git checkout -- {project.as_posix()}")
        delete(project / "dist")
        delete(project / "poetry.lock")
        for file in project.glob("*.whl"):
            delete(file)


def test_plugin_enabled():
    run("poetry build", where=DUMMY)
    artifact = next(DUMMY_DIST.glob("*.whl"))
    assert DUMMY_VERSION not in artifact.name


def test_plugin_disabled():
    data = DUMMY_PYPROJECT.read_bytes().decode("utf-8")
    data = data.replace("enable = true", "enable = false")
    DUMMY_PYPROJECT.write_bytes(data.encode("utf-8"))

    run("poetry build", where=DUMMY)
    artifact = next(DUMMY_DIST.glob("*.whl"))
    assert DUMMY_VERSION in artifact.name


def test_plugin_disabled_without_plugin_section():
    data = DUMMY_PYPROJECT.read_bytes().decode("utf-8")
    data = data.replace("[tool.poetry-dynamic-versioning]", "[tool.poetry-dynamic-versioning-x]")
    DUMMY_PYPROJECT.write_bytes(data.encode("utf-8"))

    run("poetry build", where=DUMMY)
    artifact = next(DUMMY_DIST.glob("*.whl"))
    assert DUMMY_VERSION in artifact.name


def test_plugin_disabled_without_pyproject_file():
    delete(DUMMY_PYPROJECT)
    run("poetry --help", where=DUMMY)


def test_invalid_config_for_vcs():
    data = DUMMY_PYPROJECT.read_bytes().decode("utf-8")
    data = data.replace('vcs = "git"', 'vcs = "invalid"')
    DUMMY_PYPROJECT.write_bytes(data.encode("utf-8"))

    run("poetry build", where=DUMMY, codes=[1])


def test_keep_pyproject_modifications():
    package = "cachy"
    # Using --optional to avoid actually installing the package
    if "USE_PEP621" in os.environ:
        run(f"poetry add --optional main {package}", where=DUMMY)
    else:
        run(f"poetry add --optional {package}", where=DUMMY)
    # Make sure pyproject.toml contains the new package dependency
    data = DUMMY_PYPROJECT.read_bytes().decode("utf-8")
    assert package in data


def test_poetry_run():
    # The original version is restored before the command runs:
    run(f"poetry run grep 'version = \"{DUMMY_VERSION}\"' pyproject.toml", where=DUMMY)
    # Make sure original version number is still in place:
    data = DUMMY_PYPROJECT.read_bytes().decode("utf-8")
    assert f'version = "{DUMMY_VERSION}"' in data


@pytest.mark.skipif("CI" in os.environ, reason="Avoid error: 'Inappropriate ioctl for device'")
def test_poetry_shell():
    # Make sure original version number is still in place afterwards:
    run("poetry shell", where=DUMMY)
    data = DUMMY_PYPROJECT.read_bytes().decode("utf-8")
    assert f'version = "{DUMMY_VERSION}"' in data


def test_plugin_cli_mode_and_substitution():
    run("poetry dynamic-versioning", where=DUMMY)
    # Changes persist after the command is done:
    assert f'version = "{DUMMY_VERSION}"' not in DUMMY_PYPROJECT.read_bytes().decode("utf-8")
    assert '__version__: str = "0.0.0"' not in (DUMMY / "project" / "__init__.py").read_bytes().decode("utf-8")
    assert '__version__ = "0.0.0"' not in (DUMMY / "project" / "__init__.py").read_bytes().decode("utf-8")
    assert "__version_tuple__ = (0, 0, 0)" not in (DUMMY / "project" / "__init__.py").read_text("utf8")
    assert "<0.0.0>" not in (DUMMY / "project" / "__init__.py").read_bytes().decode("utf-8")


def test_standalone_cli_mode_and_substitution():
    run("poetry-dynamic-versioning", where=DUMMY)
    # Changes persist after the command is done:
    assert f'version = "{DUMMY_VERSION}"' not in DUMMY_PYPROJECT.read_bytes().decode("utf-8")
    assert '__version__: str = "0.0.0"' not in (DUMMY / "project" / "__init__.py").read_bytes().decode("utf-8")
    assert '__version__ = "0.0.0"' not in (DUMMY / "project" / "__init__.py").read_bytes().decode("utf-8")
    assert "__version_tuple__ = (0, 0, 0)" not in (DUMMY / "project" / "__init__.py").read_text("utf8")
    assert "<0.0.0>" not in (DUMMY / "project" / "__init__.py").read_bytes().decode("utf-8")


def test_cli_mode_and_substitution_without_enable():
    data = DUMMY_PYPROJECT.read_bytes().decode("utf-8")
    data = data.replace("enable = true", "enable = false")
    DUMMY_PYPROJECT.write_bytes(data.encode("utf-8"))

    run("poetry dynamic-versioning", where=DUMMY)
    # Changes persist after the command is done:
    assert f'version = "{DUMMY_VERSION}"' not in DUMMY_PYPROJECT.read_bytes().decode("utf-8")
    assert '__version__: str = "0.0.0"' not in (DUMMY / "project" / "__init__.py").read_bytes().decode("utf-8")
    assert '__version__ = "0.0.0"' not in (DUMMY / "project" / "__init__.py").read_bytes().decode("utf-8")
    assert "__version_tuple__ = (0, 0, 0)" not in (DUMMY / "project" / "__init__.py").read_text("utf8")
    assert "<0.0.0>" not in (DUMMY / "project" / "__init__.py").read_bytes().decode("utf-8")


def test_cli_mode_plus_build_will_disable_plugin():
    run("poetry dynamic-versioning", where=DUMMY)
    run("poetry build", where=DUMMY)
    artifact = next(DUMMY_DIST.glob("*.tar.gz"))
    with tarfile.open(artifact, "r:gz") as f:
        item = "{}/pyproject.toml".format(artifact.name.replace(".tar.gz", ""))
        content = f.extractfile(item).read()
        parsed = tomlkit.parse(content)
        assert parsed["tool"]["poetry-dynamic-versioning"]["enable"] is False


def test_dependency_versions():
    run("poetry install", where=DUMMY)
    _, out = run("poetry run pip list --format freeze", where=DUMMY)
    assert "dependency-dynamic==" in out
    assert f"dependency-dynamic=={DEPENDENCY_DYNAMIC_VERSION}" not in out
    assert "dependency-static==0.0.777" in out
    assert "dependency-classic==0.0.666" in out


def test_poetry_core_as_build_system():
    project = ROOT / "tests" / "dependency-dynamic"
    dist = project / "dist"
    pyproject = project / "pyproject.toml"

    data = pyproject.read_bytes().decode("utf-8")
    data = re.sub(
        r"requires = .*",
        'requires = ["poetry-core>=1.0.0", "poetry-dynamic-versioning"]',
        data,
    )
    data = re.sub(
        r"build-backend = .*",
        'build-backend = "poetry_dynamic_versioning.backend"',
        data,
    )
    pyproject.write_bytes(data.encode("utf-8"))

    run("pip wheel . --no-build-isolation --wheel-dir dist", where=project)
    artifact = next(dist.glob("*.whl"))
    assert DEPENDENCY_DYNAMIC_VERSION not in artifact.name


def test_bumping_enabled():
    data = DUMMY_PYPROJECT.read_bytes().decode("utf-8")
    data = data.replace('vcs = "git"', "bump = true")
    data = data.replace('style = "semver"', 'style = "pep440"')
    DUMMY_PYPROJECT.write_bytes(data.encode("utf-8"))

    run("poetry build", where=DUMMY)
    artifact = next(DUMMY_DIST.glob("*.whl"))
    assert DUMMY_VERSION not in artifact.name
    assert ".post" not in artifact.name


def test_bypass():
    run("poetry build", where=DUMMY, env={"POETRY_DYNAMIC_VERSIONING_BYPASS": "1.2.3"})
    artifact = next(DUMMY_DIST.glob("*.whl"))
    assert "-1.2.3-" in artifact.name


@pytest.mark.skipif("CI" in os.environ, reason="CI uses Pipx, which doesn't play nice with this 'poetry self'")
def test_plugin_show():
    _, out = run("poetry self show")
    assert "poetry-dynamic-versioning" in out


@pytest.mark.skipif("USE_PEP621" not in os.environ, reason="Requires Poetry with PEP-621 support")
def test_pep621_with_dynamic_version():
    version = dunamai.Version.from_git().serialize()

    run("poetry-dynamic-versioning", where=DUMMY_PEP621)
    pyproject = tomlkit.parse(DUMMY_PEP621_PYPROJECT.read_bytes().decode("utf-8"))
    assert pyproject["project"]["version"] == version
    assert "version" not in pyproject["project"]["dynamic"]
    assert f'__version__ = "{version}"' in (DUMMY_PEP621 / "project_pep621" / "__init__.py").read_bytes().decode(
        "utf-8"
    )


@pytest.mark.skipif("USE_PEP621" not in os.environ, reason="Requires Poetry with PEP-621 support")
def test_pep621_with_dynamic_version_and_cleanup():
    version = dunamai.Version.from_git().serialize()

    contents_before = DUMMY_PEP621_PYPROJECT.read_bytes().decode("utf-8")

    run("poetry build", where=DUMMY_PEP621)
    contents_after = DUMMY_PEP621_PYPROJECT.read_bytes().decode("utf-8")
    assert contents_before == contents_after

    pyproject = tomlkit.parse(contents_after)
    assert "version" not in pyproject["project"]
    assert "version" in pyproject["project"]["dynamic"]
    assert '__version__ = "0.0.0"' in (DUMMY_PEP621 / "project_pep621" / "__init__.py").read_bytes().decode("utf-8")

    artifact = next(DUMMY_PEP621_DIST.glob("*.whl"))
    assert f"-{version}-" in artifact.name


@pytest.mark.skipif("USE_PEP621" not in os.environ, reason="Requires Poetry with PEP-621 support")
def test_pep621_without_dynamic_version():
    pyproject = tomlkit.parse(DUMMY_PEP621_PYPROJECT.read_bytes().decode("utf-8"))
    pyproject["project"]["dynamic"] = []
    DUMMY_PEP621_PYPROJECT.write_bytes(tomlkit.dumps(pyproject).encode("utf-8"))

    run("poetry-dynamic-versioning", codes=[1], where=DUMMY_PEP621)
    pyproject = tomlkit.parse(DUMMY_PEP621_PYPROJECT.read_bytes().decode("utf-8"))
    assert "version" not in pyproject["project"]
    assert '__version__ = "0.0.0"' in (DUMMY_PEP621 / "project_pep621" / "__init__.py").read_bytes().decode("utf-8")


def test__command_interop():
    # Just make sure these don't fail with the plugin installed.
    folders = [ROOT, ROOT / "tests" / "dependency-dynamic"]

    for folder in folders:
        run("poetry list", where=folder)