File: test_config.py

package info (click to toggle)
python-hatch-fancy-pypi-readme 25.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 296 kB
  • sloc: python: 906; makefile: 3
file content (253 lines) | stat: -rw-r--r-- 7,320 bytes parent folder | download | duplicates (3)
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
# SPDX-FileCopyrightText: 2022 Hynek Schlawack <hs@ox.cx>
#
# SPDX-License-Identifier: MIT

import pytest

from hatch_fancy_pypi_readme._config import load_and_validate_config
from hatch_fancy_pypi_readme.exceptions import ConfigurationError


class TestValidateConfig:
    @pytest.mark.parametrize(
        "cfg",
        [{"content-type": "text/markdown", "fragments": [{"text": "foo"}]}],
    )
    def test_valid(self, cfg):
        """
        Valid configurations return empty error lists.
        """
        load_and_validate_config(cfg)

    def test_missing_content_type(self):
        """
        Missing content-type is caught.
        """
        with pytest.raises(ConfigurationError) as ei:
            load_and_validate_config({"fragments": [{"text": "foo"}]})

        assert (
            [
                "tool.hatch.metadata.hooks.fancy-pypi-readme."
                "content-type is missing."
            ]
            == ei.value.errors
            == ei.value.args[0]
        )

    def test_wrong_content_type(self):
        """
        Missing content-type is caught.
        """
        with pytest.raises(ConfigurationError) as ei:
            load_and_validate_config(
                {"content-type": "text/html", "fragments": [{"text": "foo"}]}
            )

        assert [
            "tool.hatch.metadata.hooks.fancy-pypi-readme.content-type: "
            "'text/html' is not one of ['text/markdown', 'text/x-rst']"
        ] == ei.value.errors


VALID_FOR_FRAG = {"content-type": "text/markdown"}


def cow_add_frag(**kw):
    d = VALID_FOR_FRAG.copy()
    d["fragments"] = [kw]

    return d


class TestValidateConfigFragments:
    def test_empty_fragments(self):
        """
        Empty fragments are caught.
        """
        with pytest.raises(ConfigurationError) as ei:
            load_and_validate_config(
                {"content-type": "text/markdown", "fragments": []}
            )

        assert (
            [
                "tool.hatch.metadata.hooks.fancy-pypi-readme.fragments must "
                "not be empty."
            ]
            == ei.value.errors
            == ei.value.args[0]
        )

    def test_missing_fragments(self):
        """
        Missing fragments are caught.
        """
        with pytest.raises(ConfigurationError) as ei:
            load_and_validate_config({"content-type": "text/markdown"})

        assert (
            [
                "tool.hatch.metadata.hooks.fancy-pypi-readme.fragments"
                " is missing."
            ]
            == ei.value.errors
            == ei.value.args[0]
        )

    def test_empty_fragment_dict(self):
        """
        Empty fragment dicts are handled gracefully.
        """
        with pytest.raises(ConfigurationError) as ei:
            load_and_validate_config(
                {"content-type": "text/markdown", "fragments": [{}]}
            )

        assert ["Unknown fragment type {}."] == ei.value.errors

    def test_empty_text_fragment(self):
        """
        Text fragments can't be empty.
        """
        with pytest.raises(ConfigurationError) as ei:
            load_and_validate_config(cow_add_frag(text=""))

        assert ["Text fragments must not be empty."] == ei.value.errors

    def test_invalid_fragments(self):
        """
        Invalid fragments are caught.
        """
        with pytest.raises(ConfigurationError) as ei:
            load_and_validate_config(
                {
                    "content-type": "text/markdown",
                    "fragments": [
                        {"text": "this is ok"},
                        {"foo": "this is not"},
                        {"bar": "neither is this"},
                    ],
                }
            )

        assert {
            "Unknown fragment type {'foo': 'this is not'}.",
            "Unknown fragment type {'bar': 'neither is this'}.",
        } == set(ei.value.errors)

    def test_fragment_loading_errors(self):
        """
        Errors that happen while loading a fragment are propagated.
        """
        with pytest.raises(ConfigurationError) as ei:
            load_and_validate_config(
                {
                    "content-type": "text/markdown",
                    "fragments": [{"path": "yolo"}],
                }
            )

        assert ["Fragment file 'yolo' not found."] == ei.value.errors


VALID_FOR_SUB = {
    "content-type": "text/markdown",
    "fragments": [{"text": "foobar"}],
}


def cow_add_sub(**kw):
    d = VALID_FOR_SUB.copy()
    d["substitutions"] = [kw]

    return d


class TestValidateConfigSubstitutions:
    def test_invalid_substitution(self):
        """
        Invalid substitutions are caught and reported.
        """
        with pytest.raises(ConfigurationError) as ei:
            load_and_validate_config(
                {
                    "content-type": "text/markdown",
                    "fragments": [{"text": "foo"}],
                    "substitutions": [{"foo": "bar"}],
                }
            )

        assert {
            "Substitution {'foo': 'bar'} is missing a 'pattern' key.",
            "Substitution {'foo': 'bar'} is missing a 'replacement' key.",
        } == set(ei.value.errors)

    def test_empty(self):
        """
        Empty dict is not valid.
        """
        with pytest.raises(ConfigurationError) as ei:
            load_and_validate_config(cow_add_sub())

        assert {
            "Substitution {} is missing a 'pattern' key.",
            "Substitution {} is missing a 'replacement' key.",
        } == set(ei.value.errors)

    def test_ignore_case_not_bool(self):
        """
        Ignore case is either bool or nothing.
        """
        with pytest.raises(ConfigurationError) as ei:
            load_and_validate_config(
                cow_add_sub(
                    pattern="foo", replacement="bar", **{"ignore-case": 42}
                )
            )

        assert {"Value 42 for 'ignore-case' is not a bool."} == set(
            ei.value.errors
        )

    def test_pattern_no_valid_regexp(self):
        """
        Pattern must be a valid re-regexp.
        """
        with pytest.raises(ConfigurationError) as ei:
            load_and_validate_config(
                cow_add_sub(pattern="foo???", replacement="bar")
            )

        assert {
            "'foo???' is not a valid regular expression: multiple repeat at "
            "position 5"
        } == set(ei.value.errors)

    def test_replacement_not_a_string(self):
        """
        Replacements must be strings.
        """
        with pytest.raises(ConfigurationError) as ei:
            load_and_validate_config(
                cow_add_sub(pattern="foo", replacement=42)
            )

        assert {"Replacement value 42 is not a string."} == set(
            ei.value.errors
        )

    def test_substitutions_not_array(self):
        """
        Substitutions key must be a list.
        """
        cfg = VALID_FOR_SUB.copy()
        cfg["substitutions"] = {}

        with pytest.raises(ConfigurationError) as ei:
            load_and_validate_config(cfg)

        assert {
            "tool.hatch.metadata.hooks.fancy-pypi-readme.substitutions must "
            "be an array."
        } == set(ei.value.errors)