1
1
from __future__ import annotations
2
2
3
+ from types import SimpleNamespace
3
4
from typing import Any
4
5
5
- import pytest
6
-
7
6
from setuptools_scm ._integration .pyproject_reading import PyProjectData
8
7
from setuptools_scm ._integration .version_inference import VersionInferenceConfig
9
8
from setuptools_scm ._integration .version_inference import VersionInferenceNoOp
9
+ from setuptools_scm ._integration .version_inference import VersionInferenceResult
10
10
from setuptools_scm ._integration .version_inference import VersionInferenceWarning
11
11
from setuptools_scm ._integration .version_inference import get_version_inference_config
12
12
15
15
is_required = True , section_present = True , project_present = True
16
16
)
17
17
18
+ PYPROJECT_WITHOUT_TOOL_SECTION = PyProjectData .for_testing (
19
+ is_required = True , section_present = False , project_present = True
20
+ )
21
+
22
+ PYPROJECT_ONLY_REQUIRED = PyProjectData .for_testing (
23
+ is_required = True , section_present = False , project_present = False
24
+ )
25
+
26
+ OVERRIDES = SimpleNamespace (
27
+ EMPTY = {},
28
+ CALVER = {"version_scheme" : "calver" },
29
+ UNRELATED = {"key" : "value" },
30
+ INFER_VERSION = None ,
31
+ )
32
+
33
+
34
+ WARNING_PACKAGE = VersionInferenceWarning (
35
+ message = "version of test_package already set" ,
36
+ )
37
+ WARNING_NO_PACKAGE = VersionInferenceWarning (
38
+ message = "version of None already set" ,
39
+ )
40
+
41
+ NOOP = VersionInferenceNoOp ()
42
+
18
43
19
44
def expect_config (
20
45
* ,
21
46
dist_name : str | None = "test_package" ,
22
47
current_version : str | None ,
23
48
pyproject_data : PyProjectData = DEFAULT_PYPROJECT_DATA ,
24
49
overrides : dict [str , Any ] | None = None ,
25
- expected_type : type = VersionInferenceConfig ,
26
- expected_message : str | None = None ,
50
+ expected : type [VersionInferenceConfig ]
51
+ | VersionInferenceWarning
52
+ | VersionInferenceNoOp ,
27
53
) -> None :
28
54
"""Helper to test get_version_inference_config and assert expected result type."""
29
55
__tracebackhide__ = True
@@ -34,16 +60,18 @@ def expect_config(
34
60
overrides = overrides ,
35
61
)
36
62
37
- if not isinstance (result , expected_type ):
38
- pytest .fail (f"{ type (result ).__name__ } != { expected_type .__name__ } " )
63
+ expectation : VersionInferenceResult
64
+ if expected == VersionInferenceConfig :
65
+ expectation = VersionInferenceConfig (
66
+ dist_name = dist_name ,
67
+ pyproject_data = pyproject_data ,
68
+ overrides = overrides ,
69
+ )
70
+ else :
71
+ assert isinstance (expected , (VersionInferenceNoOp , VersionInferenceWarning ))
72
+ expectation = expected
39
73
40
- if expected_type == VersionInferenceWarning and expected_message :
41
- assert isinstance (result , VersionInferenceWarning )
42
- assert expected_message in result .message
43
- elif expected_type == VersionInferenceConfig :
44
- assert isinstance (result , VersionInferenceConfig )
45
- assert result .dist_name == dist_name
46
- assert result .overrides == overrides
74
+ assert result == expectation
47
75
48
76
49
77
class TestVersionInferenceDecision :
@@ -53,93 +81,85 @@ def test_missing_version_with_overrides_triggers(self) -> None:
53
81
"""Test that version_keyword context with overrides infers when no existing version."""
54
82
expect_config (
55
83
current_version = None , # version_keyword passes None when version was set by infer
56
- overrides = {"key" : "value" },
84
+ overrides = OVERRIDES .UNRELATED ,
85
+ expected = VersionInferenceConfig ,
57
86
)
58
87
59
88
def test_overrides_on_existing_version_warns (self ) -> None :
60
89
"""note: version_keyword opts out of inference if
61
90
version is set by something else or overrides are empty"""
62
91
expect_config (
63
92
current_version = "1.0.0" , # version set by something else (setup.cfg, etc.)
64
- overrides = {"key" : "value" },
65
- expected_type = VersionInferenceWarning ,
66
- expected_message = "version of test_package already set" ,
93
+ overrides = OVERRIDES .UNRELATED ,
94
+ expected = WARNING_PACKAGE ,
67
95
)
68
96
69
97
def test_version_already_set_no_overrides (self ) -> None :
70
98
"""infer_version call with existing version warns when inference is implied."""
71
99
expect_config (
72
100
current_version = "1.0.0" ,
73
101
overrides = None ,
74
- expected_type = VersionInferenceWarning ,
75
- expected_message = "version of test_package already set" ,
102
+ expected = WARNING_PACKAGE ,
76
103
)
77
104
78
105
def test_version_keyword_with_empty_overrides (self ) -> None :
79
106
"""Test that version_keyword context with empty overrides infers when no existing version."""
80
107
expect_config (
81
108
current_version = None , # version_keyword handles early exit, so this is what we see
82
- overrides = {},
109
+ overrides = OVERRIDES .EMPTY ,
110
+ expected = VersionInferenceConfig ,
83
111
)
84
112
85
113
def test_version_keyword_empty_overrides_existing_version (self ) -> None :
86
114
"""Test that version_keyword context with empty overrides and existing version errors."""
87
115
expect_config (
88
116
current_version = "1.0.0" , # version set by something else (setup.cfg, etc.)
89
- overrides = {},
90
- expected_type = VersionInferenceWarning ,
91
- expected_message = "version of test_package already set" ,
117
+ overrides = OVERRIDES .EMPTY ,
118
+ expected = WARNING_PACKAGE ,
92
119
)
93
120
94
121
def test_version_already_set_by_something_else (self ) -> None :
95
122
"""infer_version call with existing version warns when inference is implied."""
96
123
expect_config (
97
124
current_version = "1.0.0" ,
98
125
overrides = None ,
99
- expected_type = VersionInferenceWarning ,
100
- expected_message = "version of test_package already set" ,
126
+ expected = WARNING_PACKAGE ,
101
127
)
102
128
103
129
def test_no_setuptools_scm_config_infer_version (self ) -> None :
104
130
"""Test that we don't infer when setuptools-scm is not configured and infer_version called."""
105
131
expect_config (
106
132
current_version = None ,
107
- pyproject_data = PyProjectData .for_testing (
108
- is_required = False , section_present = False , project_present = True
109
- ),
133
+ pyproject_data = PYPROJECT_WITHOUT_TOOL_SECTION ,
110
134
overrides = None ,
111
- expected_type = VersionInferenceNoOp ,
135
+ expected = NOOP ,
112
136
)
113
137
114
138
def test_no_setuptools_scm_config_version_keyword (self ) -> None :
115
139
"""We infer when setuptools-scm is not configured but use_scm_version=True."""
116
140
expect_config (
117
141
current_version = None ,
118
- pyproject_data = PyProjectData .for_testing (
119
- is_required = False , section_present = False , project_present = True
120
- ),
121
- overrides = {},
142
+ pyproject_data = PYPROJECT_WITHOUT_TOOL_SECTION ,
143
+ overrides = OVERRIDES .EMPTY ,
144
+ expected = VersionInferenceConfig ,
122
145
)
123
146
124
147
def test_setuptools_scm_required_no_project_section_infer_version (self ) -> None :
125
148
"""We don't infer without tool section even if required: infer_version path."""
126
149
expect_config (
127
150
current_version = None ,
128
- pyproject_data = PyProjectData .for_testing (
129
- is_required = True , section_present = False , project_present = False
130
- ),
151
+ pyproject_data = PYPROJECT_ONLY_REQUIRED ,
131
152
overrides = None ,
132
- expected_type = VersionInferenceNoOp ,
153
+ expected = NOOP ,
133
154
)
134
155
135
156
def test_setuptools_scm_required_no_project_section_version_keyword (self ) -> None :
136
157
"""Test that we DO infer when setuptools-scm is required but no project section and use_scm_version=True."""
137
158
expect_config (
138
159
current_version = None ,
139
- pyproject_data = PyProjectData .for_testing (
140
- is_required = True , section_present = False , project_present = False
141
- ),
142
- overrides = {},
160
+ pyproject_data = PYPROJECT_ONLY_REQUIRED ,
161
+ overrides = OVERRIDES .EMPTY ,
162
+ expected = VersionInferenceConfig ,
143
163
)
144
164
145
165
def test_setuptools_scm_required_no_project_section_version_keyword_with_config (
@@ -148,20 +168,17 @@ def test_setuptools_scm_required_no_project_section_version_keyword_with_config(
148
168
"""Test that we DO infer when setuptools-scm is required but no project section and use_scm_version={config}."""
149
169
expect_config (
150
170
current_version = None ,
151
- pyproject_data = PyProjectData .for_testing (
152
- is_required = True , section_present = False , project_present = False
153
- ),
154
- overrides = {"version_scheme" : "calver" },
171
+ pyproject_data = PYPROJECT_ONLY_REQUIRED ,
172
+ overrides = OVERRIDES .CALVER ,
173
+ expected = VersionInferenceConfig ,
155
174
)
156
175
157
176
def test_setuptools_scm_required_with_project_section (self ) -> None :
158
177
"""We only infer when tool section present, regardless of required/project presence."""
159
178
expect_config (
160
179
current_version = None ,
161
- pyproject_data = PyProjectData .for_testing (
162
- is_required = True , section_present = False , project_present = True
163
- ),
164
- expected_type = VersionInferenceNoOp ,
180
+ pyproject_data = PYPROJECT_WITHOUT_TOOL_SECTION ,
181
+ expected = NOOP ,
165
182
)
166
183
167
184
def test_tool_section_present (self ) -> None :
@@ -171,19 +188,22 @@ def test_tool_section_present(self) -> None:
171
188
pyproject_data = PyProjectData .for_testing (
172
189
is_required = False , section_present = True , project_present = False
173
190
),
191
+ expected = VersionInferenceConfig ,
174
192
)
175
193
176
194
def test_both_required_and_tool_section (self ) -> None :
177
195
"""Test that we infer when both required and tool section are present."""
178
196
expect_config (
179
197
current_version = None ,
198
+ expected = VersionInferenceConfig ,
180
199
)
181
200
182
201
def test_none_dist_name (self ) -> None :
183
202
"""Test that we handle None dist_name correctly."""
184
203
expect_config (
185
204
dist_name = None ,
186
205
current_version = None ,
206
+ expected = VersionInferenceConfig ,
187
207
)
188
208
189
209
def test_version_already_set_none_dist_name (self ) -> None :
@@ -192,6 +212,5 @@ def test_version_already_set_none_dist_name(self) -> None:
192
212
dist_name = None ,
193
213
current_version = "1.0.0" ,
194
214
overrides = None ,
195
- expected_type = VersionInferenceWarning ,
196
- expected_message = "version of None already set" ,
215
+ expected = WARNING_NO_PACKAGE ,
197
216
)
0 commit comments