Skip to content

Commit d97d44a

Browse files
committed
config: extract initial paths/nodeids args logic to a function
Will be reused in the next commit.
1 parent 797b924 commit d97d44a

File tree

1 file changed

+51
-25
lines changed

1 file changed

+51
-25
lines changed

src/_pytest/config/__init__.py

+51-25
Original file line numberDiff line numberDiff line change
@@ -1223,6 +1223,49 @@ def _validate_args(self, args: List[str], via: str) -> List[str]:
12231223

12241224
return args
12251225

1226+
def _decide_args(
1227+
self,
1228+
*,
1229+
args: List[str],
1230+
pyargs: List[str],
1231+
testpaths: List[str],
1232+
invocation_dir: Path,
1233+
rootpath: Path,
1234+
warn: bool,
1235+
) -> Tuple[List[str], ArgsSource]:
1236+
"""Decide the args (initial paths/nodeids) to use given the relevant inputs.
1237+
1238+
:param warn: Whether can issue warnings.
1239+
"""
1240+
if args:
1241+
source = Config.ArgsSource.ARGS
1242+
result = args
1243+
else:
1244+
if invocation_dir == rootpath:
1245+
source = Config.ArgsSource.TESTPATHS
1246+
if pyargs:
1247+
result = testpaths
1248+
else:
1249+
result = []
1250+
for path in testpaths:
1251+
result.extend(sorted(glob.iglob(path, recursive=True)))
1252+
if testpaths and not result:
1253+
if warn:
1254+
warning_text = (
1255+
"No files were found in testpaths; "
1256+
"consider removing or adjusting your testpaths configuration. "
1257+
"Searching recursively from the current directory instead."
1258+
)
1259+
self.issue_config_time_warning(
1260+
PytestConfigWarning(warning_text), stacklevel=3
1261+
)
1262+
else:
1263+
result = []
1264+
if not result:
1265+
source = Config.ArgsSource.INCOVATION_DIR
1266+
result = [str(invocation_dir)]
1267+
return result, source
1268+
12261269
def _preparse(self, args: List[str], addopts: bool = True) -> None:
12271270
if addopts:
12281271
env_addopts = os.environ.get("PYTEST_ADDOPTS", "")
@@ -1371,34 +1414,17 @@ def parse(self, args: List[str], addopts: bool = True) -> None:
13711414
self.hook.pytest_cmdline_preparse(config=self, args=args)
13721415
self._parser.after_preparse = True # type: ignore
13731416
try:
1374-
source = Config.ArgsSource.ARGS
13751417
args = self._parser.parse_setoption(
13761418
args, self.option, namespace=self.option
13771419
)
1378-
if not args:
1379-
if self.invocation_params.dir == self.rootpath:
1380-
source = Config.ArgsSource.TESTPATHS
1381-
testpaths: List[str] = self.getini("testpaths")
1382-
if self.known_args_namespace.pyargs:
1383-
args = testpaths
1384-
else:
1385-
args = []
1386-
for path in testpaths:
1387-
args.extend(sorted(glob.iglob(path, recursive=True)))
1388-
if testpaths and not args:
1389-
warning_text = (
1390-
"No files were found in testpaths; "
1391-
"consider removing or adjusting your testpaths configuration. "
1392-
"Searching recursively from the current directory instead."
1393-
)
1394-
self.issue_config_time_warning(
1395-
PytestConfigWarning(warning_text), stacklevel=3
1396-
)
1397-
if not args:
1398-
source = Config.ArgsSource.INCOVATION_DIR
1399-
args = [str(self.invocation_params.dir)]
1400-
self.args = args
1401-
self.args_source = source
1420+
self.args, self.args_source = self._decide_args(
1421+
args=args,
1422+
pyargs=self.known_args_namespace.pyargs,
1423+
testpaths=self.getini("testpaths"),
1424+
invocation_dir=self.invocation_params.dir,
1425+
rootpath=self.rootpath,
1426+
warn=True,
1427+
)
14021428
except PrintHelp:
14031429
pass
14041430

0 commit comments

Comments
 (0)