File: test_schema.py

package info (click to toggle)
scikit-build-core 0.11.6-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,588 kB
  • sloc: python: 14,643; ansic: 254; cpp: 134; sh: 27; fortran: 18; makefile: 7
file content (130 lines) | stat: -rw-r--r-- 3,655 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
from __future__ import annotations

from pathlib import Path
from typing import Any

import pytest

from scikit_build_core._compat import tomllib
from scikit_build_core.settings.skbuild_schema import (
    generate_skbuild_schema,
    get_skbuild_schema,
)

DIR = Path(__file__).parent.resolve()

pytestmark = pytest.mark.upstream


def test_compare_schemas():
    """
    Should be the same. If not, run nox -s generate_schema
    """

    assert generate_skbuild_schema() == get_skbuild_schema()


SCHEMAS = [
    *DIR.parent.joinpath("docs/examples").glob("**/pyproject.toml"),
    *DIR.joinpath("packages").glob("**/pyproject.toml"),
]


@pytest.mark.parametrize(
    "filepath", SCHEMAS, ids=lambda x: str(x.relative_to(DIR.parent).parent)
)
def test_valid_schemas_files(filepath: Path) -> None:
    api = pytest.importorskip("validate_pyproject.api")

    with filepath.open("rb") as f:
        example = tomllib.load(f)

    validator = api.Validator()
    assert validator(example) is not None


@pytest.mark.parametrize(
    "addition",
    [
        {"minimum-version": 0.3},
        {"random": "not valid"},
        {"logging": {"level": "POODLE"}},
        {"generate": [{"path": "CMakeLists.txt"}]},
        {"generate": [{"path": "me.py", "template": "hi", "template-path": "hello"}]},
        {"generate": [{"path": "me.py", "template": ""}]},
        {"overrides": [{"cmake": {"args": ["-DFOO=BAR"]}}]},
        {"overrides": [{"select": {"python-version": ">=3.10"}}]},
        {
            "overrides": [
                {
                    "if": {"python-version": ">=3.10"},
                    "cmake.nonexist": ["-DFOO=BAR"],
                }
            ]
        },
        {"metadata": {"version": {"provider-path": True}}},
        {"metadata": {"version": {"provider": 2}}},
        {"metadata": {"invalid": {"provider": "correct"}}},
        {"cmake": {"define": {"FOO": {"env": ""}}}},
        {"cmake": {"define": {"FOO": {"default": False}}}},
    ],
)
def test_invalid_schemas(addition: dict[str, Any]) -> None:
    fastjsonschema = pytest.importorskip("fastjsonschema")
    api = pytest.importorskip("validate_pyproject.api")

    example_toml = """\
    [project]
    name = "myproj"
    version = "0"

    [tool.scikit-build]
    minimum-version = "0.3"
    """

    example = tomllib.loads(example_toml)
    example["tool"]["scikit-build"].update(**addition)

    validator = api.Validator()
    print(example)
    with pytest.raises(fastjsonschema.JsonSchemaValueException):
        validator(example)


@pytest.mark.parametrize(
    "addition",
    [
        {"generate": [{"path": "CMakeLists.txt", "template": "hi"}]},
        {"generate": [{"path": "me.py", "template-path": "hello"}]},
        {
            "overrides": [
                {
                    "if": {"python-version": ">=3.10"},
                    "cmake": {"args": ["-DFOO=BAR"]},
                }
            ]
        },
        {"metadata": {"version": {"provider-path": "string"}}},
        {"metadata": {"description": {"anything": True}}},
        {"cmake": {"define": {"FOO": "BAR"}}},
        {"cmake": {"define": {"FOO": {"env": "FOO"}}}},
        {"cmake": {"define": {"FOO": {"env": "FOO", "default": False}}}},
    ],
)
def test_valid_schemas(addition: dict[str, Any]) -> None:
    api = pytest.importorskip("validate_pyproject.api")

    example_toml = """\
    [project]
    name = "myproj"
    version = "0"

    [tool.scikit-build]
    minimum-version = "0.3"
    """

    example = tomllib.loads(example_toml)
    example["tool"]["scikit-build"].update(**addition)

    validator = api.Validator()
    validator(example)