Skip to content

Commit

Permalink
test(EntryPoint): added test for config_path being set according to X…
Browse files Browse the repository at this point in the history
…DG_CONFIG_HOME
  • Loading branch information
efa2d19 committed Sep 10, 2024
1 parent ee91c93 commit c97435d
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 8 deletions.
14 changes: 9 additions & 5 deletions mac_cleanup/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,16 @@


class EntryPoint:
if (config_home := environ.get("XDG_CONFIG_HOME")) is not None:
config_path = Path(config_home).expanduser().joinpath("mac_cleanup_py").joinpath("config.toml")
else:
config_path = Path.home().joinpath(".mac_cleanup_py")
config_path: Path
base_collector: _Collector

base_collector = _Collector()
def __init__(self):
if (config_home := environ.get("XDG_CONFIG_HOME")) is not None:
self.config_path = Path(config_home).expanduser().joinpath("mac_cleanup_py").joinpath("config.toml")
else:
self.config_path = Path.home().joinpath(".mac_cleanup_py")

self.base_collector = _Collector()

@staticmethod
def count_free_space() -> float:
Expand Down
33 changes: 30 additions & 3 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ def dummy_count_free_space(entry_self: EntryPoint) -> float: # noqa
monkeypatch.setattr("mac_cleanup.config.Config.__init__", dummy_config_init)
monkeypatch.setattr("mac_cleanup.config.Config.__call__", dummy_config_call)

# Create EntryPoint and mock it
mock_entry_point = EntryPoint()
monkeypatch.setattr(EntryPoint, "__new__", lambda: mock_entry_point)

# Simulate count_free_space results
monkeypatch.setattr(EntryPoint, "count_free_space", dummy_count_free_space)

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

# Simulate execution list in BaseCollector
monkeypatch.setattr(EntryPoint.base_collector, "_execute_list", dummy_execute_list)
monkeypatch.setattr(mock_entry_point.base_collector, "_execute_list", dummy_execute_list)

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

# Create EntryPoint and mock it
mock_entry_point = EntryPoint()
monkeypatch.setattr(EntryPoint, "__new__", lambda: mock_entry_point)

# Simulate count_dry with predefined result
monkeypatch.setattr(EntryPoint.base_collector, "_count_dry", dummy_count_dry)
monkeypatch.setattr(mock_entry_point.base_collector, "_count_dry", dummy_count_dry)

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

# Create EntryPoint and mock it
mock_entry_point = EntryPoint()
monkeypatch.setattr(EntryPoint, "__new__", lambda: mock_entry_point)

# Simulate count_dry with predefined result
monkeypatch.setattr(EntryPoint.base_collector, "_count_dry", dummy_count_dry)
monkeypatch.setattr(mock_entry_point.base_collector, "_count_dry", dummy_count_dry)

# Simulate dry run was prompted
monkeypatch.setattr("mac_cleanup.parser.Args.dry_run", True)
Expand All @@ -196,3 +208,18 @@ def dummy_input(*args: Any, **kwargs: Any) -> None: # noqa
# Check error message and exit message
assert "Do not enter symbols that can't be decoded to UTF-8" in captured_stdout
assert "Exiting..." in captured_stdout

@pytest.mark.parametrize("xdg_env_set", [True, False])
def test_config_home(self, xdg_env_set: bool, monkeypatch: MonkeyPatch):
"""Test xdg and default config in :class:`mac_cleanup.main.EntryPoint`"""

expected_path: str

if xdg_env_set:
monkeypatch.setenv("XDG_CONFIG_HOME", "config_home")
expected_path = "config_home/mac_cleanup_py/config.toml"
else:
monkeypatch.setattr("pathlib.Path.home", lambda: Pathlib("home"))
expected_path = "home/.mac_cleanup_py"

assert str(EntryPoint().config_path) == expected_path

0 comments on commit c97435d

Please sign in to comment.