diff --git a/CHANGELOG.md b/CHANGELOG.md index e5f25414..81c5c26b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ### Fixed +- Resolve the CLI's default `.env` path in the current working directory on each invocation, rather than at import time by [@kokotatan] in [#718] - Fix a package build deprecation warning caused by a non-string `license` value in `pyproject.toml` by [@kurtmckee] in [#648] - `set_key`, `unset_key` and the `dotenv set`/`unset` commands now name the `.env` path instead of an internal temporary file when its directory is missing or not writable, and the CLI prints a short error and exits with code 2 instead of a traceback by [@jamalkamaladdin] in [#711] - `set_key` and `unset_key` no longer leave a `.tmp_*` file behind on Windows when writing a read-only `.env` fails, and the error raised is the one from the failed write rather than from cleaning up the temporary file by [@MohammedAlkindi] in [#686] @@ -467,6 +468,7 @@ os.PathLike]` instead of just `os.PathLike` (#347 by [@bbc2]). [#700]: https://github.com/theskumar/python-dotenv/pull/700 [#711]: https://github.com/theskumar/python-dotenv/pull/711 [#714]: https://github.com/theskumar/python-dotenv/pull/714 +[#718]: https://github.com/theskumar/python-dotenv/pull/718 [790c5c0]: https://github.com/theskumar/python-dotenv/commit/790c5c02991100aa1bf41ee5330aca75edc51311 @@ -505,6 +507,7 @@ os.PathLike]` instead of just `os.PathLike` (#347 by [@bbc2]). [@jamalkamaladdin]: https://github.com/jamalkamaladdin [@jankislinger]: https://github.com/jankislinger [@jctanner]: https://github.com/jctanner +[@kokotatan]: https://github.com/kokotatan [@kurtmckee]: https://github.com/kurtmckee [@larsks]: https://github.com/larsks [@lsmith77]: https://github.com/lsmith77 diff --git a/src/dotenv/cli.py b/src/dotenv/cli.py index c06c7068..cdbdfbad 100644 --- a/src/dotenv/cli.py +++ b/src/dotenv/cli.py @@ -39,7 +39,7 @@ def enumerate_env() -> Optional[str]: @click.option( "-f", "--file", - default=enumerate_env(), + default=enumerate_env, type=click.Path(file_okay=True), help="Location of the .env file, defaults to .env file in current working directory.", ) diff --git a/tests/test_cli.py b/tests/test_cli.py index 8123c9dd..c83130ee 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -40,6 +40,26 @@ def test_list( assert (result.exit_code, result.output) == (0, expected) +@pytest.mark.parametrize("command", [["list"], ["get", "KEY"]]) +def test_default_file_uses_current_directory(cli, tmp_path, monkeypatch, command): + first = tmp_path / "first" + second = tmp_path / "second" + first.mkdir() + second.mkdir() + (first / ".env").write_text("KEY=first\n") + (second / ".env").write_text("KEY=second\n") + + for directory, value in [(first, "first"), (second, "second")]: + monkeypatch.chdir(directory) + result = cli.invoke(dotenv_cli, command) + expected = f"KEY={value}\n" if command == ["list"] else f"{value}\n" + assert (result.exit_code, result.output) == (0, expected) + + result = cli.invoke(dotenv_cli, ["--file", first / ".env", *command]) + expected = "KEY=first\n" if command == ["list"] else "first\n" + assert (result.exit_code, result.output) == (0, expected) + + def test_list_non_existent_file(cli): result = cli.invoke(dotenv_cli, ["--file", "nx_file", "list"])