Skip to content

Commit 32cda6d

Browse files
committed
Do not use setup.cfg file that does not have tox:tox namespace
When tox is invoked without a tox configuration file (and without command line options that could be used instead), it fails with: $ tox ERROR: tox config file (either pyproject.toml, tox.ini, setup.cfg) not found This is useful: If there is no config, test didn't run and such error should not pass silently. However, until this commit, when there was a setup.cfg file it was considered as a tox config even when there was no [tox:tox] section: $ tox GLOB sdist-make: .../setup.py python create: .../.tox/python python inst: ... python installed: ... python run-test-pre: PYTHONHASHSEED='1234567890' ___________________________________ summary ____________________________________ python: commands succeeded congratulations :) That is not safe, because setup.cfg can exist even if there is no tox configuration in it and teh success creates a false sense of safety. To fix this and to match the behavior of pyproject.toml without a tox section, tox now skips setup.cfg, if there is no [tox:tox] section. Fixes tox-dev#1045
1 parent 9d01182 commit 32cda6d

File tree

4 files changed

+30
-2
lines changed

4 files changed

+30
-2
lines changed

docs/changelog/1045.bugfix.rst

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
skip ``setup.cfg`` if it has no ``tox:tox`` namespace - by :user:`hroncok`

docs/config.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ At the moment tox supports three configuration locations prioritized in the foll
1515
As far as the configuration format at the moment we only support standard ConfigParser_ "ini-style" format
1616
(there is a plan to add a pure TOML one soon).
1717
``tox.ini`` and ``setup.cfg`` are such files. Note that ``setup.cfg`` requires the content to be under the
18-
``tox:tox`` section. ``pyproject.toml`` on the other hand is in TOML format. However, one can inline
18+
``tox:tox`` section and is otherwise ignored. ``pyproject.toml`` on the other hand is in TOML format. However, one can inline
1919
the *ini-style* format under the ``tool.tox.legacy_tox_ini`` key as a multi-line string.
2020

2121
Below you find the specification for the *ini-style* format, but you might want to skim some

src/tox/config/__init__.py

+13-1
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,10 @@ def parseconfig(args, plugins=()):
271271
content = toml_content["tool"]["tox"]["legacy_tox_ini"]
272272
except KeyError:
273273
continue
274-
ParseIni(config, config_file, content)
274+
try:
275+
ParseIni(config, config_file, content)
276+
except SkipThisIni:
277+
continue
275278
pm.hook.tox_configure(config=config) # post process config object
276279
break
277280
else:
@@ -1045,13 +1048,22 @@ def make_hashseed():
10451048
return str(random.randint(1, max_seed))
10461049

10471050

1051+
class SkipThisIni(Exception):
1052+
"""Internal exception to indicate the parsed ini file should be skipped"""
1053+
1054+
10481055
class ParseIni(object):
10491056
def __init__(self, config, ini_path, ini_data): # noqa
10501057
config.toxinipath = ini_path
10511058
using("tox.ini: {} (pid {})".format(config.toxinipath, os.getpid()))
10521059
config.toxinidir = config.toxinipath.dirpath()
10531060

10541061
self._cfg = py.iniconfig.IniConfig(config.toxinipath, ini_data)
1062+
1063+
if ini_path.basename == "setup.cfg" and "tox:tox" not in self._cfg:
1064+
verbosity1("Found no [tox:tox] section in setup.cfg, skipping.")
1065+
raise SkipThisIni()
1066+
10551067
previous_line_of = self._cfg.lineof
10561068

10571069
self.expand_section_names(self._cfg)

tests/unit/config/test_config.py

+15
Original file line numberDiff line numberDiff line change
@@ -3023,6 +3023,21 @@ def test_config_bad_pyproject_specified(initproj, capsys):
30233023
assert "ERROR:" not in out
30243024

30253025

3026+
def test_config_setup_cfg_no_tox_section(initproj, capsys):
3027+
setup_cfg = """
3028+
[nope:nope]
3029+
envlist = py37
3030+
"""
3031+
initproj("setup_cfg_no_tox-0.1", filedefs={"setup.cfg": setup_cfg})
3032+
with pytest.raises(SystemExit):
3033+
parseconfig([])
3034+
3035+
out, err = capsys.readouterr()
3036+
msg = "ERROR: tox config file (either pyproject.toml, tox.ini, setup.cfg) not found\n"
3037+
assert err == msg
3038+
assert "ERROR:" not in out
3039+
3040+
30263041
@pytest.mark.skipif(sys.platform == "win32", reason="no named pipes on Windows")
30273042
def test_config_bad_config_type_specified(monkeypatch, tmpdir, capsys):
30283043
monkeypatch.chdir(tmpdir)

0 commit comments

Comments
 (0)