File: test_examples.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 (54 lines) | stat: -rw-r--r-- 1,809 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
import copy
import logging
from pathlib import Path

import pytest

from validate_pyproject import _tomllib as tomllib
from validate_pyproject import api, cli
from validate_pyproject.error_reporting import ValidationError

from .helpers import error_file, get_tools, get_tools_as_args


def test_examples_api(example: Path) -> None:
    load_tools = get_tools(example)

    toml_equivalent = tomllib.loads(example.read_text())
    copy_toml = copy.deepcopy(toml_equivalent)
    validator = api.Validator(extra_plugins=load_tools)
    assert validator(toml_equivalent) is not None
    assert toml_equivalent == copy_toml


def test_examples_cli(example: Path) -> None:
    args = get_tools_as_args(example)

    assert cli.run(["--dump-json", str(example), *args]) == 0  # no errors


def test_invalid_examples_api(invalid_example: Path) -> None:
    load_tools = get_tools(invalid_example)

    expected_error = error_file(invalid_example).read_text("utf-8")
    toml_equivalent = tomllib.loads(invalid_example.read_text())
    validator = api.Validator(extra_plugins=load_tools)
    with pytest.raises(ValidationError) as exc_info:
        validator(toml_equivalent)
    exception_message = str(exc_info.value)
    summary = exc_info.value.summary
    for error in expected_error.splitlines():
        assert error in exception_message
        assert error in summary


def test_invalid_examples_cli(invalid_example: Path, caplog) -> None:
    args = get_tools_as_args(invalid_example)

    caplog.set_level(logging.DEBUG)
    expected_error = error_file(invalid_example).read_text("utf-8")
    with pytest.raises(SystemExit) as exc_info:
        cli.main([str(invalid_example), *args])
    assert exc_info.value.args == (1,)
    for error in expected_error.splitlines():
        assert error in caplog.text