Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recommended way to run commands after installing dependencies in the virtualenv

Tags:

virtualenv

tox

I would like to use tox to run py.test on a project which needs additional setup in addition to installing packages into the virtualenv. After creating the virtualenv and installing dependencies, some commands need to be run.

Specifically I'm talking about setting up a node and npm environment using nodeenv:

nodeenv --prebuilt -p

I see that tox allows me to provide a custom command used for installing dependencies by setting install_command in tox.ini. But I don't think this is what I want because that replaces the command (I assume pip) used to install dependencies.

I thought about using a py.test fixture with session scope to handle setting up nodeenv but that seems hacky to me as I don't want this to happen when py.test is run directly, not via tox.

What is the least insane way of achieving this?

like image 362
Feuermurmel Avatar asked Oct 22 '25 14:10

Feuermurmel


2 Answers

You can do all necessary setup after the creation of the virtualenv and the dependency installation in commands. Yes, it says "the commands to be called for testing." but if you need to do extra work to prepare for testing you can just do it right there.

It works through whatever you throw at it in the order it is given - e.g.:

  [testenv:someenv]
  deps = 
    nodeenv
    pytest
    flexmock
  commands =
     nodeenv --prebuilt -p
     ; ... and whatever else you might need to do
     py.test path/to/my/tests

If you have commands/scripts or whatever else that produces the right result but it returns a non zero exit status you can ignore that by prepending - (as in - naughty-command).

If you need more steps to happen you can wrap them in a little (Python) script and call that script instead as outlined in https://stackoverflow.com/a/47834447/2626627.

There is also an issue to add the ability to use more than one install command: https://github.com/tox-dev/tox/issues/715 is implemented.

like image 102
Oliver Bestwalter Avatar answered Oct 26 '25 20:10

Oliver Bestwalter


I had the same issue, and as it was important for me to be able to create the environment without invoking the tests (via --notest), I wanted the install to happen in the install phase and not the run phase, so I did something slightly differently. First, I created a create-env script:

#!/usr/bin/env sh

set -e

pip install $@
nodeenv --prebuilt --python-virtualenv --node=8.2.1

Made it executable, Then in tox.ini:

[tox]
skipsdist = True

[testenv]
install_command = ./create-env {opts} {packages}
deps = nodeenv
commands = node --version

This complete example runs and outputs the following:

$ tox
python create: .../.tox/python
python installdeps: nodeenv
python installed: nodeenv==1.3.0
python runtests: PYTHONHASHSEED='1150209523'
python runtests: commands[0] | node --version
v8.2.1
_____________________________________________________________________ summary ______________________________________________________________________
  python: commands succeeded
  congratulations :)

This approach has the downside that it would only work on Unix.

In tox 715, I propose the possibility of native support for multiple install commands.

like image 26
Jason R. Coombs Avatar answered Oct 26 '25 20:10

Jason R. Coombs