Skip to content

Commit eea5591

Browse files
authored
test: add version detection tests (#188)
1 parent 6e91ae1 commit eea5591

1 file changed

Lines changed: 164 additions & 0 deletions

File tree

tests/test_detect_version.py

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
# Copyright (c) 2026 ADBC Drivers Contributors
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import subprocess
16+
17+
import pytest
18+
19+
from adbc_drivers_dev.make import detect_version
20+
21+
22+
@pytest.fixture()
23+
def repo(tmp_path):
24+
subprocess.check_call(["git", "init", "--quiet"], cwd=tmp_path)
25+
subprocess.check_call(["git", "config", "user.name", "Test User"], cwd=tmp_path)
26+
subprocess.check_call(
27+
["git", "config", "user.email", "test@example.com"], cwd=tmp_path
28+
)
29+
subprocess.check_call(
30+
["git", "commit", "--quiet", "--allow-empty", "-m", "chore: initial commit"],
31+
cwd=tmp_path,
32+
)
33+
return tmp_path
34+
35+
36+
def test_no_tags(repo):
37+
version = detect_version(repo)
38+
assert version == "v0.0.1-dev"
39+
40+
41+
def test_no_tags_strict(repo):
42+
with pytest.raises(ValueError, match="No tags found"):
43+
detect_version(repo, strict=True)
44+
45+
46+
def test_on_tag(repo):
47+
subprocess.check_call(["git", "tag", "v1.2.3"], cwd=repo)
48+
version = detect_version(repo)
49+
assert version == "v1.2.3"
50+
51+
version = detect_version(repo, strict=True)
52+
assert version == "v1.2.3"
53+
54+
55+
def test_on_tag_subdir(repo):
56+
subprocess.check_call(["git", "tag", "rust/v1.2.3"], cwd=repo)
57+
version = detect_version(repo / "rust")
58+
assert version == "v1.2.3"
59+
60+
version = detect_version(repo / "rust", strict=True)
61+
assert version == "v1.2.3"
62+
63+
64+
def test_after_tag(repo):
65+
subprocess.check_call(["git", "tag", "v1.2.3"], cwd=repo)
66+
subprocess.check_call(
67+
["git", "commit", "--quiet", "--allow-empty", "-m", "feat: add new feature"],
68+
cwd=repo,
69+
)
70+
version = detect_version(repo)
71+
assert version.startswith("v1.2.3-dev.1."), version
72+
73+
74+
def test_after_tag_subdir(repo):
75+
subprocess.check_call(["git", "tag", "rust/v1.2.3"], cwd=repo)
76+
subprocess.check_call(
77+
["git", "commit", "--quiet", "--allow-empty", "-m", "feat: add new feature"],
78+
cwd=repo,
79+
)
80+
version = detect_version(repo / "rust")
81+
assert version.startswith("v1.2.3-dev.1."), version
82+
83+
84+
def test_after_prerelease(repo):
85+
subprocess.check_call(["git", "tag", "v1.2.3-alpha.1"], cwd=repo)
86+
subprocess.check_call(
87+
["git", "commit", "--quiet", "--allow-empty", "-m", "feat: add new feature"],
88+
cwd=repo,
89+
)
90+
version = detect_version(repo)
91+
# This is a weird edge case
92+
assert version.startswith("v0.0.1-dev.2."), version
93+
94+
95+
def test_after_prerelease2(repo):
96+
subprocess.check_call(["git", "tag", "v1.2.3"], cwd=repo)
97+
subprocess.check_call(
98+
["git", "commit", "--quiet", "--allow-empty", "-m", "feat: add new feature"],
99+
cwd=repo,
100+
)
101+
subprocess.check_call(["git", "tag", "v1.3.0-alpha.1"], cwd=repo)
102+
subprocess.check_call(
103+
["git", "commit", "--quiet", "--allow-empty", "-m", "feat: add new feature"],
104+
cwd=repo,
105+
)
106+
version = detect_version(repo)
107+
assert version.startswith("v1.2.3-dev.2."), version
108+
109+
110+
def test_after_prerelease_on_tag(repo):
111+
subprocess.check_call(["git", "tag", "v1.2.3-alpha.1"], cwd=repo)
112+
subprocess.check_call(
113+
["git", "commit", "--quiet", "--allow-empty", "-m", "feat: add new feature"],
114+
cwd=repo,
115+
)
116+
subprocess.check_call(["git", "tag", "v1.2.3"], cwd=repo)
117+
version = detect_version(repo)
118+
assert version == "v1.2.3"
119+
120+
121+
def test_versions_are_inverted(repo):
122+
# detect when versions are out of order
123+
subprocess.check_call(["git", "tag", "v1.2.3"], cwd=repo)
124+
subprocess.check_call(
125+
["git", "commit", "--quiet", "--allow-empty", "-m", "feat: add new feature"],
126+
cwd=repo,
127+
)
128+
subprocess.check_call(["git", "tag", "v1.2.3-alpha.1"], cwd=repo)
129+
130+
with pytest.raises(
131+
ValueError,
132+
match="Tag v1.2.3 is further from HEAD than v1.2.3-alpha.1, but has a newer version",
133+
):
134+
detect_version(repo)
135+
136+
137+
def test_strict_not_on_tag(repo):
138+
subprocess.check_call(
139+
["git", "commit", "--quiet", "--allow-empty", "-m", "feat: add new feature"],
140+
cwd=repo,
141+
)
142+
subprocess.check_call(["git", "tag", "v1.0.0"], cwd=repo)
143+
subprocess.check_call(
144+
["git", "commit", "--quiet", "--allow-empty", "-m", "feat: add new feature"],
145+
cwd=repo,
146+
)
147+
subprocess.check_call(["git", "tag", "v1.2.3"], cwd=repo)
148+
subprocess.check_call(
149+
["git", "commit", "--quiet", "--allow-empty", "-m", "feat: add new feature"],
150+
cwd=repo,
151+
)
152+
with pytest.raises(
153+
ValueError, match="is not on tag v1.2.3, but has 1 commits since"
154+
):
155+
detect_version(repo, strict=True)
156+
157+
158+
def test_strict_dirty(repo):
159+
subprocess.check_call(["git", "tag", "v1.2.3"], cwd=repo)
160+
with (repo / "foobar.txt").open("w"):
161+
pass
162+
subprocess.check_call(["git", "add", "foobar.txt"], cwd=repo)
163+
with pytest.raises(ValueError, match="has uncommitted changes"):
164+
detect_version(repo, strict=True)

0 commit comments

Comments
 (0)