File: conftest.py

package info (click to toggle)
python-tomlkit 0.6.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,296 kB
  • sloc: python: 3,280; sh: 5; makefile: 4
file content (95 lines) | stat: -rw-r--r-- 2,539 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
import io
import os
import pytest


@pytest.fixture
def example():
    def _example(name):
        with io.open(
            os.path.join(os.path.dirname(__file__), "examples", name + ".toml"),
            encoding="utf-8",
        ) as f:
            return f.read()

    return _example


@pytest.fixture
def json_example():
    def _example(name):
        with io.open(
            os.path.join(os.path.dirname(__file__), "examples", "json", name + ".json"),
            encoding="utf-8",
        ) as f:
            return f.read()

    return _example


@pytest.fixture
def invalid_example():
    def _example(name):
        with io.open(
            os.path.join(
                os.path.dirname(__file__), "examples", "invalid", name + ".toml"
            ),
            encoding="utf-8",
        ) as f:
            return f.read()

    return _example


TEST_DIR = os.path.join(os.path.dirname(__file__), "toml-test", "tests")
IGNORED_TESTS = {
    "invalid": [
        "array-mixed-types-strings-and-ints.toml",
        "array-mixed-types-arrays-and-ints.toml",
        "array-mixed-types-ints-and-floats.toml",
    ]
}


def get_tomltest_cases():
    dirs = sorted(
        f for f in os.listdir(TEST_DIR) if os.path.isdir(os.path.join(TEST_DIR, f))
    )
    assert dirs == ["invalid", "invalid-encoder", "valid"]
    rv = {}
    for d in dirs:
        rv[d] = {}
        ignored = IGNORED_TESTS.get(d, [])
        files = os.listdir(os.path.join(TEST_DIR, d))
        for f in files:
            if f in ignored:
                continue

            bn, ext = f.rsplit(".", 1)
            if bn not in rv[d]:
                rv[d][bn] = {}
            with io.open(os.path.join(TEST_DIR, d, f), encoding="utf-8") as inp:
                rv[d][bn][ext] = inp.read()
    return rv


def pytest_generate_tests(metafunc):
    test_list = get_tomltest_cases()
    if "valid_case" in metafunc.fixturenames:
        metafunc.parametrize(
            "valid_case",
            test_list["valid"].values(),
            ids=list(test_list["valid"].keys()),
        )
    elif "invalid_decode_case" in metafunc.fixturenames:
        metafunc.parametrize(
            "invalid_decode_case",
            test_list["invalid"].values(),
            ids=list(test_list["invalid"].keys()),
        )
    elif "invalid_encode_case" in metafunc.fixturenames:
        metafunc.parametrize(
            "invalid_encode_case",
            test_list["invalid-encoder"].values(),
            ids=list(test_list["invalid-encoder"].keys()),
        )