mirror of
https://github.com/Stone-Red-Code/dvc.git
synced 2026-11-10 23:10:42 +01:00
Merge pull request #3049 from pared/2896_remote_local
tests: remote: local: migrate to dir fixtures
This commit is contained in:
@@ -1,84 +1,64 @@
|
||||
import os
|
||||
|
||||
import mock
|
||||
import pytest
|
||||
|
||||
import dvc
|
||||
from dvc.exceptions import DvcException
|
||||
from dvc.remote.local import RemoteLOCAL
|
||||
from tests.utils import trees_equal
|
||||
|
||||
|
||||
class TestUnpackedDirStatusOptimization(object):
|
||||
@pytest.fixture(autouse=True)
|
||||
def setUp(self, dvc_repo, repo_dir):
|
||||
stages = dvc_repo.add(repo_dir.DATA_DIR)
|
||||
assert len(stages) == 1
|
||||
self.dir_out = stages[0].outs[0]
|
||||
self.unpacked_dir = (
|
||||
self.dir_out.cache_path + RemoteLOCAL.UNPACKED_DIR_SUFFIX
|
||||
)
|
||||
def test_dont_fail_on_unpacked_create_fail(tmp_dir, dvc):
|
||||
stage, = tmp_dir.dvc_gen({"dir": {"file": "file_content"}})
|
||||
|
||||
def test_should_not_fail_add_on_unpacked_dir_creation_exception(
|
||||
self, dvc_repo
|
||||
):
|
||||
with mock.patch.object(
|
||||
dvc.remote.local.RemoteLOCAL,
|
||||
"_create_unpacked_dir",
|
||||
side_effect=self._raise_dvc_exception,
|
||||
) as unpacked_create_spy, dvc_repo.state:
|
||||
assert not dvc_repo.cache.local.changed_cache(
|
||||
self.dir_out.checksum
|
||||
)
|
||||
assert unpacked_create_spy.call_count == 1
|
||||
with mock.patch.object(
|
||||
RemoteLOCAL, "_create_unpacked_dir", side_effect=DvcException("msg")
|
||||
) as unpacked_create_spy, dvc.state:
|
||||
assert not dvc.cache.local.changed_cache(stage.outs[0].checksum)
|
||||
assert unpacked_create_spy.call_count == 1
|
||||
|
||||
def test_should_remove_unpacked_dir_on_create_exception(self, dvc_repo):
|
||||
# artificial unpacked dir for test purpose
|
||||
os.makedirs(self.unpacked_dir)
|
||||
assert os.path.exists(self.unpacked_dir)
|
||||
|
||||
with mock.patch.object(
|
||||
dvc.remote.local.RemoteLOCAL,
|
||||
"_create_unpacked_dir",
|
||||
side_effect=self._raise_dvc_exception,
|
||||
), dvc_repo.state:
|
||||
assert not dvc_repo.cache.local.changed_cache(
|
||||
self.dir_out.checksum
|
||||
)
|
||||
def test_remove_unpacked_on_create_fail(tmp_dir, dvc):
|
||||
stage, = tmp_dir.dvc_gen({"dir": {"file": "file_content"}})
|
||||
unpacked_dir = stage.outs[0].cache_path + RemoteLOCAL.UNPACKED_DIR_SUFFIX
|
||||
|
||||
assert not os.path.exists(self.unpacked_dir)
|
||||
# artificial unpacked dir for test purpose
|
||||
os.makedirs(unpacked_dir)
|
||||
assert os.path.exists(unpacked_dir)
|
||||
|
||||
def test_should_create_unpacked_dir_on_status_check(
|
||||
self, dvc_repo, repo_dir
|
||||
):
|
||||
assert not os.path.exists(self.unpacked_dir)
|
||||
with mock.patch.object(
|
||||
RemoteLOCAL, "_create_unpacked_dir", side_effect=DvcException("msg")
|
||||
), dvc.state:
|
||||
assert not dvc.cache.local.changed_cache(stage.outs[0].checksum)
|
||||
|
||||
with dvc_repo.state:
|
||||
assert not dvc_repo.cache.local.changed_cache(
|
||||
self.dir_out.checksum
|
||||
)
|
||||
assert not os.path.exists(unpacked_dir)
|
||||
|
||||
assert os.path.exists(self.unpacked_dir)
|
||||
|
||||
trees_equal(repo_dir.DATA_DIR, self.unpacked_dir)
|
||||
def test_create_unpacked_on_status(tmp_dir, dvc):
|
||||
stage, = tmp_dir.dvc_gen({"dir": {"file": "file_content"}})
|
||||
unpacked_dir = stage.outs[0].cache_path + RemoteLOCAL.UNPACKED_DIR_SUFFIX
|
||||
assert not os.path.exists(unpacked_dir)
|
||||
|
||||
def test_should_proceed_with_full_check_on_modified_cache(self, dvc_repo):
|
||||
with dvc.state:
|
||||
assert not dvc.cache.local.changed_cache(stage.outs[0].checksum)
|
||||
assert os.path.exists(unpacked_dir)
|
||||
trees_equal("dir", unpacked_dir)
|
||||
|
||||
some_file_md5 = self.dir_out.dir_cache[0]["md5"]
|
||||
assert not os.path.exists(self.unpacked_dir)
|
||||
|
||||
with dvc_repo.state:
|
||||
assert not dvc_repo.cache.local.changed_cache(
|
||||
self.dir_out.checksum
|
||||
)
|
||||
assert os.path.exists(self.unpacked_dir)
|
||||
def test_dir_cache_changed_on_single_cache_file_modification(tmp_dir, dvc):
|
||||
stage, = tmp_dir.dvc_gen(
|
||||
{"dir": {"file1": "file1 content", "file2": "file2 content"}}
|
||||
)
|
||||
unpacked_dir = stage.outs[0].cache_path + RemoteLOCAL.UNPACKED_DIR_SUFFIX
|
||||
assert not os.path.exists(unpacked_dir)
|
||||
file_md5 = stage.outs[0].dir_cache[0]["md5"]
|
||||
|
||||
with open(dvc_repo.cache.local.get(some_file_md5), "a") as fobj:
|
||||
fobj.write("string")
|
||||
with dvc.state:
|
||||
assert not dvc.cache.local.changed_cache(stage.outs[0].checksum)
|
||||
assert os.path.exists(unpacked_dir)
|
||||
|
||||
with dvc_repo.state:
|
||||
assert dvc_repo.cache.local.changed_cache(self.dir_out.checksum)
|
||||
cache_file_path = dvc.cache.local.get(file_md5)
|
||||
with open(cache_file_path, "a") as fobj:
|
||||
fobj.write("modification")
|
||||
|
||||
@staticmethod
|
||||
def _raise_dvc_exception(*args):
|
||||
raise DvcException("msg")
|
||||
with dvc.state:
|
||||
assert dvc.cache.local.changed_cache(stage.outs[0].checksum)
|
||||
|
||||
Reference in New Issue
Block a user