#1642 Fix rootdir option

This commit is contained in:
feuillemorte 2018-01-20 22:30:01 +03:00
parent a7c39c894b
commit 83034bbd48
3 changed files with 12 additions and 34 deletions

View File

@ -990,11 +990,15 @@ class Config(object):
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)
rootdir = ns.rootdir if ns.rootdir else None
r = determine_setup(ns.inifilename, ns.file_or_dir + unknown_args, warnfunc=self.warn, rootdir_cmd_arg=rootdir)
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()
if ns.rootdir:
self.invocation_dir = self.rootdir
sys.path.append(str(self.rootdir))
self._parser.addini('addopts', 'extra command line options', 'args')
self._parser.addini('minversion', 'minimally required pytest version')
self._override_ini = ns.override_ini or ()
@ -1323,7 +1327,7 @@ def get_dirs_from_args(args):
]
def determine_setup(inifile, args, warnfunc=None):
def determine_setup(inifile, args, warnfunc=None, rootdir_cmd_arg=None):
dirs = get_dirs_from_args(args)
if inifile:
iniconfig = py.iniconfig.IniConfig(inifile)
@ -1346,6 +1350,11 @@ def determine_setup(inifile, args, warnfunc=None):
is_fs_root = os.path.splitdrive(str(rootdir))[1] == '/'
if is_fs_root:
rootdir = ancestor
if rootdir_cmd_arg:
rootdir_abs_path = py.path.local(rootdir_cmd_arg)
if not os.path.isdir(str(rootdir_abs_path)):
raise UsageError("Directory '{}' not found. Check your '--rootdir' option.".format(rootdir_abs_path))
rootdir = rootdir_abs_path
return rootdir, inifile, inicfg or {}

View File

@ -33,9 +33,6 @@ def pytest_addoption(parser):
parser.addini("testpaths", "directories to search for tests when no files or directories are given in the "
"command line.",
type="args", default=[])
parser.addini("rootdir", "define root directory for tests. If this parameter defined command argument "
"'--rootdir' will not work",
type="args", default=[])
# parser.addini("dirpatterns",
# "patterns specifying possible locations of test files",
# type="linelist", default=["**/test_*.txt",
@ -60,7 +57,7 @@ def pytest_addoption(parser):
dest="rootdir",
help="Define root directory for tests. Can be relative path: 'root_dir', './root_dir', "
"'root_dir/another_dir/'; absolute path: '/home/user/root_dir'; path with variables: "
"'$HOME/root_dir'. If parameter 'rootdir' defined in *.ini file this argument will not work")
"'$HOME/root_dir'.")
group = parser.getgroup("collect", "collection")
group.addoption('--collectonly', '--collect-only', action="store_true",
@ -292,16 +289,6 @@ class Session(nodes.FSCollector):
self._norecursepatterns = config.getini("norecursedirs")
self.startdir = py.path.local()
rootdir_ini = config.getini('rootdir')
self.rootdir = rootdir_ini[0] if rootdir_ini else config.option.rootdir
if self.rootdir:
rootdir_abs_path = py.path.local(self.rootdir)
if not os.path.isdir(str(rootdir_abs_path)):
raise UsageError("Directory '{}' not found. Check your '--rootdir' option.".format(rootdir_abs_path))
config.invocation_dir = rootdir_abs_path
config.rootdir = rootdir_abs_path
sys.path.append(str(rootdir_abs_path))
self.config.pluginmanager.register(self, name="session")
def _makeid(self):

View File

@ -266,21 +266,3 @@ def test_rootdir_option_arg(testdir):
result = testdir.runpytest("--rootdir=root")
result.stdout.fnmatch_lines(["*1 passed*"])
def test_rootdir_option_ini_file(testdir):
rootdir = testdir.mkdir("root")
rootdir.join("spoon.py").write("spoon_number = 1")
testsdir = rootdir.mkdir("tests")
testsdir.join("test_one.py").write("from spoon import spoon_number\ndef test_one():\n assert spoon_number")
result = testdir.runpytest()
result.stdout.fnmatch_lines(["*No module named*spoon*"])
testdir.makeini("""
[pytest]
rootdir=root
""")
result = testdir.runpytest()
result.stdout.fnmatch_lines(["*1 passed*"])
result = testdir.runpytest("--rootdir=ignored_argument")
result.stdout.fnmatch_lines(["*1 passed*"])