Skip to content

Commit

Permalink
dvc: properly handle relative symlinks (iterative#4275)
Browse files Browse the repository at this point in the history
  • Loading branch information
efiop authored Jul 24, 2020
1 parent 1fd8b47 commit 39c0bdb
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
3 changes: 1 addition & 2 deletions dvc/utils/fs.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,7 @@ def move(src, dst, mode=None):
tmp = f"{dst}.{uuid()}"

if os.path.islink(src):
# readlink does not accept path-like obj for Windows in Python <3.8
shutil.copy(os.readlink(os.fspath(src)), tmp)
shutil.copy(src, tmp)
os.unlink(src)
else:
shutil.move(src, tmp)
Expand Down
20 changes: 20 additions & 0 deletions tests/func/test_add.py
Original file line number Diff line number Diff line change
Expand Up @@ -806,3 +806,23 @@ def test_add_pipeline_file(tmp_dir, dvc, run_copy):

with pytest.raises(OutputIsStageFileError):
dvc.add(PIPELINE_FILE)


def test_add_symlink(tmp_dir, dvc):
tmp_dir.gen({"dir": {"bar": "bar"}})

(tmp_dir / "dir" / "foo").symlink_to(os.path.join(".", "bar"))

dvc.add(os.path.join("dir", "foo"))

assert not (tmp_dir / "dir" / "foo").is_symlink()
assert not (tmp_dir / "dir" / "bar").is_symlink()
assert (tmp_dir / "dir" / "foo").read_text() == "bar"
assert (tmp_dir / "dir" / "bar").read_text() == "bar"

assert (tmp_dir / ".dvc" / "cache").read_text() == {
"37": {"b51d194a7513e45b56f6524f2d51f2": "bar"}
}
assert not (
tmp_dir / ".dvc" / "cache" / "37" / "b51d194a7513e45b56f6524f2d51f2"
).is_symlink()

0 comments on commit 39c0bdb

Please sign in to comment.