Skip to content

Commit

Permalink
Add tests for getting the data directory on different platforms
Browse files Browse the repository at this point in the history
  • Loading branch information
nightlark committed Dec 11, 2024
1 parent 7ac43fc commit 22b8dc3
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
8 changes: 4 additions & 4 deletions surfactant/configmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ def _get_config_file_path(self) -> Path:
config_dir = Path(self.config_dir)
else:
if platform.system() == "Windows":
config_dir = Path(os.getenv("APPDATA", os.path.expanduser("~\\AppData\\Roaming")))
config_dir = Path(os.getenv("APPDATA", str(Path("~\\AppData\\Roaming"))))
else:
config_dir = Path(os.getenv("XDG_CONFIG_HOME", os.path.expanduser("~/.config")))
config_dir = Path(os.getenv("XDG_CONFIG_HOME", str(Path("~/.config"))))
config_dir = config_dir / self.app_name
return config_dir / "config.toml"

Expand Down Expand Up @@ -149,8 +149,8 @@ def get_data_dir_path(self) -> Path:
Path: The path to the data directory.
"""
if platform.system() == "Windows":
data_dir = Path(os.getenv("LOCALAPPDATA", os.path.expanduser("~\\AppData\\Local")))
data_dir = Path(os.getenv("LOCALAPPDATA", str(Path("~\\AppData\\Local"))))
else:
data_dir = Path(os.getenv("XDG_DATA_HOME", os.path.expanduser("~/.local/share")))
data_dir = Path(os.getenv("XDG_DATA_HOME", str(Path("~/.local/share"))))
data_dir = data_dir / self.app_name
return data_dir
28 changes: 26 additions & 2 deletions tests/config/test_configmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,19 +57,43 @@ def test_config_file_creation(config_manager):
@pytest.mark.skipif(platform.system() != "Windows", reason="Test specific to Windows platform")
def test_windows_config_path():
config_manager = ConfigManager(app_name="testapp")
config_path = config_manager._get_config_file_path() # pylint: disable=protected-access
config_path = config_manager._get_config_file_path().expanduser() # pylint: disable=protected-access
expected_config_dir = Path(os.getenv("APPDATA", str(Path("~\\AppData\\Roaming").expanduser())))
assert expected_config_dir in config_path.parents
assert config_path.parts[-2:] == ("testapp", "config.toml")
# delete instance so other tests don't accidentally use it
config_manager.delete_instance("testapp")


@pytest.mark.skipif(platform.system() == "Windows", reason="Test specific to Unix-like platforms")
def test_unix_config_path():
config_manager = ConfigManager(app_name="testapp")
config_path = config_manager._get_config_file_path() # pylint: disable=protected-access
config_path = config_manager._get_config_file_path().expanduser() # pylint: disable=protected-access
expected_config_dir = Path(os.getenv("XDG_CONFIG_HOME", str(Path("~/.config").expanduser())))
assert expected_config_dir in config_path.parents
assert config_path.parts[-2:] == ("testapp", "config.toml")
# delete instance so other tests don't accidentally use it
config_manager.delete_instance("testapp")


@pytest.mark.skipif(platform.system() != "Windows", reason="Test specific to Windows platform")
def test_windows_data_dir_path():
config_manager = ConfigManager(app_name="testapp")
data_dir = config_manager.get_data_dir_path().expanduser() # pylint: disable=protected-access
expected_data_dir = Path(os.getenv("LOCALAPPDATA", str(Path("~\\AppData\\Local").expanduser())))
assert expected_data_dir in data_dir.parents
assert data_dir.name == "testapp"
# delete instance so other tests don't accidentally use it
config_manager.delete_instance("testapp")


@pytest.mark.skipif(platform.system() == "Windows", reason="Test specific to Unix-like platforms")
def test_unix_data_dir_path():
config_manager = ConfigManager(app_name="testapp")
data_dir = config_manager.get_data_dir_path().expanduser() # pylint: disable=protected-access
expected_data_dir = Path(os.getenv("XDG_DATA_HOME", str(Path("~/.local/share").expanduser())))
assert expected_data_dir in data_dir.parents
assert data_dir.name == "testapp"
# delete instance so other tests don't accidentally use it
config_manager.delete_instance("testapp")

Expand Down

0 comments on commit 22b8dc3

Please sign in to comment.