-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_generate_switcher.py
More file actions
389 lines (296 loc) · 14.8 KB
/
test_generate_switcher.py
File metadata and controls
389 lines (296 loc) · 14.8 KB
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
# SPDX-FileCopyrightText: 2025 Autodesk, Inc.
# SPDX-License-Identifier: Apache-2.0
"""Tests for scripts/generate_switcher.py"""
import json
import os
import sys
from unittest.mock import patch
import pytest
# Ensure the scripts directory is importable, but avoid leaking sys.path changes
_scripts_dir = os.path.join(os.path.dirname(__file__), '..', '..', 'scripts')
_original_sys_path = list(sys.path)
try:
sys.path.insert(0, _scripts_dir)
from generate_switcher import (
parse_version_tags,
sort_versions,
generate_switcher_json,
get_version_from_json,
_validate_version_progression,
)
finally:
sys.path[:] = _original_sys_path
MOCK_VERSION_JSON = 'generate_switcher.get_version_from_json'
# ---------------------------------------------------------------------------
# parse_version_tags
# ---------------------------------------------------------------------------
@pytest.mark.core
@pytest.mark.scripts
@pytest.mark.generate_switcher
class TestParseVersionTags:
"""Tests for tag filtering against the vX.Y.Z version tag pattern."""
def test_accepts_v_prefixed_tags(self):
tags = ['v1.0.0', 'v2.3.4', 'v27.0.0']
assert parse_version_tags(tags) == tags
def test_rejects_tags_without_v_prefix(self):
tags = ['1.0.0', '2.3.4', '27.0.0']
assert parse_version_tags(tags) == []
def test_filters_tags_without_v_prefix(self):
tags = ['v1.0.0', '2.0.0', 'v3.1.2']
assert parse_version_tags(tags) == ['v1.0.0', 'v3.1.2']
def test_rejects_prerelease_tags(self):
tags = ['v1.0.0rc1', 'v1.0.0a1', 'v1.0.0b2']
assert parse_version_tags(tags) == []
def test_rejects_post_release_tags(self):
assert parse_version_tags(['v1.0.0.post1']) == []
def test_rejects_dev_tags(self):
assert parse_version_tags(['v1.0.0.dev3']) == []
def test_rejects_local_tags(self):
assert parse_version_tags(['v1.0.0+local']) == []
def test_rejects_non_version_tags(self):
tags = ['release-1', 'latest', 'foo', 'docs-v2']
assert parse_version_tags(tags) == []
def test_filters_mixed_valid_and_invalid(self):
tags = ['v1.0.0', 'v2.0.0rc1', 'latest', '3.0.0', 'v4.0.0.dev1']
assert parse_version_tags(tags) == ['v1.0.0']
def test_empty_input(self):
assert parse_version_tags([]) == []
# ---------------------------------------------------------------------------
# sort_versions
# ---------------------------------------------------------------------------
@pytest.mark.core
@pytest.mark.scripts
@pytest.mark.generate_switcher
class TestSortVersions:
"""Tests for descending version sort."""
def test_descending_order(self):
tags = ['v1.0.0', 'v3.0.0', 'v2.0.0']
assert sort_versions(tags) == ['v3.0.0', 'v2.0.0', 'v1.0.0']
def test_patch_ordering(self):
tags = ['v1.0.2', 'v1.0.0', 'v1.0.1']
assert sort_versions(tags) == ['v1.0.2', 'v1.0.1', 'v1.0.0']
def test_many_versions_ordering(self):
tags = ['v1.0.0', 'v2.0.0', 'v3.0.0']
assert sort_versions(tags) == ['v3.0.0', 'v2.0.0', 'v1.0.0']
def test_single_tag(self):
assert sort_versions(['v1.0.0']) == ['v1.0.0']
# ---------------------------------------------------------------------------
# generate_switcher_json — basic structure (no include_current)
# ---------------------------------------------------------------------------
@pytest.mark.core
@pytest.mark.scripts
@pytest.mark.generate_switcher
class TestGenerateSwitcherJsonStructure:
"""Tests for output format and latest-flag logic."""
def test_empty_tags_returns_empty(self):
assert generate_switcher_json([]) == []
@patch(MOCK_VERSION_JSON, return_value='v1.0.0')
def test_single_tag_is_latest(self, _):
result = generate_switcher_json(['v1.0.0'])
assert len(result) == 1
assert result[0]['version'] == 'v1.0.0'
assert result[0]['is_latest'] is True
assert '(latest)' in result[0]['name']
@patch(MOCK_VERSION_JSON, return_value='v3.0.0')
def test_latest_flag_only_on_first(self, _):
result = generate_switcher_json(['v1.0.0', 'v2.0.0', 'v3.0.0'])
assert result[0]['is_latest'] is True
assert result[0]['version'] == 'v3.0.0'
for entry in result[1:]:
assert entry['is_latest'] is False
@patch(MOCK_VERSION_JSON, return_value='v1.0.0')
def test_url_uses_tag_name(self, _):
result = generate_switcher_json(['v1.0.0'])
assert result[0]['url'] == '../v1.0.0/'
@patch(MOCK_VERSION_JSON, return_value='v1.0.0')
def test_url_preserves_v_prefix(self, _):
result = generate_switcher_json(['v1.0.0'])
assert result[0]['url'] == '../v1.0.0/'
@patch(MOCK_VERSION_JSON, return_value='v2.0.0')
def test_all_entries_have_required_keys(self, _):
result = generate_switcher_json(['v1.0.0', 'v2.0.0'])
for entry in result:
assert 'version' in entry
assert 'name' in entry
assert 'url' in entry
assert 'is_latest' in entry
# ---------------------------------------------------------------------------
# generate_switcher_json — include_current with version.json
# ---------------------------------------------------------------------------
@pytest.mark.core
@pytest.mark.scripts
@pytest.mark.generate_switcher
class TestIncludeCurrent:
"""Tests for the include_current flag and version.json integration."""
@patch(MOCK_VERSION_JSON, return_value='v2.0.1')
def test_newer_version_added_as_latest(self, _):
result = generate_switcher_json(['v2.0.0', 'v1.0.0'], include_current=True)
assert result[0]['version'] == 'v2.0.1'
assert result[0]['is_latest'] is True
assert result[1]['is_latest'] is False
@patch(MOCK_VERSION_JSON, return_value='v2.0.0')
def test_same_version_not_added(self, _):
result = generate_switcher_json(['v2.0.0', 'v1.0.0'], include_current=True)
assert result[0]['version'] == 'v2.0.0'
assert len(result) == 2
@patch(MOCK_VERSION_JSON, return_value='v1.0.0')
def test_older_version_not_added(self, _):
result = generate_switcher_json(['v2.0.0', 'v1.0.0'], include_current=True)
assert result[0]['version'] == 'v2.0.0'
assert len(result) == 2
@patch(MOCK_VERSION_JSON, return_value=None)
def test_none_version_json_no_effect(self, _):
result = generate_switcher_json(['v2.0.0'], include_current=True)
assert len(result) == 1
assert result[0]['is_latest'] is True
@patch(MOCK_VERSION_JSON, return_value='v2.0.1')
def test_newer_version_not_added_without_include_current(self, _):
"""version.json is read and validated, but not added to output."""
result = generate_switcher_json(['v2.0.0', 'v1.0.0'], include_current=False)
assert len(result) == 2
assert result[0]['version'] == 'v2.0.0'
assert result[0]['is_latest'] is True
@patch(MOCK_VERSION_JSON, return_value='v2.0.0')
def test_same_version_without_include_current(self, _):
"""Same version in version.json is harmless regardless of flag."""
result = generate_switcher_json(['v2.0.0'], include_current=False)
assert len(result) == 1
# ---------------------------------------------------------------------------
# Version progression rules
# ---------------------------------------------------------------------------
@pytest.mark.core
@pytest.mark.scripts
@pytest.mark.generate_switcher
class TestVersionProgression:
"""Tests for version skip / progression validation."""
@patch(MOCK_VERSION_JSON, return_value='v1.0.1')
def test_valid_patch_bump(self, _):
result = generate_switcher_json(['v1.0.0'], include_current=True)
assert result[0]['version'] == 'v1.0.1'
@patch(MOCK_VERSION_JSON, return_value='v1.1.0')
def test_valid_minor_bump(self, _):
result = generate_switcher_json(['v1.0.0'], include_current=True)
assert result[0]['version'] == 'v1.1.0'
@patch(MOCK_VERSION_JSON, return_value='v2.0.0')
def test_valid_major_bump(self, _):
result = generate_switcher_json(['v1.0.0'], include_current=True)
assert result[0]['version'] == 'v2.0.0'
@patch(MOCK_VERSION_JSON, return_value='v1.0.3')
def test_skipped_patch_raises(self, _):
with pytest.raises(ValueError, match="Cannot skip versions"):
generate_switcher_json(['v1.0.0'], include_current=True)
@patch(MOCK_VERSION_JSON, return_value='v1.3.0')
def test_skipped_minor_raises(self, _):
with pytest.raises(ValueError, match="Cannot skip versions"):
generate_switcher_json(['v1.0.0'], include_current=True)
@patch(MOCK_VERSION_JSON, return_value='v3.0.0')
def test_skipped_major_raises(self, _):
with pytest.raises(ValueError, match="Cannot skip versions"):
generate_switcher_json(['v1.0.0'], include_current=True)
@patch(MOCK_VERSION_JSON, return_value='v1.1.1')
def test_minor_bump_with_nonzero_patch_raises(self, _):
with pytest.raises(ValueError, match="patch must be 0"):
generate_switcher_json(['v1.0.0'], include_current=True)
@patch(MOCK_VERSION_JSON, return_value='v2.1.0')
def test_major_bump_with_nonzero_minor_raises(self, _):
with pytest.raises(ValueError, match="minor and patch must be 0"):
generate_switcher_json(['v1.0.0'], include_current=True)
@patch(MOCK_VERSION_JSON, return_value='v2.0.1')
def test_major_bump_with_nonzero_patch_raises(self, _):
with pytest.raises(ValueError, match="minor and patch must be 0"):
generate_switcher_json(['v1.0.0'], include_current=True)
@pytest.mark.core
@pytest.mark.scripts
@pytest.mark.generate_switcher
class TestValidationRunsWithoutIncludeCurrent:
"""Validation fires on the default (CI) path even without include_current."""
@patch(MOCK_VERSION_JSON, return_value='v1.0.3')
def test_skipped_patch_raises_without_flag(self, _):
with pytest.raises(ValueError, match="Cannot skip versions"):
generate_switcher_json(['v1.0.0'], include_current=False)
@patch(MOCK_VERSION_JSON, return_value='v1.3.0')
def test_skipped_minor_raises_without_flag(self, _):
with pytest.raises(ValueError, match="Cannot skip versions"):
generate_switcher_json(['v1.0.0'], include_current=False)
@patch(MOCK_VERSION_JSON, return_value='v3.0.0')
def test_skipped_major_raises_without_flag(self, _):
with pytest.raises(ValueError, match="Cannot skip versions"):
generate_switcher_json(['v1.0.0'], include_current=False)
@patch(MOCK_VERSION_JSON, return_value='v1.1.1')
def test_minor_bump_nonzero_patch_raises_without_flag(self, _):
with pytest.raises(ValueError, match="patch must be 0"):
generate_switcher_json(['v1.0.0'], include_current=False)
@patch(MOCK_VERSION_JSON, return_value='v2.1.0')
def test_major_bump_nonzero_minor_raises_without_flag(self, _):
with pytest.raises(ValueError, match="minor and patch must be 0"):
generate_switcher_json(['v1.0.0'], include_current=False)
@patch(MOCK_VERSION_JSON, return_value='v1.0.1')
def test_valid_bump_without_flag_does_not_add_to_output(self, _):
"""Valid progression is fine — version just isn't added to output."""
result = generate_switcher_json(['v1.0.0'], include_current=False)
assert len(result) == 1
assert result[0]['version'] == 'v1.0.0'
@pytest.mark.core
@pytest.mark.scripts
@pytest.mark.generate_switcher
class TestValidateVersionProgressionDirect:
"""Direct tests for the _validate_version_progression helper."""
def test_valid_patch_bump(self):
_validate_version_progression('v1.0.1', 'v1.0.0')
def test_valid_minor_bump(self):
_validate_version_progression('v1.1.0', 'v1.0.0')
def test_valid_major_bump(self):
_validate_version_progression('v2.0.0', 'v1.0.0')
def test_older_version_is_noop(self):
_validate_version_progression('v1.0.0', 'v2.0.0')
def test_same_version_is_noop(self):
_validate_version_progression('v1.0.0', 'v1.0.0')
def test_skipped_patch_raises(self):
with pytest.raises(ValueError, match="Cannot skip versions"):
_validate_version_progression('v1.0.3', 'v1.0.0')
def test_skipped_minor_raises(self):
with pytest.raises(ValueError, match="Cannot skip versions"):
_validate_version_progression('v1.3.0', 'v1.0.0')
def test_skipped_major_raises(self):
with pytest.raises(ValueError, match="Cannot skip versions"):
_validate_version_progression('v3.0.0', 'v1.0.0')
# ---------------------------------------------------------------------------
# get_version_from_json
# ---------------------------------------------------------------------------
@pytest.mark.core
@pytest.mark.scripts
@pytest.mark.generate_switcher
class TestGetVersionFromJson:
"""Tests for version.json reading and validation."""
def test_valid_version_json(self, tmp_path):
version_file = tmp_path / 'version.json'
version_file.write_text(json.dumps({"major": 1, "minor": 2, "patch": 3}))
with patch('generate_switcher.VERSION_JSON', str(version_file)):
assert get_version_from_json() == 'v1.2.3'
def test_missing_field_raises(self, tmp_path):
version_file = tmp_path / 'version.json'
version_file.write_text(json.dumps({"major": 1, "minor": 2}))
with patch('generate_switcher.VERSION_JSON', str(version_file)):
with pytest.raises(ValueError, match="missing required field"):
get_version_from_json()
def test_non_numeric_values_raises(self, tmp_path):
version_file = tmp_path / 'version.json'
version_file.write_text(json.dumps({"major": "abc", "minor": 0, "patch": 0}))
with patch('generate_switcher.VERSION_JSON', str(version_file)):
with pytest.raises(ValueError, match="non-numeric"):
get_version_from_json()
def test_negative_values_raises(self, tmp_path):
version_file = tmp_path / 'version.json'
version_file.write_text(json.dumps({"major": -1, "minor": 0, "patch": 0}))
with patch('generate_switcher.VERSION_JSON', str(version_file)):
with pytest.raises(ValueError, match="negative"):
get_version_from_json()
def test_missing_file_returns_none(self):
with patch('generate_switcher.VERSION_JSON', '/nonexistent/version.json'):
assert get_version_from_json() is None
def test_invalid_json_raises(self, tmp_path):
version_file = tmp_path / 'version.json'
version_file.write_text('not valid json{{{')
with patch('generate_switcher.VERSION_JSON', str(version_file)):
with pytest.raises(ValueError, match="not valid JSON"):
get_version_from_json()