Skip to content

Commit 26259c1

Browse files
authored
Merge pull request #242 from karlch/use-tmp_path
Tests: use the tmp_path fixture instead of tmpdir
2 parents 41c5760 + 5b67565 commit 26259c1

19 files changed

+201
-156
lines changed

tests/conftest.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,3 +134,9 @@ def _retrieve_file_from_web(url: str, path: str) -> None:
134134
with open(path, "wb") as f:
135135
f.write(data)
136136
print("... success")
137+
138+
139+
@pytest.fixture()
140+
def tmpdir():
141+
"""Override tmpdir to raise a ValueError."""
142+
raise ValueError("Use the 'tmp_path' fixture instead of 'tmpdir'")

tests/end2end/conftest.py

Lines changed: 41 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
"""Fixtures and bdd-given steps related to setup and cleanup for end2end testing."""
88

99
import logging
10-
import os
1110

1211
from PyQt5.QtGui import QPixmap
1312

@@ -33,8 +32,8 @@ def ensureqtbot(qtbot):
3332

3433

3534
@pytest.fixture(autouse=True)
36-
def mock_directories(tmpdir):
37-
directory = tmpdir.join(".vimiv-data")
35+
def mock_directories(tmp_path):
36+
directory = tmp_path / ".vimiv-data"
3837
utils.xdg.basedir = str(directory)
3938
yield
4039
utils.xdg.basedir = None
@@ -79,13 +78,13 @@ def faster_wait_times():
7978
###############################################################################
8079
@bdd.given("I start vimiv")
8180
@bdd.given("I open any directory")
82-
def start_vimiv(tmpdir):
83-
start_directory(tmpdir)
81+
def start_vimiv(tmp_path):
82+
start_directory(tmp_path)
8483

8584

8685
@bdd.given(bdd.parsers.parse("I start vimiv with {args}"))
87-
def start_vimiv_with_args(tmpdir, args):
88-
start_directory(tmpdir, args=args.split())
86+
def start_vimiv_with_args(tmp_path, args):
87+
start_directory(tmp_path, args=args.split())
8988

9089

9190
@bdd.given("I run vimiv --version")
@@ -95,51 +94,51 @@ def run_vimiv_version():
9594

9695

9796
@bdd.given("I start vimiv with --log-level <level>")
98-
def start_vimiv_log_level(tmpdir, level):
99-
start_directory(tmpdir, args=["--log-level", level])
97+
def start_vimiv_log_level(tmp_path, level):
98+
start_directory(tmp_path, args=["--log-level", level])
10099

101100

102101
@bdd.given("I open a directory for which I do not have access permissions")
103-
def start_directory_without_permission(tmpdir):
104-
start_directory(tmpdir, permission=0o666)
102+
def start_directory_without_permission(tmp_path):
103+
start_directory(tmp_path, permission=0o666)
105104

106105

107106
@bdd.given(bdd.parsers.parse("I open a directory with {n_children:d} paths"))
108-
def start_directory_with_n_paths(tmpdir, n_children):
109-
start_directory(tmpdir, n_children=n_children)
107+
def start_directory_with_n_paths(tmp_path, n_children):
108+
start_directory(tmp_path, n_children=n_children)
110109

111110

112111
@bdd.given(bdd.parsers.parse("I open a directory with {n_images:d} images"))
113-
def start_directory_with_n_images(tmpdir, n_images):
114-
start_directory(tmpdir, n_images=n_images)
112+
def start_directory_with_n_images(tmp_path, n_images):
113+
start_directory(tmp_path, n_images=n_images)
115114

116115

117116
@bdd.given("I open any image")
118-
def start_any_image(tmpdir):
119-
start_image(tmpdir)
117+
def start_any_image(tmp_path):
118+
start_image(tmp_path)
120119

121120

122121
@bdd.given(bdd.parsers.parse("I open any image of size {size}"))
123-
def start_any_image_of_size(tmpdir, size):
122+
def start_any_image_of_size(tmp_path, size):
124123
size = [int(elem) for elem in size.split("x")]
125-
start_image(tmpdir, size=size)
124+
start_image(tmp_path, size=size)
126125

127126

128127
@bdd.given(bdd.parsers.parse("I open {n_images:d} images"))
129-
def start_n_images(tmpdir, n_images):
130-
start_image(tmpdir, n_images=n_images)
128+
def start_n_images(tmp_path, n_images):
129+
start_image(tmp_path, n_images=n_images)
131130

132131

133132
@bdd.given(bdd.parsers.parse("I open {n_images:d} images with {args}"))
134-
def start_n_images_with_args(tmpdir, n_images, args):
135-
start_image(tmpdir, n_images=n_images, args=args.split())
133+
def start_n_images_with_args(tmp_path, n_images, args):
134+
start_image(tmp_path, n_images=n_images, args=args.split())
136135

137136

138137
@bdd.given(bdd.parsers.parse("I open the image '{basename}'"))
139-
def start_image_name(tmpdir, basename):
140-
path = str(tmpdir.join(basename))
141-
create_image(path)
142-
start([path])
138+
def start_image_name(tmp_path, basename):
139+
filename = str(tmp_path / basename)
140+
create_image(filename)
141+
start([filename])
143142

144143

145144
@bdd.given("I open an animated gif")
@@ -173,45 +172,46 @@ def check_stderr(output, text):
173172
###############################################################################
174173
# helpers #
175174
###############################################################################
176-
def start_directory(tmpdir, n_children=0, n_images=0, permission=0o777, args=None):
175+
def start_directory(tmp_path, n_children=0, n_images=0, permission=0o777, args=None):
177176
"""Run vimiv startup using one directory as the passed path."""
178177
args = args if args is not None else []
179-
directory = tmpdir.mkdir("directory")
180-
os.chmod(str(directory), permission)
178+
directory = tmp_path / "directory"
179+
directory.mkdir(mode=permission)
181180

182181
for i in range(n_children):
183-
directory.mkdir(f"child_{i + 1:02d}")
182+
(directory / f"child_{i + 1:02d}").mkdir()
184183

185184
create_n_images(directory, n_images)
186185

187-
start([str(directory)] + args)
186+
start([directory] + args)
188187

189188

190-
def start_image(tmpdir, n_images=1, size=(300, 300), args=None):
189+
def start_image(tmp_path, n_images=1, size=(300, 300), args=None):
191190
"""Run vimiv startup using n images as the passed paths."""
192-
paths = create_n_images(tmpdir, n_images, size=size)
193-
args = paths if args is None else paths + args
194-
start(args)
191+
paths = create_n_images(tmp_path, n_images, size=size)
192+
argv = paths if args is None else paths + args
193+
start(argv)
195194

196195

197196
def start(argv):
198197
"""Run vimiv startup passing argv as argument list."""
198+
argv = [str(arg) for arg in argv]
199199
args = startup.setup_pre_app(argv)
200200
startup.setup_post_app(args)
201201

202202

203-
def create_n_images(tmpdir, number, size=(300, 300), imgformat="jpg"):
203+
def create_n_images(directory, number, size=(300, 300), imgformat="jpg"):
204204
paths = []
205205
for i in range(1, number + 1):
206206
basename = f"image.{imgformat}" if number == 1 else f"image_{i:02d}.{imgformat}"
207-
path = str(tmpdir.join(basename))
208-
create_image(path, size=size)
207+
path = directory / basename
208+
create_image(str(path.resolve()), size=size)
209209
paths.append(path)
210210
return paths
211211

212212

213-
def create_image(path: str, *, size=(300, 300)):
214-
QPixmap(*size).save(path)
213+
def create_image(filename: str, *, size=(300, 300)):
214+
QPixmap(*size).save(filename)
215215

216216

217217
class Output:

tests/end2end/features/command/test_expand_wildcards_bdd.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@
1414

1515

1616
@pytest.fixture(autouse=True)
17-
def home_directory(tmpdir, mocker):
17+
def home_directory(tmp_path, mocker):
1818
"""Fixture to mock os.path.expanduser to return a different home directory."""
19-
new_home = str(tmpdir.mkdir("home"))
19+
directory = tmp_path / "home"
20+
directory.mkdir()
21+
new_home = str(directory)
2022

2123
def expand_user(path):
2224
return path.replace("~", new_home)

tests/end2end/features/conftest.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"""Fixtures and bdd-like steps for usage during end2end testing."""
88

99
import os
10+
import pathlib
1011

1112
from PyQt5.QtCore import Qt, QProcess, QTimer
1213
from PyQt5.QtGui import QFocusEvent
@@ -253,17 +254,17 @@ def create_directory(name):
253254

254255
@bdd.when(bdd.parsers.parse("I create the file '{name}'"))
255256
def create_file(name):
256-
assert not os.path.exists(name), f"Not overriding existing file '{name}'"
257-
with open(name, "w") as f:
258-
f.write("")
257+
path = pathlib.Path(name)
258+
assert not path.exists(), f"Not overriding existing file '{name}'"
259+
path.touch()
259260

260261

261262
@bdd.when(bdd.parsers.parse("I create the tag file '{name}'"))
262263
def create_tag_file(name):
263-
os.makedirs(api.mark.tagdir, mode=0o700, exist_ok=True)
264-
path = os.path.join(api.mark.tagdir, name)
265-
with open(path, "w") as f:
266-
f.write("")
264+
directory = pathlib.Path(api.mark.tagdir)
265+
directory.mkdir(mode=0o700, parents=True, exist_ok=True)
266+
path = directory / name
267+
path.touch()
267268

268269

269270
###############################################################################

tests/end2end/features/image/test_imageopen_bdd.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,18 @@
1515

1616

1717
@bdd.when("I open broken images")
18-
def open_broken_images(tmpdir):
19-
_open_file(tmpdir, b"\211PNG\r\n\032\n") # PNG
20-
_open_file(tmpdir, b"000000JFIF") # JPG
21-
_open_file(tmpdir, b"GIF89a") # GIF
22-
_open_file(tmpdir, b"II") # TIFF
23-
_open_file(tmpdir, b"BM") # BMP
18+
def open_broken_images(tmp_path):
19+
_open_file(tmp_path, b"\211PNG\r\n\032\n") # PNG
20+
_open_file(tmp_path, b"000000JFIF") # JPG
21+
_open_file(tmp_path, b"GIF89a") # GIF
22+
_open_file(tmp_path, b"II") # TIFF
23+
_open_file(tmp_path, b"BM") # BMP
2424

2525

26-
def _open_file(tmpdir, data):
26+
def _open_file(directory, data):
2727
"""Open a file containing the bytes from data."""
28-
path = str(tmpdir.join("broken"))
29-
with open(path, "wb") as f:
30-
f.write(data)
31-
assert imghdr.what(path) is not None, "Invalid magic bytes in test setup"
32-
api.open_paths([path])
28+
path = directory / "broken"
29+
path.write_bytes(data)
30+
filename = str(path)
31+
assert imghdr.what(filename) is not None, "Invalid magic bytes in test setup"
32+
api.open_paths([filename])

tests/integration/conftest.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@ def create_custom_parser(default_parser, **sections):
2424

2525

2626
@pytest.fixture()
27-
def custom_configfile(tmpdir, custom_configparser):
27+
def custom_configfile(tmp_path, custom_configparser):
2828
"""Fixture to create a custom config file from a configparser."""
2929

3030
def create_custom_configfile(basename, read, default_parser, **sections):
3131
parser = custom_configparser(default_parser, **sections)
32-
path = tmpdir.join(basename)
32+
path = tmp_path / basename
3333
with open(path, "w") as f:
3434
parser.write(f)
3535
read(str(path))

tests/integration/test_read_bindings.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def reset_to_default(cleanup_helper):
3232

3333

3434
@pytest.fixture(scope="function")
35-
def keyspath(tmpdir, custom_configfile, request):
35+
def keyspath(custom_configfile, request):
3636
"""Fixture to create a custom keybindings file for reading."""
3737
yield custom_configfile(
3838
"keys.conf", keyfile.read, keyfile.get_default_parser, **request.param

tests/integration/test_read_settings.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def reset_to_default(cleanup_helper):
6161

6262

6363
@pytest.fixture(scope="function")
64-
def configpath(tmpdir, custom_configfile, request):
64+
def configpath(custom_configfile, request):
6565
yield custom_configfile(
6666
"vimiv.conf", configfile.read, configfile.get_default_parser, **request.param
6767
)

tests/unit/api/test_mark.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@ def mark(qtbot, mocker, monkeypatch):
2828

2929

3030
@pytest.fixture(autouse=True)
31-
def tagdir(tmpdir, mocker):
32-
tmp_tagdir = tmpdir.mkdir("tags")
31+
def tagdir(tmp_path, mocker):
32+
tmp_tagdir = tmp_path / "tags"
33+
tmp_tagdir.mkdir()
3334
mocker.patch.object(Tag, "dirname", return_value=str(tmp_tagdir))
3435
yield str(tmp_tagdir)
3536

tests/unit/commands/test_history.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,10 @@ def mixed_history():
4141

4242

4343
@pytest.fixture()
44-
def history_file(tmpdir, mocker):
45-
path = str(tmpdir.join("history"))
46-
mocker.patch.object(vimiv.commands.history, "filename", return_value=path)
47-
yield path
44+
def history_file(tmp_path, mocker):
45+
filename = str(tmp_path / "history")
46+
mocker.patch.object(vimiv.commands.history, "filename", return_value=filename)
47+
yield filename
4848

4949

5050
def test_update_history(history):

tests/unit/config/test_config.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
("[SECTION]\na=0\na=1\n", "duplicate key"),
2727
],
2828
)
29-
def test_sysexit_on_broken_config(mocker, tmpdir, content, message):
29+
def test_sysexit_on_broken_config(mocker, tmp_path, content, message):
3030
"""Ensure SystemExit is correctly raised for various broken config files.
3131
3232
Args:
@@ -36,8 +36,8 @@ def test_sysexit_on_broken_config(mocker, tmpdir, content, message):
3636
print("Ensuring system exit with", message)
3737
mock_logger = mocker.Mock()
3838
parser = configparser.ConfigParser()
39-
path = tmpdir.join("configfile")
40-
path.write(content)
39+
path = tmp_path / "configfile"
40+
path.write_text(content)
4141
with pytest.raises(SystemExit, match=str(customtypes.Exit.err_config)):
4242
config.read_log_exception(parser, mock_logger, str(path))
4343
mock_logger.critical.assert_called_once()

tests/unit/config/test_external_configparser.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ def parser():
2727

2828

2929
@pytest.fixture()
30-
def config(tmpdir):
30+
def config(tmp_path):
3131
"""Fixture to retrieve a written config file with external references."""
3232
parser = configparser.ConfigParser()
3333
parser[SECTION_NAME][OPTION_NAME] = "${env:" + ENV_VARIABLE.name + "}"
34-
path = tmpdir.join("config.ini")
34+
path = tmp_path / "config.ini"
3535
with open(path, "w") as f:
3636
parser.write(f)
3737
yield str(path)

tests/unit/config/test_styles.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def new_style(request):
2020

2121

2222
@pytest.fixture
23-
def style_file(tmpdir):
23+
def style_file(tmp_path):
2424
"""Fixture to create a style file with different properties."""
2525

2626
def create_style_file(color="#FFF", font=None, n_colors=16, header=True, **options):
@@ -33,9 +33,9 @@ def create_style_file(color="#FFF", font=None, n_colors=16, header=True, **optio
3333
header. If False, omit the STYLE section header.
3434
options: Further style options passed.
3535
"""
36-
path = str(tmpdir.join("style"))
36+
filename = str(tmp_path / "style")
3737
if not header:
38-
return path
38+
return filename
3939
parser = configparser.ConfigParser()
4040
parser.add_section("STYLE")
4141
for i in range(n_colors):
@@ -44,9 +44,9 @@ def create_style_file(color="#FFF", font=None, n_colors=16, header=True, **optio
4444
parser["STYLE"]["font"] = font
4545
for key, value in options.items():
4646
parser["STYLE"][key] = value
47-
with open(path, "w") as f:
47+
with open(filename, "w") as f:
4848
parser.write(f)
49-
return path
49+
return filename
5050

5151
return create_style_file
5252

0 commit comments

Comments
 (0)