-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_dconf_manager.py
169 lines (151 loc) · 3.62 KB
/
test_dconf_manager.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
from __future__ import annotations
import io
import os
import textwrap
from collections.abc import Sequence
from unittest import mock
from dconf_manager import HierarchicalSet
from dconf_manager import main
EXPECTED_OUTPUT = """\
\033[32m> add/AddedKey=1
\033[31m< clear/foo/bar/blah=50
\033[31m< overwrite/a=1
\033[32m> overwrite/a=10
\033[31m< overwrite/b=2
\033[32m> overwrite/new=5
"""
EXPECTED_OUTPUT_WITH_IGNORED = """\
\033[32m> add/AddedKey=1
\033[38;5;244m? clear/keep=5
\033[31m< clear/foo/bar/blah=50
\033[38;5;244m? clear/foo/bar/exclude/no=1
\033[38;5;244m? clear/food/hi=1
\033[38;5;244m? ignored/a=1
\033[31m< overwrite/a=1
\033[32m> overwrite/a=10
\033[31m< overwrite/b=2
\033[32m> overwrite/new=5
"""
def test_hierarchical_set() -> None:
s = HierarchicalSet[int]()
assert str(s) == ""
assert [] not in s
assert [0] not in s
s.add([1, 2, 3])
assert str(s) == textwrap.dedent(
"""\
1
2
3
*"""
)
s.add([1, 2, 3, 4])
assert str(s) == textwrap.dedent(
"""\
1
2
3
*"""
)
assert [] not in s
assert [0] not in s
assert [1] not in s
assert [1, 2] not in s
assert [1, 2, 3] in s
assert [1, 2, 3, 3] in s
assert (1, 2, 3, 4) in s
s.add((1, 2))
assert str(s) == textwrap.dedent(
"""\
1
2
*"""
)
assert [] not in s
assert [0] not in s
assert [1] not in s
assert [1, 2] in s
assert [1, 2, 3] in s
assert [1, 2, 3, 3] in s
assert (1, 2, 3, 4) in s
s.add([2])
assert str(s) == textwrap.dedent(
"""\
1
2
*
2
*"""
)
assert [] not in s
assert [0] not in s
assert [1] not in s
assert [2] in s
assert [2, 5] in s
s.add([])
assert str(s) == "*"
assert [] in s
assert [0] in s
assert [1] in s
assert [5, 6, 7, 8] in s
@mock.patch("dconf_manager.dconf_dump")
@mock.patch("dconf_manager.dconf_write")
@mock.patch("dconf_manager.dconf_reset")
def _test_main(
apply: bool,
show_ignored: bool,
reset: mock.Mock,
write: mock.Mock,
dump: mock.Mock,
) -> tuple[Sequence[tuple[object, ...]], Sequence[tuple[object, ...]], str]:
config = textwrap.dedent(
"""\
[ignored]
a=1
[overwrite]
a=1
b=2
[clear]
keep=5
[clear/foo/bar]
blah=50
[clear/foo/bar/exclude]
no=1
[clear/food]
hi=1
"""
)
dump.return_value = config
input = os.path.join(os.path.dirname(__file__), "test-data", "input.ini")
stdout = io.StringIO()
args = [input, "--root", "/the/root"]
if show_ignored:
args.append("--show-ignored")
if apply:
args.append("--apply")
with mock.patch("sys.stdout", stdout):
main(args)
dump.assert_called_once_with("/the/root")
return write.call_args_list, reset.call_args_list, stdout.getvalue()
def test_diff() -> None:
writes, resets, stdout = _test_main(False, False)
assert not writes
assert not resets
assert stdout == EXPECTED_OUTPUT
def test_diff_with_ignored() -> None:
writes, resets, stdout = _test_main(False, True)
assert not writes
assert not resets
assert stdout == EXPECTED_OUTPUT_WITH_IGNORED
def test_apply() -> None:
writes, resets, stdout = _test_main(True, False)
assert writes == [
mock.call("/the/root/add/AddedKey", "1"),
mock.call("/the/root/overwrite/a", "10"),
mock.call("/the/root/overwrite/new", "5"),
]
assert resets == [
mock.call("/the/root/clear/foo/bar/blah"),
mock.call("/the/root/overwrite/b"),
]
assert stdout == EXPECTED_OUTPUT