File: test_cli.py

package info (click to toggle)
setuptools-scm 8.2.1-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 588 kB
  • sloc: python: 4,567; sh: 27; makefile: 6
file content (82 lines) | stat: -rw-r--r-- 2,072 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
from __future__ import annotations

import io

from contextlib import redirect_stdout

import pytest

from setuptools_scm._cli import main

from .conftest import DebugMode
from .test_git import wd as wd_fixture  # noqa: F401 (evil fixture reuse)
from .wd_wrapper import WorkDir

PYPROJECT_TOML = "pyproject.toml"
PYPROJECT_SIMPLE = "[tool.setuptools_scm]"
PYPROJECT_ROOT = '[tool.setuptools_scm]\nroot=".."'


def get_output(args: list[str]) -> str:
    with redirect_stdout(io.StringIO()) as out:
        main(args)
    return out.getvalue()


warns_cli_root_override = pytest.warns(
    UserWarning, match="root .. is overridden by the cli arg .*"
)

exits_with_not_found = pytest.raises(SystemExit, match="no version found for")


def test_cli_find_pyproject(
    wd: WorkDir, monkeypatch: pytest.MonkeyPatch, debug_mode: DebugMode
) -> None:
    wd.commit_testfile()
    wd.write(PYPROJECT_TOML, PYPROJECT_SIMPLE)
    monkeypatch.chdir(wd.cwd)
    out = get_output([])
    assert out.startswith("0.1.dev1+")

    with exits_with_not_found:
        get_output(["--root=.."])

    wd.write(PYPROJECT_TOML, PYPROJECT_ROOT)
    with exits_with_not_found:
        print(get_output(["-c", PYPROJECT_TOML]))

    with warns_cli_root_override, exits_with_not_found:
        get_output(["-c", PYPROJECT_TOML, "--root=.."])

    with warns_cli_root_override:
        out = get_output(["-c", PYPROJECT_TOML, "--root=."])
    assert out.startswith("0.1.dev1+")


def test_cli_force_version_files(
    wd: WorkDir, monkeypatch: pytest.MonkeyPatch, debug_mode: DebugMode
) -> None:
    debug_mode.disable()
    wd.commit_testfile()
    wd.write(
        PYPROJECT_TOML,
        """
[project]
name = "test"
[tool.setuptools_scm]
version_file = "ver.py"
""",
    )
    monkeypatch.chdir(wd.cwd)

    version_file = wd.cwd.joinpath("ver.py")
    assert not version_file.exists()

    get_output([])
    assert not version_file.exists()

    output = get_output(["--force-write-version-files"])
    assert version_file.exists()

    assert output[:5] in version_file.read_text("utf-8")