From d4887816c3209a92d730b9ee6b817bb7726d241e Mon Sep 17 00:00:00 2001 From: zueve Date: Thu, 28 Jan 2021 12:33:48 +0200 Subject: [PATCH] Add --override/--no-override flag to "dotenv run" 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. --- CHANGELOG.md | 5 ++++- src/dotenv/cli.py | 15 ++++++++++++--- tests/test_cli.py | 28 ++++++++++++++++++++++++++++ 3 files changed, 44 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 14f10d37..5a5b276b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 @@ -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 diff --git a/src/dotenv/cli.py b/src/dotenv/cli.py index e17d248f..51f25e8d 100644 --- a/src/dotenv/cli.py +++ b/src/dotenv/cli.py @@ -107,9 +107,14 @@ 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): @@ -117,7 +122,11 @@ def run(ctx, commandline): '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.') diff --git a/tests/test_cli.py b/tests/test_cli.py index 23404e70..a048ef3b 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -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")