def __exit__(self, *kwargs): super().__exit__(*kwargs) repo = Repo(get_path("root")) repo.add(files) for file in files: repo.commit(file) repo.push(files)
class TestCheckoutCorruptedCacheDir(TestDvc): def test(self): # NOTE: using 'copy' so that cache and link don't have same inode ret = main(["config", "cache.type", "copy"]) self.assertEqual(ret, 0) self.dvc = DvcRepo(".") stages = self.dvc.add(self.DATA_DIR) self.assertEqual(len(stages), 1) self.assertEqual(len(stages[0].outs), 1) out = stages[0].outs[0] # NOTE: modifying cache file for one of the files inside the directory # to check if dvc will detect that the cache is corrupted. _, entry_hash = next( self.dvc.cache.local.load_dir_cache(out.hash_info).items()) cache = os.fspath( self.dvc.cache.local.tree.hash_to_path_info(entry_hash.value)) os.chmod(cache, 0o644) with open(cache, "w+") as fobj: fobj.write("1") with pytest.raises(CheckoutError): self.dvc.checkout(force=True) self.assertFalse(os.path.exists(cache))
class TestCheckoutCorruptedCacheDir(TestDvc): def test(self): # NOTE: using 'copy' so that cache and link don't have same inode ret = main(["config", "cache.type", "copy"]) self.assertEqual(ret, 0) self.dvc = DvcRepo(".") stages = self.dvc.add(self.DATA_DIR) self.assertEqual(len(stages), 1) self.assertEqual(len(stages[0].outs), 1) out = stages[0].outs[0] # NOTE: modifying cache file for one of the files inside the directory # to check if dvc will detect that the cache is corrupted. entry = self.dvc.cache.local.load_dir_cache(out.checksum)[0] checksum = entry[self.dvc.cache.local.PARAM_CHECKSUM] cache = self.dvc.cache.local.get(checksum) os.chmod(cache, 0o644) with open(cache, "w+") as fobj: fobj.write("1") with pytest.raises(CheckoutError): self.dvc.checkout(force=True) self.assertFalse(os.path.exists(cache))
def test_update_import(dvc_repo, erepo): src = "version" dst = src stage = dvc_repo.imp(erepo.root_dir, src, dst, rev="branch") assert os.path.exists(dst) assert os.path.isfile(dst) with open(dst, "r+") as fobj: assert fobj.read() == "branch" # update data repo = Repo(erepo.root_dir) saved_dir = os.getcwd() os.chdir(erepo.root_dir) repo.scm.checkout("branch") os.unlink("version") erepo.create("version", "updated") repo.add("version") repo.scm.add([".gitignore", "version.dvc"]) repo.scm.commit("updated") repo.scm.checkout("master") repo.scm.close() os.chdir(saved_dir) # Caching in external repos doesn't see upstream updates within single # cli call, so we need to clean the caches to see the changes. clean_repos() assert dvc_repo.status([stage.path]) == {} dvc_repo.update(stage.path) assert dvc_repo.status([stage.path]) == {} assert os.path.exists(dst) assert os.path.isfile(dst) with open(dst, "r+") as fobj: assert fobj.read() == "updated"
def test_update_import(dvc_repo, erepo): src = "version" dst = src stage = dvc_repo.imp(erepo.root_dir, src, dst, rev="branch") assert os.path.exists(dst) assert os.path.isfile(dst) with open(dst, "r+") as fobj: assert fobj.read() == "branch" # update data repo = Repo(erepo.root_dir) saved_dir = os.getcwd() os.chdir(erepo.root_dir) repo.scm.checkout("branch") os.unlink("version") erepo.create("version", "updated") repo.add("version") repo.scm.add([".gitignore", "version.dvc"]) repo.scm.commit("updated") repo.scm.checkout("master") repo.scm.git.close() os.chdir(saved_dir) assert dvc_repo.status(stage.path) == {} dvc_repo.update(stage.path) assert dvc_repo.status(stage.path) == {} assert os.path.exists(dst) assert os.path.isfile(dst) with open(dst, "r+") as fobj: assert fobj.read() == "updated"
class TestGCMultipleDvcRepos(TestDvcGit): def _check_cache(self, num): total = 0 for root, dirs, files in os.walk(os.path.join(".dvc", "cache")): total += len(files) self.assertEqual(total, num) def setUp(self): super().setUp() self.additional_path = TestDir.mkdtemp() self.additional_git = Repo.init(self.additional_path) self.additional_dvc = DvcRepo.init(self.additional_path) cache_path = os.path.join(self._root_dir, ".dvc", "cache") config_path = os.path.join(self.additional_path, ".dvc", "config.local") cfg = configobj.ConfigObj() cfg.filename = config_path cfg["cache"] = {"dir": cache_path} cfg.write() self.additional_dvc = DvcRepo(self.additional_path) def test(self): # ADD FILE ONLY IN MAIN PROJECT fname = "only_in_first" with open(fname, "w+") as fobj: fobj.write("only in main repo") stages = self.dvc.add(fname) self.assertEqual(len(stages), 1) # ADD FILE IN MAIN PROJECT THAT IS ALSO IN SECOND PROJECT fname = "in_both" with open(fname, "w+") as fobj: fobj.write("in both repos") stages = self.dvc.add(fname) self.assertEqual(len(stages), 1) cwd = os.getcwd() os.chdir(self.additional_path) # ADD FILE ONLY IN SECOND PROJECT fname = os.path.join(self.additional_path, "only_in_second") with open(fname, "w+") as fobj: fobj.write("only in additional repo") stages = self.additional_dvc.add(fname) self.assertEqual(len(stages), 1) # ADD FILE IN SECOND PROJECT THAT IS ALSO IN MAIN PROJECT fname = os.path.join(self.additional_path, "in_both") with open(fname, "w+") as fobj: fobj.write("in both repos") stages = self.additional_dvc.add(fname) self.assertEqual(len(stages), 1) os.chdir(cwd) self._check_cache(3) self.dvc.gc(repos=[self.additional_path], workspace=True) self._check_cache(3) self.dvc.gc(workspace=True) self._check_cache(2)