From f580e1a594f220b974abe0a2de85339f479c0542 Mon Sep 17 00:00:00 2001 From: Walter Scheper Date: Thu, 10 Mar 2016 21:43:49 -0500 Subject: Support configuration through setup.cfg When the usual search for a config file fails, try loading setup.cfg with a section prefix of tox. Modeled on coverage's use of setup.cfg as an alternative config file. --- tests/test_config.py | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ tox/config.py | 23 ++++++++++++++++++----- 2 files changed, 67 insertions(+), 5 deletions(-) diff --git a/tests/test_config.py b/tests/test_config.py index 372cd4f..a7ddb12 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -632,6 +632,55 @@ class TestIniParser: py.test.raises(tox.exception.ConfigError, 'reader.getbool("key5")') +class TestIniParserPrefix: + def test_basic_section_access(self, tmpdir, newconfig): + config = newconfig(""" + [p:section] + key=value + """) + reader = SectionReader("section", config._cfg, prefix="p") + x = reader.getstring("key") + assert x == "value" + assert not reader.getstring("hello") + x = reader.getstring("hello", "world") + assert x == "world" + + def test_fallback_sections(self, tmpdir, newconfig): + config = newconfig(""" + [p:mydefault] + key2=value2 + [p:section] + key=value + """) + reader = SectionReader("section", config._cfg, prefix="p", + fallbacksections=['p:mydefault']) + x = reader.getstring("key2") + assert x == "value2" + x = reader.getstring("key3") + assert not x + x = reader.getstring("key3", "world") + assert x == "world" + + def test_value_matches_prefixed_section_substituion(self): + assert is_section_substitution("{[p:setup]commands}") + + def test_value_doesn_match_prefixed_section_substitution(self): + assert is_section_substitution("{[p: ]commands}") is None + assert is_section_substitution("{[p:setup]}") is None + assert is_section_substitution("{[p:setup] commands}") is None + + def test_other_section_substitution(self, newconfig): + config = newconfig(""" + [p:section] + key = rue + [p:testenv] + key = t{[p:section]key} + """) + reader = SectionReader("testenv", config._cfg, prefix="p") + x = reader.getstring("key") + assert x == "true" + + class TestConfigTestEnv: def test_commentchars_issue33(self, tmpdir, newconfig): config = newconfig(""" diff --git a/tox/config.py b/tox/config.py index 67454bc..aaca0ca 100644 --- a/tox/config.py +++ b/tox/config.py @@ -224,7 +224,10 @@ def parseconfig(args=None, plugins=()): if inipath.check(): break else: - feedback("toxini file %r not found" % (basename), sysexit=True) + inipath = py.path.local().join('setup.cfg') + if not inipath.check(): + feedback("toxini file %r not found" % (basename), sysexit=True) + try: parseini(config, inipath) except tox.exception.InterpreterNotFound: @@ -644,12 +647,18 @@ class parseini: self._cfg = py.iniconfig.IniConfig(config.toxinipath) config._cfg = self._cfg self.config = config + + if inipath.basename == 'setup.cfg': + prefix = 'tox' + else: + prefix = None ctxname = getcontextname() if ctxname == "jenkins": - reader = SectionReader("tox:jenkins", self._cfg, fallbacksections=['tox']) + reader = SectionReader("tox:jenkins", self._cfg, prefix=prefix, + fallbacksections=['tox']) distshare_default = "{toxworkdir}/distshare" elif not ctxname: - reader = SectionReader("tox", self._cfg) + reader = SectionReader("tox", self._cfg, prefix=prefix) distshare_default = "{homedir}/.tox/distshare" else: raise ValueError("invalid context") @@ -853,8 +862,12 @@ is_section_substitution = re.compile("{\[[^{}\s]+\]\S+?}").match class SectionReader: - def __init__(self, section_name, cfgparser, fallbacksections=None, factors=()): - self.section_name = section_name + def __init__(self, section_name, cfgparser, fallbacksections=None, + factors=(), prefix=None): + if prefix is None: + self.section_name = section_name + else: + self.section_name = "%s:%s" % (prefix, section_name) self._cfg = cfgparser self.fallbacksections = fallbacksections or [] self.factors = factors -- cgit v1.2.1 From 03dd843fba44bc2fad91da6f2f7bb828a98ab923 Mon Sep 17 00:00:00 2001 From: Oliver Bestwalter Date: Mon, 20 Jun 2016 17:18:50 +0200 Subject: make flakes pass --- tests/test_config.py | 2 +- tox.ini | 3 ++- tox/_pytestplugin.py | 2 +- tox/config.py | 8 ++++---- tox/session.py | 1 - 5 files changed, 8 insertions(+), 8 deletions(-) diff --git a/tests/test_config.py b/tests/test_config.py index ffc8343..82248d8 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -1441,7 +1441,7 @@ class TestGlobalOptions: minversion = 10.0 """ with py.test.raises(tox.exception.MinVersionError): - config = newconfig([], inisource) + newconfig([], inisource) def test_skip_missing_interpreters_true(self, tmpdir, newconfig, monkeypatch): inisource = """ diff --git a/tox.ini b/tox.ini index 3dd30dc..a2bbd96 100644 --- a/tox.ini +++ b/tox.ini @@ -34,7 +34,7 @@ platform=linux deps = pytest-flakes>=0.2 pytest-pep8 -commands = +commands = py.test --flakes -m flakes tox tests py.test --pep8 -m pep8 tox tests @@ -60,3 +60,4 @@ pep8maxlinelength = 99 # E731 - do not assign a lambda expression, use a def pep8ignore = *.py W503 E402 E731 +flakes-ignore = ImportStarUsage diff --git a/tox/_pytestplugin.py b/tox/_pytestplugin.py index 4dfc497..785070d 100644 --- a/tox/_pytestplugin.py +++ b/tox/_pytestplugin.py @@ -195,7 +195,7 @@ class Cmd: return py.std.subprocess.Popen(argv, stdout=stdout, stderr=stderr, **kw) def run(self, *argv): - if argv[0] == "tox" and sys.version_info[:2] < (2,7): + if argv[0] == "tox" and sys.version_info[:2] < (2, 7): pytest.skip("can not run tests involving calling tox on python2.6. " "(and python2.6 is about to be deprecated anyway)") argv = [str(x) for x in argv] diff --git a/tox/config.py b/tox/config.py index 1fd702c..f4c65dc 100644 --- a/tox/config.py +++ b/tox/config.py @@ -520,10 +520,10 @@ def tox_addoption(parser): parser.add_testenv_attribute_obj(InstallcmdOption()) parser.add_testenv_attribute( - name = "list_dependencies_command", - type = "argv", - default = "python -m pip freeze", - help = "list dependencies for a virtual environment") + name="list_dependencies_command", + type="argv", + default="python -m pip freeze", + help="list dependencies for a virtual environment") parser.add_testenv_attribute_obj(DepOption()) diff --git a/tox/session.py b/tox/session.py index 1d51b92..cb5cc66 100644 --- a/tox/session.py +++ b/tox/session.py @@ -537,7 +537,6 @@ class Session: # write out version dependency information action = self.newaction(venv, "envreport") with action: - python = venv.getcommandpath("python") args = venv.envconfig.list_dependencies_command output = venv._pcall(args, cwd=self.config.toxinidir, -- cgit v1.2.1 -- cgit v1.2.1