14

以前我手动使用了一个看起来像这样的 Makefile:

.PHONY: all
all: tests

.PHONY: tests
tests: py_env
    bash -c 'source py_env/bin/activate && py.test tests'

py_env: requirements_dev.txt setup.py
    rm -rf py_env
    virtualenv py_env
    bash -c 'source py_env/bin/activate && pip install -r requirements_dev.txt'

这有一个很好的副作用,如果我更改 requirements_dev.txt 或 setup.py,它会重建我的 virtualenv。但是感觉有点笨重。

我想用tox做类似的事情。我知道tox有一个--recreate选择,但我宁愿在需要时才调用它。

我的新设置是这样的:

# Makefile
.PHONY: all
all: tests

.PHONY: tests
tests:
    tox

# tox.ini
[tox]
project = my_project
envlist = py26,py27

[testenv]
install_command = pip install --use-wheel {opts} {packages}
deps = -rrequirements_dev.txt
commands =
    py.test {posargs:tests}

理想的解决方案将只使用 in 中的内容tox,但可接受的解决方案将涉及 Makefile 和--recreate标志。

4

2 回答 2

14

对于这个问题,似乎在 tox 中有一个未解决的问题。

https://github.com/tox-dev/tox/issues/149(单击并添加您的评论和投票,让作者了解该问题的普遍性)

我们需要提交补丁或解决它。想到的解决方法:

  1. 直接在tox.ini. 使用您的构建系统确保 tox.ini 与requirements.txt.
  2. 向您的 Makefile 添加一条规则,该规则在 requirements.txt 更改时执行 tox --recreate 。

解决方法 2 似乎最简单。

于 2014-04-12T17:04:56.220 回答
6

这是我最终使用的 Makefile 解决方法:

REBUILD_FLAG =

.PHONY: all
all: tests

.PHONY: tests
tests: .venv.touch
    tox $(REBUILD_FLAG)

.venv.touch: setup.py requirements.txt requirements_dev.txt
    $(eval REBUILD_FLAG := --recreate)
    touch .venv.touch

例子:

$ make tests
touch .venv.touch
tox --recreate
[[ SNIP ]]
$ make tests
tox 
[[ SNIP ]]
$ touch requirements.txt
$ make tests
touch .venv.touch
tox --recreate
[[ SNIP ]]
于 2014-04-13T06:19:09.430 回答