From 9b2ef002ea5dfe4299dfdf51989098dd7fa94798 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Redzy=C5=84ski?= Date: Wed, 19 Feb 2020 19:25:04 +0100 Subject: [PATCH] Stage: create: reset repo after new stage creation (#3349) --- dvc/repo/__init__.py | 1 + dvc/repo/add.py | 1 + tests/func/test_add.py | 7 +++++++ tests/unit/repo/test_repo.py | 17 +++++++++++++++++ 4 files changed, 26 insertions(+) diff --git a/dvc/repo/__init__.py b/dvc/repo/__init__.py index 36dc6b073c..6205e24e51 100644 --- a/dvc/repo/__init__.py +++ b/dvc/repo/__init__.py @@ -24,6 +24,7 @@ def locked(f): @wraps(f) def wrapper(repo, *args, **kwargs): with repo.lock, repo.state: + repo._reset() ret = f(repo, *args, **kwargs) # Our graph cache is no longer valid after we release the repo.lock repo._reset() diff --git a/dvc/repo/add.py b/dvc/repo/add.py index f216d6ddec..b0a0fa586c 100644 --- a/dvc/repo/add.py +++ b/dvc/repo/add.py @@ -126,6 +126,7 @@ def _create_stages(repo, targets, fname, pbar=None): stage = Stage.create( repo, outs=[out], accompany_outs=True, fname=fname ) + repo._reset() if not stage: if pbar is not None: diff --git a/tests/func/test_add.py b/tests/func/test_add.py index 13b83c6315..95407ee7d0 100644 --- a/tests/func/test_add.py +++ b/tests/func/test_add.py @@ -655,3 +655,10 @@ def test_add_from_data_dir(tmp_dir, scm, dvc): "tracked output: 'dir'.\n" "To include '{out}' in 'dir', run 'dvc commit dir.dvc'" ).format(out=os.path.join("dir", "file2")) + + +def test_not_raises_on_re_add(tmp_dir, dvc): + tmp_dir.dvc_gen("file", "file content") + + tmp_dir.gen({"file2": "file2 content", "file": "modified file"}) + dvc.add(["file2", "file"]) diff --git a/tests/unit/repo/test_repo.py b/tests/unit/repo/test_repo.py index d544da7930..b6ee260de9 100644 --- a/tests/unit/repo/test_repo.py +++ b/tests/unit/repo/test_repo.py @@ -2,6 +2,8 @@ import pytest +from dvc.repo import locked + def test_is_dvc_internal(dvc): assert dvc.is_dvc_internal(os.path.join("path", "to", ".dvc", "file")) @@ -49,3 +51,18 @@ def test_used_cache(tmp_dir, dvc, path): used_cache._items == expected._items and used_cache.external == expected.external ) + + +def test_locked(mocker): + repo = mocker.MagicMock() + repo.method = locked(repo.method) + + args = {} + kwargs = {} + repo.method(repo, args, kwargs) + + assert repo.method_calls == [ + mocker.call._reset(), + mocker.call.method(repo, args, kwargs), + mocker.call._reset(), + ]