Skip to content

Commit 791faea

Browse files
authored
Allow passing config directory without filename (#2550)
Fixes #2340
1 parent 4b7d1ff commit 791faea

File tree

5 files changed

+46
-2
lines changed

5 files changed

+46
-2
lines changed

docs/changelog/2340.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Allow passing config directory without filename. - by :user:`ssbarnea`.

docs/config.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,9 @@ examples first and use this page as a reference.
7979

8080
.. code-block:: toml
8181
82-
[tool:tox]
82+
[tool.tox]
8383
legacy_tox_ini = """
84+
[tox]
8485
min_version = 4.0
8586
env_list =
8687
py310

src/tox/config/cli/parser.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ def add_core_arguments(parser: ArgumentParser) -> None:
316316
default=None,
317317
type=Path,
318318
of_type=Optional[Path],
319-
help="configuration file for tox (if not specified will discover one)",
319+
help="configuration file/folder for tox (if not specified will discover one)",
320320
)
321321
parser.add_argument(
322322
"--workdir",

src/tox/config/source/discover.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,17 @@ def discover_source(config_file: Path | None, root_dir: Path | None) -> Source:
2626
src = _locate_source()
2727
if src is None:
2828
src = _create_default_source(root_dir)
29+
elif config_file.is_dir():
30+
src = None
31+
for src_type in SOURCE_TYPES:
32+
candidate: Path = config_file / src_type.FILENAME
33+
try:
34+
src = src_type(candidate)
35+
break
36+
except ValueError:
37+
continue
38+
if src is None:
39+
raise HandledError(f"could not find any config file in {config_file}")
2940
else:
3041
src = _load_exact_source(config_file)
3142
return src

tests/config/cli/test_cli_ini.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@
1111

1212
from tox.config.cli.ini import IniConfig
1313
from tox.config.cli.parse import get_options
14+
from tox.config.cli.parser import Parsed
1415
from tox.config.loader.api import Override
16+
from tox.config.main import Config
17+
from tox.config.source import discover_source
1518
from tox.pytest import CaptureFixture, LogCaptureFixture, MonkeyPatch
1619
from tox.session.env_select import CliEnv
1720
from tox.session.state import State
@@ -199,3 +202,31 @@ def test_cli_ini_with_interpolated(tmp_path: Path, monkeypatch: MonkeyPatch) ->
199202
monkeypatch.setenv("TOX_CONFIG_FILE", str(to))
200203
conf = IniConfig()
201204
assert conf.get("a", str)
205+
206+
207+
@pytest.mark.parametrize(
208+
("conf_arg", "filename", "content"),
209+
[
210+
pytest.param("", "tox.ini", "[tox]", id="ini-dir"),
211+
pytest.param("tox.ini", "tox.ini", "[tox]", id="ini"),
212+
pytest.param("", "setup.cfg", "[tox:tox]", id="cfg-dir"),
213+
pytest.param("setup.cfg", "setup.cfg", "[tox:tox]", id="cfg"),
214+
pytest.param("", "pyproject.toml", '[tool.tox]\nlegacy_tox_ini = """\n[tox]\n"""\n', id="toml-dir"),
215+
pytest.param("pyproject.toml", "pyproject.toml", '[tool.tox]\nlegacy_tox_ini = """\n[tox]\n"""\n', id="toml"),
216+
],
217+
)
218+
def test_conf_arg(tmp_path: Path, conf_arg: str, filename: str, content: str) -> None:
219+
dest = tmp_path / "c"
220+
dest.mkdir()
221+
if filename:
222+
cfg = dest / filename
223+
cfg.write_bytes(content.encode(encoding="utf-8"))
224+
225+
config_file = dest / conf_arg
226+
source = discover_source(config_file, None)
227+
228+
Config.make(
229+
Parsed(work_dir=dest, override=[], config_file=config_file, root_dir=None),
230+
pos_args=[],
231+
source=source,
232+
)

0 commit comments

Comments
 (0)