Skip to content

Commit 29bb0ed

Browse files
Oberon00nicoddemus
andcommitted
Move _uniquepath to pathlib as unique_path.
Co-authored-by: Bruno Oliveira <[email protected]>
1 parent a98270e commit 29bb0ed

File tree

3 files changed

+19
-12
lines changed

3 files changed

+19
-12
lines changed

src/_pytest/config/__init__.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,13 @@
3030
from _pytest.compat import importlib_metadata
3131
from _pytest.outcomes import fail
3232
from _pytest.outcomes import Skipped
33+
from _pytest.pathlib import unique_path
3334
from _pytest.warning_types import PytestConfigWarning
3435

3536
hookimpl = HookimplMarker("pytest")
3637
hookspec = HookspecMarker("pytest")
3738

3839

39-
def _uniquepath(path):
40-
return type(path)(os.path.normcase(str(path.realpath())))
41-
42-
4340
class ConftestImportFailure(Exception):
4441
def __init__(self, path, excinfo):
4542
Exception.__init__(self, path, excinfo)
@@ -370,7 +367,7 @@ def _set_initial_conftests(self, namespace):
370367
"""
371368
current = py.path.local()
372369
self._confcutdir = (
373-
_uniquepath(current.join(namespace.confcutdir, abs=True))
370+
unique_path(current.join(namespace.confcutdir, abs=True))
374371
if namespace.confcutdir
375372
else None
376373
)
@@ -409,7 +406,7 @@ def _getconftestmodules(self, path):
409406
else:
410407
directory = path
411408

412-
directory = _uniquepath(directory)
409+
directory = unique_path(directory)
413410

414411
# XXX these days we may rather want to use config.rootdir
415412
# and allow users to opt into looking into the rootdir parent
@@ -438,7 +435,7 @@ def _importconftest(self, conftestpath):
438435
# Use realpath to avoid loading the same conftest twice
439436
# with build systems that create build directories containing
440437
# symlinks to actual files.
441-
conftestpath = _uniquepath(conftestpath)
438+
conftestpath = unique_path(conftestpath)
442439
try:
443440
return self._conftestpath2mod[conftestpath]
444441
except KeyError:

src/_pytest/pathlib.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from os.path import expanduser
1212
from os.path import expandvars
1313
from os.path import isabs
14+
from os.path import normcase
1415
from os.path import sep
1516
from posixpath import sep as posix_sep
1617

@@ -334,3 +335,12 @@ def fnmatch_ex(pattern, path):
334335
def parts(s):
335336
parts = s.split(sep)
336337
return {sep.join(parts[: i + 1]) or sep for i in range(len(parts))}
338+
339+
340+
def unique_path(path):
341+
"""Returns a unique path in case-insensitive (but case-preserving) file
342+
systems such as Windows.
343+
344+
This is needed only for ``py.path.local``; ``pathlib.Path`` handles this
345+
natively with ``resolve()``."""
346+
return type(path)(normcase(str(path.realpath())))

testing/test_conftest.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
import py
55

66
import pytest
7-
from _pytest.config import _uniquepath
87
from _pytest.config import PytestPluginManager
98
from _pytest.main import ExitCode
9+
from _pytest.pathlib import unique_path
1010

1111

1212
def ConftestWithSetinitial(path):
@@ -143,11 +143,11 @@ def test_conftestcutdir(testdir):
143143
# but we can still import a conftest directly
144144
conftest._importconftest(conf)
145145
values = conftest._getconftestmodules(conf.dirpath())
146-
assert values[0].__file__.startswith(str(_uniquepath(conf)))
146+
assert values[0].__file__.startswith(str(unique_path(conf)))
147147
# and all sub paths get updated properly
148148
values = conftest._getconftestmodules(p)
149149
assert len(values) == 1
150-
assert values[0].__file__.startswith(str(_uniquepath(conf)))
150+
assert values[0].__file__.startswith(str(unique_path(conf)))
151151

152152

153153
def test_conftestcutdir_inplace_considered(testdir):
@@ -156,7 +156,7 @@ def test_conftestcutdir_inplace_considered(testdir):
156156
conftest_setinitial(conftest, [conf.dirpath()], confcutdir=conf.dirpath())
157157
values = conftest._getconftestmodules(conf.dirpath())
158158
assert len(values) == 1
159-
assert values[0].__file__.startswith(str(_uniquepath(conf)))
159+
assert values[0].__file__.startswith(str(unique_path(conf)))
160160

161161

162162
@pytest.mark.parametrize("name", "test tests whatever .dotdir".split())
@@ -166,7 +166,7 @@ def test_setinitial_conftest_subdirs(testdir, name):
166166
conftest = PytestPluginManager()
167167
conftest_setinitial(conftest, [sub.dirpath()], confcutdir=testdir.tmpdir)
168168
if name not in ("whatever", ".dotdir"):
169-
assert _uniquepath(subconftest) in conftest._conftestpath2mod
169+
assert unique_path(subconftest) in conftest._conftestpath2mod
170170
assert len(conftest._conftestpath2mod) == 1
171171
else:
172172
assert subconftest not in conftest._conftestpath2mod

0 commit comments

Comments
 (0)