about summary refs log tree commit diff stats
path: root/tests/test_path_manager.py
blob: 1069b09ab565a59ee19023c59bc79f0e9031d83f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
"""Tests for fungus.path_manager — PathManager static methods."""

import os
from unittest.mock import patch

from fungus.path_manager import PathManager


class TestUserConfig:
    def test_default_fallback(self):
        """When XDG_CONFIG_HOME is unset, falls back to ~/.config."""
        env = os.environ.copy()
        env.pop("XDG_CONFIG_HOME", None)
        with patch.dict(os.environ, env, clear=True):
            result = PathManager.user_config()
            assert "config" in result
            assert result.endswith(".config")

    def test_custom_xdg_config_home(self):
        with patch.dict(os.environ, {"XDG_CONFIG_HOME": "/tmp/myconfig"}):
            result = PathManager.user_config("fungus-launcher")
            assert result == "/tmp/myconfig/fungus-launcher"

    def test_joins_multiple_segments(self):
        with patch.dict(os.environ, {"XDG_CONFIG_HOME": "/tmp/c"}):
            result = PathManager.user_config("fungus-launcher", "sub", "dir")
            assert result == "/tmp/c/fungus-launcher/sub/dir"


class TestUserData:
    def test_default_fallback(self):
        """When XDG_DATA_HOME is unset, falls back to ~/.local/share."""
        env = os.environ.copy()
        env.pop("XDG_DATA_HOME", None)
        with patch.dict(os.environ, env, clear=True):
            result = PathManager.user_data()
            assert "local/share" in result

    def test_custom_xdg_data_home(self):
        with patch.dict(os.environ, {"XDG_DATA_HOME": "/tmp/mydata"}):
            result = PathManager.user_data("Steam")
            assert result == "/tmp/mydata/Steam"


class TestFindBinary:
    def test_finds_existing_binary(self):
        """Should find python3 which is on the PATH."""
        result = PathManager.find_binary("python3")
        assert result != ""

    def test_nonexistent_binary(self):
        with patch.dict(os.environ, {"PATH": "/nonexistent"}):
            result = PathManager.find_binary("somebinary")
            assert result == ""


class TestGetIcon:
    def test_returns_fallback_when_not_found(self):
        """When no icon exists, returns the last fallback path."""
        result = PathManager.get_icon("nonexistent-icon.png")
        assert "nonexistent-icon.png" in result


class TestSystemData:
    def test_searches_xdg_data_dirs(self):
        """Should return the first existing path from XDG_DATA_DIRS."""
        with patch.dict(
            os.environ,
            {"XDG_DATA_DIRS": "/nonexistent1:/nonexistent2:/usr/share"},
            clear=True,
        ):
            result = PathManager.system_data("icons")
            assert result == "/usr/share/icons"

    def test_returns_first_existing(self):
        """Should return the first existing directory, not the last."""
        with patch.dict(
            os.environ,
            {"XDG_DATA_DIRS": "/usr/share:/usr/local/share"},
            clear=True,
        ):
            result = PathManager.system_data("applications")
            # /usr/share/applications should exist
            assert "applications" in result