File: test_cli_parse.py

package info (click to toggle)
python-check-jsonschema 0.34.1%2Breally0.25.0-1
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 2,232 kB
  • sloc: python: 3,890; makefile: 4
file content (269 lines) | stat: -rw-r--r-- 7,357 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
from __future__ import annotations

from unittest import mock

import click
import pytest
from click.testing import CliRunner

from check_jsonschema import main as cli_main
from check_jsonschema.cli.parse_result import ParseResult, SchemaLoadingMode


class BoxedContext:
    ref = None


@pytest.fixture
def boxed_context():
    return BoxedContext()


@pytest.fixture
def mock_parse_result():
    args = ParseResult()
    with mock.patch("check_jsonschema.cli.main_command.ParseResult") as m:
        m.return_value = args
        yield args


@pytest.fixture(autouse=True)
def mock_cli_exec(boxed_context):
    def get_ctx(*args):
        boxed_context.ref = click.get_current_context()

    with mock.patch(
        "check_jsonschema.cli.main_command.execute", side_effect=get_ctx
    ) as m:
        yield m


@pytest.fixture
def runner() -> CliRunner:
    return CliRunner(mix_stderr=False)


@pytest.mark.parametrize(
    "schemafile,builtin_schema,check_metaschema,expect_mode",
    [
        ("foo.json", None, False, SchemaLoadingMode.filepath),
        (None, "foo", False, SchemaLoadingMode.builtin),
        (None, None, True, SchemaLoadingMode.metaschema),
    ],
)
def test_parse_result_set_schema(
    schemafile, builtin_schema, check_metaschema, expect_mode
):
    args = ParseResult()
    # starts as None (always)
    assert args.schema_path is None

    args.set_schema(schemafile, builtin_schema, check_metaschema)
    assert args.schema_mode == expect_mode
    if schemafile:
        assert args.schema_path == schemafile
    if builtin_schema:
        assert args.schema_path == builtin_schema
    if check_metaschema:
        assert args.schema_path is None


def test_requires_some_args(runner):
    result = runner.invoke(cli_main, [])
    assert result.exit_code == 2


def test_schemafile_and_instancefile(runner, mock_parse_result):
    runner.invoke(cli_main, ["--schemafile", "schema.json", "foo.json"])
    assert mock_parse_result.schema_mode == SchemaLoadingMode.filepath
    assert mock_parse_result.schema_path == "schema.json"
    assert mock_parse_result.instancefiles == ("foo.json",)


def test_requires_at_least_one_instancefile(runner):
    result = runner.invoke(cli_main, ["--schemafile", "schema.json"])
    assert result.exit_code == 2


def test_requires_schemafile(runner):
    result = runner.invoke(cli_main, ["foo.json"])
    assert result.exit_code == 2


def test_no_cache_defaults_false(runner, mock_parse_result):
    runner.invoke(cli_main, ["--schemafile", "schema.json", "foo.json"])
    assert mock_parse_result.disable_cache is False


def test_no_cache_flag_is_true(runner, mock_parse_result):
    runner.invoke(cli_main, ["--schemafile", "schema.json", "foo.json", "--no-cache"])
    assert mock_parse_result.disable_cache is True


@pytest.mark.parametrize(
    "cmd_args",
    [
        [
            "--schemafile",
            "x.json",
            "--builtin-schema",
            "vendor.travis",
            "foo.json",
        ],
        [
            "--schemafile",
            "x.json",
            "--builtin-schema",
            "vendor.travis",
            "--check-metaschema",
            "foo.json",
        ],
        [
            "--schemafile",
            "x.json",
            "--check-metaschema",
            "foo.json",
        ],
        [
            "--builtin-schema",
            "vendor.travis",
            "--check-metaschema",
            "foo.json",
        ],
    ],
)
def test_mutex_schema_opts(runner, cmd_args):
    result = runner.invoke(cli_main, cmd_args)
    assert result.exit_code == 2
    assert "are mutually exclusive" in result.stderr


@pytest.mark.parametrize(
    "cmd_args",
    [
        ["--version"],
        ["--help"],
        ["-h"],
    ],
)
def test_supports_common_option(runner, cmd_args):
    result = runner.invoke(cli_main, cmd_args)
    assert result.exit_code == 0


@pytest.mark.parametrize(
    "setting,expect_value", [(None, None), ("1", False), ("0", False)]
)
def test_no_color_env_var(runner, monkeypatch, setting, expect_value, boxed_context):
    if setting is None:
        monkeypatch.delenv("NO_COLOR", raising=False)
    else:
        monkeypatch.setenv("NO_COLOR", setting)

    runner.invoke(cli_main, ["--schemafile", "schema.json", "foo.json"])
    assert boxed_context.ref.color == expect_value


@pytest.mark.parametrize(
    "setting,expected_value",
    [(None, None), ("auto", None), ("always", True), ("never", False)],
)
def test_color_cli_option(runner, setting, expected_value, boxed_context):
    args = ["--schemafile", "schema.json", "foo.json"]
    if setting:
        args.extend(("--color", setting))
    runner.invoke(cli_main, args)
    assert boxed_context.ref.color == expected_value


def test_no_color_env_var_overrides_cli_option(
    runner, monkeypatch, mock_cli_exec, boxed_context
):
    monkeypatch.setenv("NO_COLOR", "1")
    runner.invoke(
        cli_main, ["--color=always", "--schemafile", "schema.json", "foo.json"]
    )
    assert boxed_context.ref.color is False


@pytest.mark.parametrize(
    "setting,expected_value",
    [("auto", 0), ("always", 0), ("never", 0), ("anything_else", 2)],
)
def test_color_cli_option_is_choice(runner, setting, expected_value):
    assert (
        runner.invoke(
            cli_main, ["--color", setting, "--schemafile", "schema.json", "foo.json"]
        ).exit_code
        == expected_value
    )


def test_formats_default_to_enabled(runner, mock_parse_result):
    runner.invoke(cli_main, ["--schemafile", "schema.json", "foo.json"])
    assert mock_parse_result.disable_all_formats is False
    assert mock_parse_result.disable_formats == ()


@pytest.mark.parametrize(
    "addargs",
    (
        [
            "--disable-formats",
            "uri-reference",
            "--disable-formats",
            "date-time",
        ],
        ["--disable-formats", "uri-reference,date-time"],
    ),
)
def test_disable_selected_formats(runner, mock_parse_result, addargs):
    runner.invoke(
        cli_main,
        [
            "--schemafile",
            "schema.json",
            "foo.json",
        ]
        + addargs,
    )
    assert mock_parse_result.disable_all_formats is False
    assert set(mock_parse_result.disable_formats) == {"uri-reference", "date-time"}


@pytest.mark.parametrize(
    "addargs",
    (
        [
            "--disable-formats",
            "uri-reference",
            "--disable-formats",
            "date-time",
            "--disable-formats",
            "*",
        ],
        ["--disable-formats", "*"],
        ["--disable-formats", "*,email"],
    ),
)
def test_disable_all_formats(runner, mock_parse_result, addargs):
    # this should be an override, with or without other args
    runner.invoke(
        cli_main,
        [
            "--schemafile",
            "schema.json",
            "foo.json",
        ]
        + addargs,
    )
    assert mock_parse_result.disable_all_formats is True


def test_disable_format_deprecated_flag(runner, mock_parse_result):
    # this should be an override, with or without other args
    with pytest.warns(UserWarning, match="'--disable-format' is deprecated"):
        runner.invoke(
            cli_main, ["--schemafile", "schema.json", "foo.json", "--disable-format"]
        )
    assert mock_parse_result.disable_all_formats is True