Skip to content

Commit 2e4fef4

Browse files
committed
test: increase test robustness
1 parent ae6fdea commit 2e4fef4

File tree

1 file changed

+92
-35
lines changed

1 file changed

+92
-35
lines changed

tests/test_metadata_config.py

Lines changed: 92 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -6,31 +6,17 @@
66
from hatch_nodejs_version.metadata_source import NodeJSMetadataHook
77

88

9-
class TestMetadata:
10-
@pytest.mark.parametrize(
11-
"alt_package_json",
12-
[None, "package-other.json"],
13-
)
14-
@pytest.mark.parametrize(
15-
"pyproject_field",
16-
["urls", "description", "name", "keywords", "author", "maintainers", "license"],
17-
)
18-
def test_metadata_from_package(self, project, alt_package_json, pyproject_field):
19-
# Create a simple project
20-
(project / "pyproject.toml").write_text(
21-
f"""
9+
TRIVIAL_PYPROJECT_CONTENTS = """
2210
[build-backend]
2311
requires = ["hatchling", "hatch-vcs"]
2412
build-backend = "hatchling.build"
2513
[project]
2614
name = "my-app"
2715
version = "0.0.0"
2816
[tool.hatch.metadata.hooks.nodejs]
29-
"""
30-
)
31-
package_json = "package.json" if alt_package_json is None else alt_package_json
32-
(project / package_json).write_text(
33-
"""
17+
"""
18+
19+
DEMO_PACKAGE_CONTENTS = """
3420
{
3521
"name": "my-app",
3622
"version": "1.0.0",
@@ -63,27 +49,98 @@ def test_metadata_from_package(self, project, alt_package_json, pyproject_field)
6349
}
6450
}
6551
"""
66-
)
52+
53+
EXPECTED_METADATA = {
54+
"license": "MIT",
55+
"urls": {
56+
"bug tracker": "https://www.send-help.com",
57+
"repository": "https://github.com/some/code.git",
58+
"homepage": "https://where-the-heart-is.com",
59+
},
60+
"authors": [
61+
{
62+
"name": "Alice Roberts",
63+
"email": "[email protected]",
64+
}
65+
],
66+
"maintainers": [{"name": "Isaac Newton", "email": "[email protected]"}],
67+
"keywords": ["what", "is", "the", "time"],
68+
"description": "A terrible package",
69+
"name": "my-app",
70+
}
71+
72+
73+
class TestMetadata:
74+
@pytest.mark.parametrize(
75+
"alt_package_json",
76+
[None, "package-other.json"],
77+
)
78+
def test_all_metadata(self, project, alt_package_json):
79+
# Create a simple project
80+
package_json = "package.json" if alt_package_json is None else alt_package_json
81+
(project / "pyproject.toml").write_text(TRIVIAL_PYPROJECT_CONTENTS)
82+
(project / package_json).write_text(DEMO_PACKAGE_CONTENTS)
83+
6784
config = {} if alt_package_json is None else {"path": alt_package_json}
85+
metadata = {}
6886
metadata_source = NodeJSMetadataHook(project, config=config)
87+
metadata_source.update(metadata)
88+
89+
assert metadata == EXPECTED_METADATA
90+
91+
@pytest.mark.parametrize(
92+
"pyproject_field",
93+
[
94+
"urls",
95+
"description",
96+
"name",
97+
"keywords",
98+
"authors",
99+
"maintainers",
100+
"license",
101+
],
102+
)
103+
def test_subset_metadata(self, project, pyproject_field):
104+
# Create a simple project
105+
(project / "pyproject.toml").write_text(TRIVIAL_PYPROJECT_CONTENTS)
106+
(project / "package.json").write_text(DEMO_PACKAGE_CONTENTS)
107+
108+
config = {"fields": [pyproject_field]}
69109

70110
metadata = {}
111+
metadata_source = NodeJSMetadataHook(project, config=config)
71112
metadata_source.update(metadata)
72113

73-
assert metadata["license"] == "MIT"
74-
assert metadata["urls"] == {
75-
"bug tracker": "https://www.send-help.com",
76-
"repository": "https://github.com/some/code.git",
77-
"homepage": "https://where-the-heart-is.com",
78-
}
79-
assert metadata["author"] == {
80-
"name": "Alice Roberts",
81-
"email": "[email protected]",
82-
}
114+
assert pyproject_field in metadata
115+
assert len(metadata) == len(config["fields"])
116+
assert metadata[pyproject_field] == EXPECTED_METADATA[pyproject_field]
83117

84-
assert metadata["maintainers"] == [
85-
{"name": "Isaac Newton", "email": "[email protected]"}
86-
]
87-
assert metadata["keywords"] == ["what", "is", "the", "time"]
88-
assert metadata["description"] == "A terrible package"
89-
assert metadata["name"] == "my-app"
118+
def test_contributors_as_maintainers(self, project):
119+
# Create a simple project
120+
(project / "pyproject.toml").write_text(TRIVIAL_PYPROJECT_CONTENTS)
121+
(project / "package.json").write_text(DEMO_PACKAGE_CONTENTS)
122+
123+
metadata = {}
124+
metadata_source = NodeJSMetadataHook(
125+
project, config={"contributors-as-maintainers": True}
126+
)
127+
metadata_source.update(metadata)
128+
129+
assert metadata["authors"] == EXPECTED_METADATA["authors"]
130+
assert metadata["maintainers"] == EXPECTED_METADATA["maintainers"]
131+
132+
def test_contributors_as_authors(self, project):
133+
# Create a simple project
134+
(project / "pyproject.toml").write_text(TRIVIAL_PYPROJECT_CONTENTS)
135+
(project / "package.json").write_text(DEMO_PACKAGE_CONTENTS)
136+
137+
metadata = {}
138+
metadata_source = NodeJSMetadataHook(
139+
project, config={"contributors-as-maintainers": False}
140+
)
141+
metadata_source.update(metadata)
142+
143+
assert (
144+
metadata["authors"]
145+
== EXPECTED_METADATA["authors"] + EXPECTED_METADATA["maintainers"]
146+
)

0 commit comments

Comments
 (0)