5

My current workflow is github PRs and Builds tested on Travis CI, with tox testing pytest and reporting coverage to codeclimate.

travis.yml

os:
- linux
sudo: false
language: python
python:
- "3.3"
- "3.4"
- "3.5"
- "pypy3"
- "pypy3.3-5.2-alpha1"
- "nightly"
install: pip install tox-travis
script: tox

tox.ini

[tox]
envlist = py33, py34, py35, pypy3, docs, flake8, nightly, pypy3.3-5.2-alpha1

[tox:travis]
3.5 = py35, docs, flake8

[testenv]
deps = -rrequirements.txt
platform =
    win: windows
    linux: linux
commands =
    py.test --cov=pyCardDeck --durations=10 tests

[testenv:py35]
commands =
    py.test --cov=pyCardDeck --durations=10 tests
    codeclimate-test-reporter --file .coverage
passenv =
    CODECLIMATE_REPO_TOKEN
    TRAVIS_BRANCH
    TRAVIS_JOB_ID
    TRAVIS_PULL_REQUEST
    CI_NAME

However, Travis isn't passing my environmental variables for pull requests, which makes my coverage reporting fail. Travis documentation shows this as solution:

script:
   - 'if [ "$TRAVIS_PULL_REQUEST" != "false" ]; then bash ./travis/run_on_pull_requests; fi'
   - 'if [ "$TRAVIS_PULL_REQUEST" = "false" ]; then bash ./travis/run_on_non_pull_requests; fi'

However, in tox this doesn't work as tox is using subprocess python module and doesn't recognize if as a command (naturally).

How do I run codeclimate-test-reporter only for builds and not for pull requests based on the TRAVIS_PULL_REQUEST variable? Do I have to create my own script and call that? Is there a smarter solution?

iScrE4m
  • 882
  • 1
  • 12
  • 31
  • 1
    `tox.ini` files does not understand flow control (which is understandable, they are `.ini` files). Creating custom script with logic is one option. Second option is to move CI-related functionality to `.travis.yml`. At the end of the day, `tox.ini` should contain logic which may and should be run on each dev local machine. [`pylint`](https://github.com/PyCQA/pylint/blob/master/tox.ini) does something like that. `coveralls` venv is declared in tox, but is triggered by entry in `.travis.yml`. – Łukasz Rogalski Sep 18 '16 at 20:28
  • 1
    I think to make control flow via bash to work, you'd need tox to pass `shell=True` into subprocess. That's hard-coded, and buried pretty deep: https://github.com/tox-dev/tox/blob/85cce631a1bab5fe056ed2cbbcab49ebaf51b259/tox/session.py#L226 – Lucas Wiman Sep 18 '16 at 23:46
  • Could something like `/bin/bash -c` work? – iScrE4m Sep 19 '16 at 14:08

2 Answers2

1

You can have two tox.ini files and call that from travis.yml

script: if [ $TRAVIS_PULL_REQUEST ]; then tox -c tox_nocodeclimate.ini; else tox -c tox.ini; fi

postelrich
  • 3,274
  • 5
  • 38
  • 65
  • That sounds like a pain to maintain honestly, my current solution (https://github.com/iScrE4m/pyCardDeck/blob/master/setup.py#L34) is a bit better, but I still don't like it – iScrE4m Sep 18 '16 at 20:51
0

My solution is going through setup.py command which takes care of everything

Tox.ini

[testenv:py35]
commands =
    python setup.py testcov
passenv = ...

setup.py

class PyTestCov(Command):
    description = "run tests and report them to codeclimate"
    user_options = []

    def initialize_options(self):
        pass

    def finalize_options(self):
        pass

    def run(self):
        errno = call(["py.test --cov=pyCardDeck --durations=10 tests"], shell=True)
        if os.getenv("TRAVIS_PULL_REQUEST") == "false":
            call(["python -m codeclimate_test_reporter --file .coverage"], shell=True)
        raise SystemExit(errno)

 ...


  cmdclass={'testcov': PyTestCov},
iScrE4m
  • 882
  • 1
  • 12
  • 31