Skip to content
Open
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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

<!-- contributors -->
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/dotenv/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
)
Expand Down
20 changes: 20 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"])

Expand Down
Loading