Tox 运行 命令基于环境变量
Tox running command based on env variable
我当前的工作流程是 github 在 Travis CI 上测试的 PR 和构建,使用 tox 测试 pytest 并向 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
但是,Travis 没有为拉取请求传递我的环境变量,这导致我的覆盖率报告失败。 Travis 文档将此显示为解决方案:
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'
但是,在 tox 中这不起作用,因为 tox 使用子进程 python 模块并且不能将 if 识别为命令(自然地)。
如何 运行 codeclimate-test-reporter 仅用于构建而不用于基于 TRAVIS_PULL_REQUEST 变量的拉取请求?我必须创建自己的脚本并调用它吗?有没有更聪明的解决方案?
您可以有两个 tox.ini
文件并从 travis.yml
调用它
script: if [ $TRAVIS_PULL_REQUEST ]; then tox -c tox_nocodeclimate.ini; else tox -c tox.ini; fi
我的解决方案是通过 setup.py 命令来处理所有事情
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},
我当前的工作流程是 github 在 Travis CI 上测试的 PR 和构建,使用 tox 测试 pytest 并向 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
但是,Travis 没有为拉取请求传递我的环境变量,这导致我的覆盖率报告失败。 Travis 文档将此显示为解决方案:
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'
但是,在 tox 中这不起作用,因为 tox 使用子进程 python 模块并且不能将 if 识别为命令(自然地)。
如何 运行 codeclimate-test-reporter 仅用于构建而不用于基于 TRAVIS_PULL_REQUEST 变量的拉取请求?我必须创建自己的脚本并调用它吗?有没有更聪明的解决方案?
您可以有两个 tox.ini
文件并从 travis.yml
script: if [ $TRAVIS_PULL_REQUEST ]; then tox -c tox_nocodeclimate.ini; else tox -c tox.ini; fi
我的解决方案是通过 setup.py 命令来处理所有事情
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},