|
9 | 9 | from dvc.hash_info import HashInfo |
10 | 10 | from dvc.objects.errors import ObjectFormatError |
11 | 11 | from dvc.utils import relpath |
12 | | -from tests.basic_env import TestDir, TestDvc |
13 | | - |
14 | | - |
15 | | -class TestCache(TestDvc): |
16 | | - def setUp(self): |
17 | | - super().setUp() |
18 | | - self.cache1_md5 = "123" |
19 | | - self.cache2_md5 = "234" |
20 | | - self.cache1 = os.path.join( |
21 | | - self.dvc.odb.local.cache_dir, |
22 | | - self.cache1_md5[0:2], |
23 | | - self.cache1_md5[2:], |
24 | | - ) |
25 | | - self.cache2 = os.path.join( |
26 | | - self.dvc.odb.local.cache_dir, |
27 | | - self.cache2_md5[0:2], |
28 | | - self.cache2_md5[2:], |
29 | | - ) |
30 | | - self.create(self.cache1, "1") |
31 | | - self.create(self.cache2, "2") |
32 | 12 |
|
33 | | - def test_all(self): |
34 | | - md5_list = list(ODBManager(self.dvc).local.all()) |
35 | | - self.assertEqual(len(md5_list), 2) |
36 | | - self.assertIn(self.cache1_md5, md5_list) |
37 | | - self.assertIn(self.cache2_md5, md5_list) |
38 | 13 |
|
39 | | - def test_get(self): |
40 | | - cache = ODBManager(self.dvc).local.hash_to_path(self.cache1_md5) |
41 | | - self.assertEqual(os.fspath(cache), self.cache1) |
| 14 | +def test_cache(tmp_dir, dvc): |
| 15 | + cache1_md5 = "123" |
| 16 | + cache2_md5 = "234" |
| 17 | + cache1 = os.path.join( |
| 18 | + dvc.odb.local.cache_dir, |
| 19 | + cache1_md5[0:2], |
| 20 | + cache1_md5[2:], |
| 21 | + ) |
| 22 | + cache2 = os.path.join( |
| 23 | + dvc.odb.local.cache_dir, |
| 24 | + cache2_md5[0:2], |
| 25 | + cache2_md5[2:], |
| 26 | + ) |
| 27 | + tmp_dir.gen({cache1: "1", cache2: "2"}) |
42 | 28 |
|
| 29 | + assert os.path.exists(cache1) |
| 30 | + assert os.path.exists(cache2) |
43 | 31 |
|
44 | | -class TestCacheLoadBadDirCache(TestDvc): |
45 | | - def _do_test(self, ret): |
46 | | - self.assertTrue(isinstance(ret, list)) |
47 | | - self.assertEqual(len(ret), 0) |
| 32 | + odb = ODBManager(dvc) |
48 | 33 |
|
49 | | - def test(self): |
50 | | - from dvc.data import load |
| 34 | + md5_list = list(odb.local.all()) |
| 35 | + assert len(md5_list) == 2 |
| 36 | + assert cache1_md5 in md5_list |
| 37 | + assert cache2_md5 in md5_list |
51 | 38 |
|
52 | | - dir_hash = "123.dir" |
53 | | - fname = os.fspath(self.dvc.odb.local.hash_to_path(dir_hash)) |
54 | | - self.create(fname, "<clearly>not,json") |
55 | | - with pytest.raises(ObjectFormatError): |
56 | | - load(self.dvc.odb.local, HashInfo("md5", dir_hash)) |
| 39 | + odb_cache1 = odb.local.hash_to_path(cache1_md5) |
| 40 | + odb_cache2 = odb.local.hash_to_path(cache2_md5) |
| 41 | + assert os.fspath(odb_cache1) == cache1 |
| 42 | + assert os.fspath(odb_cache2) == cache2 |
57 | 43 |
|
58 | | - dir_hash = "234.dir" |
59 | | - fname = os.fspath(self.dvc.odb.local.hash_to_path(dir_hash)) |
60 | | - self.create(fname, '{"a": "b"}') |
61 | | - with pytest.raises(ObjectFormatError): |
62 | | - load(self.dvc.odb.local, HashInfo("md5", dir_hash)) |
63 | 44 |
|
| 45 | +def test_cache_load_bad_dir_cache(tmp_dir, dvc): |
| 46 | + from dvc.data import load |
64 | 47 |
|
65 | | -class TestExternalCacheDir(TestDvc): |
66 | | - def test(self): |
67 | | - cache_dir = TestDvc.mkdtemp() |
| 48 | + dir_hash = "123.dir" |
| 49 | + fname = os.fspath(dvc.odb.local.hash_to_path(dir_hash)) |
| 50 | + tmp_dir.gen({fname: "<clearly>not,json"}) |
| 51 | + with pytest.raises(ObjectFormatError): |
| 52 | + load(dvc.odb.local, HashInfo("md5", dir_hash)) |
68 | 53 |
|
69 | | - ret = main(["config", "cache.dir", cache_dir]) |
70 | | - self.assertEqual(ret, 0) |
| 54 | + dir_hash = "234.dir" |
| 55 | + fname = os.fspath(dvc.odb.local.hash_to_path(dir_hash)) |
| 56 | + tmp_dir.gen({fname: '{"a": "b"}'}) |
| 57 | + with pytest.raises(ObjectFormatError): |
| 58 | + load(dvc.odb.local, HashInfo("md5", dir_hash)) |
71 | 59 |
|
72 | | - self.assertFalse(os.path.exists(self.dvc.odb.local.cache_dir)) |
73 | 60 |
|
74 | | - ret = main(["add", self.FOO]) |
75 | | - self.assertEqual(ret, 0) |
| 61 | +def test_external_cache_dir(tmp_dir, dvc, make_tmp_dir): |
| 62 | + cache_dir = make_tmp_dir("cache") |
76 | 63 |
|
77 | | - ret = main(["add", self.DATA_DIR]) |
78 | | - self.assertEqual(ret, 0) |
| 64 | + with dvc.config.edit() as conf: |
| 65 | + conf["cache"]["dir"] = cache_dir.fs_path |
| 66 | + assert not os.path.exists(dvc.odb.local.cache_dir) |
| 67 | + dvc.odb = ODBManager(dvc) |
79 | 68 |
|
80 | | - self.assertFalse(os.path.exists(".dvc/cache")) |
81 | | - self.assertNotEqual(len(os.listdir(cache_dir)), 0) |
| 69 | + tmp_dir.dvc_gen({"foo": "foo"}) |
82 | 70 |
|
83 | | - def test_remote_references(self): |
84 | | - ssh_url = "ssh://user@localhost:23" |
85 | | - assert main(["remote", "add", "storage", ssh_url]) == 0 |
86 | | - assert main(["remote", "add", "cache", "remote://storage/tmp"]) == 0 |
87 | | - assert main(["config", "cache.ssh", "cache"]) == 0 |
| 71 | + tmp_dir.dvc_gen( |
| 72 | + { |
| 73 | + "data_dir": { |
| 74 | + "data": "data_dir/data", |
| 75 | + "data_sub_dir": {"data_sub": "data_dir/data_sub_dir/data_sub"}, |
| 76 | + } |
| 77 | + } |
| 78 | + ) |
| 79 | + |
| 80 | + assert not os.path.exists(".dvc/cache") |
| 81 | + assert len(os.listdir(cache_dir)) != 0 |
88 | 82 |
|
89 | | - self.dvc.__init__() |
90 | 83 |
|
91 | | - assert self.dvc.odb.ssh.fs_path == "/tmp" |
| 84 | +def test_remote_cache_references(tmp_dir, dvc): |
| 85 | + with dvc.config.edit() as conf: |
| 86 | + conf["remote"]["storage"] = {"url": "ssh://user@localhost:23"} |
| 87 | + conf["remote"]["cache"] = {"url": "remote://storage/tmp"} |
| 88 | + conf["cache"]["ssh"] = "cache" |
92 | 89 |
|
| 90 | + dvc.odb = ODBManager(dvc) |
93 | 91 |
|
94 | | -class TestSharedCacheDir(TestDir): |
95 | | - def test(self): |
96 | | - cache_dir = os.path.abspath(os.path.join(os.curdir, "cache")) |
97 | | - for d in ["dir1", "dir2"]: |
98 | | - os.mkdir(d) |
99 | | - os.chdir(d) |
| 92 | + assert dvc.odb.ssh.fs_path == "/tmp" |
100 | 93 |
|
| 94 | + |
| 95 | +def test_shared_cache_dir(tmp_dir): |
| 96 | + cache_dir = os.path.abspath(os.path.join(os.curdir, "cache")) |
| 97 | + for d in ["dir1", "dir2"]: |
| 98 | + os.mkdir(d) |
| 99 | + with (tmp_dir / d).chdir(): |
101 | 100 | ret = main(["init", "--no-scm"]) |
102 | | - self.assertEqual(ret, 0) |
| 101 | + assert ret == 0 |
103 | 102 |
|
104 | 103 | ret = main(["config", "cache.dir", cache_dir]) |
105 | | - self.assertEqual(ret, 0) |
106 | | - |
107 | | - self.assertFalse(os.path.exists(os.path.join(".dvc", "cache"))) |
| 104 | + assert ret == 0 |
108 | 105 |
|
109 | | - with open("common", "w+", encoding="utf-8") as fd: |
110 | | - fd.write("common") |
| 106 | + assert not os.path.exists(os.path.join(".dvc", "cache")) |
111 | 107 |
|
112 | | - with open("unique", "w+", encoding="utf-8") as fd: |
113 | | - fd.write(d) |
| 108 | + (tmp_dir / d).gen({"common": "common", "unique": d}) |
114 | 109 |
|
115 | 110 | ret = main(["add", "common", "unique"]) |
116 | | - self.assertEqual(ret, 0) |
| 111 | + assert ret == 0 |
117 | 112 |
|
118 | | - os.chdir("..") |
| 113 | + assert not os.path.exists(os.path.join("dir1", ".dvc", "cache")) |
| 114 | + assert not os.path.exists(os.path.join("dir2", ".dvc", "cache")) |
119 | 115 |
|
120 | | - self.assertFalse(os.path.exists(os.path.join("dir1", ".dvc", "cache"))) |
121 | | - self.assertFalse(os.path.exists(os.path.join("dir2", ".dvc", "cache"))) |
122 | | - |
123 | | - subdirs = list( |
124 | | - filter( |
125 | | - lambda x: os.path.isdir(os.path.join(cache_dir, x)), |
126 | | - os.listdir(cache_dir), |
127 | | - ) |
128 | | - ) |
129 | | - self.assertEqual(len(subdirs), 3) |
130 | | - self.assertEqual( |
131 | | - len(os.listdir(os.path.join(cache_dir, subdirs[0]))), 1 |
132 | | - ) |
133 | | - self.assertEqual( |
134 | | - len(os.listdir(os.path.join(cache_dir, subdirs[1]))), 1 |
135 | | - ) |
136 | | - self.assertEqual( |
137 | | - len(os.listdir(os.path.join(cache_dir, subdirs[2]))), 1 |
| 116 | + subdirs = list( |
| 117 | + filter( |
| 118 | + lambda x: os.path.isdir(os.path.join(cache_dir, x)), |
| 119 | + os.listdir(cache_dir), |
138 | 120 | ) |
| 121 | + ) |
| 122 | + assert len(subdirs) == 3 |
| 123 | + assert len(os.listdir(os.path.join(cache_dir, subdirs[0]))) == 1 |
139 | 124 |
|
| 125 | + assert len(os.listdir(os.path.join(cache_dir, subdirs[1]))) == 1 |
| 126 | + assert len(os.listdir(os.path.join(cache_dir, subdirs[2]))) == 1 |
140 | 127 |
|
141 | | -class TestCacheLinkType(TestDvc): |
142 | | - def test(self): |
143 | | - ret = main(["config", "cache.type", "reflink,copy"]) |
144 | | - self.assertEqual(ret, 0) |
145 | 128 |
|
146 | | - ret = main(["add", self.FOO]) |
147 | | - self.assertEqual(ret, 0) |
| 129 | +def test_cache_link_type(tmp_dir, scm, dvc): |
| 130 | + with dvc.config.edit() as conf: |
| 131 | + conf["cache"]["type"] = "reflink,copy" |
| 132 | + dvc.odb = ODBManager(dvc) |
148 | 133 |
|
| 134 | + stages = tmp_dir.dvc_gen({"foo": "foo"}) |
| 135 | + assert len(stages) == 1 |
| 136 | + assert (tmp_dir / "foo").read_text().strip() == "foo" |
149 | 137 |
|
150 | | -class TestCmdCacheDir(TestDvc): |
151 | | - def test(self): |
152 | | - ret = main(["cache", "dir"]) |
153 | | - self.assertEqual(ret, 0) |
154 | 138 |
|
155 | | - def test_abs_path(self): |
156 | | - dname = os.path.join(os.path.dirname(self._root_dir), "dir") |
157 | | - ret = main(["cache", "dir", dname]) |
158 | | - self.assertEqual(ret, 0) |
| 139 | +def test_cmd_cache_dir(tmp_dir, scm, dvc): |
| 140 | + ret = main(["cache", "dir"]) |
| 141 | + assert ret == 0 |
159 | 142 |
|
160 | | - config = configobj.ConfigObj(self.dvc.config.files["repo"]) |
161 | | - self.assertEqual(config["cache"]["dir"], dname) |
162 | 143 |
|
163 | | - def test_relative_path(self): |
164 | | - tmpdir = os.path.realpath(self.mkdtemp()) |
165 | | - dname = relpath(tmpdir) |
166 | | - ret = main(["cache", "dir", dname]) |
167 | | - self.assertEqual(ret, 0) |
| 144 | +def test_cmd_cache_abs_path(tmp_dir, scm, dvc, make_tmp_dir): |
| 145 | + cache_dir = make_tmp_dir("cache") |
| 146 | + ret = main(["cache", "dir", cache_dir.fs_path]) |
| 147 | + assert ret == 0 |
| 148 | + |
| 149 | + config = configobj.ConfigObj(dvc.config.files["repo"]) |
| 150 | + assert config["cache"]["dir"] == cache_dir.fs_path |
| 151 | + |
| 152 | + |
| 153 | +def test_cmd_cache_relative_path(tmp_dir, scm, dvc, make_tmp_dir): |
| 154 | + cache_dir = make_tmp_dir("cache") |
| 155 | + dname = relpath(cache_dir) |
| 156 | + ret = main(["cache", "dir", dname]) |
| 157 | + assert ret == 0 |
| 158 | + |
| 159 | + dvc.config.load() |
| 160 | + dvc.odb = ODBManager(dvc) |
168 | 161 |
|
169 | | - # NOTE: we are in the repo's root and config is in .dvc/, so |
170 | | - # dir path written to config should be just one level above. |
171 | | - rel = os.path.join("..", dname) |
172 | | - config = configobj.ConfigObj(self.dvc.config.files["repo"]) |
173 | | - self.assertEqual(config["cache"]["dir"], rel.replace("\\", "/")) |
| 162 | + # NOTE: we are in the repo's root and config is in .dvc/, so |
| 163 | + # dir path written to config should be just one level above. |
| 164 | + rel = os.path.join("..", dname) |
| 165 | + config = configobj.ConfigObj(dvc.config.files["repo"]) |
| 166 | + assert config["cache"]["dir"] == rel.replace("\\", "/") |
174 | 167 |
|
175 | | - ret = main(["add", self.FOO]) |
176 | | - self.assertEqual(ret, 0) |
| 168 | + tmp_dir.dvc_gen({"foo": "foo"}) |
177 | 169 |
|
178 | | - subdirs = os.listdir(tmpdir) |
179 | | - self.assertEqual(len(subdirs), 1) |
180 | | - files = os.listdir(os.path.join(tmpdir, subdirs[0])) |
181 | | - self.assertEqual(len(files), 1) |
| 170 | + subdirs = os.listdir(cache_dir) |
| 171 | + assert len(subdirs) == 1 |
| 172 | + files = os.listdir(os.path.join(cache_dir, subdirs[0])) |
| 173 | + assert len(files) == 1 |
182 | 174 |
|
183 | 175 |
|
184 | 176 | def test_default_cache_type(dvc): |
|
0 commit comments