Skip to content

Commit

Permalink
Add --override/--no-override flag to "dotenv run"
Browse files Browse the repository at this point in the history
This makes it possible to not override previously defined environment
variables when running `dotenv run`.  It defaults to `--override` for
compatibility with the previous behavior.
  • Loading branch information
zueve authored and bbc2 committed Mar 27, 2021
1 parent b96db46 commit d488781
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 4 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

_There are no unreleased changes at this time._
### Added

- Add `--override`/`--no-override` option to `dotenv run` (#312 by [@zueve] and [@bbc2]).

## [0.16.0] - 2021-03-27

Expand Down Expand Up @@ -242,6 +244,7 @@ _There are no unreleased changes at this time._
[@venthur]: https://github.com/venthur
[@x-yuri]: https://github.com/x-yuri
[@yannham]: https://github.com/yannham
[@zueve]: https://github.com/zueve

[Unreleased]: https://github.com/theskumar/python-dotenv/compare/v0.16.0...HEAD
[0.16.0]: https://github.com/theskumar/python-dotenv/compare/v0.15.0...v0.16.0
Expand Down
15 changes: 12 additions & 3 deletions src/dotenv/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,17 +107,26 @@ def unset(ctx, key):

@cli.command(context_settings={'ignore_unknown_options': True})
@click.pass_context
@click.option(
"--override/--no-override",
default=True,
help="Override variables from the environment file with those from the .env file.",
)
@click.argument('commandline', nargs=-1, type=click.UNPROCESSED)
def run(ctx, commandline):
# type: (click.Context, List[str]) -> None
def run(ctx, override, commandline):
# type: (click.Context, bool, List[str]) -> None
"""Run command with environment variables present."""
file = ctx.obj['FILE']
if not os.path.isfile(file):
raise click.BadParameter(
'Invalid value for \'-f\' "%s" does not exist.' % (file),
ctx=ctx
)
dotenv_as_dict = {to_env(k): to_env(v) for (k, v) in dotenv_values(file).items() if v is not None}
dotenv_as_dict = {
to_env(k): to_env(v)
for (k, v) in dotenv_values(file).items()
if v is not None and (override or to_env(k) not in os.environ)
}

if not commandline:
click.echo('No command given.')
Expand Down
28 changes: 28 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,34 @@ def test_run(tmp_path):
assert result == "b\n"


def test_run_with_existing_variable(tmp_path):
sh.cd(str(tmp_path))
dotenv_file = str(tmp_path / ".env")
with open(dotenv_file, "w") as f:
f.write("a=b")

result = sh.dotenv("run", "printenv", "a", _env={"LANG": "en_US.UTF-8", "a": "c"})

assert result == "b\n"


def test_run_with_existing_variable_not_overridden(tmp_path):
sh.cd(str(tmp_path))
dotenv_file = str(tmp_path / ".env")
with open(dotenv_file, "w") as f:
f.write("a=b")

result = sh.dotenv(
"run",
"--no-override",
"printenv",
"a",
_env={"LANG": "en_US.UTF-8", "a": "c"},
)

assert result == "c\n"


def test_run_with_none_value(tmp_path):
sh.cd(str(tmp_path))
dotenv_file = str(tmp_path / ".env")
Expand Down

0 comments on commit d488781

Please sign in to comment.