File: conftest.py

package info (click to toggle)
python-tomlkit 0.13.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 5,096 kB
  • sloc: python: 5,985; makefile: 20; sh: 5
file content (105 lines) | stat: -rw-r--r-- 2,957 bytes parent folder | download | duplicates (2)
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
import os

import pytest


@pytest.fixture
def example():
    def _example(name):
        with 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 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 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 = {
    "valid": [
        "float/inf-and-nan",  # Can't compare nan
    ]
}


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", "valid"]
    rv = {"invalid_encode": {}}
    for d in dirs:
        rv[d] = {}
        ignored = IGNORED_TESTS.get(d, [])

        for root, _, files in os.walk(os.path.join(TEST_DIR, d)):
            relpath = os.path.relpath(root, os.path.join(TEST_DIR, d))
            if relpath == ".":
                relpath = ""
            for f in files:
                try:
                    bn, ext = f.rsplit(".", 1)
                except ValueError:
                    bn, ext = f.rsplit("-", 1)
                key = f"{relpath}/{bn}"
                if ext == "multi":
                    continue
                if key in ignored:
                    continue
                if d == "invalid" and relpath == "encoding":
                    rv["invalid_encode"][bn] = os.path.join(root, f)
                    continue
                if key not in rv[d]:
                    rv[d][key] = {}
                with open(os.path.join(root, f), encoding="utf-8") as inp:
                    rv[d][key][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_encode"].values(),
            ids=list(test_list["invalid_encode"].keys()),
        )