|
1 | 1 | import os |
2 | 2 |
|
3 | 3 | import mock |
4 | | -import pytest |
5 | 4 |
|
6 | | -import dvc |
7 | 5 | from dvc.exceptions import DvcException |
8 | 6 | from dvc.remote.local import RemoteLOCAL |
9 | 7 | from tests.utils import trees_equal |
10 | 8 |
|
11 | 9 |
|
12 | | -class TestUnpackedDirStatusOptimization(object): |
13 | | - @pytest.fixture(autouse=True) |
14 | | - def setUp(self, dvc_repo, repo_dir): |
15 | | - stages = dvc_repo.add(repo_dir.DATA_DIR) |
16 | | - assert len(stages) == 1 |
17 | | - self.dir_out = stages[0].outs[0] |
18 | | - self.unpacked_dir = ( |
19 | | - self.dir_out.cache_path + RemoteLOCAL.UNPACKED_DIR_SUFFIX |
20 | | - ) |
21 | | - |
22 | | - def test_should_not_fail_add_on_unpacked_dir_creation_exception( |
23 | | - self, dvc_repo |
24 | | - ): |
25 | | - with mock.patch.object( |
26 | | - dvc.remote.local.RemoteLOCAL, |
27 | | - "_create_unpacked_dir", |
28 | | - side_effect=self._raise_dvc_exception, |
29 | | - ) as unpacked_create_spy, dvc_repo.state: |
30 | | - assert not dvc_repo.cache.local.changed_cache( |
31 | | - self.dir_out.checksum |
32 | | - ) |
33 | | - assert unpacked_create_spy.call_count == 1 |
34 | | - |
35 | | - def test_should_remove_unpacked_dir_on_create_exception(self, dvc_repo): |
36 | | - # artificial unpacked dir for test purpose |
37 | | - os.makedirs(self.unpacked_dir) |
38 | | - assert os.path.exists(self.unpacked_dir) |
39 | | - |
40 | | - with mock.patch.object( |
41 | | - dvc.remote.local.RemoteLOCAL, |
42 | | - "_create_unpacked_dir", |
43 | | - side_effect=self._raise_dvc_exception, |
44 | | - ), dvc_repo.state: |
45 | | - assert not dvc_repo.cache.local.changed_cache( |
46 | | - self.dir_out.checksum |
47 | | - ) |
48 | | - |
49 | | - assert not os.path.exists(self.unpacked_dir) |
50 | | - |
51 | | - def test_should_create_unpacked_dir_on_status_check( |
52 | | - self, dvc_repo, repo_dir |
53 | | - ): |
54 | | - assert not os.path.exists(self.unpacked_dir) |
55 | | - |
56 | | - with dvc_repo.state: |
57 | | - assert not dvc_repo.cache.local.changed_cache( |
58 | | - self.dir_out.checksum |
59 | | - ) |
60 | | - |
61 | | - assert os.path.exists(self.unpacked_dir) |
62 | | - |
63 | | - trees_equal(repo_dir.DATA_DIR, self.unpacked_dir) |
64 | | - |
65 | | - def test_should_proceed_with_full_check_on_modified_cache(self, dvc_repo): |
66 | | - |
67 | | - some_file_md5 = self.dir_out.dir_cache[0]["md5"] |
68 | | - assert not os.path.exists(self.unpacked_dir) |
69 | | - |
70 | | - with dvc_repo.state: |
71 | | - assert not dvc_repo.cache.local.changed_cache( |
72 | | - self.dir_out.checksum |
73 | | - ) |
74 | | - assert os.path.exists(self.unpacked_dir) |
75 | | - |
76 | | - with open(dvc_repo.cache.local.get(some_file_md5), "a") as fobj: |
77 | | - fobj.write("string") |
78 | | - |
79 | | - with dvc_repo.state: |
80 | | - assert dvc_repo.cache.local.changed_cache(self.dir_out.checksum) |
81 | | - |
82 | | - @staticmethod |
83 | | - def _raise_dvc_exception(*args): |
84 | | - raise DvcException("msg") |
| 10 | +def test_dont_fail_on_unpacked_create_fail(tmp_dir, dvc): |
| 11 | + stage, = tmp_dir.dvc_gen({"dir": {"file": "file_content"}}) |
| 12 | + |
| 13 | + with mock.patch.object( |
| 14 | + RemoteLOCAL, "_create_unpacked_dir", side_effect=DvcException("msg") |
| 15 | + ) as unpacked_create_spy, dvc.state: |
| 16 | + assert not dvc.cache.local.changed_cache(stage.outs[0].checksum) |
| 17 | + assert unpacked_create_spy.call_count == 1 |
| 18 | + |
| 19 | + |
| 20 | +def test_remove_unpacked_on_create_fail(tmp_dir, dvc): |
| 21 | + stage, = tmp_dir.dvc_gen({"dir": {"file": "file_content"}}) |
| 22 | + unpacked_dir = stage.outs[0].cache_path + RemoteLOCAL.UNPACKED_DIR_SUFFIX |
| 23 | + |
| 24 | + # artificial unpacked dir for test purpose |
| 25 | + os.makedirs(unpacked_dir) |
| 26 | + assert os.path.exists(unpacked_dir) |
| 27 | + |
| 28 | + with mock.patch.object( |
| 29 | + RemoteLOCAL, "_create_unpacked_dir", side_effect=DvcException("msg") |
| 30 | + ), dvc.state: |
| 31 | + assert not dvc.cache.local.changed_cache(stage.outs[0].checksum) |
| 32 | + |
| 33 | + assert not os.path.exists(unpacked_dir) |
| 34 | + |
| 35 | + |
| 36 | +def test_create_unpacked_on_status(tmp_dir, dvc): |
| 37 | + stage, = tmp_dir.dvc_gen({"dir": {"file": "file_content"}}) |
| 38 | + unpacked_dir = stage.outs[0].cache_path + RemoteLOCAL.UNPACKED_DIR_SUFFIX |
| 39 | + assert not os.path.exists(unpacked_dir) |
| 40 | + |
| 41 | + with dvc.state: |
| 42 | + assert not dvc.cache.local.changed_cache(stage.outs[0].checksum) |
| 43 | + assert os.path.exists(unpacked_dir) |
| 44 | + trees_equal("dir", unpacked_dir) |
| 45 | + |
| 46 | + |
| 47 | +def test_dir_cache_changed_on_single_cache_file_modification(tmp_dir, dvc): |
| 48 | + stage, = tmp_dir.dvc_gen( |
| 49 | + {"dir": {"file1": "file1 content", "file2": "file2 content"}} |
| 50 | + ) |
| 51 | + unpacked_dir = stage.outs[0].cache_path + RemoteLOCAL.UNPACKED_DIR_SUFFIX |
| 52 | + assert not os.path.exists(unpacked_dir) |
| 53 | + file_md5 = stage.outs[0].dir_cache[0]["md5"] |
| 54 | + |
| 55 | + with dvc.state: |
| 56 | + assert not dvc.cache.local.changed_cache(stage.outs[0].checksum) |
| 57 | + assert os.path.exists(unpacked_dir) |
| 58 | + |
| 59 | + cache_file_path = dvc.cache.local.get(file_md5) |
| 60 | + with open(cache_file_path, "a") as fobj: |
| 61 | + fobj.write("modification") |
| 62 | + |
| 63 | + with dvc.state: |
| 64 | + assert dvc.cache.local.changed_cache(stage.outs[0].checksum) |
0 commit comments