File: test_metadata_config.py

package info (click to toggle)
python-hatch-nodejs-version 0.4.0-1
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 188 kB
  • sloc: python: 661; makefile: 3
file content (186 lines) | stat: -rw-r--r-- 5,937 bytes parent folder | download
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
# SPDX-FileCopyrightText: 2022-present Angus Hollands <goosey15@gmail.com>
#
# SPDX-License-Identifier: MIT
import json

import pytest

from hatch_nodejs_version.metadata_source import NodeJSMetadataHook

TRIVIAL_PYPROJECT_CONTENTS = """
[build-backend]
requires = ["hatchling", "hatch-vcs"]
build-backend = "hatchling.build"
[project]
name = "my-app"
version = "0.0.0"
[tool.hatch.metadata.hooks.nodejs]
"""

TRIVIAL_PACKAGE_CONTENTS = """
{
  "name": "my-app",
  "version": "1.0.0",
  "description": "A terrible package",
  "keywords": [
    "what",
    "is",
    "the",
    "time"
  ],
  "homepage": "https://where-the-heart-is.com",
  "bugs": {
    "url": "https://www.send-help.com",
    "email": "funky@rider.com"
  },
  "license": "MIT",
  "author": {
    "name": "Alice Roberts",
    "email": "alice.roberts@bbc.lol"
  },
  "contributors": [
        {
        "name": "Isaac Newton",
        "email": "isaac.newton@apple.com"
      }
  ],
  "repository": {
    "type": "git",
    "url": "https://github.com/some/code.git"
  }
}
"""

TRIVIAL_EXPECTED_METADATA = {
    "license": "MIT",
    "urls": {
        "Bug Tracker": "https://www.send-help.com",
        "Repository": "https://github.com/some/code.git",
        "Homepage": "https://where-the-heart-is.com",
    },
    "authors": [
        {
            "name": "Alice Roberts",
            "email": "alice.roberts@bbc.lol",
        }
    ],
    "maintainers": [{"name": "Isaac Newton", "email": "isaac.newton@apple.com"}],
    "keywords": ["what", "is", "the", "time"],
    "description": "A terrible package",
    "name": "my-app",
}


class TestMetadata:
    @pytest.mark.parametrize(
        "alt_package_json",
        [None, "package-other.json"],
    )
    def test_all_metadata(self, project, alt_package_json):
        # Create a simple project
        package_json = "package.json" if alt_package_json is None else alt_package_json
        (project / "pyproject.toml").write_text(TRIVIAL_PYPROJECT_CONTENTS)
        (project / package_json).write_text(TRIVIAL_PACKAGE_CONTENTS)

        config = {} if alt_package_json is None else {"path": alt_package_json}
        metadata = {}
        metadata_source = NodeJSMetadataHook(project, config=config)
        metadata_source.update(metadata)

        assert metadata == TRIVIAL_EXPECTED_METADATA

    @pytest.mark.parametrize(
        "pyproject_field",
        [
            "urls",
            "description",
            "name",
            "keywords",
            "authors",
            "maintainers",
            "license",
        ],
    )
    def test_subset_metadata(self, project, pyproject_field):
        # Create a simple project
        (project / "pyproject.toml").write_text(TRIVIAL_PYPROJECT_CONTENTS)
        (project / "package.json").write_text(TRIVIAL_PACKAGE_CONTENTS)

        config = {"fields": [pyproject_field]}

        metadata = {}
        metadata_source = NodeJSMetadataHook(project, config=config)
        metadata_source.update(metadata)

        assert pyproject_field in metadata
        assert len(metadata) == len(config["fields"])
        assert metadata[pyproject_field] == TRIVIAL_EXPECTED_METADATA[pyproject_field]

    def test_contributors_as_maintainers(self, project):
        # Create a simple project
        (project / "pyproject.toml").write_text(TRIVIAL_PYPROJECT_CONTENTS)
        (project / "package.json").write_text(TRIVIAL_PACKAGE_CONTENTS)

        metadata = {}
        metadata_source = NodeJSMetadataHook(
            project, config={"contributors-as-maintainers": True}
        )
        metadata_source.update(metadata)

        assert metadata["authors"] == TRIVIAL_EXPECTED_METADATA["authors"]
        assert metadata["maintainers"] == TRIVIAL_EXPECTED_METADATA["maintainers"]

    def test_contributors_as_authors(self, project):
        # Create a simple project
        (project / "pyproject.toml").write_text(TRIVIAL_PYPROJECT_CONTENTS)
        (project / "package.json").write_text(TRIVIAL_PACKAGE_CONTENTS)

        metadata = {}
        metadata_source = NodeJSMetadataHook(
            project, config={"contributors-as-maintainers": False}
        )
        metadata_source.update(metadata)

        assert (
            metadata["authors"]
            == TRIVIAL_EXPECTED_METADATA["authors"]
            + TRIVIAL_EXPECTED_METADATA["maintainers"]
        )

    def test_labels(self, project):
        # Create a simple project
        (project / "pyproject.toml").write_text(TRIVIAL_PYPROJECT_CONTENTS)
        (project / "package.json").write_text(TRIVIAL_PACKAGE_CONTENTS)

        metadata = {}
        metadata_source = NodeJSMetadataHook(
            project,
            config={
                "repository-label": "the-repository",
                "bugs-label": "the-bug-tracker",
                "homepage-label": "the-homepage",
            },
        )
        metadata_source.update(metadata)

        urls = metadata["urls"]
        assert urls["the-repository"] == "https://github.com/some/code.git"
        assert urls["the-bug-tracker"] == "https://www.send-help.com"
        assert urls["the-homepage"] == "https://where-the-heart-is.com"

    def test_authors_accepted_as_strings(self, project):
        original_package_content = json.loads(TRIVIAL_PACKAGE_CONTENTS)
        updated_package_content = original_package_content.copy()
        author_as_string = (
            f"{original_package_content['author']['name']} "
            f"<{original_package_content['author']['email']}>"
        )
        updated_package_content["author"] = author_as_string
        (project / "pyproject.toml").write_text(TRIVIAL_PYPROJECT_CONTENTS)
        (project / "package.json").write_text(json.dumps(updated_package_content))

        config = {}
        metadata = {}
        metadata_source = NodeJSMetadataHook(project, config=config)
        metadata_source.update(metadata)
        assert metadata == TRIVIAL_EXPECTED_METADATA