def test_pattern_trie_tree(tmp_dir, dvc): tmp_dir.gen( { "top": { "first": { DvcIgnore.DVCIGNORE_FILE: "a\nb\nc", "middle": { "second": { DvcIgnore.DVCIGNORE_FILE: "d\ne\nf", "bottom": {}, } }, }, }, "other": {DvcIgnore.DVCIGNORE_FILE: "1\n2\n3"}, } ) ignore_pattern_trie = None for ignore in dvc.tree.dvcignore.ignores: if isinstance(ignore, DvcIgnorePatternsTrie): ignore_pattern_trie = ignore break assert ignore_pattern_trie is not None ignore_pattern_top = ignore_pattern_trie[os.fspath(tmp_dir / "top")] ignore_pattern_other = ignore_pattern_trie[os.fspath(tmp_dir / "other")] ignore_pattern_first = ignore_pattern_trie[ os.fspath(tmp_dir / "top" / "first") ] ignore_pattern_middle = ignore_pattern_trie[ os.fspath(tmp_dir / "top" / "first" / "middle") ] ignore_pattern_second = ignore_pattern_trie[ os.fspath(tmp_dir / "top" / "first" / "middle" / "second") ] ignore_pattern_bottom = ignore_pattern_trie[ os.fspath(tmp_dir / "top" / "first" / "middle" / "second" / "bottom") ] assert not ignore_pattern_top assert ( DvcIgnorePatterns([], os.fspath(tmp_dir / "top")) == ignore_pattern_top ) assert ( DvcIgnorePatterns(["1", "2", "3"], os.fspath(tmp_dir / "other")) == ignore_pattern_other ) assert ( DvcIgnorePatterns( ["a", "b", "c"], os.fspath(tmp_dir / "top" / "first") ) == ignore_pattern_first ) assert ( DvcIgnorePatterns( ["a", "b", "c"], os.fspath(tmp_dir / "top" / "first") ) == ignore_pattern_middle ) assert ( DvcIgnorePatterns( [ "a", "b", "c", "/middle/second/**/d", "/middle/second/**/e", "/middle/second/**/f", ], os.fspath(tmp_dir / "top" / "first"), ) == ignore_pattern_second ) assert ( DvcIgnorePatterns( [ "a", "b", "c", "/middle/second/**/d", "/middle/second/**/e", "/middle/second/**/f", ], os.fspath(tmp_dir / "top" / "first"), ) == ignore_pattern_bottom )
def mock_dvcignore(dvcignore_path, patterns): tree = MagicMock() with patch.object(tree, "open", mock_open(read_data="\n".join(patterns))): ignore_patterns = DvcIgnorePatterns(dvcignore_path, tree) return ignore_patterns
def test_pattern_trie_fs(tmp_dir, dvc): tmp_dir.gen({ "top": { "first": { DvcIgnore.DVCIGNORE_FILE: "a\nb\nc", "middle": { "second": { DvcIgnore.DVCIGNORE_FILE: "d\ne\nf", "bottom": {}, } }, } }, "other": { DvcIgnore.DVCIGNORE_FILE: "1\n2\n3" }, }) dvc._reset() dvcignore = dvc.dvcignore ignore_pattern_top = dvcignore._get_trie_pattern(os.fspath(tmp_dir / "top")) ignore_pattern_other = dvcignore._get_trie_pattern( os.fspath(tmp_dir / "other")) ignore_pattern_first = dvcignore._get_trie_pattern( os.fspath(tmp_dir / "top" / "first")) ignore_pattern_middle = dvcignore._get_trie_pattern( os.fspath(tmp_dir / "top" / "first" / "middle")) ignore_pattern_second = dvcignore._get_trie_pattern( os.fspath(tmp_dir / "top" / "first" / "middle" / "second")) ignore_pattern_bottom = dvcignore._get_trie_pattern( os.fspath(tmp_dir / "top" / "first" / "middle" / "second" / "bottom")) base_pattern = ( _to_pattern_info_list([".hg/", ".git/", ".git", ".dvc/"]), os.fspath(tmp_dir), ) first_pattern = merge_patterns( os.path, *base_pattern, _to_pattern_info_list(["a", "b", "c"]), os.fspath(tmp_dir / "top" / "first"), ) second_pattern = merge_patterns( os.path, *first_pattern, _to_pattern_info_list(["d", "e", "f"]), os.fspath(tmp_dir / "top" / "first" / "middle" / "second"), ) other_pattern = merge_patterns( os.path, *base_pattern, _to_pattern_info_list(["1", "2", "3"]), os.fspath(tmp_dir / "other"), ) assert DvcIgnorePatterns(*base_pattern, os.sep) == ignore_pattern_top assert DvcIgnorePatterns(*other_pattern, os.sep) == ignore_pattern_other assert (DvcIgnorePatterns(*first_pattern, os.sep) == ignore_pattern_first == ignore_pattern_middle) assert (DvcIgnorePatterns(*second_pattern, os.sep) == ignore_pattern_second == ignore_pattern_bottom)
def mock_dvcignore(dvcignore_path, patterns): fs = MagicMock() with patch.object(fs, "open", mock_open(read_data="\n".join(patterns))): ignore_patterns = DvcIgnorePatterns.from_files(dvcignore_path, fs) return ignore_patterns