File: test_formats.py

package info (click to toggle)
python-validate-pyproject 0.24.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,340 kB
  • sloc: python: 3,053; makefile: 46; sh: 25
file content (459 lines) | stat: -rw-r--r-- 14,610 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
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
import logging
import os
from itertools import chain
from unittest.mock import Mock

import pytest

from validate_pyproject import api, formats

_chain_iter = chain.from_iterable

# The following examples were taken by inspecting some opensource projects in the python
# community
ENTRYPOINT_EXAMPLES = {
    "django": {
        "console_scripts": {
            "django-admin": "django.core.management:execute_from_command_line"
        }
    },
    "pandas": {
        "pandas_plotting_backends": {"matplotlib": "pandas:plotting._matplotlib"},
    },
    "PyScaffold": {
        "console_scripts": {"putup": "pyscaffold.cli:run"},
        "pyscaffold.cli": {
            "config": "pyscaffold.extensions.config:Config",
            "interactive": "pyscaffold.extensions.interactive:Interactive",
            "venv": "pyscaffold.extensions.venv:Venv",
            "namespace": "pyscaffold.extensions.namespace:Namespace",
            "no_skeleton": "pyscaffold.extensions.no_skeleton:NoSkeleton",
            "pre_commit": "pyscaffold.extensions.pre_commit:PreCommit",
            "no_tox": "pyscaffold.extensions.no_tox:NoTox",
            "gitlab": "pyscaffold.extensions.gitlab_ci:GitLab",
            "cirrus": "pyscaffold.extensions.cirrus:Cirrus",
            "no_pyproject": "pyscaffold.extensions.no_pyproject:NoPyProject",
        },
    },
    "setuptools-scm": {
        "distutils.setup_keywords": {
            "use_scm_version": "setuptools_scm.integration:version_keyword",
        },
        "setuptools.file_finders": {
            "setuptools_scm": "setuptools_scm.integration:find_files",
        },
        "setuptools.finalize_distribution_options": {
            "setuptools_scm": "setuptools_scm.integration:infer_version",
        },
        "setuptools_scm.files_command": {
            ".hg": "setuptools_scm.file_finder_hg:hg_find_files",
            ".git": "setuptools_scm.file_finder_git:git_find_files",
        },
        "setuptools_scm.local_scheme": {
            "node-and-date": "setuptools_scm.version:get_local_node_and_date",
            "node-and-timestamp": "setuptools_scm.version:get_local_node_and_timestamp",
            "dirty-tag": "setuptools_scm.version:get_local_dirty_tag",
            "no-local-version": "setuptools_scm.version:get_no_local_node",
        },
        "setuptools_scm.parse_scm": {
            ".hg": "setuptools_scm.hg:parse",
            ".git": "setuptools_scm.git:parse",
        },
        "setuptools_scm.parse_scm_fallback": {
            ".hg_archival.txt": "setuptools_scm.hg:parse_archival",
            "PKG-INFO": "setuptools_scm.hacks:parse_pkginfo",
            "pip-egg-info": "setuptools_scm.hacks:parse_pip_egg_info",
            "setup.py": "setuptools_scm.hacks:fallback_version",
        },
        "setuptools_scm.version_scheme": {
            "guess-next-dev": "setuptools_scm.version:guess_next_dev_version",
            "post-release": "setuptools_scm.version:postrelease_version",
            "python-simplified-semver": "setuptools_scm.version:simplified_semver_version",
            "release-branch-semver": "setuptools_scm.version:release_branch_semver_version",
            "no-guess-dev": "setuptools_scm.version:no_guess_dev_version",
            "calver-by-date": "setuptools_scm.version:calver_by_date",
        },
    },
    "anyio": {
        "pytest11": {
            "anyio": "anyio.pytest_plugin",
        },
    },
}


@pytest.mark.parametrize(
    "example", _chain_iter(v.keys() for v in ENTRYPOINT_EXAMPLES.values())
)
def test_entrypoint_group(example):
    assert formats.python_entrypoint_group(example)


@pytest.mark.parametrize(
    "example",
    _chain_iter(
        _chain_iter(e.keys() for e in v.values()) for v in ENTRYPOINT_EXAMPLES.values()
    ),
)
def test_entrypoint_name(example):
    assert formats.python_entrypoint_name(example)


@pytest.mark.parametrize("example", [" invalid", "=invalid", "[invalid]", "[invalid"])
def test_entrypoint_invalid_name(example):
    assert formats.python_entrypoint_name(example) is False


@pytest.mark.parametrize("example", ["val[id", "also valid"])
def test_entrypoint_name_not_recommended(example, caplog):
    caplog.set_level(logging.WARNING)
    assert formats.python_entrypoint_name(example) is True
    assert "does not follow recommended pattern" in caplog.text


@pytest.mark.parametrize(
    "example",
    _chain_iter(
        _chain_iter(e.values() for e in v.values())
        for v in ENTRYPOINT_EXAMPLES.values()
    ),
)
def test_entrypoint_references(example):
    assert formats.python_entrypoint_reference(example)
    assert formats.pep517_backend_reference(example)
    assert formats.pep517_backend_reference(example.replace(":", "."))


def test_entrypoint_references_with_extras():
    example = "test.module:func [invalid"
    assert formats.python_entrypoint_reference(example) is False

    example = "test.module:func [valid]"
    assert formats.python_entrypoint_reference(example)
    assert formats.pep517_backend_reference(example) is False

    example = "test.module:func [valid, extras]"
    assert formats.python_entrypoint_reference(example)

    example = "test.module:func [??inva#%@!lid??]"
    assert formats.python_entrypoint_reference(example) is False


@pytest.mark.parametrize("example", ["module", "invalid-module"])
def test_invalid_entrypoint_references(example):
    result = example == "module"
    assert formats.python_entrypoint_reference(example) is result


@pytest.mark.parametrize("example", ["λ", "a", "_"])
def test_valid_python_identifier(example):
    assert formats.python_identifier(example)


@pytest.mark.parametrize("example", ["a.b", "x+y", " a", "☺"])
def test_invalid_python_identifier(example):
    assert formats.python_identifier(example) is False


@pytest.mark.parametrize(
    "example",
    [
        "0.9.10",
        "1988.12",
        "1.01rc1",
        "0.99a9",
        "3.14b5",
        "1.42.post0",
        "1.73a2.post0",
        "2.23.post6.dev0",
        "3!6.0",
        "1.0+abc.7",
        "v4.0.1",
    ],
)
def test_valid_pep440(example):
    assert formats.pep440(example)


@pytest.mark.parametrize(
    "example",
    [
        "0-9-10",
        "v4.0.1.mysuffix",
        "p4.0.2",
    ],
)
def test_invalid_pep440(example):
    assert formats.pep440(example) is False


@pytest.mark.parametrize(
    "example",
    [
        "~= 0.9, >= 1.0, != 1.3.4.*, < 2.0",
        ">= 1.4.5, == 1.4.*",
        "~= 2.2.post3",
        "!= 1.1.post1",
    ],
)
def test_valid_pep508_versionspec(example):
    assert formats.pep508_versionspec(example)


@pytest.mark.parametrize(
    "example",
    [
        "~ 0.9, ~> 1.0, - 1.3.4.*",
        "- 1.3.4.*",
        "~> 1.0",
        "~ 0.9",
        "@ file:///localbuilds/pip-1.3.1.zip",
        'v1.0; python_version<"2.7"',
    ],
)
def test_invalid_pep508_versionspec(example):
    assert formats.pep508_versionspec(example) is False


@pytest.mark.parametrize(
    "example",
    [
        "https://python.org",
        "http://python.org",
        "http://localhost:8000",
        "ftp://python.org",
        "scheme://netloc/path;parameters?query#fragment",
    ],
)
def test_valid_url(example):
    assert formats.url(example)


@pytest.mark.parametrize(
    "example",
    [
        "",
        42,
        "p@python.org",
        "http:python.org",
        "/python.org",
    ],
)
def test_invalid_url(example):
    assert formats.url(example) is False


@pytest.mark.parametrize(
    "example",
    [
        "ab",
        "ab.c.d",
        "abc._d.λ",
    ],
)
def test_valid_module_name(example):
    assert formats.python_module_name(example) is True


@pytest.mark.parametrize(
    "example",
    [
        "-",
        " ",
        "ab-cd",
        ".example",
    ],
)
def test_invalid_module_name(example):
    assert formats.python_module_name(example) is False


@pytest.mark.parametrize(
    "example",
    [
        "pip-run",
        "abc-d-λ",
        "abc-d-λ.xyz-e",
        "abc-d.λ-xyz-e",
    ],
)
def test_valid_module_name_relaxed(example):
    assert formats.python_module_name_relaxed(example) is True


@pytest.mark.parametrize(
    "example",
    [
        "pip run",
        "-pip-run",
        "pip-run-",
        "pip-run-stubs",
    ],
)
def test_invalid_module_name_relaxed(example):
    assert formats.python_module_name_relaxed(example) is False


@pytest.mark.parametrize(
    "example",
    [
        "MIT",
        "Bsd-3-clause",
        "mit and (apache-2.0 or bsd-2-clause)",
        "MIT OR GPL-2.0-or-later OR (FSFUL AND BSD-2-Clause)",
        "GPL-3.0-only WITH Classpath-exception-2.0 OR BSD-3-Clause",
        "LicenseRef-Special-License OR CC0-1.0 OR Unlicense",
        "LicenseRef-Public-Domain",
        "licenseref-proprietary",
        "LicenseRef-Beerware-4.2",
        "(LicenseRef-Special-License OR LicenseRef-OtherLicense) OR Unlicense",
    ],
)
def test_valid_pep639_license_expression(example):
    assert formats.SPDX(example) is True


@pytest.mark.parametrize(
    "example",
    [
        "",
        "Use-it-after-midnight",
        "LicenseRef-License with spaces",
        "LicenseRef-License_with_underscores",
        "or",
        "and",
        "with",
        "mit or",
        "mit and",
        "mit with",
        "or mit",
        "and mit",
        "with mit",
        "(mit",
        "mit)",
        "mit or or apache-2.0",
        # Missing an operator before `(`.
        "mit or apache-2.0 (bsd-3-clause and MPL-2.0)",
        # "2-BSD-Clause is not a valid license.
        "Apache-2.0 OR 2-BSD-Clause",
    ],
)
def test_invalid_pep639_license_expression(example):
    assert formats.SPDX(example) is False


class TestClassifiers:
    """The ``_TroveClassifier`` class and ``_download_classifiers`` are part of the
    private API and therefore need to be tested.

    By constantly testing them we can make sure the URL used to download classifiers and
    the format they are presented are still supported by PyPI.

    If at any point these tests start to fail, we know that we need to change strategy.
    """

    VALID_CLASSIFIERS = (
        "Development Status :: 5 - Production/Stable",
        "Framework :: Django",
        "Operating System :: POSIX",
        "Programming Language :: Python :: 3 :: Only",
        "private :: not really a classifier",
    )

    def test_does_not_break_public_function_detection(self):
        # See https://github.com/abravalheri/validate-pyproject/issues/12

        # When `trove_classifiers` is defined from the dependency package
        # it will be a function.
        # When it is defined based on the download, it will be a custom object.

        # In both cases the `_TroveClassifier` class should not be made public,
        # but the instance should

        trove_classifier = formats._TroveClassifier()
        _formats = Mock(
            _TroveClassifier=formats._TroveClassifier,
            trove_classifiers=trove_classifier,
        )
        fns = api._get_public_functions(_formats)
        assert fns == {"trove-classifier": trove_classifier}

        # Make sure the object and the function have the same name
        assert "trove-classifier" in api.FORMAT_FUNCTIONS
        normalized_name = trove_classifier.__name__.replace("_", "-")
        assert normalized_name == "trove-classifier"
        assert normalized_name in api.FORMAT_FUNCTIONS

        func_name = trove_classifier.__name__
        assert getattr(formats, func_name) in api.FORMAT_FUNCTIONS.values()

    def test_download(self):
        try:
            classifiers = formats._download_classifiers()
        except Exception as ex:
            pytest.xfail(f"Error with download: {ex.__class__.__name__} - {ex}")
        assert isinstance(classifiers, str)
        assert bytes(classifiers, "utf-8")

    @pytest.mark.skip(reason="no downloads allowed while package build")
    def test_downloaded(self, monkeypatch):
        if os.name != "posix":
            # Mock on Windows (problems with SSL)
            downloader = Mock(return_value="\n".join(self.VALID_CLASSIFIERS))
            monkeypatch.setattr(formats, "_download_classifiers", downloader)

        validator = formats._TroveClassifier()
        assert validator("Made Up :: Classifier") is False
        assert validator.downloaded is not None
        assert validator.downloaded is not False
        assert len(validator.downloaded) > 3

    def test_valid_download_only_once(self, monkeypatch):
        if os.name == "posix":
            # Really download to make sure the API is still exposed by PyPI
            downloader = Mock(side_effect=formats._download_classifiers)
        else:
            # Mock on Windows (problems with SSL)
            downloader = Mock(return_value="\n".join(self.VALID_CLASSIFIERS))

        monkeypatch.setattr(formats, "_download_classifiers", downloader)
        validator = formats._TroveClassifier()
        for classifier in self.VALID_CLASSIFIERS:
            assert validator(classifier) is True
        downloader.assert_called_once()

    @pytest.mark.parametrize(
        "no_network", ("NO_NETWORK", "VALIDATE_PYPROJECT_NO_NETWORK")
    )
    def test_always_valid_with_no_network(self, monkeypatch, no_network):
        monkeypatch.setenv(no_network, "1")
        validator = formats._TroveClassifier()
        assert validator("Made Up :: Classifier") is True
        assert not validator.downloaded
        assert validator("Other Made Up :: Classifier") is True
        assert not validator.downloaded

    def test_always_valid_with_skip_download(self):
        validator = formats._TroveClassifier()
        validator._disable_download()
        assert validator("Made Up :: Classifier") is True
        assert not validator.downloaded
        assert validator("Other Made Up :: Classifier") is True
        assert not validator.downloaded

    def test_always_valid_after_download_error(self, monkeypatch):
        def _failed_download():
            raise OSError()

        monkeypatch.setattr(formats, "_download_classifiers", _failed_download)
        validator = formats._TroveClassifier()
        assert validator("Made Up :: Classifier") is True
        assert not validator.downloaded
        assert validator("Other Made Up :: Classifier") is True
        assert not validator.downloaded


@pytest.mark.skip(reason="no downloads allowed while package build")
def test_private_classifier():
    assert formats.trove_classifier("private :: Keep Off PyPI") is True
    assert formats.trove_classifier("private:: Keep Off PyPI") is False