diff options
Diffstat (limited to 'tests/test_utils_pure.py')
| -rw-r--r-- | tests/test_utils_pure.py | 396 |
1 files changed, 396 insertions, 0 deletions
diff --git a/tests/test_utils_pure.py b/tests/test_utils_pure.py new file mode 100644 index 0000000..d7f79f1 --- /dev/null +++ b/tests/test_utils_pure.py @@ -0,0 +1,396 @@ +"""Tests for pure utility functions not covered in test_utils.py. + +Covers: + - is_valid_prefix_path + - is_safe_prefix_path + - _resolve_runner_to_directory + - has_proton_installed +""" + +from unittest.mock import patch + + +# ── is_valid_prefix_path ────────────────────────────────────────────────── + + +class TestIsValidPrefixPath: + """Tests for fungus.utils_pure.is_valid_prefix_path.""" + + def test_inside_prefixes_dir(self, tmp_path): + """Path inside the configured prefixes directory returns True.""" + from fungus.utils_pure import is_valid_prefix_path + + prefixes = tmp_path / "fungus" / "prefixes" + prefixes.mkdir(parents=True) + result = is_valid_prefix_path(str(prefixes / "umu-1234"), str(prefixes)) + assert result is True + + def test_is_prefixes_dir_itself(self, tmp_path): + """The prefixes directory itself is valid.""" + from fungus.utils_pure import is_valid_prefix_path + + prefixes = tmp_path / "fungus" / "prefixes" + prefixes.mkdir(parents=True) + result = is_valid_prefix_path(str(prefixes), str(prefixes)) + assert result is True + + def test_outside_prefixes_dir(self, tmp_path): + """Path outside the prefixes directory returns False.""" + from fungus.utils_pure import is_valid_prefix_path + + prefixes = tmp_path / "fungus" / "prefixes" + prefixes.mkdir(parents=True) + result = is_valid_prefix_path( + str(tmp_path / "other" / "stuff"), str(prefixes) + ) + assert result is False + + def test_parent_of_prefixes_dir(self, tmp_path): + """Parent directory of prefixes_dir is not valid.""" + from fungus.utils_pure import is_valid_prefix_path + + prefixes = tmp_path / "fungus" / "prefixes" + prefixes.mkdir(parents=True) + result = is_valid_prefix_path(str(tmp_path / "fungus"), str(prefixes)) + assert result is False + + def test_deeply_nested_path(self, tmp_path): + """Deeply nested paths inside prefixes_dir are valid.""" + from fungus.utils_pure import is_valid_prefix_path + + prefixes = tmp_path / "fungus" / "prefixes" + prefixes.mkdir(parents=True) + deep = prefixes / "a" / "b" / "c" / "d" + result = is_valid_prefix_path(str(deep), str(prefixes)) + assert result is True + + def test_similar_prefix_name_not_matched(self, tmp_path): + """A directory with a similar name but not under prefixes_dir is rejected.""" + from fungus.utils_pure import is_valid_prefix_path + + prefixes = tmp_path / "fungus" / "prefixes" + prefixes.mkdir(parents=True) + result = is_valid_prefix_path( + str(tmp_path / "fungus" / "prefixes_extra" / "game"), str(prefixes) + ) + assert result is False + + def test_expands_tilde(self, tmp_path, monkeypatch): + """Tilde in path is expanded before checking.""" + from fungus.utils_pure import is_valid_prefix_path + + prefixes = tmp_path / "fungus" / "prefixes" + prefixes.mkdir(parents=True) + # Set HOME so ~ expands to tmp_path + monkeypatch.setenv("HOME", str(tmp_path)) + result = is_valid_prefix_path("~/fungus/prefixes/some-game", str(prefixes)) + assert result is True + + +# ── is_safe_prefix_path ─────────────────────────────────────────────────── + + +class TestIsSafePrefixPath: + """Tests for fungus.utils_pure.is_safe_prefix_path.""" + + def test_inside_user_data(self, tmp_path): + """Path inside user-data directory returns True.""" + from fungus.utils_pure import is_safe_prefix_path + + user_data = tmp_path / "test_data" + user_data.mkdir() + result = is_safe_prefix_path( + str(user_data / "fungus-launcher" / "games"), + user_data=str(user_data), + ) + assert result is True + + def test_inside_user_config(self, tmp_path): + """Path inside user-config directory returns True.""" + from fungus.utils_pure import is_safe_prefix_path + + user_config = tmp_path / "test_config" + user_config.mkdir() + result = is_safe_prefix_path( + str(user_config / "fungus-launcher" / "config.ini"), + user_config=str(user_config), + ) + assert result is True + + def test_is_user_data_root(self, tmp_path): + """The user-data root itself is safe.""" + from fungus.utils_pure import is_safe_prefix_path + + user_data = tmp_path / "test_data" + user_data.mkdir() + result = is_safe_prefix_path(str(user_data), user_data=str(user_data)) + assert result is True + + def test_is_user_config_root(self, tmp_path): + """The user-config root itself is safe.""" + from fungus.utils_pure import is_safe_prefix_path + + user_config = tmp_path / "test_config" + user_config.mkdir() + result = is_safe_prefix_path( + str(user_config), user_config=str(user_config) + ) + assert result is True + + def test_outside_both_safe_roots(self, tmp_path): + """Path outside both safe roots returns False.""" + from fungus.utils_pure import is_safe_prefix_path + + user_data = tmp_path / "test_data" + user_config = tmp_path / "test_config" + user_data.mkdir() + user_config.mkdir() + result = is_safe_prefix_path( + "/tmp/random/file.txt", + user_data=str(user_data), + user_config=str(user_config), + ) + assert result is False + + def test_parent_of_safe_roots(self, tmp_path): + """Parent of safe roots is not safe.""" + from fungus.utils_pure import is_safe_prefix_path + + user_data = tmp_path / "test_data" + user_config = tmp_path / "test_config" + user_data.mkdir() + user_config.mkdir() + # Parent of user_data is tmp_path itself + result = is_safe_prefix_path( + str(tmp_path), + user_data=str(user_data), + user_config=str(user_config), + ) + assert result is False + + def test_deeply_nested_safe_path(self, tmp_path): + """Deeply nested paths inside safe roots are valid.""" + from fungus.utils_pure import is_safe_prefix_path + + user_data = tmp_path / "test_data" + user_data.mkdir() + deep = user_data / "a" / "b" / "c" + result = is_safe_prefix_path( + str(deep), user_data=str(user_data) + ) + assert result is True + + def test_similar_name_not_matched(self, tmp_path): + """A path with a similar name but not under a safe root is rejected.""" + from fungus.utils_pure import is_safe_prefix_path + + user_data = tmp_path / "test_data" + user_data.mkdir() + # Sibling directory with a similar name + sibling = tmp_path / "test_data_extra" + result = is_safe_prefix_path( + str(sibling / "file"), user_data=str(user_data) + ) + assert result is False + + +# ── _resolve_runner_to_directory ────────────────────────────────────────── + + +class TestResolveRunnerToDirectory: + """Tests for fungus.utils_pure._resolve_runner_to_directory.""" + + def _make_compat_tree(self, tmp_path, subdirs): + """Create a compatibilitytools.d directory tree.""" + compat = tmp_path / "Steam" / "compatibilitytools.d" + compat.mkdir(parents=True) + for d in subdirs: + (compat / d).mkdir() + return compat + + def test_direct_name_match(self, tmp_path): + """A runner name that matches a directory name passes through.""" + from fungus.utils_pure import _resolve_runner_to_directory + + self._make_compat_tree(tmp_path, ["GE-Proton11-1"]) + with patch("fungus.constants.share_dir", str(tmp_path)): + result = _resolve_runner_to_directory("GE-Proton11-1") + assert result == "GE-Proton11-1" + + def test_alias_ge_proton_latest(self, tmp_path): + """'Proton-GE Latest' alias resolves to the newest GE-Proton directory.""" + from fungus.utils_pure import _resolve_runner_to_directory + + self._make_compat_tree(tmp_path, [ + "GE-Proton9-11", + "GE-Proton10-1", + "GE-Proton11-1", + ]) + with patch("fungus.constants.share_dir", str(tmp_path)): + result = _resolve_runner_to_directory("Proton-GE Latest") + assert result == "GE-Proton11-1" + + def test_alias_ge_proton_latest_default(self, tmp_path): + """'GE-Proton Latest (default)' alias also resolves to newest.""" + from fungus.utils_pure import _resolve_runner_to_directory + + self._make_compat_tree(tmp_path, [ + "GE-Proton8-22", + "GE-Proton10-5", + ]) + with patch("fungus.constants.share_dir", str(tmp_path)): + result = _resolve_runner_to_directory("GE-Proton Latest (default)") + assert result == "GE-Proton10-5" + + def test_alias_proton_em_latest(self, tmp_path): + """'Proton-EM Latest' alias resolves to newest Proton-EM.""" + from fungus.utils_pure import _resolve_runner_to_directory + + self._make_compat_tree(tmp_path, [ + "Proton-EM-1.0", + "Proton-EM-2.0", + ]) + with patch("fungus.constants.share_dir", str(tmp_path)): + result = _resolve_runner_to_directory("Proton-EM Latest") + assert result == "Proton-EM-2.0" + + def test_alias_umu_proton_returns_none(self, tmp_path): + """'UMU-Proton Latest' has no directory equivalent → None.""" + from fungus.utils_pure import _resolve_runner_to_directory + + self._make_compat_tree(tmp_path, ["UMU-Proton-latest"]) + with patch("fungus.constants.share_dir", str(tmp_path)): + result = _resolve_runner_to_directory("UMU-Proton Latest") + assert result is None + + def test_unknown_runner_returns_none(self, tmp_path): + """Unknown runner aliases return None.""" + from fungus.utils_pure import _resolve_runner_to_directory + + self._make_compat_tree(tmp_path, ["GE-Proton11-1"]) + with patch("fungus.constants.share_dir", str(tmp_path)): + result = _resolve_runner_to_directory("Linux-Native") + assert result is None + + def test_no_compat_directory_returns_none(self, tmp_path): + """When compatibilitytools.d doesn't exist, returns None.""" + from fungus.utils_pure import _resolve_runner_to_directory + + with patch("fungus.constants.share_dir", str(tmp_path)): + result = _resolve_runner_to_directory("GE-Proton11-1") + assert result is None + + def test_no_matching_versions_returns_none(self, tmp_path): + """When no directories match the family, returns None.""" + from fungus.utils_pure import _resolve_runner_to_directory + + self._make_compat_tree(tmp_path, ["SomeOtherTool-1.0"]) + with patch("fungus.constants.share_dir", str(tmp_path)): + result = _resolve_runner_to_directory("Proton-GE Latest") + assert result is None + + def test_newest_version_wins(self, tmp_path): + """Among matching versions, the newest one is returned.""" + from fungus.utils_pure import _resolve_runner_to_directory + + self._make_compat_tree(tmp_path, [ + "GE-Proton7-27", + "GE-Proton9-24", + "GE-Proton10-7", + ]) + with patch("fungus.constants.share_dir", str(tmp_path)): + result = _resolve_runner_to_directory("GE-Proton10-7") + assert result == "GE-Proton10-7" + + def test_direct_name_not_a_dir_returns_none(self, tmp_path): + """If the runner name doesn't match any directory, returns None.""" + from fungus.utils_pure import _resolve_runner_to_directory + + self._make_compat_tree(tmp_path, ["GE-Proton11-1"]) + with patch("fungus.constants.share_dir", str(tmp_path)): + result = _resolve_runner_to_directory("NonExistentRunner") + assert result is None + + +# ── has_proton_installed ────────────────────────────────────────────────── + + +class TestHasProtonInstalled: + """Tests for fungus.utils_pure.has_proton_installed.""" + + def _make_compat_tree(self, tmp_path, entries): + """Create a compatibilitytools.d directory tree with given entries.""" + compat = tmp_path / "Steam" / "compatibilitytools.d" + compat.mkdir(parents=True) + for entry in entries: + if entry.endswith("/"): + (compat / entry.rstrip("/")).mkdir() + else: + (compat / entry).mkdir() + return compat + + def test_no_compat_directory(self, tmp_path): + """When compatibilitytools.d doesn't exist, returns False.""" + from fungus.utils_pure import has_proton_installed + + with patch("fungus.constants.share_dir", str(tmp_path)): + result = has_proton_installed() + assert result is False + + def test_only_legacy_runtime(self, tmp_path): + """Only LegacyRuntime directory → no proton installed.""" + from fungus.utils_pure import has_proton_installed + + self._make_compat_tree(tmp_path, ["LegacyRuntime/"]) + with patch("fungus.constants.share_dir", str(tmp_path)): + result = has_proton_installed() + assert result is False + + def test_ge_proton_installed(self, tmp_path): + """GE-Proton directory → proton is installed.""" + from fungus.utils_pure import has_proton_installed + + self._make_compat_tree(tmp_path, ["GE-Proton11-1/"]) + with patch("fungus.constants.share_dir", str(tmp_path)): + result = has_proton_installed() + assert result is True + + def test_proton_em_installed(self, tmp_path): + """Proton-EM directory → proton is installed.""" + from fungus.utils_pure import has_proton_installed + + self._make_compat_tree(tmp_path, ["Proton-EM-2.0/"]) + with patch("fungus.constants.share_dir", str(tmp_path)): + result = has_proton_installed() + assert result is True + + def test_official_proton_installed(self, tmp_path): + """Official Proton directory → proton is installed.""" + from fungus.utils_pure import has_proton_installed + + self._make_compat_tree(tmp_path, ["Proton-10.0-1/"]) + with patch("fungus.constants.share_dir", str(tmp_path)): + result = has_proton_installed() + assert result is True + + def test_mixed_entries(self, tmp_path): + """Mixed LegacyRuntime + actual proton → returns True.""" + from fungus.utils_pure import has_proton_installed + + self._make_compat_tree(tmp_path, [ + "LegacyRuntime/", + "GE-Proton9-11/", + "some-other-tool/", + ]) + with patch("fungus.constants.share_dir", str(tmp_path)): + result = has_proton_installed() + assert result is True + + def test_empty_directory(self, tmp_path): + """Empty compatibilitytools.d → returns False.""" + from fungus.utils_pure import has_proton_installed + + self._make_compat_tree(tmp_path, []) + with patch("fungus.constants.share_dir", str(tmp_path)): + result = has_proton_installed() + assert result is False |
