Skip to content

Commit 3dbd662

Browse files
committed
test: use pytest's tmp_path_factory fixture instead of tempfile
1 parent 7c82428 commit 3dbd662

File tree

8 files changed

+67
-65
lines changed

8 files changed

+67
-65
lines changed

test/t/conftest.py

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@
22
import os
33
import re
44
import shlex
5-
import shutil
65
import subprocess
76
import sys
8-
import tempfile
97
import time
108
from enum import Enum
119
from pathlib import Path
@@ -190,7 +188,7 @@ def get_testdir():
190188

191189
@pytest.fixture(scope="session")
192190
def test_session_tmpdir(tmp_path_factory) -> Path:
193-
tmpdir = tmp_path_factory.mktemp("bash-completion-test_")
191+
tmpdir = tmp_path_factory.mktemp("bash-completion.session.")
194192

195193
user_dir_1 = tmpdir / "bash-completion"
196194
user_dir_2 = tmpdir / "bash-completion-fallback"
@@ -231,9 +229,8 @@ def test_session_tmpdir(tmp_path_factory) -> Path:
231229

232230

233231
@pytest.fixture(scope="class")
234-
def bash(request, test_session_tmpdir) -> pexpect.spawn:
232+
def bash(request, test_session_tmpdir, tmp_path_factory) -> pexpect.spawn:
235233
logfile: Optional[TextIO] = None
236-
tmpdir = None
237234
bash = None
238235

239236
if os.environ.get("BASH_COMPLETION_TEST_LOGFILE"):
@@ -268,10 +265,8 @@ def bash(request, test_session_tmpdir) -> pexpect.spawn:
268265
testdir, "fixtures", marker.kwargs.get("cwd")
269266
)
270267
elif "temp_cwd" in marker.kwargs and marker.kwargs.get("temp_cwd"):
271-
tmpdir = tempfile.TemporaryDirectory(
272-
prefix="bash-completion-test_"
273-
)
274-
cwd = tmpdir.name
268+
tmpdir = tmp_path_factory.mktemp("bash-completion.bash.")
269+
cwd = str(tmpdir)
275270
if cwd is None:
276271
cwd = os.path.join(testdir, "fixtures")
277272
os.chdir(cwd)
@@ -371,8 +366,6 @@ def bash(request, test_session_tmpdir) -> pexpect.spawn:
371366
# Clean up
372367
if bash:
373368
bash.close()
374-
if tmpdir:
375-
tmpdir.cleanup()
376369
if logfile and logfile != sys.stdout:
377370
logfile.close()
378371

@@ -961,7 +954,7 @@ def in_container() -> bool:
961954

962955

963956
def prepare_fixture_dir(
964-
request, files: Iterable[str], dirs: Iterable[str]
957+
tmp_path_factory, files: Iterable[str], dirs: Iterable[str]
965958
) -> Path:
966959
"""
967960
Fixture to prepare a test dir with dummy contents on the fly.
@@ -970,8 +963,7 @@ def prepare_fixture_dir(
970963
prepare a dir on the fly rather than including their fixtures in git and
971964
the tarball. This is to work better with case insensitive file systems.
972965
"""
973-
tempdir = Path(tempfile.mkdtemp(prefix="bash-completion-fixture-dir"))
974-
request.addfinalizer(lambda: shutil.rmtree(str(tempdir)))
966+
tempdir = tmp_path_factory.mktemp("bash-completion.fixture_dir.")
975967

976968
old_cwd = os.getcwd()
977969
try:

test/t/test_man.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class TestMan:
1616
assumed_present = "man"
1717

1818
@pytest.fixture
19-
def colonpath(self, request, bash):
19+
def colonpath(self, bash, tmp_path_factory):
2020
try:
2121
assert_bash_exec(bash, "uname -s 2>&1 | grep -qiF cygwin")
2222
except AssertionError:
@@ -25,7 +25,7 @@ def colonpath(self, request, bash):
2525
pytest.skip("Cygwin doesn't like paths with colons")
2626

2727
tmpdir = prepare_fixture_dir(
28-
request,
28+
tmp_path_factory,
2929
files=["man/man3/Bash::Completion.3pm.gz"],
3030
dirs=["man", "man/man3"],
3131
)

test/t/test_scp.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -189,12 +189,12 @@ def prefix_paths(prefix, paths):
189189
)
190190

191191
@pytest.fixture
192-
def tmpdir_backslash(self, request, bash):
192+
def tmpdir_backslash(self, bash, tmp_path_factory):
193193
if sys.platform.startswith("win"):
194194
pytest.skip("Filenames not allowed on Windows")
195195

196196
tmpdir = prepare_fixture_dir(
197-
request, files=["local_path-file\\"], dirs=[]
197+
tmp_path_factory, files=["local_path-file\\"], dirs=[]
198198
)
199199
return tmpdir
200200

@@ -211,11 +211,11 @@ def test_remote_path_ending_with_backslash(self, bash):
211211
assert completion.output == r"thetical\\\\ "
212212

213213
@pytest.fixture
214-
def tmpdir_mkfifo(self, request, bash):
214+
def tmpdir_mkfifo(self, bash, tmp_path_factory):
215215
# We prepare two files: 1) a named pipe and 2) a regular file ending
216216
# with the same name but an extra special character "|".
217217
tmpdir = prepare_fixture_dir(
218-
request,
218+
tmp_path_factory,
219219
files=["local_path_2-pipe|"],
220220
dirs=[],
221221
)
@@ -257,12 +257,12 @@ def test_local_path_with_spaces_2(self, completion):
257257
assert completion == r"\ conf"
258258

259259
@pytest.fixture
260-
def tmpdir_backslash_2(self, request, bash):
260+
def tmpdir_backslash_2(self, bash, tmp_path_factory):
261261
if sys.platform.startswith("win"):
262262
pytest.skip("Filenames not allowed on Windows")
263263

264264
tmpdir = prepare_fixture_dir(
265-
request,
265+
tmp_path_factory,
266266
files=["backslash-a b.txt", r"backslash-a\ b.txt"],
267267
dirs=[],
268268
)

test/t/test_slapt_get.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import os.path
2-
from tempfile import mkstemp
32

43
import pytest
54

@@ -9,17 +8,19 @@
98
@pytest.mark.bashcomp(cmd="slapt-get")
109
class TestSlaptGet:
1110
@pytest.fixture(scope="class")
12-
def slapt_getrc(self, request, bash):
13-
fd, fname = mkstemp(prefix="slapt-getrc.", text=True)
14-
request.addfinalizer(lambda: os.remove(fname))
15-
with os.fdopen(fd, "w") as f:
16-
print(
17-
"WORKINGDIR=%s/"
18-
% os.path.join(bash.cwd, *"slackware var slapt-get".split()),
19-
file=f,
20-
)
21-
print("SOURCE=file:///home/", file=f)
22-
return fname
11+
def slapt_getrc(self, bash, tmp_path_factory):
12+
working_dir = os.path.join(
13+
bash.cwd, *"slackware var slapt-get".split()
14+
)
15+
16+
tmpdir = tmp_path_factory.mktemp(
17+
"bash-completion._comp_cmd_slapt_get."
18+
)
19+
tmpfile = tmpdir / "slapt-getrc.0"
20+
tmpfile.write_text(
21+
f"WORKINGDIR={working_dir}/\nSOURCE=file:///home/\n"
22+
)
23+
return str(tmpfile)
2324

2425
@pytest.mark.complete("slapt-get -", require_cmd=True)
2526
def test_1(self, completion):

test/t/test_slapt_src.py

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import os
2-
from tempfile import mkstemp
32

43
import pytest
54

@@ -9,18 +8,17 @@
98
@pytest.mark.bashcomp(cmd="slapt-src")
109
class TestSlaptSrc:
1110
@pytest.fixture(scope="class")
12-
def slapt_srcrc(self, request, bash):
13-
fd, fname = mkstemp(prefix="slapt-srcrc.", text=True)
14-
request.addfinalizer(lambda: os.remove(fname))
15-
with os.fdopen(fd, "w") as f:
16-
print(
17-
"BUILDDIR=%s/"
18-
% os.path.join(
19-
bash.cwd, *"slackware usr src slapt-src".split()
20-
),
21-
file=f,
22-
)
23-
return fname
11+
def slapt_srcrc(self, bash, tmp_path_factory):
12+
build_dir = os.path.join(
13+
bash.cwd, *"slackware usr src slapt-src".split()
14+
)
15+
16+
tmpdir = tmp_path_factory.mktemp(
17+
"bash-completion._comp_cmd_slapt_src."
18+
)
19+
tmpfile = tmpdir / "slapt-srcrc.0"
20+
tmpfile.write_text(f"BUILDDIR={build_dir}/\n")
21+
return str(tmpfile)
2422

2523
@pytest.mark.complete("slapt-src -", require_cmd=True)
2624
def test_1(self, completion):

test/t/test_sshfs.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@ def test_1(self, completion):
1212
assert completion
1313

1414
@pytest.fixture
15-
def tmpdir_backslash(self, request, bash):
15+
def tmpdir_backslash(self, bash, tmp_path_factory):
1616
if sys.platform.startswith("win"):
1717
pytest.skip("Filenames not allowed on Windows")
1818

1919
tmpdir = prepare_fixture_dir(
20-
request, files=["local_path-file\\"], dirs=["local_path-dir"]
20+
tmp_path_factory,
21+
files=["local_path-file\\"],
22+
dirs=["local_path-dir"],
2123
)
2224
return tmpdir
2325

test/t/unit/test_unit_compgen_filedir.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
import os
2-
import shutil
32
import sys
4-
import tempfile
5-
from pathlib import Path
63

74
import pytest
85

@@ -42,11 +39,12 @@ def functions(self, request, bash):
4239
)
4340

4441
@pytest.fixture(scope="class")
45-
def non_windows_testdir(self, request, bash):
42+
def non_windows_testdir(self, bash, tmp_path_factory):
4643
if sys.platform.startswith("win"):
4744
pytest.skip("Filenames not allowed on Windows")
48-
tempdir = Path(tempfile.mkdtemp(prefix="bash-completion_filedir"))
49-
request.addfinalizer(lambda: shutil.rmtree(str(tempdir)))
45+
tempdir = tmp_path_factory.mktemp(
46+
"bash-completion._comp_compgen_filedir."
47+
)
5048
subdir = tempdir / 'a"b'
5149
subdir.mkdir()
5250
(subdir / "d").touch()

test/t/unit/test_unit_load.py

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22

33
import pytest
44

5-
from conftest import assert_bash_exec, bash_env_saved, prepare_fixture_dir
5+
from conftest import assert_bash_exec, bash_env_saved
66

77

88
@pytest.mark.bashcomp(cmd=None, cwd="_comp_load")
99
class TestCompLoad:
1010
@pytest.fixture
11-
def fixture_dir(self, request, bash):
11+
def fixture_dir(self, bash, tmp_path_factory):
1212
"""Construct the fixture directory in a temporary directory.
1313
1414
Some of the tests use specific setups of symbolic links. However, if
@@ -24,15 +24,26 @@ def fixture_dir(self, request, bash):
2424
set up symbolic links.
2525
"""
2626

27-
tmpdir = prepare_fixture_dir(request, files=[], dirs=[])
27+
tmpdir = tmp_path_factory.mktemp("bash-completion._comp_load.")
28+
29+
# Note: I tried to use
30+
#
31+
# shutil.copytree(os.getcwd(), tmpdir, dirs_exist_ok=True)
32+
#
33+
# but it turned out that shutil.copytree of Python 3.7 (used in centos7
34+
# and debian10 in the docker images) does not provide the
35+
# "dirs_exist_ok" option. We continue to use "cp -R" here. We may
36+
# update this line after we drop the support for Bash 4.4.
2837
assert_bash_exec(bash, "cp -R %s/* %s/" % (os.getcwd(), tmpdir))
29-
assert_bash_exec(bash, "mkdir -p %s/bin" % tmpdir)
30-
assert_bash_exec(
31-
bash, "ln -sf ../prefix1/bin/cmd1 %s/bin/cmd1" % tmpdir
32-
)
33-
assert_bash_exec(
34-
bash, "ln -sf ../prefix1/sbin/cmd2 %s/bin/cmd2" % tmpdir
35-
)
38+
39+
bin_dir = tmpdir / "bin"
40+
bin_dir.mkdir()
41+
42+
cmd1 = bin_dir / "cmd1"
43+
cmd2 = bin_dir / "cmd2"
44+
cmd1.symlink_to("../prefix1/bin/cmd1")
45+
cmd2.symlink_to("../prefix1/sbin/cmd2")
46+
3647
return str(tmpdir)
3748

3849
def test_userdir_1(self, bash, fixture_dir):

0 commit comments

Comments
 (0)