Skip to content
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

Add --override/--no-override flag to "dotenv run" #312

Merged
merged 1 commit into from Mar 27, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion CHANGELOG.md
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
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
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