Skip to content

Commit 2656785

Browse files
introduce config.make_path - beginning of supporting other path implementations
1 parent 3b47cb4 commit 2656785

File tree

2 files changed

+57
-4
lines changed

2 files changed

+57
-4
lines changed

_pytest/config.py

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -972,11 +972,31 @@ def _initini(self, args):
972972
ns, unknown_args = self._parser.parse_known_and_unknown_args(args, namespace=self.option.copy())
973973
r = determine_setup(ns.inifilename, ns.file_or_dir + unknown_args, warnfunc=self.warn)
974974
self.rootdir, self.inifile, self.inicfg = r
975-
self._parser.extra_info['rootdir'] = self.rootdir
976-
self._parser.extra_info['inifile'] = self.inifile
977-
self.invocation_dir = py.path.local()
978975
self._parser.addini('addopts', 'extra command line options', 'args')
979976
self._parser.addini('minversion', 'minimally required pytest version')
977+
self._parser.addini('pathtype', 'path implementation to be used', default='pylib')
978+
979+
self.invocation_dir = self.make_path()
980+
self.rootdir = self.make_path(self.rootdir)
981+
self.inifile = self.make_path(self.inifile)
982+
self._parser.extra_info['rootdir'] = self.rootdir
983+
self._parser.extra_info['inifile'] = self.inifile
984+
985+
def make_path(self, input=None):
986+
if input is None:
987+
input = os.getcwd()
988+
pathtype = self.getini('pathtype')
989+
if pathtype == 'pylib':
990+
return py.path.local(input)
991+
elif pathtype == 'pathlib':
992+
# for pythons that dont use fspath
993+
if isinstance(input, py.path.local):
994+
input = str(input)
995+
import pathlib
996+
return pathlib.Path(input)
997+
elif pathtype == 'pathlib2':
998+
import pathlib2
999+
return pathlib2.Path(input)
9801000

9811001
def _consider_importhook(self, args, entrypoint_name):
9821002
"""Install the PEP 302 import hook if using assertion re-writing.
@@ -1311,7 +1331,7 @@ def determine_setup(inifile, args, warnfunc=None):
13111331
if rootdir is None:
13121332
rootdir = get_common_ancestor([py.path.local(), ancestor])
13131333
is_fs_root = os.path.splitdrive(str(rootdir))[1] == os.sep
1314-
if is_fs_root:
1334+
if is_fs_root:
13151335
rootdir = ancestor
13161336
return rootdir, inifile, inicfg or {}
13171337

testing/test_config.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,23 @@
44
from _pytest.config import getcfg, get_common_ancestor, determine_setup
55
from _pytest.main import EXIT_NOTESTSCOLLECTED
66

7+
try:
8+
from pathlib import Path as PathlibPath
9+
except ImportError:
10+
PathlibPath = None
11+
12+
try:
13+
from pathlib2 import Path as Pathlib2Path
14+
except ImportError:
15+
Pathlib2Path = None
16+
17+
PATHIMPLS = {
18+
'pylib': py.path.local,
19+
'pathlib': PathlibPath,
20+
'pathlib2': Pathlib2Path,
21+
}
22+
23+
724
class TestParseIni:
825

926
@pytest.mark.parametrize('section, filename',
@@ -73,6 +90,19 @@ def test_toxini_before_lower_pytestini(self, testdir):
7390
config = testdir.parseconfigure(sub)
7491
assert config.getini("minversion") == "2.0"
7592

93+
@pytest.mark.parametrize('pathimpl, pathtype', sorted(PATHIMPLS.items()))
94+
def test_configure_makepath(self, testdir, pathimpl, pathtype):
95+
if pathtype is None:
96+
pytest.skip(
97+
"{} path implementation is missing, not testing"
98+
.format(pathimpl))
99+
testdir.makeini("""
100+
[pytest]
101+
pathtype = {}
102+
""".format(pathimpl))
103+
config = testdir.parseconfigure('-p', 'no:cacheprovider')
104+
assert isinstance(config.make_path(testdir.tmpdir), pathtype)
105+
76106
@pytest.mark.xfail(reason="probably not needed")
77107
def test_confcutdir(self, testdir):
78108
sub = testdir.mkdir("sub")
@@ -809,4 +839,7 @@ def test_with_existing_file_in_subdir(self, tmpdir):
809839
rootdir, inifile, inicfg = determine_setup(None, ['a/exist'])
810840
assert rootdir == tmpdir
811841
assert inifile is None
842+
843+
844+
812845

0 commit comments

Comments
 (0)