snark17
snark17

Reputation: 399

Run a conditional bash command inside of tox

I'm trying to run conditional bash commands in Tox.

If the user passes 'stag' into the script, tox should run one curl command; if they pass 'prod' into the script, it should run another curl command.

[testenv]
ENV=$1
whitelist_externals=
    /bin/bash
deps=
    -rrequirements.txt
commands=
    bash -ec 'curl https://this_is_just_sample_test.com'
    pytest test/test.py

When I try introducing a condition to the batch command:

[testenv]
ENV=$1
whitelist_externals=
    /bin/bash
deps=
    -rrequirements.txt
commands=
    bash -ec 'if [$1 == "stag"]; then curl https://this_is_just_sample_test.com fi'
    pytest test/test.py

I get the following error message:

ERROR: InvocationError for command '/bin/bash -ec if [$1 = "stag"]; then curl https://this_is_just_sample_test.com fi' (exited with code 2)

Upvotes: 3

Views: 3496

Answers (1)

phd
phd

Reputation: 94521

Let me fix the code for you (I added spaces for [] command, changed parameter to $0 and added a semicolon):

[testenv]
whitelist_externals=
    /bin/bash
deps=
    -rrequirements.txt
commands=
    bash -ec 'if [ "$0" == "stag" ]; then curl https://this_is_just_sample_test.com; fi' {posargs}
    pytest test/test.py

{posargs} is a substitution that allows to pass command line parameters from tox invocation. Like this:

$ tox stag

Upvotes: 4

Related Questions