-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathplugin.py
115 lines (94 loc) · 3.24 KB
/
plugin.py
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
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
import os.path
import sys
import warnings
import io
import pytest
from functools import reduce
from pytest_variables import errors
def default(module, path, loader):
with io.open(path, "r", encoding="utf8") as f:
try:
return module.load(f, Loader=getattr(module, loader))
except (AttributeError, TypeError): # Module is not yaml or no loader
return module.load(f)
variables_key = pytest.StashKey[dict]()
parser_table = {
"json": ("json", default),
"hjson": ("hjson", default),
"yml": ("yaml", default),
"yaml": ("yaml", default),
"toml": ("toml", default),
}
def import_parser(path, import_type, parser_func, loader=None):
try:
__import__(import_type)
mod = sys.modules[import_type]
except ImportError:
sys.exit(
"{0} import error, please make sure that {0} is "
"installed".format(import_type)
)
return parser_func(mod, path, loader)
def pytest_addoption(parser):
parser.addini(
"yaml_loader",
default="FullLoader",
help="Which loader to use when parsing yaml",
)
group = parser.getgroup("debugconfig")
group.addoption(
"--variables",
action="append",
default=[],
metavar="path",
help="path to variables file.",
)
def _merge(a, b, path=None):
"""merges b and a configurations.
Based on http://bit.ly/2uFUHgb
"""
if path is None:
path = []
for key in b:
if key in a:
if isinstance(a[key], dict) and isinstance(b[key], dict):
_merge(a[key], b[key], path + [str(key)])
elif a[key] == b[key]:
pass # same leaf value
else:
# b wins
a[key] = b[key]
else:
a[key] = b[key]
return a
def pytest_configure(config):
config.stash[variables_key] = {}
paths = config.getoption("variables")
loader = config.getini("yaml_loader")
for path in paths:
ext = os.path.splitext(path)[1][1:].lower() or "json"
try:
import_type, parser_func = parser_table[ext]
variables = import_parser(path, import_type, parser_func, loader)
except KeyError:
warnings.warn(
UserWarning(
"Could not find a parser for the file extension '{0}'. "
"Supported extensions are: {1}".format(
ext, ", ".join(sorted(parser_table.keys()))
)
)
)
variables = import_parser(path, *parser_table["json"])
except ValueError as e:
raise errors.ValueError("Unable to parse {0}: {1}".format(path, e))
if not isinstance(variables, dict):
raise errors.ValueError("Unable to parse {0}".format(path))
reduce(_merge, [config.stash[variables_key], variables])
@pytest.fixture(scope="session")
def variables(pytestconfig):
"""Provide test variables from a specified file"""
return pytestconfig.stash[variables_key]