4
4
import os
5
5
import sys
6
6
import typing
7
+ from tempfile import gettempdir
7
8
8
9
import pytest
9
10
13
14
if typing .TYPE_CHECKING :
14
15
from pathlib import Path
15
16
17
+ from pyfakefs .fake_filesystem import FakeFilesystem
16
18
from pytest_mock import MockerFixture
17
19
18
20
@@ -97,7 +99,7 @@ def _func_to_path(func: str) -> XDGVariable | None:
97
99
"user_cache_dir" : XDGVariable ("XDG_CACHE_HOME" , "~/.cache" ),
98
100
"user_state_dir" : XDGVariable ("XDG_STATE_HOME" , "~/.local/state" ),
99
101
"user_log_dir" : XDGVariable ("XDG_STATE_HOME" , "~/.local/state" ),
100
- "user_runtime_dir" : XDGVariable ("XDG_RUNTIME_DIR" , "/run/user/ 1234" ),
102
+ "user_runtime_dir" : XDGVariable ("XDG_RUNTIME_DIR" , "/tmp/runtime- 1234" ), # noqa: S108
101
103
"site_runtime_dir" : XDGVariable ("XDG_RUNTIME_DIR" , "/run" ),
102
104
}
103
105
return mapping .get (func )
@@ -154,10 +156,10 @@ def test_platform_on_bsd(monkeypatch: pytest.MonkeyPatch, mocker: MockerFixture,
154
156
155
157
assert Unix ().site_runtime_dir == "/var/run"
156
158
157
- mocker .patch ("pathlib.Path.exists " , return_value = True )
159
+ mocker .patch ("os.access " , return_value = True )
158
160
assert Unix ().user_runtime_dir == "/var/run/user/1234"
159
161
160
- mocker .patch ("pathlib.Path.exists " , return_value = False )
162
+ mocker .patch ("os.access " , return_value = False )
161
163
assert Unix ().user_runtime_dir == "/tmp/runtime-1234" # noqa: S108
162
164
163
165
@@ -173,6 +175,35 @@ def test_platform_on_win32(monkeypatch: pytest.MonkeyPatch, mocker: MockerFixtur
173
175
sys .modules ["platformdirs.unix" ] = prev_unix
174
176
175
177
178
+ @pytest .mark .usefixtures ("_getuid" )
179
+ @pytest .mark .parametrize (
180
+ ("platform" , "default_dir" ),
181
+ [
182
+ ("freebsd" , "/var/run/user/1234" ),
183
+ ("linux" , "/run/user/1234" ),
184
+ ],
185
+ )
186
+ def test_xdg_runtime_dir_unset (
187
+ monkeypatch : pytest .MonkeyPatch , mocker : MockerFixture , fs : FakeFilesystem , platform : str , default_dir : str
188
+ ) -> None :
189
+ monkeypatch .delenv ("XDG_RUNTIME_DIR" , raising = False )
190
+ mocker .patch ("sys.platform" , platform )
191
+
192
+ fs .create_dir (default_dir )
193
+
194
+ assert Unix ().user_runtime_dir .startswith (default_dir )
195
+
196
+ # If the default directory isn't writable, we shouldn't use it.
197
+ fs .chmod (default_dir , 0o000 )
198
+ assert not Unix ().user_runtime_dir .startswith (default_dir )
199
+ assert Unix ().user_runtime_dir .startswith (gettempdir ())
200
+
201
+ # If the runtime directory doesn't exist, we shouldn't use it.
202
+ fs .rmdir (default_dir )
203
+ assert not Unix ().user_runtime_dir .startswith (default_dir )
204
+ assert Unix ().user_runtime_dir .startswith (gettempdir ())
205
+
206
+
176
207
def test_ensure_exists_creates_folder (mocker : MockerFixture , tmp_path : Path ) -> None :
177
208
mocker .patch .dict (os .environ , {"XDG_DATA_HOME" : str (tmp_path )})
178
209
data_path = Unix (appname = "acme" , ensure_exists = True ).user_data_path
0 commit comments