From 0139d47882ac060d9df53adb8fbb72699b8f2bdc Mon Sep 17 00:00:00 2001 From: pared Date: Fri, 19 Apr 2019 18:59:44 -0700 Subject: [PATCH] tree: walk: operate on tree absolute root --- dvc/scm/git/tree.py | 4 +-- dvc/scm/tree.py | 2 +- tests/func/test_metrics.py | 27 +++++++++++++++ tests/func/test_tree.py | 70 +++++++++++++++++++++++++++++--------- 4 files changed, 84 insertions(+), 19 deletions(-) diff --git a/dvc/scm/git/tree.py b/dvc/scm/git/tree.py index 3cb21834be..3312942ff5 100644 --- a/dvc/scm/git/tree.py +++ b/dvc/scm/git/tree.py @@ -109,7 +109,7 @@ def _walk( tree.abspath, ignore_file_handler=ignore_file_handler ) dirs, nondirs = dvc_ignore_filter(tree.path, dirs, nondirs) - yield os.path.normpath(tree.path), dirs, nondirs + yield os.path.normpath(tree.abspath), dirs, nondirs for i in dirs: for x in self._walk( @@ -121,7 +121,7 @@ def _walk( yield x if not topdown: - yield os.path.normpath(tree.path), dirs, nondirs + yield os.path.normpath(tree.abspath), dirs, nondirs def walk(self, top, topdown=True, ignore_file_handler=None): """Directory tree generator. diff --git a/dvc/scm/tree.py b/dvc/scm/tree.py index c411a7076f..8a555902f9 100644 --- a/dvc/scm/tree.py +++ b/dvc/scm/tree.py @@ -72,7 +72,7 @@ def onerror(e): raise e for root, dirs, files in dvc_walk( - top, + os.path.abspath(top), topdown=topdown, onerror=onerror, ignore_file_handler=ignore_file_handler, diff --git a/tests/func/test_metrics.py b/tests/func/test_metrics.py index 90370264a9..fec94b501e 100644 --- a/tests/func/test_metrics.py +++ b/tests/func/test_metrics.py @@ -279,6 +279,33 @@ def test_formatted_output(self): assert expected_txt in stdout assert expected_json in stdout + def test_show_all_should_be_current_dir_agnostic(self): + os.chdir(self.DATA_DIR) + + metrics = self.dvc.metrics.show(all_branches=True) + self.assertMetricsHaveRelativePaths(metrics) + + def assertMetricsHaveRelativePaths(self, metrics): + root_relpath = os.path.relpath(self.dvc.root_dir) + metric_path = os.path.join(root_relpath, "metric") + metric_json_path = os.path.join(root_relpath, "metric_json") + metric_tsv_path = os.path.join(root_relpath, "metric_tsv") + metric_htsv_path = os.path.join(root_relpath, "metric_htsv") + metric_csv_path = os.path.join(root_relpath, "metric_csv") + metric_hcsv_path = os.path.join(root_relpath, "metric_hcsv") + for branch in ["bar", "baz", "foo"]: + self.assertEqual( + set(metrics[branch].keys()), + { + metric_path, + metric_json_path, + metric_tsv_path, + metric_htsv_path, + metric_csv_path, + metric_hcsv_path, + }, + ) + class TestMetricsRecursive(TestDvc): def setUp(self): diff --git a/tests/func/test_tree.py b/tests/func/test_tree.py index 6f6ec11532..9792bd8e66 100644 --- a/tests/func/test_tree.py +++ b/tests/func/test_tree.py @@ -97,38 +97,66 @@ def convert_to_sets(walk_results): class TestWalkInNoSCM(AssertWalkEqualMixin, TestDir): def test(self): - tree = WorkingTree() + tree = WorkingTree(self._root_dir) self.assertWalkEqual( - tree.walk("."), + tree.walk(self._root_dir), [ - (".", ["data_dir"], ["code.py", "bar", "тест", "foo"]), - (join("data_dir"), ["data_sub_dir"], ["data"]), - (join("data_dir", "data_sub_dir"), [], ["data_sub"]), + ( + self._root_dir, + ["data_dir"], + ["code.py", "bar", "тест", "foo"], + ), + (join(self._root_dir, "data_dir"), ["data_sub_dir"], ["data"]), + ( + join(self._root_dir, "data_dir", "data_sub_dir"), + [], + ["data_sub"], + ), ], ) def test_subdir(self): - tree = WorkingTree() + tree = WorkingTree(self._root_dir) self.assertWalkEqual( tree.walk(join("data_dir", "data_sub_dir")), - [(join("data_dir", "data_sub_dir"), [], ["data_sub"])], + [ + ( + join(self._root_dir, "data_dir", "data_sub_dir"), + [], + ["data_sub"], + ) + ], ) class TestWalkInGit(AssertWalkEqualMixin, TestGit): def test_nobranch(self): - tree = WorkingTree() + tree = WorkingTree(self._root_dir) self.assertWalkEqual( tree.walk("."), [ - (".", ["data_dir"], ["bar", "тест", "code.py", "foo"]), - ("data_dir", ["data_sub_dir"], ["data"]), - (join("data_dir", "data_sub_dir"), [], ["data_sub"]), + ( + self._root_dir, + ["data_dir"], + ["bar", "тест", "code.py", "foo"], + ), + (join(self._root_dir, "data_dir"), ["data_sub_dir"], ["data"]), + ( + join(self._root_dir, "data_dir", "data_sub_dir"), + [], + ["data_sub"], + ), ], ) self.assertWalkEqual( tree.walk(join("data_dir", "data_sub_dir")), - [(join("data_dir", "data_sub_dir"), [], ["data_sub"])], + [ + ( + join(self._root_dir, "data_dir", "data_sub_dir"), + [], + ["data_sub"], + ) + ], ) def test_branch(self): @@ -139,12 +167,22 @@ def test_branch(self): self.assertWalkEqual( tree.walk("."), [ - (".", ["data_dir"], ["code.py"]), - ("data_dir", ["data_sub_dir"], []), - (join("data_dir", "data_sub_dir"), [], ["data_sub"]), + (self._root_dir, ["data_dir"], ["code.py"]), + (join(self._root_dir, "data_dir"), ["data_sub_dir"], []), + ( + join(self._root_dir, "data_dir", "data_sub_dir"), + [], + ["data_sub"], + ), ], ) self.assertWalkEqual( tree.walk(join("data_dir", "data_sub_dir")), - [(join("data_dir", "data_sub_dir"), [], ["data_sub"])], + [ + ( + join(self._root_dir, "data_dir", "data_sub_dir"), + [], + ["data_sub"], + ) + ], )