summaryrefslogtreecommitdiff
path: root/lib/spack/spack/test/schema.py
blob: 916e61cf26c8216c6c60b3ee0663e6ac959c7f9c (plain) (blame)
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
# Copyright 2013-2023 Lawrence Livermore National Security, LLC and other
# Spack Project Developers. See the top-level COPYRIGHT file for details.
#
# SPDX-License-Identifier: (Apache-2.0 OR MIT)

import json
import os.path

import jsonschema
import pytest

import spack.paths
import spack.schema


@pytest.fixture()
def validate_spec_schema():
    return {
        "type": "object",
        "validate_spec": True,
        "patternProperties": {r"\w[\w-]*": {"type": "string"}},
    }


@pytest.fixture()
def module_suffixes_schema():
    return {
        "type": "object",
        "properties": {
            "tcl": {
                "type": "object",
                "patternProperties": {
                    r"\w[\w-]*": {
                        "type": "object",
                        "properties": {
                            "suffixes": {
                                "validate_spec": True,
                                "patternProperties": {r"\w[\w-]*": {"type": "string"}},
                            }
                        },
                    }
                },
            }
        },
    }


@pytest.fixture(scope="module")
def meta_schema():
    """Meta schema for JSON schema validation (Draft 4)"""
    meta_schema_file = os.path.join(spack.paths.test_path, "data", "jsonschema_meta.json")
    with open(meta_schema_file) as f:
        ms = json.load(f)
    return ms


@pytest.mark.regression("9857")
def test_validate_spec(validate_spec_schema):
    v = spack.schema.Validator(validate_spec_schema)
    data = {"foo@3.7": "bar"}

    # Validate good data (the key is a spec)
    v.validate(data)

    # Check that invalid data throws
    data["^python@3.7@"] = "baz"
    with pytest.raises(jsonschema.ValidationError, match="unexpected tokens"):
        v.validate(data)


@pytest.mark.regression("9857")
def test_module_suffixes(module_suffixes_schema):
    v = spack.schema.Validator(module_suffixes_schema)
    data = {"tcl": {"all": {"suffixes": {"^python@2.7@": "py2.7"}}}}

    with pytest.raises(jsonschema.ValidationError, match="unexpected tokens"):
        v.validate(data)


@pytest.mark.regression("10246")
@pytest.mark.parametrize(
    "config_name",
    [
        "compilers",
        "config",
        "definitions",
        "env",
        "merged",
        "mirrors",
        "modules",
        "packages",
        "repos",
    ],
)
def test_schema_validation(meta_schema, config_name):
    import importlib

    module_name = "spack.schema.{0}".format(config_name)
    module = importlib.import_module(module_name)
    schema = getattr(module, "schema")

    # If this validation throws the test won't pass
    jsonschema.validate(schema, meta_schema)


def test_deprecated_properties(module_suffixes_schema):
    # Test that an error is reported when 'error: True'
    msg_fmt = r"deprecated properties detected [properties={properties}]"
    module_suffixes_schema["deprecatedProperties"] = {
        "properties": ["tcl"],
        "message": msg_fmt,
        "error": True,
    }
    v = spack.schema.Validator(module_suffixes_schema)
    data = {"tcl": {"all": {"suffixes": {"^python": "py"}}}}

    expected_match = "deprecated properties detected"
    with pytest.raises(jsonschema.ValidationError, match=expected_match):
        v.validate(data)

    # Test that just a warning is reported when 'error: False'
    module_suffixes_schema["deprecatedProperties"] = {
        "properties": ["tcl"],
        "message": msg_fmt,
        "error": False,
    }
    v = spack.schema.Validator(module_suffixes_schema)
    data = {"tcl": {"all": {"suffixes": {"^python": "py"}}}}
    # The next validation doesn't raise anymore
    v.validate(data)