Skip to content

Commit f600d16

Browse files
Add empty file tests for all KeyValueFileManagers (TOML, YAML, INI)
Co-authored-by: nathanjmcdougall <18602289+nathanjmcdougall@users.noreply.github.com> Agent-Logs-Url: https://github.com/usethis-python/usethis-python/sessions/2248f896-c78b-417b-b5ef-a5379fdd9d5d
1 parent 9729cbd commit f600d16

3 files changed

Lines changed: 181 additions & 0 deletions

File tree

tests/usethis/_file/ini/test_ini_io_.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,24 @@ def relative_path(self) -> Path:
4242
pass
4343

4444
class TestReadFile:
45+
def test_empty_file(self, tmp_path: Path):
46+
# Arrange
47+
class MyINIFileManager(INIFileManager):
48+
@property
49+
@override
50+
def relative_path(self) -> Path:
51+
return Path("valid.ini")
52+
53+
valid_file = tmp_path / "valid.ini"
54+
valid_file.touch()
55+
56+
# Act
57+
with change_cwd(tmp_path), MyINIFileManager() as manager:
58+
result = manager.read_file()
59+
60+
# Assert
61+
assert list(result.sections()) == []
62+
4563
def test_invalid_file(self, tmp_path: Path):
4664
# Arrange
4765
class MyINIFileManager(INIFileManager):
@@ -162,6 +180,42 @@ def relative_path(self) -> Path:
162180
assert result["section"]["key"].value == "value"
163181

164182
class TestContains:
183+
def test_empty_file_no_keys(self, tmp_path: Path):
184+
# Arrange
185+
class MyINIFileManager(INIFileManager):
186+
@property
187+
@override
188+
def relative_path(self) -> Path:
189+
return Path("valid.ini")
190+
191+
valid_file = tmp_path / "valid.ini"
192+
valid_file.touch()
193+
194+
# Act
195+
with change_cwd(tmp_path), MyINIFileManager() as manager:
196+
result = [] in manager
197+
198+
# Assert
199+
assert result is True
200+
201+
def test_empty_file_section_missing(self, tmp_path: Path):
202+
# Arrange
203+
class MyINIFileManager(INIFileManager):
204+
@property
205+
@override
206+
def relative_path(self) -> Path:
207+
return Path("valid.ini")
208+
209+
valid_file = tmp_path / "valid.ini"
210+
valid_file.touch()
211+
212+
# Act
213+
with change_cwd(tmp_path), MyINIFileManager() as manager:
214+
result = ["section"] in manager
215+
216+
# Assert
217+
assert result is False
218+
165219
def test_option_key_exists(self, tmp_path: Path):
166220
# Arrange
167221
class MyINIFileManager(INIFileManager):
@@ -362,6 +416,24 @@ def relative_path(self) -> Path:
362416
assert result is True
363417

364418
class TestGetItem:
419+
def test_empty_file(self, tmp_path: Path):
420+
# Arrange
421+
class MyINIFileManager(INIFileManager):
422+
@property
423+
@override
424+
def relative_path(self) -> Path:
425+
return Path("valid.ini")
426+
427+
valid_file = tmp_path / "valid.ini"
428+
valid_file.touch()
429+
430+
# Act
431+
with change_cwd(tmp_path), MyINIFileManager() as manager:
432+
result = manager[[]]
433+
434+
# Assert
435+
assert result == {}
436+
365437
def test_file_doesnt_exist_raises(self, tmp_path: Path):
366438
# Arrange
367439
class MyINIFileManager(INIFileManager):

tests/usethis/_file/toml/test_toml_io_.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,34 @@ def relative_path(self) -> Path:
4747
with change_cwd(tmp_path), MyTOMLFileManager() as manager:
4848
assert [] in manager
4949

50+
def test_empty_file_no_keys(self, tmp_path: Path) -> None:
51+
# Arrange
52+
class MyTOMLFileManager(TOMLFileManager):
53+
@property
54+
@override
55+
def relative_path(self) -> Path:
56+
return Path("pyproject.toml")
57+
58+
(tmp_path / "pyproject.toml").touch()
59+
60+
# Act, Assert
61+
with change_cwd(tmp_path), MyTOMLFileManager() as manager:
62+
assert [] in manager
63+
64+
def test_empty_file_key_missing(self, tmp_path: Path) -> None:
65+
# Arrange
66+
class MyTOMLFileManager(TOMLFileManager):
67+
@property
68+
@override
69+
def relative_path(self) -> Path:
70+
return Path("pyproject.toml")
71+
72+
(tmp_path / "pyproject.toml").touch()
73+
74+
# Act, Assert
75+
with change_cwd(tmp_path), MyTOMLFileManager() as manager:
76+
assert ["a"] not in manager
77+
5078
def test_list_of_keys_not_in_scalar(self, tmp_path: Path) -> None:
5179
# Arrange
5280
class MyTOMLFileManager(TOMLFileManager):
@@ -79,6 +107,20 @@ def relative_path(self) -> Path:
79107
with change_cwd(tmp_path), MyTOMLFileManager() as manager:
80108
assert manager[[]] == {"tool": {"usethis": {"a": "b"}}}
81109

110+
def test_empty_file(self, tmp_path: Path) -> None:
111+
# Arrange
112+
class MyTOMLFileManager(TOMLFileManager):
113+
@property
114+
@override
115+
def relative_path(self) -> Path:
116+
return Path("pyproject.toml")
117+
118+
(tmp_path / "pyproject.toml").touch()
119+
120+
# Act, Assert
121+
with change_cwd(tmp_path), MyTOMLFileManager() as manager:
122+
assert manager[[]] == {}
123+
82124
def test_scalar_not_mapping(self, tmp_path: Path) -> None:
83125
# Arrange
84126
class MyTOMLFileManager(TOMLFileManager):

tests/usethis/_file/yaml/test_yaml_io_.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,25 @@ def relative_path(self) -> Path:
9797
assert isinstance(manager._content, YAMLDocument)
9898
assert isinstance(manager._content.content, CommentedMap)
9999

100+
def test_empty_file(self, tmp_path: Path):
101+
# Arrange
102+
class MyYAMLFileManager(YAMLFileManager):
103+
@property
104+
@override
105+
def relative_path(self) -> Path:
106+
return Path("my_yaml_file.yaml")
107+
108+
(tmp_path / "my_yaml_file.yaml").touch()
109+
110+
with change_cwd(tmp_path), MyYAMLFileManager() as manager:
111+
# Act
112+
manager.read_file()
113+
114+
# Assert
115+
assert isinstance(manager._content, YAMLDocument)
116+
assert isinstance(manager._content.content, CommentedMap)
117+
assert len(manager._content.content) == 0
118+
100119
def test_file_not_found(self, tmp_path: Path):
101120
# Arrange
102121
class MyYAMLFileManager(YAMLFileManager):
@@ -239,6 +258,38 @@ def relative_path(self) -> Path:
239258
# Act, Assert
240259
assert manager.__contains__(["key"])
241260

261+
def test_empty_file_no_keys(self, tmp_path: Path):
262+
# Arrange
263+
class MyYAMLFileManager(YAMLFileManager):
264+
@property
265+
@override
266+
def relative_path(self) -> Path:
267+
return Path("my_yaml_file.yaml")
268+
269+
(tmp_path / "my_yaml_file.yaml").touch()
270+
271+
with change_cwd(tmp_path), MyYAMLFileManager() as manager:
272+
manager.read_file()
273+
274+
# Act, Assert
275+
assert manager.__contains__([])
276+
277+
def test_empty_file_key_missing(self, tmp_path: Path):
278+
# Arrange
279+
class MyYAMLFileManager(YAMLFileManager):
280+
@property
281+
@override
282+
def relative_path(self) -> Path:
283+
return Path("my_yaml_file.yaml")
284+
285+
(tmp_path / "my_yaml_file.yaml").touch()
286+
287+
with change_cwd(tmp_path), MyYAMLFileManager() as manager:
288+
manager.read_file()
289+
290+
# Act, Assert
291+
assert not manager.__contains__(["key"])
292+
242293
def test_single_map_two_keys(self, tmp_path: Path):
243294
# Arrange
244295
class MyYAMLFileManager(YAMLFileManager):
@@ -344,6 +395,22 @@ def relative_path(self) -> Path:
344395
# Assert
345396
assert value == "value"
346397

398+
def test_empty_file(self, tmp_path: Path):
399+
# Arrange
400+
class MyYAMLFileManager(YAMLFileManager):
401+
@property
402+
@override
403+
def relative_path(self) -> Path:
404+
return Path("my_yaml_file.yaml")
405+
406+
(tmp_path / "my_yaml_file.yaml").touch()
407+
408+
with change_cwd(tmp_path), MyYAMLFileManager() as manager:
409+
manager.read_file()
410+
411+
# Act, Assert
412+
assert manager[[]] == {}
413+
347414
def test_empty_keys(self, tmp_path: Path):
348415
# Arrange
349416
class MyYAMLFileManager(YAMLFileManager):

0 commit comments

Comments
 (0)