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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
|
from pathlib import Path
import pytest
from config_file.config_file import ConfigFile
from config_file.parsers.base_parser import BaseParser
@pytest.mark.parametrize(
"file_contents, file_name", [("[calendar]\nsunday_index = 0\n\n", "config.ini")]
)
def test_that_config_file_initializes_correctly(tmp_path, file_contents, file_name):
temp = tmp_path / file_name
temp.write_text(file_contents)
config = ConfigFile(temp)
assert config.path == str(temp)
assert config.contents == temp.read_text()
assert isinstance(config.parser, BaseParser)
@pytest.mark.parametrize(
"file_contents, file_name, original_file_name",
[("[calendar]\nsunday_index = 0\n\n", "config.ini", "config.original.ini")],
)
def test_that_config_file_can_restore_the_original(
tmpdir, file_contents, file_name, original_file_name
):
config_path = tmpdir / file_name
config_path.write_text(file_contents, encoding="utf-8")
original_config_path = tmpdir / original_file_name
original_config_path.write_text(file_contents, encoding="utf-8")
config = ConfigFile(str(config_path))
config.set("calendar.sunday_index", 0)
config.restore_original()
assert config.stringify() == ConfigFile(str(original_config_path)).stringify()
assert config_path.read_text(encoding="utf-8") == original_config_path.read_text(
encoding="utf-8"
)
@pytest.mark.parametrize(
"file_extension, specify_original_config_path", [("ini", False), ("ini", True)]
)
def test_missing_config_files_during_restore(
tmpdir, file_extension, specify_original_config_path
):
with pytest.raises(OSError):
temp_path = tmpdir / f"config.{file_extension}"
temp_path.write("")
config = ConfigFile(str(temp_path))
if specify_original_config_path:
specify_original_config_path = f"does_not_exist.{file_extension}"
else:
config.path = f"does_not_exist.{file_extension}"
specify_original_config_path = None
config.restore_original(original_file_path=specify_original_config_path)
@pytest.mark.parametrize(
"file_contents, file_name", [("[calendar]\nsunday_index = 0\n\n", "config.ini")]
)
def test_that_config_file_can_save(tmpdir, file_contents, file_name):
config_path = tmpdir / file_name
config_path.write_text(file_contents, encoding="utf-8")
config = ConfigFile(str(config_path))
config.set("calendar.sunday_index", 1)
config.save()
assert config.stringify() == Path(config_path).read_text(encoding="utf-8")
@pytest.mark.parametrize(
"section_key, value, parse_type, file_name, file_contents",
[
(
"calendar.sunday_index",
0,
True,
"config.ini",
"[calendar]\nsunday_index = 0\n\n",
),
(
"calendar.sunday_index",
"0",
False,
"config.ini",
"[calendar]\nsunday_index = 0\n\n",
),
],
)
def test_that_config_file_can_get(
tmpdir, section_key, value, parse_type, file_name, file_contents
):
config_path = tmpdir / file_name
config_path.write_text(file_contents, encoding="utf-8")
config = ConfigFile(str(config_path))
assert config.get(section_key, parse_type=parse_type) == value
@pytest.mark.parametrize(
"file_extension, file_contents, section_key, expected_result",
[
("ini", "[calendar]\nsunday_index = 0\n\n", "calendar.sunday_index", True),
("ini", "[calendar]\nsunday_index = 0\n\n", "calendar.blah", False),
("ini", "[calendar]\nsunday_index = 0\n\n", "calendar", True),
],
)
def test_that_config_file_can_find_sections_and_keys(
tmpdir, file_extension, file_contents, section_key, expected_result
):
config_path = tmpdir / f"config.{file_extension}"
config_path.write_text(file_contents, encoding="utf-8")
config = ConfigFile(str(config_path))
assert config.has(section_key) == expected_result
@pytest.mark.parametrize(
"section_key, file_name, file_contents, deleted_file_contents",
[
(
"calendar.sunday_index",
"config.ini",
"[calendar]\nsunday_index = 0\n\n",
"[calendar]\n\n",
),
],
)
def test_that_config_file_can_delete(
tmpdir, section_key, file_name, file_contents, deleted_file_contents
):
config_path = tmpdir / file_name
config_path.write_text(file_contents, encoding="utf-8")
config = ConfigFile(str(config_path))
config.delete(section_key)
assert config.stringify() == deleted_file_contents
@pytest.mark.parametrize(
"path, is_dir", [("invalid", False), ("invalid", True), ("invalid.conf", False)]
)
def test_invalid_config_paths(tmpdir, path, is_dir):
with pytest.raises(ValueError):
temp_path = tmpdir / path
temp_path.write("")
ConfigFile(str(tmpdir)) if is_dir else ConfigFile(str(temp_path))
def test_that_custom_parser_can_be_used(tmpdir):
class CustomParser(BaseParser):
def __init__(self, file_contents):
super().__init__(file_contents)
def get(self, key, parse_type=True):
return key
def set(self, key, value):
return key, value
def delete(self, section_key):
return section_key
def has(self, section_key: str) -> bool:
return True
def stringify(self) -> str:
return str(self.parsed_content)
def parse(self, file_contents: str):
return file_contents
config_path = tmpdir / "config.conf"
config_path.write_text("", encoding="utf-8")
config = ConfigFile(str(config_path), parser=CustomParser)
assert isinstance(config.parser, CustomParser)
assert config.get("key") == "key"
assert config.set("key", "value") == ("key", "value")
assert config.delete("section_key") == "section_key"
assert config.has("blah")
assert config.stringify() == ""
def test_that_a_tidle_in_the_config_path_expands_to_the_absolute_path():
config_name = "fjlajbkfdajfdioanfks.ini"
config_path = "~/" + config_name
try:
Path(config_path).expanduser().touch()
assert ConfigFile(config_path).path == str(Path.home() / config_name)
finally:
Path(config_path).expanduser().unlink()
|