Skip to content

Commit

Permalink
Refactor (#79)
Browse files Browse the repository at this point in the history
* Split out the PyPy monkeypatch
* Move Travis checks into hooks
  • Loading branch information
ryanhiebert authored Oct 19, 2017
1 parent b3c99ab commit e83cadf
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 43 deletions.
5 changes: 0 additions & 5 deletions src/tox_travis/after.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

# Exit code constants. They are purposely undocumented.
# Please do not depend on their values.
UNKNOWN_ENVIRONMENT = 31
NO_GITHUB_TOKEN = 32
INVALID_POLLING_INTERVAL = 33
INCOMPLETE_TRAVIS_ENVIRONMENT = 34
Expand All @@ -39,10 +38,6 @@ def subcommand_test(self):

def travis_after():
"""Wait for all jobs to finish, then exit successfully."""
if not os.environ.get('TRAVIS'):
print('Not a Travis environment.', file=sys.stderr)
sys.exit(UNKNOWN_ENVIRONMENT)

# after-all disabled for pull requests
if os.environ.get('TRAVIS_PULL_REQUEST', 'false') != 'false':
return
Expand Down
18 changes: 18 additions & 0 deletions src/tox_travis/hacks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import os

try:
from tox.config import default_factors
except ImportError:
default_factors = None


def pypy_version_monkeypatch():
"""Patch Tox to work with non-default PyPy 3 versions."""
# Travis virtualenv do not provide `pypy3`, which tox tries to execute.
# This doesnt affect Travis python version `pypy3`, as the pyenv pypy3
# is in the PATH.
# https://github.com/travis-ci/travis-ci/issues/6304
# Force use of the virtualenv `python`.
version = os.environ.get('TRAVIS_PYTHON_VERSION')
if version and default_factors and version.startswith('pypy3.3-'):
default_factors['pypy3'] = 'python'
19 changes: 13 additions & 6 deletions src/tox_travis/hooks.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
"""Tox hook implementations."""
import os
import tox
from .toxenv import default_toxenv, override_ignore_outcome
from .toxenv import (
default_toxenv,
override_ignore_outcome,
)
from .hacks import pypy_version_monkeypatch
from .after import travis_after_monkeypatch


Expand All @@ -11,13 +16,15 @@ def tox_addoption(parser):
'--travis-after', dest='travis_after', action='store_true',
help='Exit successfully after all Travis jobs complete successfully.')

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


@tox.hookimpl
def tox_configure(config):
"""Check for the presence of the added options."""
if config.option.travis_after:
travis_after_monkeypatch()

override_ignore_outcome(config)
if 'TRAVIS' in os.environ:
if config.option.travis_after:
travis_after_monkeypatch()
override_ignore_outcome(config)
21 changes: 0 additions & 21 deletions src/tox_travis/toxenv.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,12 @@
import re
import py
import tox.config

from tox.config import _split_env as split_env
try:
from tox.config import default_factors
except ImportError:
default_factors = None

from .utils import TRAVIS_FACTORS, parse_dict


def default_toxenv():
"""Default TOXENV automatically based on the Travis environment."""
if 'TRAVIS' not in os.environ:
return

if 'TOXENV' in os.environ:
return # Skip any processing if already set

Expand All @@ -40,15 +31,6 @@ def default_toxenv():
passthru=len(desired_factors) == 1)
os.environ.setdefault('TOXENV', ','.join(matched))

# Travis virtualenv do not provide `pypy3`, which tox tries to execute.
# This doesnt affect Travis python version `pypy3`, as the pyenv pypy3
# is in the PATH.
# https://github.com/travis-ci/travis-ci/issues/6304
# Force use of the virtualenv `python`.
version = os.environ.get('TRAVIS_PYTHON_VERSION')
if version and default_factors and version.startswith('pypy3.3-'):
default_factors['pypy3'] = 'python'


def get_declared_envs(config):
"""Get the full list of envs from the tox config.
Expand Down Expand Up @@ -220,9 +202,6 @@ def env_matches(declared, desired):

def override_ignore_outcome(config):
"""Override ignore_outcome if unignore_outcomes is set to True."""
if 'TRAVIS' not in os.environ:
return

tox_config = py.iniconfig.IniConfig('tox.ini')
travis_reader = tox.config.SectionReader("travis", tox_config)
if travis_reader.getbool('unignore_outcomes', False):
Expand Down
11 changes: 0 additions & 11 deletions tests/test_after.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,6 @@
class TestAfter:
"""Test the logic of waiting for other jobs to finish."""

def test_not_travis(self, mocker, capsys):
"""Raise with the right message when not in Travis."""
mocker.patch('tox_travis.after.after_config_matches',
return_value=True)
with pytest.raises(SystemExit) as excinfo:
travis_after()

assert excinfo.value.code == 31
out, err = capsys.readouterr()
assert 'Not a Travis environment.' in err

def test_pull_request(self, mocker, monkeypatch, capsys):
"""Pull requests should not run after-all."""
mocker.patch('tox_travis.after.after_config_matches',
Expand Down

0 comments on commit e83cadf

Please sign in to comment.