Skip to content

Commit 6d86a04

Browse files
authored
Align interface of tests.lib.path.Path.mkdir with pathlib.Path.… (#6888)
2 parents f718a8f + f805f32 commit 6d86a04

10 files changed

+35
-20
lines changed

tests/functional/test_install.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -551,7 +551,8 @@ def test_editable_install__local_dir_no_setup_py_with_pyproject(
551551
Test installing in editable mode from a local directory with no setup.py
552552
but that does have pyproject.toml.
553553
"""
554-
local_dir = script.scratch_path.joinpath('temp').mkdir()
554+
local_dir = script.scratch_path.joinpath('temp')
555+
local_dir.mkdir()
555556
pyproject_path = local_dir.joinpath('pyproject.toml')
556557
pyproject_path.write_text('')
557558

tests/functional/test_pep517.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77

88

99
def make_project(tmpdir, requires=[], backend=None):
10-
project_dir = (tmpdir / 'project').mkdir()
10+
project_dir = tmpdir / 'project'
11+
project_dir.mkdir()
1112
buildsys = {'requires': requires}
1213
if backend:
1314
buildsys['build-backend'] = backend
@@ -125,7 +126,8 @@ def test_pep517_install_with_no_cache_dir(script, tmpdir, data):
125126

126127

127128
def make_pyproject_with_setup(tmpdir, build_system=True, set_backend=True):
128-
project_dir = (tmpdir / 'project').mkdir()
129+
project_dir = tmpdir / 'project'
130+
project_dir.mkdir()
129131
setup_script = (
130132
'from setuptools import setup\n'
131133
)
@@ -161,7 +163,8 @@ def make_pyproject_with_setup(tmpdir, build_system=True, set_backend=True):
161163

162164
project_dir.joinpath('pyproject.toml').write_text(project_data)
163165
project_dir.joinpath('setup.py').write_text(setup_script)
164-
package_dir = (project_dir / "pep517_test").mkdir()
166+
package_dir = project_dir / "pep517_test"
167+
package_dir.mkdir()
165168
package_dir.joinpath('__init__.py').write_text('__version__ = "0.1"')
166169
return project_dir, "pep517_test"
167170

tests/functional/test_uninstall_user.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def test_uninstall_editable_from_usersite(self, script, data):
5151
"""
5252
Test uninstall editable local user install
5353
"""
54-
script.user_site_path.mkdir(parents=True)
54+
assert script.user_site_path.exists()
5555

5656
# install
5757
to_install = data.packages.joinpath("FSPkg")

tests/lib/__init__.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,8 @@ def __init__(self, base_path, *args, **kwargs):
451451
)
452452

453453
# Create a Directory to use as a scratch pad
454-
self.scratch_path = base_path.joinpath("scratch").mkdir()
454+
self.scratch_path = base_path.joinpath("scratch")
455+
self.scratch_path.mkdir()
455456

456457
# Set our default working directory
457458
kwargs.setdefault("cwd", self.scratch_path)
@@ -988,7 +989,7 @@ def hello():
988989

989990
for fname in files:
990991
path = script.temp_path / fname
991-
path.parent.mkdir()
992+
path.parent.mkdir(exist_ok=True)
992993
path.write_text(files[fname])
993994

994995
retval = script.scratch_path / archive_name

tests/lib/path.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -154,18 +154,19 @@ def exists(self):
154154
"""
155155
return os.path.exists(self)
156156

157-
def mkdir(self, mode=0x1FF, parents=False): # 0o777
157+
def mkdir(self, mode=0x1FF, exist_ok=False, parents=False): # 0o777
158158
"""
159159
Creates a directory, if it doesn't exist already.
160160
161161
:param parents: Whether to create parent directories.
162162
"""
163-
if self.exists():
164-
return self
165163

166164
maker_func = os.makedirs if parents else os.mkdir
167-
maker_func(self, mode)
168-
return self
165+
try:
166+
maker_func(self, mode)
167+
except OSError:
168+
if not exist_ok or not os.path.isdir(self):
169+
raise
169170

170171
def unlink(self):
171172
"""

tests/lib/venv.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ def _create(self, clear=False):
7575
context = builder.ensure_directories(self.location)
7676
builder.create_configuration(context)
7777
builder.setup_python(context)
78-
self.site.mkdir(parents=True)
78+
self.site.mkdir(parents=True, exist_ok=True)
7979
self.sitecustomize = self._sitecustomize
8080
self.user_site_packages = self._user_site_packages
8181

tests/unit/test_collector.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,8 @@ def make_fake_html_response(url):
358358
def test_get_html_page_directory_append_index(tmpdir):
359359
"""`_get_html_page()` should append "index.html" to a directory URL.
360360
"""
361-
dirpath = tmpdir.mkdir("something")
361+
dirpath = tmpdir / "something"
362+
dirpath.mkdir()
362363
dir_url = "file:///{}".format(
363364
urllib_request.pathname2url(dirpath).lstrip("/"),
364365
)

tests/unit/test_compat.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ def test_get_path_uid_without_NOFOLLOW(monkeypatch):
3131
@pytest.mark.skipif("sys.platform == 'win32'")
3232
@pytest.mark.skipif("not hasattr(os, 'symlink')")
3333
def test_get_path_uid_symlink(tmpdir):
34-
f = tmpdir.mkdir("symlink").joinpath("somefile")
34+
f = tmpdir / "symlink" / "somefile"
35+
f.parent.mkdir()
3536
f.write_text("content")
3637
fs = f + '_link'
3738
os.symlink(f, fs)
@@ -43,7 +44,8 @@ def test_get_path_uid_symlink(tmpdir):
4344
@pytest.mark.skipif("not hasattr(os, 'symlink')")
4445
def test_get_path_uid_symlink_without_NOFOLLOW(tmpdir, monkeypatch):
4546
monkeypatch.delattr("os.O_NOFOLLOW")
46-
f = tmpdir.mkdir("symlink").joinpath("somefile")
47+
f = tmpdir / "symlink" / "somefile"
48+
f.parent.mkdir()
4749
f.write_text("content")
4850
fs = f + '_link'
4951
os.symlink(f, fs)

tests/unit/test_locations.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,8 @@ def test_distutils_config_file_read(self, tmpdir, monkeypatch):
9696
# This deals with nt/posix path differences
9797
install_scripts = os.path.normcase(os.path.abspath(
9898
os.path.join(os.path.sep, 'somewhere', 'else')))
99-
f = tmpdir.mkdir("config").joinpath("setup.cfg")
99+
f = tmpdir / "config" / "setup.cfg"
100+
f.parent.mkdir()
100101
f.write_text("[install]\ninstall-scripts=" + install_scripts)
101102
from distutils.dist import Distribution
102103
# patch the function that returns what config files are present
@@ -116,7 +117,8 @@ def test_install_lib_takes_precedence(self, tmpdir, monkeypatch):
116117
# This deals with nt/posix path differences
117118
install_lib = os.path.normcase(os.path.abspath(
118119
os.path.join(os.path.sep, 'somewhere', 'else')))
119-
f = tmpdir.mkdir("config").joinpath("setup.cfg")
120+
f = tmpdir / "config" / "setup.cfg"
121+
f.parent.mkdir()
120122
f.write_text("[install]\ninstall-lib=" + install_lib)
121123
from distutils.dist import Distribution
122124
# patch the function that returns what config files are present

tests/unit/test_utils.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,9 @@ def test_rmtree_errorhandler_readonly_directory(tmpdir):
303303
Test rmtree_errorhandler makes the given read-only directory writable.
304304
"""
305305
# Create read only directory
306-
path = str((tmpdir / 'subdir').mkdir())
306+
subdir_path = tmpdir / 'subdir'
307+
subdir_path.mkdir()
308+
path = str(subdir_path)
307309
os.chmod(path, stat.S_IREAD)
308310

309311
# Make sure mock_func is called with the given path
@@ -321,7 +323,9 @@ def test_rmtree_errorhandler_reraises_error(tmpdir):
321323
by the given unreadable directory.
322324
"""
323325
# Create directory without read permission
324-
path = str((tmpdir / 'subdir').mkdir())
326+
subdir_path = tmpdir / 'subdir'
327+
subdir_path.mkdir()
328+
path = str(subdir_path)
325329
os.chmod(path, stat.S_IWRITE)
326330

327331
mock_func = Mock()

0 commit comments

Comments
 (0)