-
-
Notifications
You must be signed in to change notification settings - Fork 316
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Expand variables referenced as `$VAR` or `${VAR}`. - Detect infinite recursion in expansion (self-reference).
- Loading branch information
1 parent
69b4bc9
commit c61bfb0
Showing
5 changed files
with
114 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,28 @@ And use it with ``settings.py`` as follows: | |
:start-after: -code-begin- | ||
:end-before: -overview- | ||
|
||
Variables can contain references to another variables: ``$VAR`` or ``${VAR}``. | ||
Referenced variables are searched in the environment and within all definitions | ||
in the ``.env`` file. References are checked for recursion (self-reference). | ||
Exception is thrown if any reference results in infinite loop on any level | ||
of recursion. Variable values are substituted similar to shell parameter | ||
expansion. Example: | ||
|
||
.. code-block:: shell | ||
# shell | ||
export POSTGRES_USERNAME='user' POSTGRES_PASSWORD='SECRET' | ||
.. code-block:: shell | ||
# .env | ||
POSTGRES_HOSTNAME='example.com' | ||
POSTGRES_DB='database' | ||
DATABASE_URL="postgres://${POSTGRES_USERNAME}:${POSTGRES_PASSWORD}@${POSTGRES_HOSTNAME}:5432/${POSTGRES_DB}" | ||
The value of ``DATABASE_URL`` variable will become | ||
``postgres://user:[email protected]:5432/database``. | ||
|
||
The ``.env`` file should be specific to the environment and not checked into | ||
version control, it is best practice documenting the ``.env`` file with an example. | ||
For example, you can also add ``.env.dist`` with a template of your variables to | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import pytest | ||
|
||
from environ import Env, Path | ||
from environ.compat import ImproperlyConfigured | ||
|
||
|
||
class TestExpansion: | ||
def setup_method(self, method): | ||
Env.ENVIRON = {} | ||
self.env = Env() | ||
self.env.read_env(Path(__file__, is_file=True)('test_expansion.txt')) | ||
|
||
def test_expansion(self): | ||
assert self.env('HELLO') == 'Hello, world!' | ||
|
||
def test_braces(self): | ||
assert self.env('BRACES') == 'Hello, world!' | ||
|
||
def test_recursion(self): | ||
with pytest.raises(ImproperlyConfigured) as excinfo: | ||
self.env('RECURSIVE') | ||
assert str(excinfo.value) == "Environment variable 'RECURSIVE' recursively references itself (eventually)" | ||
|
||
def test_transitive(self): | ||
with pytest.raises(ImproperlyConfigured) as excinfo: | ||
self.env('R4') | ||
assert str(excinfo.value) == "Environment variable 'R4' recursively references itself (eventually)" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
VAR1='Hello' | ||
VAR2='world' | ||
HELLO="$VAR1, $VAR2!" | ||
BRACES="${VAR1}, ${VAR2}!" | ||
RECURSIVE="This variable is $RECURSIVE" | ||
R1="$R2" | ||
R2="$R3" | ||
R3="$R4" | ||
R4="$R1" |