Skip to content

How can I set a default value for missing environment variables? #491

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
brianbruggeman opened this issue Mar 23, 2017 · 9 comments
Closed

Comments

@brianbruggeman
Copy link

Also here: https://stackoverflow.com/questions/42982267/tox-set-default-values-for-missing-environment-variables

I've found that I would really like to use environment variables to control flow - especially when testing. But I also would like to set a default behavior based on whether or not an environment variable is present. In bash, I'd just use: ${ENV_VAR:-default_value}, but I don't have that basic option in Tox (unless I missed it).

Is there a way?

@brianbruggeman brianbruggeman changed the title [Feature Request] Set A default variable for missing environment variables [Feature Request] Set A default value for missing environment variables Mar 23, 2017
@obestwalter
Copy link
Member

obestwalter commented Mar 24, 2017

This works via the env substitution. An example for overriding the toxworkdir with a default:

tox.ini

[tox]
envlist = py
skipsdist = true

[testenv]
setenv = TOX_WORKDIR = {env:TOX_WORKDIR:.tox}
commands = python ./envecho.py

envecho.py

#!/usr/bin/env python
import os

print(os.getenv("TOX_WORKDIR"))

output without env set:

$ tox
       
py create: /home/oliver/Dropbox/projects/tox/lab/mini/.tox/py
py installed: appdirs==1.4.3,packaging==16.8,pyparsing==2.2.0,six==1.10.0
py runtests: PYTHONHASHSEED='2704413679'
py runtests: commands[0] | python ./envecho.py
.tox
______________________________ summary ______________________________
  py: commands succeeded
  congratulations :)

output with env set:

$ TOX_WORKDIR=/tmp/ tox
       
py installed: appdirs==1.4.3,packaging==16.8,pyparsing==2.2.0,six==1.10.0
py runtests: PYTHONHASHSEED='30398946'
py runtests: commands[0] | python ./envecho.py
/tmp/
______________________________ summary ______________________________
  py: commands succeeded
  congratulations :)

@obestwalter obestwalter changed the title [Feature Request] Set A default value for missing environment variables How can I set a default value for missing environment variables? Mar 24, 2017
@obestwalter
Copy link
Member

It's dawning on me, that you might want to set the value of the environment variable itself if it is not set from the outside ... is that right?

@brianbruggeman
Copy link
Author

Yes.

@brianbruggeman
Copy link
Author

But I'm wondering if I can do something fancy...

[tox-env-identifier]
...
setenv
    VAR_NAME = {env:VAR_NAME:default_value}
...

@obestwalter
Copy link
Member

Fixed the answer after understanding the question properly :)

@obestwalter
Copy link
Member

Correct :)

@brianbruggeman
Copy link
Author

brianbruggeman commented Mar 24, 2017

Can I do something like this?

setenv =
    SPARK_PYTHONPATH = {env:SPARK_PYTHONPATH:env:SPARK_HOME/python}
    PYTHONPATH = {env:SPARK_PYTHONPATH}:{toxinidir}

@obestwalter
Copy link
Member

setenv =
SPARK_PYTHONPATH = {env:SPARK_PYTHONPATH:env:SPARK_HOME/python}

AFAIK you can not access the content of env vars in setenv, you can only set them.

PYTHONPATH = {env:SPARK_PYTHONPATH}:{toxinidir}

I think the correct syntax would be PYTHONPATH = {env:SPARK_PYTHONPATH:{toxinidir}} but when I try this I get: {env:HOME:/home/oliver/Dropbox/projects/tox/lab/mini} as output of the env var, so the current substtution engine cannot handle that (or I am getting something wrong also).

If you start doing magic like this you can always add a thin wrapper to call your tests and do the extra magic there. This is not super elegant but that way you have full flexibility. You can take an ini file with some substitution magic only so far without creating a hideous monster.

@brianbruggeman
Copy link
Author

brianbruggeman commented Mar 24, 2017

In the case of the PYTHONPATH variable above, it already does explicitly what I want (concatenates SPARK_PYTHONPATH with the current toxini folder). So I wasn't too concerned there.

But SPARK_PYTHONPATH is the real issue as it requires knowledge of the SPARK_HOME value to set a valid default value. I completely empathize with trying to support recursive/layered structures like this... And the truth is that I'm not sure any of the default configuration technologies really handle this kind of scenario: ini, json, yaml, toml, etc. But I'm finding this pattern more common now, and I'm going to need to figure out a better solution for it. Requiring environment variables set to a specific value is not a great one.

Thank you for your help here!

@tox-dev tox-dev locked and limited conversation to collaborators Jan 14, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants