diff --git a/_pytest/config.py b/_pytest/config.py index 81f39cda485..4e247ff1e94 100644 --- a/_pytest/config.py +++ b/_pytest/config.py @@ -972,11 +972,31 @@ def _initini(self, args): ns, unknown_args = self._parser.parse_known_and_unknown_args(args, namespace=self.option.copy()) r = determine_setup(ns.inifilename, ns.file_or_dir + unknown_args, warnfunc=self.warn) self.rootdir, self.inifile, self.inicfg = r - self._parser.extra_info['rootdir'] = self.rootdir - self._parser.extra_info['inifile'] = self.inifile - self.invocation_dir = py.path.local() self._parser.addini('addopts', 'extra command line options', 'args') self._parser.addini('minversion', 'minimally required pytest version') + self._parser.addini('pathtype', 'path implementation to be used', default='pylib') + + self.invocation_dir = self.make_path() + self.rootdir = self.make_path(self.rootdir) + self.inifile = self.make_path(self.inifile) + self._parser.extra_info['rootdir'] = self.rootdir + self._parser.extra_info['inifile'] = self.inifile + + def make_path(self, input=None): + if input is None: + input = os.getcwd() + pathtype = self.getini('pathtype') + if pathtype == 'pylib': + return py.path.local(input) + elif pathtype == 'pathlib': + # for pythons that dont use fspath + if isinstance(input, py.path.local): + input = str(input) + import pathlib + return pathlib.Path(input) + elif pathtype == 'pathlib2': + import pathlib2 + return pathlib2.Path(input) def _consider_importhook(self, args, entrypoint_name): """Install the PEP 302 import hook if using assertion re-writing. @@ -1311,7 +1331,7 @@ def determine_setup(inifile, args, warnfunc=None): if rootdir is None: rootdir = get_common_ancestor([py.path.local(), ancestor]) is_fs_root = os.path.splitdrive(str(rootdir))[1] == os.sep - if is_fs_root: + if is_fs_root: rootdir = ancestor return rootdir, inifile, inicfg or {} diff --git a/testing/test_config.py b/testing/test_config.py index e6aa423e83a..bcd8f9c58ad 100644 --- a/testing/test_config.py +++ b/testing/test_config.py @@ -4,6 +4,23 @@ from _pytest.config import getcfg, get_common_ancestor, determine_setup from _pytest.main import EXIT_NOTESTSCOLLECTED +try: + from pathlib import Path as PathlibPath +except ImportError: + PathlibPath = None + +try: + from pathlib2 import Path as Pathlib2Path +except ImportError: + Pathlib2Path = None + +PATHIMPLS = { + 'pylib': py.path.local, + 'pathlib': PathlibPath, + 'pathlib2': Pathlib2Path, +} + + class TestParseIni: @pytest.mark.parametrize('section, filename', @@ -73,6 +90,20 @@ def test_toxini_before_lower_pytestini(self, testdir): config = testdir.parseconfigure(sub) assert config.getini("minversion") == "2.0" + @pytest.mark.parametrize('pathimpl, pathtype', sorted(PATHIMPLS.items())) + def test_configure_makepath(self, testdir, pathimpl, pathtype): + if pathtype is None: + pytest.skip( + "{} path implementation is missing, not testing" + .format(pathimpl)) + testdir.makeini(""" + [pytest] + pathtype = {} + """.format(pathimpl)) + # disable cacheprovider to get out of breaking bad + config = testdir.parseconfigure('-p', 'no:cacheprovider') + assert isinstance(config.make_path(testdir.tmpdir), pathtype) + @pytest.mark.xfail(reason="probably not needed") def test_confcutdir(self, testdir): sub = testdir.mkdir("sub") @@ -809,4 +840,7 @@ def test_with_existing_file_in_subdir(self, tmpdir): rootdir, inifile, inicfg = determine_setup(None, ['a/exist']) assert rootdir == tmpdir assert inifile is None + + +