Merge pull request #6839 from blueyed/typing-get_dirs_from_args-parseconfig

typing: get_dirs_from_args, parseconfig
This commit is contained in:
Ran Benita 2020-05-05 21:11:26 +03:00 committed by GitHub
commit 46d768503e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 27 additions and 23 deletions

View File

@ -86,14 +86,14 @@ def get_common_ancestor(paths: Iterable[py.path.local]) -> py.path.local:
return common_ancestor return common_ancestor
def get_dirs_from_args(args): def get_dirs_from_args(args: Iterable[str]) -> List[py.path.local]:
def is_option(x): def is_option(x: str) -> bool:
return str(x).startswith("-") return x.startswith("-")
def get_file_part_from_node_id(x): def get_file_part_from_node_id(x: str) -> str:
return str(x).split("::")[0] return x.split("::")[0]
def get_dir_from_path(path): def get_dir_from_path(path: py.path.local) -> py.path.local:
if path.isdir(): if path.isdir():
return path return path
return py.path.local(path.dirname) return py.path.local(path.dirname)

View File

@ -29,6 +29,7 @@ from _pytest.capture import MultiCapture
from _pytest.capture import SysCapture from _pytest.capture import SysCapture
from _pytest.compat import TYPE_CHECKING from _pytest.compat import TYPE_CHECKING
from _pytest.config import _PluggyPlugin from _pytest.config import _PluggyPlugin
from _pytest.config import Config
from _pytest.config import ExitCode from _pytest.config import ExitCode
from _pytest.fixtures import FixtureRequest from _pytest.fixtures import FixtureRequest
from _pytest.main import Session from _pytest.main import Session
@ -995,7 +996,7 @@ class Testdir:
args.append("--basetemp=%s" % self.tmpdir.dirpath("basetemp")) args.append("--basetemp=%s" % self.tmpdir.dirpath("basetemp"))
return args return args
def parseconfig(self, *args): def parseconfig(self, *args: Union[str, py.path.local]) -> Config:
"""Return a new pytest Config instance from given commandline args. """Return a new pytest Config instance from given commandline args.
This invokes the pytest bootstrapping code in _pytest.config to create This invokes the pytest bootstrapping code in _pytest.config to create
@ -1011,7 +1012,7 @@ class Testdir:
import _pytest.config import _pytest.config
config = _pytest.config._prepareconfig(args, self.plugins) config = _pytest.config._prepareconfig(args, self.plugins) # type: Config
# we don't know what the test will do with this half-setup config # we don't know what the test will do with this half-setup config
# object and thus we make sure it gets unconfigured properly in any # object and thus we make sure it gets unconfigured properly in any
# case (otherwise capturing could still be active, for example) # case (otherwise capturing could still be active, for example)

View File

@ -22,6 +22,7 @@ from _pytest.assertion.rewrite import rewrite_asserts
from _pytest.config import ExitCode from _pytest.config import ExitCode
from _pytest.pathlib import make_numbered_dir from _pytest.pathlib import make_numbered_dir
from _pytest.pathlib import Path from _pytest.pathlib import Path
from _pytest.pytester import Testdir
def rewrite(src): def rewrite(src):
@ -943,11 +944,11 @@ class TestAssertionRewriteHookDetails:
) )
assert testdir.runpytest().ret == 0 assert testdir.runpytest().ret == 0
def test_write_pyc(self, testdir, tmpdir, monkeypatch): def test_write_pyc(self, testdir: Testdir, tmpdir, monkeypatch) -> None:
from _pytest.assertion.rewrite import _write_pyc from _pytest.assertion.rewrite import _write_pyc
from _pytest.assertion import AssertionState from _pytest.assertion import AssertionState
config = testdir.parseconfig([]) config = testdir.parseconfig()
state = AssertionState(config, "rewrite") state = AssertionState(config, "rewrite")
source_path = str(tmpdir.ensure("source.py")) source_path = str(tmpdir.ensure("source.py"))
pycpath = tmpdir.join("pyc").strpath pycpath = tmpdir.join("pyc").strpath

View File

@ -3,6 +3,8 @@ import re
import sys import sys
import textwrap import textwrap
import py.path
import _pytest._code import _pytest._code
import pytest import pytest
from _pytest.compat import importlib_metadata from _pytest.compat import importlib_metadata
@ -900,55 +902,55 @@ class TestRootdir:
assert get_common_ancestor([no_path.join("a")]) == tmpdir assert get_common_ancestor([no_path.join("a")]) == tmpdir
@pytest.mark.parametrize("name", "setup.cfg tox.ini pytest.ini".split()) @pytest.mark.parametrize("name", "setup.cfg tox.ini pytest.ini".split())
def test_with_ini(self, tmpdir, name) -> None: def test_with_ini(self, tmpdir: py.path.local, name: str) -> None:
inifile = tmpdir.join(name) inifile = tmpdir.join(name)
inifile.write("[pytest]\n" if name != "setup.cfg" else "[tool:pytest]\n") inifile.write("[pytest]\n" if name != "setup.cfg" else "[tool:pytest]\n")
a = tmpdir.mkdir("a") a = tmpdir.mkdir("a")
b = a.mkdir("b") b = a.mkdir("b")
for args in ([tmpdir], [a], [b]): for args in ([str(tmpdir)], [str(a)], [str(b)]):
rootdir, parsed_inifile, _ = determine_setup(None, args) rootdir, parsed_inifile, _ = determine_setup(None, args)
assert rootdir == tmpdir assert rootdir == tmpdir
assert parsed_inifile == inifile assert parsed_inifile == inifile
rootdir, parsed_inifile, _ = determine_setup(None, [b, a]) rootdir, parsed_inifile, _ = determine_setup(None, [str(b), str(a)])
assert rootdir == tmpdir assert rootdir == tmpdir
assert parsed_inifile == inifile assert parsed_inifile == inifile
@pytest.mark.parametrize("name", "setup.cfg tox.ini".split()) @pytest.mark.parametrize("name", "setup.cfg tox.ini".split())
def test_pytestini_overrides_empty_other(self, tmpdir, name) -> None: def test_pytestini_overrides_empty_other(self, tmpdir: py.path.local, name) -> None:
inifile = tmpdir.ensure("pytest.ini") inifile = tmpdir.ensure("pytest.ini")
a = tmpdir.mkdir("a") a = tmpdir.mkdir("a")
a.ensure(name) a.ensure(name)
rootdir, parsed_inifile, _ = determine_setup(None, [a]) rootdir, parsed_inifile, _ = determine_setup(None, [str(a)])
assert rootdir == tmpdir assert rootdir == tmpdir
assert parsed_inifile == inifile assert parsed_inifile == inifile
def test_setuppy_fallback(self, tmpdir) -> None: def test_setuppy_fallback(self, tmpdir: py.path.local) -> None:
a = tmpdir.mkdir("a") a = tmpdir.mkdir("a")
a.ensure("setup.cfg") a.ensure("setup.cfg")
tmpdir.ensure("setup.py") tmpdir.ensure("setup.py")
rootdir, inifile, inicfg = determine_setup(None, [a]) rootdir, inifile, inicfg = determine_setup(None, [str(a)])
assert rootdir == tmpdir assert rootdir == tmpdir
assert inifile is None assert inifile is None
assert inicfg == {} assert inicfg == {}
def test_nothing(self, tmpdir, monkeypatch) -> None: def test_nothing(self, tmpdir: py.path.local, monkeypatch) -> None:
monkeypatch.chdir(str(tmpdir)) monkeypatch.chdir(str(tmpdir))
rootdir, inifile, inicfg = determine_setup(None, [tmpdir]) rootdir, inifile, inicfg = determine_setup(None, [str(tmpdir)])
assert rootdir == tmpdir assert rootdir == tmpdir
assert inifile is None assert inifile is None
assert inicfg == {} assert inicfg == {}
def test_with_specific_inifile(self, tmpdir) -> None: def test_with_specific_inifile(self, tmpdir: py.path.local) -> None:
inifile = tmpdir.ensure("pytest.ini") inifile = tmpdir.ensure("pytest.ini")
rootdir, _, _ = determine_setup(inifile, [tmpdir]) rootdir, _, _ = determine_setup(str(inifile), [str(tmpdir)])
assert rootdir == tmpdir assert rootdir == tmpdir
def test_with_arg_outside_cwd_without_inifile(self, tmpdir, monkeypatch) -> None: def test_with_arg_outside_cwd_without_inifile(self, tmpdir, monkeypatch) -> None:
monkeypatch.chdir(str(tmpdir)) monkeypatch.chdir(str(tmpdir))
a = tmpdir.mkdir("a") a = tmpdir.mkdir("a")
b = tmpdir.mkdir("b") b = tmpdir.mkdir("b")
rootdir, inifile, _ = determine_setup(None, [a, b]) rootdir, inifile, _ = determine_setup(None, [str(a), str(b)])
assert rootdir == tmpdir assert rootdir == tmpdir
assert inifile is None assert inifile is None
@ -956,7 +958,7 @@ class TestRootdir:
a = tmpdir.mkdir("a") a = tmpdir.mkdir("a")
b = tmpdir.mkdir("b") b = tmpdir.mkdir("b")
inifile = a.ensure("pytest.ini") inifile = a.ensure("pytest.ini")
rootdir, parsed_inifile, _ = determine_setup(None, [a, b]) rootdir, parsed_inifile, _ = determine_setup(None, [str(a), str(b)])
assert rootdir == a assert rootdir == a
assert inifile == parsed_inifile assert inifile == parsed_inifile