Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,9 @@ Pipenv env var | Poetry env var
-------------- | ----------------------
PIPENV_DOTENV_LOCATION | POETRY_DOTENV_LOCATION
PIPENV_DONT_LOAD_ENV | POETRY_DONT_LOAD_ENV

### Overriding existing environment variables

By default, this plugin will override existing environment variables. This is because this plugin was built to make onboarding for users coming from `pipenv` as seamless as possible. If you want to prevent existing environment variables from being overridden, you can set the `POETRY_DOTENV_DONT_OVERRIDE` environment variable to `true`.[^1]

[^1]: See [#16](https://github.com/mpeteuil/poetry-dotenv-plugin/pull/16) for background.
7 changes: 6 additions & 1 deletion poetry_dotenv_plugin/dotenv_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,9 @@ def load_dotenv(
io.write_line("<debug>Loading environment variables.</debug>")

path = POETRY_DOTENV_LOCATION or dotenv.find_dotenv(usecwd=True)
dotenv.load_dotenv(dotenv_path=path, override=True)
POETRY_DOTENV_DONT_OVERRIDE = os.environ.get("POETRY_DOTENV_DONT_OVERRIDE", "")
DOTENV_OVERRIDE = not POETRY_DOTENV_DONT_OVERRIDE.lower() in (
"true",
"1",
)
dotenv.load_dotenv(dotenv_path=path, override=DOTENV_OVERRIDE)
42 changes: 42 additions & 0 deletions tests/test_system.sh
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,46 @@ function test_end_to_end_system_with_dotenv_location_override() {
fi
}

function test_end_to_end_system_with_default_env_overrides() {
# Setup
local expected
expected='foo'
create_dotenv_file

local output
output=$(export MY_ENV_VAR='bar' && poetry run python -c "import os; print(os.environ['MY_ENV_VAR'])")

# Cleanup
delete_dotenv_file

if [ "$expected" = "$output" ]; then
printf "test_end_to_end_system_with_default_env_overrides: PASSED\n"
else
printf "Expected '$expected', but got '%s'.\n" "$output"
exit 1
fi
}

function test_end_to_end_system_without_env_overrides() {
# Setup
local expected
expected='bar'
create_dotenv_file

local output
output=$(export MY_ENV_VAR='bar' POETRY_DOTENV_DONT_OVERRIDE=true && poetry run python -c "import os; print(os.environ['MY_ENV_VAR'])")

# Cleanup
delete_dotenv_file

if [ "$expected" = "$output" ]; then
printf "test_end_to_end_system_without_env_overrides: PASSED\n"
else
printf "Expected '$expected', but got '%s'.\n" "$output"
exit 1
fi
}

function test_end_to_end_system_without_loading_dotenv_file() {
# Setup
local expected
Expand All @@ -78,4 +118,6 @@ function test_end_to_end_system_without_loading_dotenv_file() {

test_end_to_end_system_with_default_dotenv_file
test_end_to_end_system_with_dotenv_location_override
test_end_to_end_system_with_default_env_overrides
test_end_to_end_system_without_env_overrides
test_end_to_end_system_without_loading_dotenv_file