Skip to content
Open
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
36 changes: 36 additions & 0 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,24 @@ def test_set_key_follow_symlinks(tmp_path):
assert symlink.is_symlink()


@pytest.mark.skipif(
sys.platform == "win32", reason="symlinks require elevated privileges on Windows"
)
def test_set_key_symlink_keeps_target_contents(tmp_path):
# The target holds a key the set does not touch, so losing the contents the
# path resolved to would show up here.
target = tmp_path / "target.env"
target.write_text("b=x\n")
symlink = tmp_path / ".env"
symlink.symlink_to(target)

dotenv.set_key(symlink, "a", "y")

assert target.read_text() == "b=x\n"
assert not symlink.is_symlink()
assert symlink.read_text() == "b=x\na='y'\n"


@pytest.mark.skipif(
sys.platform != "win32" and os.geteuid() == 0,
reason="Root user can access files even with 000 permissions.",
Expand Down Expand Up @@ -365,6 +383,24 @@ def test_unset_key_follow_symlinks(tmp_path):
assert symlink.is_symlink()


@pytest.mark.skipif(
sys.platform == "win32", reason="symlinks require elevated privileges on Windows"
)
def test_unset_key_symlink_keeps_target_contents(tmp_path):
# The target holds a second key the unset does not touch, so losing the
# contents the path resolved to would show up here.
target = tmp_path / "target.env"
target.write_text("a=x\nb=y\n")
symlink = tmp_path / ".env"
symlink.symlink_to(target)

dotenv.unset_key(symlink, "a")

assert target.read_text() == "a=x\nb=y\n"
assert not symlink.is_symlink()
assert symlink.read_text() == "b=y\n"


def prepare_file_hierarchy(path):
"""
Create a temporary folder structure like the following:
Expand Down