Skip to content

Commit 78b8b8b

Browse files
committed
Reformat test_appdirs.py, add type annotations, and switch to @pytest.mark.usefixtures
1 parent 188266b commit 78b8b8b

File tree

1 file changed

+43
-23
lines changed

1 file changed

+43
-23
lines changed

tests/unit/test_appdirs.py

Lines changed: 43 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import os
33
import posixpath
44
import sys
5+
from typing import Generator
56
from unittest import mock
67

78
import pytest
@@ -11,7 +12,8 @@
1112

1213

1314
@pytest.fixture()
14-
def platformdirs_win32(monkeypatch):
15+
def platformdirs_win32(monkeypatch: pytest.MonkeyPatch) -> Generator[None, None, None]:
16+
1517
# Monkeypatch platformdirs to pretend we're running on Windows
1618

1719
with monkeypatch.context() as m:
@@ -24,7 +26,8 @@ def platformdirs_win32(monkeypatch):
2426

2527

2628
@pytest.fixture()
27-
def platformdirs_darwin(monkeypatch):
29+
def platformdirs_darwin(monkeypatch: pytest.MonkeyPatch) -> Generator[None, None, None]:
30+
2831
# Monkeypatch platformdirs to pretend we're running on macOS
2932

3033
with monkeypatch.context() as m:
@@ -37,7 +40,8 @@ def platformdirs_darwin(monkeypatch):
3740

3841

3942
@pytest.fixture()
40-
def platformdirs_linux(monkeypatch):
43+
def platformdirs_linux(monkeypatch: pytest.MonkeyPatch) -> Generator[None, None, None]:
44+
4145
# Monkeypatch platformdirs to pretend we're running on Linux
4246

4347
with monkeypatch.context() as m:
@@ -50,11 +54,12 @@ def platformdirs_linux(monkeypatch):
5054

5155

5256
class TestUserCacheDir:
53-
def test_user_cache_dir_win(self, monkeypatch: pytest.MonkeyPatch, platformdirs_win32) -> None:
57+
@pytest.mark.usefixtures("platformdirs_win32")
58+
def test_user_cache_dir_win(self, monkeypatch: pytest.MonkeyPatch) -> None:
5459
_get_win_folder = mock.Mock(return_value="C:\\Users\\test\\AppData\\Local")
5560

5661
monkeypatch.setattr(
57-
platformdirs.windows,
62+
platformdirs.windows, # type: ignore[attr-defined]
5863
"get_win_folder",
5964
_get_win_folder,
6065
raising=False,
@@ -66,27 +71,31 @@ def test_user_cache_dir_win(self, monkeypatch: pytest.MonkeyPatch, platformdirs_
6671
)
6772
assert _get_win_folder.call_args_list == [mock.call("CSIDL_LOCAL_APPDATA")]
6873

69-
def test_user_cache_dir_osx(self, monkeypatch: pytest.MonkeyPatch, platformdirs_darwin) -> None:
74+
@pytest.mark.usefixtures("platformdirs_darwin")
75+
def test_user_cache_dir_osx(self, monkeypatch: pytest.MonkeyPatch) -> None:
7076
monkeypatch.setenv("HOME", "/home/test")
7177

7278
assert appdirs.user_cache_dir("pip") == "/home/test/Library/Caches/pip"
7379

74-
def test_user_cache_dir_linux(self, monkeypatch: pytest.MonkeyPatch, platformdirs_linux) -> None:
80+
@pytest.mark.usefixtures("platformdirs_linux")
81+
def test_user_cache_dir_linux(self, monkeypatch: pytest.MonkeyPatch) -> None:
7582
monkeypatch.delenv("XDG_CACHE_HOME", raising=False)
7683
monkeypatch.setenv("HOME", "/home/test")
7784

7885
assert appdirs.user_cache_dir("pip") == "/home/test/.cache/pip"
7986

87+
@pytest.mark.usefixtures("platformdirs_linux")
8088
def test_user_cache_dir_linux_override(
81-
self, monkeypatch: pytest.MonkeyPatch, platformdirs_linux
89+
self, monkeypatch: pytest.MonkeyPatch
8290
) -> None:
8391
monkeypatch.setenv("XDG_CACHE_HOME", "/home/test/.other-cache")
8492
monkeypatch.setenv("HOME", "/home/test")
8593

8694
assert appdirs.user_cache_dir("pip") == "/home/test/.other-cache/pip"
8795

96+
@pytest.mark.usefixtures("platformdirs_linux")
8897
def test_user_cache_dir_linux_home_slash(
89-
self, monkeypatch: pytest.MonkeyPatch, platformdirs_linux
98+
self, monkeypatch: pytest.MonkeyPatch
9099
) -> None:
91100
# Verify that we are not affected by https://bugs.python.org/issue14768
92101
monkeypatch.delenv("XDG_CACHE_HOME", raising=False)
@@ -116,11 +125,12 @@ def my_get_win_folder(csidl_name):
116125

117126

118127
class TestSiteConfigDirs:
119-
def test_site_config_dirs_win(self, monkeypatch: pytest.MonkeyPatch, platformdirs_win32) -> None:
128+
@pytest.mark.usefixtures("platformdirs_win32")
129+
def test_site_config_dirs_win(self, monkeypatch: pytest.MonkeyPatch) -> None:
120130
_get_win_folder = mock.Mock(return_value="C:\\ProgramData")
121131

122132
monkeypatch.setattr(
123-
platformdirs.windows,
133+
platformdirs.windows, # type: ignore[attr-defined]
124134
"get_win_folder",
125135
_get_win_folder,
126136
raising=False,
@@ -129,21 +139,24 @@ def test_site_config_dirs_win(self, monkeypatch: pytest.MonkeyPatch, platformdir
129139
assert appdirs.site_config_dirs("pip") == ["C:\\ProgramData\\pip"]
130140
assert _get_win_folder.call_args_list == [mock.call("CSIDL_COMMON_APPDATA")]
131141

132-
def test_site_config_dirs_osx(self, monkeypatch: pytest.MonkeyPatch, platformdirs_darwin) -> None:
142+
@pytest.mark.usefixtures("platformdirs_darwin")
143+
def test_site_config_dirs_osx(self, monkeypatch: pytest.MonkeyPatch) -> None:
133144
monkeypatch.setenv("HOME", "/home/test")
134145

135146
assert appdirs.site_config_dirs("pip") == [
136147
"/Library/Preferences/pip",
137148
"/Library/Application Support/pip",
138149
]
139150

140-
def test_site_config_dirs_linux(self, monkeypatch: pytest.MonkeyPatch, platformdirs_linux) -> None:
151+
@pytest.mark.usefixtures("platformdirs_linux")
152+
def test_site_config_dirs_linux(self, monkeypatch: pytest.MonkeyPatch) -> None:
141153
monkeypatch.delenv("XDG_CONFIG_DIRS", raising=False)
142154

143155
assert appdirs.site_config_dirs("pip") == ["/etc/xdg/pip", "/etc"]
144156

157+
@pytest.mark.usefixtures("platformdirs_linux")
145158
def test_site_config_dirs_linux_override(
146-
self, monkeypatch: pytest.MonkeyPatch, platformdirs_linux
159+
self, monkeypatch: pytest.MonkeyPatch
147160
) -> None:
148161
monkeypatch.setattr(os, "pathsep", ":")
149162
monkeypatch.setenv("XDG_CONFIG_DIRS", "/spam:/etc:/etc/xdg")
@@ -155,22 +168,24 @@ def test_site_config_dirs_linux_override(
155168
"/etc",
156169
]
157170

171+
@pytest.mark.usefixtures("platformdirs_linux")
158172
def test_site_config_dirs_linux_empty(
159-
self, monkeypatch: pytest.MonkeyPatch, platformdirs_linux
173+
self, monkeypatch: pytest.MonkeyPatch
160174
) -> None:
161175
monkeypatch.setattr(os, "pathsep", ":")
162176
monkeypatch.setenv("XDG_CONFIG_DIRS", "")
163177
assert appdirs.site_config_dirs("pip") == ["/etc/xdg/pip", "/etc"]
164178

165179

166180
class TestUserConfigDir:
181+
@pytest.mark.usefixtures("platformdirs_win32")
167182
def test_user_config_dir_win_no_roaming(
168-
self, monkeypatch: pytest.MonkeyPatch, platformdirs_win32
183+
self, monkeypatch: pytest.MonkeyPatch
169184
) -> None:
170185
_get_win_folder = mock.Mock(return_value="C:\\Users\\test\\AppData\\Local")
171186

172187
monkeypatch.setattr(
173-
platformdirs.windows,
188+
platformdirs.windows, # type: ignore[attr-defined]
174189
"get_win_folder",
175190
_get_win_folder,
176191
raising=False,
@@ -182,13 +197,14 @@ def test_user_config_dir_win_no_roaming(
182197
)
183198
assert _get_win_folder.call_args_list == [mock.call("CSIDL_LOCAL_APPDATA")]
184199

200+
@pytest.mark.usefixtures("platformdirs_win32")
185201
def test_user_config_dir_win_yes_roaming(
186-
self, monkeypatch: pytest.MonkeyPatch, platformdirs_win32
202+
self, monkeypatch: pytest.MonkeyPatch
187203
) -> None:
188204
_get_win_folder = mock.Mock(return_value="C:\\Users\\test\\AppData\\Roaming")
189205

190206
monkeypatch.setattr(
191-
platformdirs.windows,
207+
platformdirs.windows, # type: ignore[attr-defined]
192208
"get_win_folder",
193209
_get_win_folder,
194210
raising=False,
@@ -199,7 +215,8 @@ def test_user_config_dir_win_yes_roaming(
199215
)
200216
assert _get_win_folder.call_args_list == [mock.call("CSIDL_APPDATA")]
201217

202-
def test_user_config_dir_osx(self, monkeypatch: pytest.MonkeyPatch, platformdirs_darwin) -> None:
218+
@pytest.mark.usefixtures("platformdirs_darwin")
219+
def test_user_config_dir_osx(self, monkeypatch: pytest.MonkeyPatch) -> None:
203220
monkeypatch.setenv("HOME", "/home/test")
204221

205222
if os.path.isdir("/home/test/Library/Application Support/"):
@@ -210,22 +227,25 @@ def test_user_config_dir_osx(self, monkeypatch: pytest.MonkeyPatch, platformdirs
210227
else:
211228
assert appdirs.user_config_dir("pip") == "/home/test/.config/pip"
212229

213-
def test_user_config_dir_linux(self, monkeypatch: pytest.MonkeyPatch, platformdirs_darwin) -> None:
230+
@pytest.mark.usefixtures("platformdirs_linux")
231+
def test_user_config_dir_linux(self, monkeypatch: pytest.MonkeyPatch) -> None:
214232
monkeypatch.delenv("XDG_CONFIG_HOME", raising=False)
215233
monkeypatch.setenv("HOME", "/home/test")
216234

217235
assert appdirs.user_config_dir("pip") == "/home/test/.config/pip"
218236

237+
@pytest.mark.usefixtures("platformdirs_linux")
219238
def test_user_config_dir_linux_override(
220-
self, monkeypatch: pytest.MonkeyPatch, platformdirs_linux
239+
self, monkeypatch: pytest.MonkeyPatch
221240
) -> None:
222241
monkeypatch.setenv("XDG_CONFIG_HOME", "/home/test/.other-config")
223242
monkeypatch.setenv("HOME", "/home/test")
224243

225244
assert appdirs.user_config_dir("pip") == "/home/test/.other-config/pip"
226245

246+
@pytest.mark.usefixtures("platformdirs_linux")
227247
def test_user_config_dir_linux_home_slash(
228-
self, monkeypatch: pytest.MonkeyPatch, platformdirs_linux
248+
self, monkeypatch: pytest.MonkeyPatch
229249
) -> None:
230250
# Verify that we are not affected by https://bugs.python.org/issue14768
231251
monkeypatch.delenv("XDG_CONFIG_HOME", raising=False)

0 commit comments

Comments
 (0)