Skip to content

Commit c97435d

Browse files
committed
test(EntryPoint): added test for config_path being set according to XDG_CONFIG_HOME
1 parent ee91c93 commit c97435d

File tree

2 files changed

+39
-8
lines changed

2 files changed

+39
-8
lines changed

mac_cleanup/main.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,16 @@
1010

1111

1212
class EntryPoint:
13-
if (config_home := environ.get("XDG_CONFIG_HOME")) is not None:
14-
config_path = Path(config_home).expanduser().joinpath("mac_cleanup_py").joinpath("config.toml")
15-
else:
16-
config_path = Path.home().joinpath(".mac_cleanup_py")
13+
config_path: Path
14+
base_collector: _Collector
1715

18-
base_collector = _Collector()
16+
def __init__(self):
17+
if (config_home := environ.get("XDG_CONFIG_HOME")) is not None:
18+
self.config_path = Path(config_home).expanduser().joinpath("mac_cleanup_py").joinpath("config.toml")
19+
else:
20+
self.config_path = Path.home().joinpath(".mac_cleanup_py")
21+
22+
self.base_collector = _Collector()
1923

2024
@staticmethod
2125
def count_free_space() -> float:

tests/test_main.py

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ def dummy_count_free_space(entry_self: EntryPoint) -> float: # noqa
7575
monkeypatch.setattr("mac_cleanup.config.Config.__init__", dummy_config_init)
7676
monkeypatch.setattr("mac_cleanup.config.Config.__call__", dummy_config_call)
7777

78+
# Create EntryPoint and mock it
79+
mock_entry_point = EntryPoint()
80+
monkeypatch.setattr(EntryPoint, "__new__", lambda: mock_entry_point)
81+
7882
# Simulate count_free_space results
7983
monkeypatch.setattr(EntryPoint, "count_free_space", dummy_count_free_space)
8084

@@ -86,7 +90,7 @@ def dummy_count_free_space(entry_self: EntryPoint) -> float: # noqa
8690
]
8791

8892
# Simulate execution list in BaseCollector
89-
monkeypatch.setattr(EntryPoint.base_collector, "_execute_list", dummy_execute_list)
93+
monkeypatch.setattr(mock_entry_point.base_collector, "_execute_list", dummy_execute_list)
9094

9195
# Call entrypoint
9296
main()
@@ -128,8 +132,12 @@ def dummy_config_call(config_path_: Pathlib, configuration_prompted: bool) -> No
128132
monkeypatch.setattr("mac_cleanup.config.Config.__init__", dummy_config_init)
129133
monkeypatch.setattr("mac_cleanup.config.Config.__call__", dummy_config_call)
130134

135+
# Create EntryPoint and mock it
136+
mock_entry_point = EntryPoint()
137+
monkeypatch.setattr(EntryPoint, "__new__", lambda: mock_entry_point)
138+
131139
# Simulate count_dry with predefined result
132-
monkeypatch.setattr(EntryPoint.base_collector, "_count_dry", dummy_count_dry)
140+
monkeypatch.setattr(mock_entry_point.base_collector, "_count_dry", dummy_count_dry)
133141

134142
# Simulate empty cleanup
135143
monkeypatch.setattr(EntryPoint, "cleanup", dummy_cleanup)
@@ -177,8 +185,12 @@ def dummy_input(*args: Any, **kwargs: Any) -> None: # noqa
177185
monkeypatch.setattr("mac_cleanup.config.Config.__init__", dummy_config_init)
178186
monkeypatch.setattr("mac_cleanup.config.Config.__call__", dummy_config_call)
179187

188+
# Create EntryPoint and mock it
189+
mock_entry_point = EntryPoint()
190+
monkeypatch.setattr(EntryPoint, "__new__", lambda: mock_entry_point)
191+
180192
# Simulate count_dry with predefined result
181-
monkeypatch.setattr(EntryPoint.base_collector, "_count_dry", dummy_count_dry)
193+
monkeypatch.setattr(mock_entry_point.base_collector, "_count_dry", dummy_count_dry)
182194

183195
# Simulate dry run was prompted
184196
monkeypatch.setattr("mac_cleanup.parser.Args.dry_run", True)
@@ -196,3 +208,18 @@ def dummy_input(*args: Any, **kwargs: Any) -> None: # noqa
196208
# Check error message and exit message
197209
assert "Do not enter symbols that can't be decoded to UTF-8" in captured_stdout
198210
assert "Exiting..." in captured_stdout
211+
212+
@pytest.mark.parametrize("xdg_env_set", [True, False])
213+
def test_config_home(self, xdg_env_set: bool, monkeypatch: MonkeyPatch):
214+
"""Test xdg and default config in :class:`mac_cleanup.main.EntryPoint`"""
215+
216+
expected_path: str
217+
218+
if xdg_env_set:
219+
monkeypatch.setenv("XDG_CONFIG_HOME", "config_home")
220+
expected_path = "config_home/mac_cleanup_py/config.toml"
221+
else:
222+
monkeypatch.setattr("pathlib.Path.home", lambda: Pathlib("home"))
223+
expected_path = "home/.mac_cleanup_py"
224+
225+
assert str(EntryPoint().config_path) == expected_path

0 commit comments

Comments
 (0)