Skip to content

Commit

Permalink
Move _uniquepath to pathlib as unique_path.
Browse files Browse the repository at this point in the history
Co-authored-by: Bruno Oliveira <[email protected]>
  • Loading branch information
Oberon00 and nicoddemus committed Aug 28, 2019
1 parent a98270e commit 29bb0ed
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 12 deletions.
11 changes: 4 additions & 7 deletions src/_pytest/config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,13 @@
from _pytest.compat import importlib_metadata
from _pytest.outcomes import fail
from _pytest.outcomes import Skipped
from _pytest.pathlib import unique_path
from _pytest.warning_types import PytestConfigWarning

hookimpl = HookimplMarker("pytest")
hookspec = HookspecMarker("pytest")


def _uniquepath(path):
return type(path)(os.path.normcase(str(path.realpath())))


class ConftestImportFailure(Exception):
def __init__(self, path, excinfo):
Exception.__init__(self, path, excinfo)
Expand Down Expand Up @@ -370,7 +367,7 @@ def _set_initial_conftests(self, namespace):
"""
current = py.path.local()
self._confcutdir = (
_uniquepath(current.join(namespace.confcutdir, abs=True))
unique_path(current.join(namespace.confcutdir, abs=True))
if namespace.confcutdir
else None
)
Expand Down Expand Up @@ -409,7 +406,7 @@ def _getconftestmodules(self, path):
else:
directory = path

directory = _uniquepath(directory)
directory = unique_path(directory)

# XXX these days we may rather want to use config.rootdir
# and allow users to opt into looking into the rootdir parent
Expand Down Expand Up @@ -438,7 +435,7 @@ def _importconftest(self, conftestpath):
# Use realpath to avoid loading the same conftest twice
# with build systems that create build directories containing
# symlinks to actual files.
conftestpath = _uniquepath(conftestpath)
conftestpath = unique_path(conftestpath)
try:
return self._conftestpath2mod[conftestpath]
except KeyError:
Expand Down
10 changes: 10 additions & 0 deletions src/_pytest/pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from os.path import expanduser
from os.path import expandvars
from os.path import isabs
from os.path import normcase
from os.path import sep
from posixpath import sep as posix_sep

Expand Down Expand Up @@ -334,3 +335,12 @@ def fnmatch_ex(pattern, path):
def parts(s):
parts = s.split(sep)
return {sep.join(parts[: i + 1]) or sep for i in range(len(parts))}


def unique_path(path):
"""Returns a unique path in case-insensitive (but case-preserving) file
systems such as Windows.
This is needed only for ``py.path.local``; ``pathlib.Path`` handles this
natively with ``resolve()``."""
return type(path)(normcase(str(path.realpath())))
10 changes: 5 additions & 5 deletions testing/test_conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
import py

import pytest
from _pytest.config import _uniquepath
from _pytest.config import PytestPluginManager
from _pytest.main import ExitCode
from _pytest.pathlib import unique_path


def ConftestWithSetinitial(path):
Expand Down Expand Up @@ -143,11 +143,11 @@ def test_conftestcutdir(testdir):
# but we can still import a conftest directly
conftest._importconftest(conf)
values = conftest._getconftestmodules(conf.dirpath())
assert values[0].__file__.startswith(str(_uniquepath(conf)))
assert values[0].__file__.startswith(str(unique_path(conf)))
# and all sub paths get updated properly
values = conftest._getconftestmodules(p)
assert len(values) == 1
assert values[0].__file__.startswith(str(_uniquepath(conf)))
assert values[0].__file__.startswith(str(unique_path(conf)))


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


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

0 comments on commit 29bb0ed

Please sign in to comment.