Skip to content

Commit

Permalink
Configure envlist in tox_configure
Browse files Browse the repository at this point in the history
This is a cleaner method of configuring the desired envs to run,
by overriding tox's config.envlist.

There are a few things worth noting:

1. Because we override envlist directly, instead of proxying
   through the TOXENV environment variable, it eliminates an
   inconsistency where all envs run when no environments are
   detected to run, because TOXENV was set to the empty string
   and ignored.
2. Because we override envlist later, we must also check manually
   for envs passed directly into tox, so we don't override in
   that case (config.option.env).
3. To maintain backward compatibility, envs that don't exist
   in the tox.ini aren't automatically created like they are
   when using the TOXENV environment variable, so we have to
   manually add the envconfigs necessary for those.
  • Loading branch information
ryanhiebert committed Oct 21, 2017
1 parent f28adbb commit 85cb196
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/tox_travis/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ def tox_addoption(parser):
help='Exit successfully after all Travis jobs complete successfully.')

if 'TRAVIS' in os.environ:
default_toxenv()
pypy_version_monkeypatch()


Expand All @@ -27,4 +26,5 @@ def tox_configure(config):
if 'TRAVIS' in os.environ:
if config.option.travis_after:
travis_after_monkeypatch()
default_toxenv(config)
override_ignore_outcome(config)
45 changes: 39 additions & 6 deletions src/tox_travis/toxenv.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
from .utils import TRAVIS_FACTORS, parse_dict


def default_toxenv():
"""Default TOXENV automatically based on the Travis environment."""
if 'TOXENV' in os.environ:
def default_toxenv(config):
"""Default envlist automatically based on the Travis environment."""
if 'TOXENV' in os.environ or config.option.env:
return # Skip any processing if already set

ini = py.iniconfig.IniConfig('tox.ini')
Expand All @@ -29,7 +29,40 @@ def default_toxenv():
# Find matching envs
matched = match_envs(declared_envs, desired_envs,
passthru=len(desired_factors) == 1)
os.environ.setdefault('TOXENV', ','.join(matched))

# Make the envconfig for undeclared matched envs
autogen_envconfigs(config, set(matched) - set(config.envconfigs))

config.envlist = matched


def autogen_envconfigs(config, envs):
"""Make the envconfigs for undeclared envs.
This is a stripped-down version of parseini.__init__ made for making
an envconfig.
"""
prefix = 'tox' if config.toxinipath.basename == 'setup.cfg' else None
reader = tox.config.SectionReader("tox", config._cfg, prefix=prefix)
distshare_default = "{homedir}/.tox/distshare"
reader.addsubstitutions(toxinidir=config.toxinidir,
homedir=config.homedir)

reader.addsubstitutions(toxworkdir=config.toxworkdir)
config.distdir = reader.getpath("distdir", "{toxworkdir}/dist")
reader.addsubstitutions(distdir=config.distdir)
config.distshare = reader.getpath("distshare", distshare_default)
reader.addsubstitutions(distshare=config.distshare)

# Create the undeclared envs
for env in envs:
make_envconfig = tox.config.parseini.make_envconfig
# Dig past the unbound method in Python 2
make_envconfig = getattr(make_envconfig, '__func__', make_envconfig)

section = tox.config.testenvprefix + env
config.envconfigs[env] = make_envconfig(
config, env, section, reader._subs, config)


def get_declared_envs(ini):
Expand Down Expand Up @@ -205,5 +238,5 @@ def override_ignore_outcome(config):
tox_config = py.iniconfig.IniConfig('tox.ini')
travis_reader = tox.config.SectionReader("travis", tox_config)
if travis_reader.getbool('unignore_outcomes', False):
for env in config.envlist:
config.envconfigs[env].ignore_outcome = False
for envconfig in config.envconfigs.values():
envconfig.ignore_outcome = False
6 changes: 3 additions & 3 deletions tests/test_toxenv.py
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ def test_travis_ignore_outcome(self, tmpdir, monkeypatch):
"""Test ignore_outcome without setting obey_outcomes."""
tox_ini = tox_ini_ignore_outcome
with self.configure(
tmpdir, monkeypatch, tox_ini, travis_language='generic'
tmpdir, monkeypatch, tox_ini, travis_version='3.4'
):
config = self.tox_config()
assert config["testenv:py34"]["ignore_outcome"] == "True"
Expand All @@ -426,7 +426,7 @@ def test_travis_ignore_outcome_unignore_outcomes(self, tmpdir,
"""Test ignore_outcome setting unignore_outcomes = False."""
tox_ini = tox_ini_ignore_outcome_unignore_outcomes
with self.configure(
tmpdir, monkeypatch, tox_ini, travis_language='generic'
tmpdir, monkeypatch, tox_ini, travis_version='3.4'
):
config = self.tox_config()
assert config["testenv:py34"]["ignore_outcome"] == "False"
Expand All @@ -436,7 +436,7 @@ def test_travis_ignore_outcome_not_unignore_outcomes(self, tmpdir,
"""Test ignore_outcome setting unignore_outcomes = False (default)."""
tox_ini = tox_ini_ignore_outcome_not_unignore_outcomes
with self.configure(
tmpdir, monkeypatch, tox_ini, travis_language='generic'
tmpdir, monkeypatch, tox_ini, travis_version='3.4'
):
config = self.tox_config()
assert config["testenv:py34"]["ignore_outcome"] == "True"
Expand Down

0 comments on commit 85cb196

Please sign in to comment.