Skip to content

Commit cc53c30

Browse files
committed
refactor: cleanup error messages
feat: add `allow_local_segment` option test: validate `allow_local_segment` option refactor: change `allow_local_segment` to `allow_build_segment` refactor: change `allow_build_segment` to `allow_build_metadata` refactor: drop `allow_build_metadata` option
1 parent 79fb969 commit cc53c30

File tree

3 files changed

+60
-44
lines changed

3 files changed

+60
-44
lines changed

hatch_nodejs_version/metadata_source.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def path(self) -> str:
4040
version_file = self.config.get("path", "package.json")
4141
if not isinstance(version_file, str):
4242
raise TypeError(
43-
"Option `path` for build hook `{}` must be a string".format(
43+
"Option `path` for metadata hook `{}` must be a string".format(
4444
self.PLUGIN_NAME
4545
)
4646
)
@@ -60,7 +60,7 @@ def fields(self) -> None | set[str]:
6060
isinstance(fields, list) and all(isinstance(f, str) for f in fields)
6161
):
6262
raise TypeError(
63-
"Option `fields` for build hook `{}` "
63+
"Option `fields` for metadata hook `{}` "
6464
"must be a list of strings".format(self.PLUGIN_NAME)
6565
)
6666
self.__fields = set(fields)
@@ -74,7 +74,7 @@ def contributors_as_maintainers(self) -> bool:
7474
)
7575
if not isinstance(contributors_as_maintainers, bool):
7676
raise TypeError(
77-
"Option `contributors-as-maintainers` for build hook `{}` "
77+
"Option `contributors-as-maintainers` for metadata hook `{}` "
7878
"must be a boolean".format(self.PLUGIN_NAME)
7979
)
8080
self.__contributors_as_maintainers = contributors_as_maintainers
@@ -87,7 +87,7 @@ def homepage_label(self) -> bool:
8787

8888
if not isinstance(homepage_label, str):
8989
raise TypeError(
90-
"Option `homepage-label` for build hook `{}` "
90+
"Option `homepage-label` for metadata hook `{}` "
9191
"must be a string".format(self.PLUGIN_NAME)
9292
)
9393
self.__homepage_label = homepage_label
@@ -100,9 +100,8 @@ def bugs_label(self) -> bool:
100100

101101
if not isinstance(bug_tracker_label, str):
102102
raise TypeError(
103-
"Option `bugs-label` for build hook `{}` must be a string".format(
104-
self.PLUGIN_NAME
105-
)
103+
"Option `bugs-label` for metadata hook `{}` "
104+
"must be a string".format(self.PLUGIN_NAME)
106105
)
107106
self.__bugs_label = bug_tracker_label
108107
return self.__bugs_label
@@ -114,7 +113,7 @@ def repository_label(self) -> bool:
114113

115114
if not isinstance(bug_tracker_label, str):
116115
raise TypeError(
117-
"Option `repository-label` for build hook `{}` "
116+
"Option `repository-label` for metadata hook `{}` "
118117
"must be a string".format(self.PLUGIN_NAME)
119118
)
120119
self.__repository_label = bug_tracker_label

hatch_nodejs_version/version_source.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,7 @@ def path(self):
9494

9595
return self.__path
9696

97-
@staticmethod
98-
def node_version_to_python(version: str) -> str:
97+
def node_version_to_python(self, version: str) -> str:
9998
# NodeJS version strings are a near superset of Python version strings
10099
match = re.match(
101100
r"^\s*" + NODE_VERSION_PATTERN + r"\s*$",
@@ -118,8 +117,7 @@ def node_version_to_python(version: str) -> str:
118117

119118
return "".join(parts)
120119

121-
@staticmethod
122-
def python_version_to_node(version: str) -> str:
120+
def python_version_to_node(self, version: str) -> str:
123121
# NodeJS version strings are a near superset of Python version strings
124122
match = re.match(
125123
r"^\s*" + PYTHON_VERSION_PATTERN + r"\s*$",

tests/test_version_config.py

Lines changed: 51 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -20,51 +20,70 @@
2020
("1.4.5-preview0", "1.4.5preview0"),
2121
("1.4.5-preview0+build1.0.0", "1.4.5preview0+build1.0.0"),
2222
("1.4.5-preview0+build-1.0.0", "1.4.5preview0+build-1.0.0"),
23-
("1.4.5-preview0+build-1_0.0", "1.4.5preview0+build-1_0.0"),
24-
]
25-
26-
BAD_NODE_VERSIONS = [
27-
"1.4",
28-
"1.4.5a0",
29-
"1.4.5-c0.post1",
30-
"1.4.5-rc0.post1.dev2",
31-
"1.4.5-rc0.post1+-bad",
32-
"1.4.5-rc0.post1+bad_",
33-
]
34-
BAD_PYTHON_VERSIONS = [
35-
"1.4",
36-
"1.4.5ab",
37-
"1.4.5-c0.smoke2",
38-
"1.4.5rc.post1@dev2",
39-
"1.4.5rc0.post1+-bad",
40-
"1.4.5rc0.post1+bad_",
23+
("1.4.5-preview0+good-1_0.0", "1.4.5preview0+good-1_0.0"),
4124
]
4225

4326

4427
class TestVersion:
45-
@pytest.mark.parametrize(
46-
"node_version, python_version",
47-
GOOD_NODE_PYTHON_VERSIONS,
48-
)
49-
def test_parse_correct(self, node_version, python_version):
50-
node_version_parsed = NodeJSVersionSource.python_version_to_node(python_version)
51-
assert node_version_parsed == node_version
52-
5328
@pytest.mark.parametrize(
5429
"python_version",
55-
BAD_PYTHON_VERSIONS,
30+
[
31+
"1.4",
32+
"1.4.5ab",
33+
"1.4.5-c0.smoke2",
34+
"1.4.5rc.post1@dev2",
35+
"1.4.5rc0.post1+-bad",
36+
"1.4.5rc0.post1+bad_",
37+
],
5638
)
57-
def test_parse_python_incorrect(self, python_version):
39+
def test_parse_python_incorrect(self, project, python_version):
40+
# Create a simple project
41+
(project / "pyproject.toml").write_text(
42+
"""
43+
[build-system]
44+
requires = ["hatchling", "hatch-vcs"]
45+
build-backend = "hatchling.build"
46+
[project]
47+
name = "my-app"
48+
dynamic = ["version"]
49+
[tool.hatch.version]
50+
source = "nodejs"
51+
"""
52+
)
53+
config = {}
54+
version_source = NodeJSVersionSource(project, config=config)
5855
with pytest.raises(ValueError, match=".* did not match regex"):
59-
NodeJSVersionSource.python_version_to_node(python_version)
56+
version_source.python_version_to_node(python_version)
6057

6158
@pytest.mark.parametrize(
6259
"node_version",
63-
BAD_NODE_VERSIONS,
60+
[
61+
"1.4",
62+
"1.4.5a0",
63+
"1.4.5-c0.post1",
64+
"1.4.5-rc0.post1.dev2",
65+
"1.4.5-rc0.post1+-bad",
66+
"1.4.5-rc0.post1+bad_",
67+
],
6468
)
65-
def test_parse_node_incorrect(self, node_version):
69+
def test_parse_node_incorrect(self, project, node_version):
70+
# Create a simple project
71+
(project / "pyproject.toml").write_text(
72+
"""
73+
[build-system]
74+
requires = ["hatchling", "hatch-vcs"]
75+
build-backend = "hatchling.build"
76+
[project]
77+
name = "my-app"
78+
dynamic = ["version"]
79+
[tool.hatch.version]
80+
source = "nodejs"
81+
"""
82+
)
83+
config = {}
84+
version_source = NodeJSVersionSource(project, config=config)
6685
with pytest.raises(ValueError, match=".* did not match regex"):
67-
NodeJSVersionSource.node_version_to_python(node_version)
86+
version_source.node_version_to_python(node_version)
6887

6988
@pytest.mark.parametrize(
7089
"node_version, python_version",

0 commit comments

Comments
 (0)