File: test_version_inference.py

package info (click to toggle)
setuptools-scm 9.2.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,108 kB
  • sloc: python: 7,068; sh: 18; makefile: 7
file content (255 lines) | stat: -rw-r--r-- 8,516 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
from __future__ import annotations

from types import SimpleNamespace
from typing import Any

import pytest

from setuptools_scm._integration.pyproject_reading import PyProjectData
from setuptools_scm._integration.version_inference import VersionInferenceConfig
from setuptools_scm._integration.version_inference import VersionInferenceNoOp
from setuptools_scm._integration.version_inference import VersionInferenceResult
from setuptools_scm._integration.version_inference import VersionInferenceWarning
from setuptools_scm._integration.version_inference import get_version_inference_config

# Common test data
PYPROJECT = SimpleNamespace(
    DEFAULT=PyProjectData.for_testing(
        is_required=True, section_present=True, project_present=True
    ),
    WITHOUT_TOOL_SECTION=PyProjectData.for_testing(
        is_required=True, section_present=False, project_present=True
    ),
    ONLY_REQUIRED=PyProjectData.for_testing(
        is_required=True, section_present=False, project_present=False
    ),
    WITHOUT_PROJECT=PyProjectData.for_testing(
        is_required=True, section_present=True, project_present=False
    ),
)

OVERRIDES = SimpleNamespace(
    NOT_GIVEN=None,
    EMPTY={},
    CALVER={"version_scheme": "calver"},
    UNRELATED={"key": "value"},
)


WARNING_PACKAGE = VersionInferenceWarning(
    message="version of test_package already set",
)
WARNING_NO_PACKAGE = VersionInferenceWarning(
    message="version of None already set",
)

NOOP = VersionInferenceNoOp()


def expect_config(
    *,
    dist_name: str | None = "test_package",
    current_version: str | None,
    pyproject_data: PyProjectData = PYPROJECT.DEFAULT,
    overrides: dict[str, Any] | None = None,
    expected: type[VersionInferenceConfig]
    | VersionInferenceWarning
    | VersionInferenceNoOp,
) -> None:
    """Helper to test get_version_inference_config and assert expected result type."""
    __tracebackhide__ = True
    result = get_version_inference_config(
        dist_name=dist_name,
        current_version=current_version,
        pyproject_data=pyproject_data,
        overrides=overrides,
    )

    expectation: VersionInferenceResult
    if expected == VersionInferenceConfig:
        expectation = VersionInferenceConfig(
            dist_name=dist_name,
            pyproject_data=pyproject_data,
            overrides=overrides,
        )
    else:
        assert isinstance(expected, (VersionInferenceNoOp, VersionInferenceWarning))
        expectation = expected

    assert result == expectation


infer_implied = pytest.mark.parametrize(
    ("overrides", "pyproject_data"),
    [
        pytest.param(
            OVERRIDES.EMPTY, PYPROJECT.DEFAULT, id="empty_overrides_default_pyproject"
        ),
        pytest.param(
            OVERRIDES.EMPTY,
            PYPROJECT.WITHOUT_TOOL_SECTION,
            id="empty_overrides_without_tool_section",
        ),
        pytest.param(
            OVERRIDES.NOT_GIVEN,
            PYPROJECT.DEFAULT,
            id="infer_version_default_pyproject",
        ),
    ],
)


@pytest.mark.parametrize("package_name", ["test_package", None])
@infer_implied
def test_implied_with_version_warns(
    package_name: str | None,
    overrides: dict[str, Any] | None,
    pyproject_data: PyProjectData,
) -> None:
    expect_config(
        dist_name=package_name,
        current_version="1.0.0",
        pyproject_data=pyproject_data,
        overrides=overrides,
        expected=WARNING_PACKAGE if package_name else WARNING_NO_PACKAGE,
    )


@pytest.mark.parametrize("package_name", ["test_package", None])
@infer_implied
def test_implied_without_version_infers(
    package_name: str | None,
    overrides: dict[str, Any] | None,
    pyproject_data: PyProjectData,
) -> None:
    expect_config(
        dist_name=package_name,
        current_version=None,
        pyproject_data=pyproject_data,
        overrides=overrides,
        expected=VersionInferenceConfig,
    )


def test_no_config_no_infer() -> None:
    expect_config(
        current_version=None,
        pyproject_data=PYPROJECT.WITHOUT_TOOL_SECTION,
        overrides=OVERRIDES.NOT_GIVEN,
        expected=NOOP,
    )


class TestVersionInferenceDecision:
    """Test the version inference decision logic."""

    def test_setuptools_scm_required_no_project_section_infer_version(self) -> None:
        """We don't infer without tool section even if required: infer_version path."""
        expect_config(
            current_version=None,
            pyproject_data=PYPROJECT.ONLY_REQUIRED,
            overrides=None,
            expected=NOOP,
        )

    def test_setuptools_scm_required_no_project_section_version_keyword(self) -> None:
        """Test that we DO infer when setuptools-scm is required but no project section and use_scm_version=True."""
        expect_config(
            current_version=None,
            pyproject_data=PYPROJECT.ONLY_REQUIRED,
            overrides=OVERRIDES.EMPTY,
            expected=VersionInferenceConfig,
        )

    def test_setuptools_scm_required_no_project_section_version_keyword_with_config(
        self,
    ) -> None:
        """Test that we DO infer when setuptools-scm is required but no project section and use_scm_version={config}."""
        expect_config(
            current_version=None,
            pyproject_data=PYPROJECT.ONLY_REQUIRED,
            overrides=OVERRIDES.CALVER,
            expected=VersionInferenceConfig,
        )

    def test_tool_section_present(self) -> None:
        """We infer when tool section is present."""
        expect_config(
            current_version=None,
            pyproject_data=PYPROJECT.WITHOUT_PROJECT,
            expected=VersionInferenceConfig,
        )

    def test_simple_extra_with_dynamic_version_infers(self) -> None:
        """We infer when setuptools-scm[simple] is in build-system.requires and version is dynamic."""
        pyproject_data = PyProjectData.for_testing(
            is_required=True,
            section_present=False,
            project_present=True,
            has_dynamic_version=True,
            build_requires=["setuptools-scm[simple]"],
        )
        expect_config(
            current_version=None,
            pyproject_data=pyproject_data,
            expected=VersionInferenceConfig,
        )

    def test_simple_extra_without_dynamic_version_no_infer(self) -> None:
        """We don't infer when setuptools-scm[simple] is present but version is not dynamic."""
        pyproject_data = PyProjectData.for_testing(
            is_required=True,
            section_present=False,
            project_present=True,
            has_dynamic_version=False,
            build_requires=["setuptools-scm[simple]"],
        )
        expect_config(
            current_version=None,
            pyproject_data=pyproject_data,
            expected=NOOP,
        )

    def test_no_simple_extra_with_dynamic_version_no_infer(self) -> None:
        """We don't infer when setuptools-scm (without simple extra) is present even with dynamic version."""
        pyproject_data = PyProjectData.for_testing(
            is_required=True,
            section_present=False,
            project_present=True,
            has_dynamic_version=True,
            build_requires=["setuptools-scm"],
        )
        expect_config(
            current_version=None,
            pyproject_data=pyproject_data,
            expected=NOOP,
        )

    def test_simple_extra_no_project_section_no_infer(self) -> None:
        """We don't infer when setuptools-scm[simple] is present but no project section."""
        pyproject_data = PyProjectData.for_testing(
            is_required=True,
            section_present=False,
            project_present=False,
            build_requires=["setuptools-scm[simple]"],
        )
        expect_config(
            current_version=None,
            pyproject_data=pyproject_data,
            expected=NOOP,
        )

    def test_simple_extra_with_version_warns(self) -> None:
        """We warn when setuptools-scm[simple] is present with dynamic version but version is already set."""
        pyproject_data = PyProjectData.for_testing(
            is_required=True,
            section_present=False,
            project_present=True,
            has_dynamic_version=True,
            build_requires=["setuptools-scm[simple]"],
        )
        expect_config(
            current_version="1.0.0",
            pyproject_data=pyproject_data,
            expected=WARNING_PACKAGE,
        )