From e3cfa7348cbefaec3aad918ee7d528bb34a53eaf Mon Sep 17 00:00:00 2001 From: karajan1001 Date: Thu, 13 Aug 2020 06:22:03 +0800 Subject: [PATCH] Result inconsistency of the dvcignore interfaces (#4378) Fix #4333, as `is_ignored_file` and `is_ignored_dir` and `__call__` share the same call `DvcIgnorePatterns.matches`, we only need to focus on the out of repo situation. 1. Add tests for the `is_ignored_file` and `is_ignored_dir` for the out of repo condition. 2. Now `is_ignored_file` and `is_ignored_dir` returns false for those files outside the repo. --- dvc/ignore.py | 3 ++- tests/func/test_ignore.py | 11 +++++++++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/dvc/ignore.py b/dvc/ignore.py index 25997f187..ea93db805 100644 --- a/dvc/ignore.py +++ b/dvc/ignore.py @@ -274,7 +274,7 @@ class DvcIgnoreFilter: def _is_ignored(self, path, is_dir=False): if self._outside_repo(path): - return True + return False dirname, basename = os.path.split(os.path.normpath(path)) ignore_pattern = self._get_trie_pattern(dirname) if ignore_pattern: @@ -296,6 +296,7 @@ class DvcIgnoreFilter: return self._is_ignored(path, True) def is_ignored_file(self, path): + path = os.path.abspath(path) return self._is_ignored(path, False) def _outside_repo(self, path): diff --git a/tests/func/test_ignore.py b/tests/func/test_ignore.py index 13885b43f..b68f0f4f7 100644 --- a/tests/func/test_ignore.py +++ b/tests/func/test_ignore.py @@ -159,10 +159,17 @@ def test_match_nested(tmp_dir, dvc): def test_ignore_external(tmp_dir, scm, dvc, tmp_path_factory): tmp_dir.gen(".dvcignore", "*.backup\ntmp") ext_dir = TmpDir(os.fspath(tmp_path_factory.mktemp("external_dir"))) - ext_dir.gen({"y.backup": "y", "tmp": "ext tmp"}) + ext_dir.gen({"y.backup": "y", "tmp": {"file": "ext tmp"}}) result = {relpath(f, ext_dir) for f in dvc.tree.walk_files(ext_dir)} - assert result == {"y.backup", "tmp"} + assert result == {"y.backup", os.path.join("tmp", "file")} + assert ( + dvc.tree.dvcignore.is_ignored_dir(os.fspath(ext_dir / "tmp")) is False + ) + assert ( + dvc.tree.dvcignore.is_ignored_file(os.fspath(ext_dir / "y.backup")) + is False + ) def test_ignore_subrepo(tmp_dir, scm, dvc):