Python 基于env变量的Tox运行命令
我目前的工作流程是github PRs,构建在Travis CI上进行测试,tox测试pytest,并向codeclimate报告覆盖范围 travis.ymlPython 基于env变量的Tox运行命令,python,travis-ci,tox,code-climate,Python,Travis Ci,Tox,Code Climate,我目前的工作流程是github PRs,构建在Travis CI上进行测试,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.in
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没有为pull请求传递我的环境变量,这使得我的覆盖率报告失败。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模块,并且不能识别是否为命令(当然)
如何仅为构建运行codeclimate test reporter,而不是基于TRAVIS_pull_请求变量的pull请求运行codeclimate test reporter?我必须创建自己的脚本并调用它吗?有更智能的解决方案吗?您可以拥有两个
tox.ini
文件,并从travis.yml
script:if[$TRAVIS_PULL_REQUEST];然后tox-c tox_nocodeclimate.ini;else-tox-c-tox.ini;fi
我的解决方案是通过setup.py命令,它会处理所有问题
伊氏弓形虫
[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},
tox.ini
文件不理解流控制(可以理解,它们是.ini
文件)。使用逻辑创建自定义脚本是一种选择。第二个选项是将CI相关功能移动到.travis.yml
。在一天结束时,tox.ini
应该包含可以并且应该在每个dev本地机器上运行的逻辑。做类似的事情<代码>工作服venv在tox中声明,但由.travis.yml
中的条目触发。我认为要通过bash使控制流工作,需要tox将shell=True
传递到子流程中。这是硬编码的,而且埋藏得很深:像/bin/bash-c
这样的东西能工作吗?老实说,这听起来很难维护,我目前的解决方案()有点更好,但我仍然不喜欢它