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
|
import textwrap
import pytest
from pkgcheck import cli
from snakeoil.cli import arghparse
class TestConfigFileParser:
@pytest.fixture(autouse=True)
def _create_argparser(self, tmp_path):
self.config_file = str(tmp_path / "config")
self.parser = arghparse.ArgumentParser()
self.namespace = arghparse.Namespace()
self.config_parser = cli.ConfigFileParser(self.parser)
def test_no_configs(self):
config = self.config_parser.parse_config(())
assert config.sections() == []
namespace = self.config_parser.parse_config_options(self.namespace)
assert vars(namespace) == {}
def test_ignored_configs(self):
# nonexistent config files are ignored
config = self.config_parser.parse_config(("foo", "bar"))
assert config.sections() == []
def test_bad_config_format(self, capsys):
with open(self.config_file, "w") as f:
f.write("foobar\n")
with pytest.raises(SystemExit) as excinfo:
self.config_parser.parse_config((self.config_file,))
out, err = capsys.readouterr()
assert not out
assert "parsing config file failed:" in err
assert excinfo.value.code == 2
def test_nonexistent_config_options(self, capsys):
"""Nonexistent parser arguments cause errors."""
with open(self.config_file, "w") as f:
f.write(
textwrap.dedent(
"""
[DEFAULT]
foo=bar
"""
)
)
with pytest.raises(SystemExit) as excinfo:
self.config_parser.parse_config_options(self.namespace, configs=[self.config_file])
out, err = capsys.readouterr()
assert not out
assert "failed loading config: unknown arguments: --foo=bar" in err
assert excinfo.value.code == 2
def test_config_options(self):
self.parser.add_argument("--foo")
with open(self.config_file, "w") as f:
f.write(
textwrap.dedent(
"""
[DEFAULT]
foo=bar
"""
)
)
namespace = self.parser.parse_args(["--foo", "foo"])
assert namespace.foo == "foo"
# config args override matching namespace attrs
namespace = self.config_parser.parse_config_options(namespace, configs=[self.config_file])
assert namespace.foo == "bar"
def test_config_checksets(self):
namespace = self.parser.parse_args([])
namespace.config_checksets = {}
# checksets section exists with no entries
with open(self.config_file, "w") as f:
f.write(
textwrap.dedent(
"""
[CHECKSETS]
"""
)
)
namespace = self.config_parser.parse_config_options(namespace, configs=[self.config_file])
assert namespace.config_checksets == {}
# checksets section with entries including empty set
with open(self.config_file, "w") as f:
f.write(
textwrap.dedent(
"""
[CHECKSETS]
set1=keyword
set2=check,-keyword
set3=
"""
)
)
namespace = self.config_parser.parse_config_options(namespace, configs=[self.config_file])
assert namespace.config_checksets == {"set1": ["keyword"], "set2": ["check", "-keyword"]}
|