def test_compilation_bound_start(self): p = Pattern.create("/*.py") assert p.bound_start is True assert p.bound_end is True assert str(p.file_pattern) == "*.py" assert p.sections == [] p = Pattern.create("/test/*.py") assert p.bound_start is True assert p.bound_end is True assert str(p.file_pattern) == "*.py" assert p.sections == [Section(["test"])] p = Pattern.create("/test/dir/*") assert p.bound_start is True assert p.bound_end is True assert p.file_pattern == "*" assert p.sections == [Section(["test", "dir"])] p = Pattern.create("/start/**/test/*.py") assert p.bound_start is True assert p.bound_end is True assert str(p.file_pattern) == "*.py" assert p.sections == [Section(["start"]), Section(["test"])] p = Pattern.create("/start/**/test/**/*.py") assert p.bound_start is True assert p.bound_end is False assert str(p.file_pattern) == "*.py" assert p.sections == [Section(["start"]), Section(["test"])]
def test_basic(self): py = Pattern.create("*.py") cvs = Pattern.create("**/CVS/**/*") pycache = Pattern.create("__pycache__/**/*") ps = PatternSet() assert ps.all_files() is False assert [pat for pat in ps.iter()] == [] s = ["a.py", "b.py"] match(ps, s, []) ps.append(py) assert ps.all_files() is False assert [pat for pat in ps.iter()] == [py] s = ["a.py", "b.py"] match(ps, s, s) s = ["a.py", "b.py", "anything.goes"] match(ps, s, ["a.py", "b.py"]) ps.append(cvs) assert ps.all_files() is True assert [pat for pat in ps.iter()] == [py, cvs] match(ps, s, s) ps.remove(cvs) ps.append(pycache) assert ps.all_files() is True assert [pat for pat in ps.iter()] == [py, pycache] match(ps, s, s) ps.remove(pycache) assert ps.all_files() is False assert [pat for pat in ps.iter()] == [py] match(ps, s, ["a.py", "b.py"])
def test_file_pattern(self): p = Pattern.create("*.py") match(p, [], []) match(p, ["a.no", "py"], []) match(p, ["x.px", "a.py", "a.pz", "b.py", "py"], ["a.py", "b.py"]) p = Pattern.create("?bc.txt") match(p, ["a.no", "py"], []) match(p, ["abc.txt", "bbc.txt", "not.txt"], ["abc.txt", "bbc.txt"])
def test_complex_compilation(self): p1 = Pattern.create("dir/file.txt") p2 = Pattern.create("**/dir/file.txt") p3 = Pattern.create("/**/dir/file.txt") assert p1.sections == p2.sections assert p2.sections == p3.sections assert p1.bound_start is False assert p1.bound_start == p2.bound_start == p3.bound_start assert p1.bound_end is True assert p1.bound_end == p2.bound_end == p3.bound_end
def test_patterns_test_no_match(self): bound_start_top_all = Pattern.create("/test/*") bound_start_top_py = Pattern.create("/test/*.py") bound_start_sub_all = Pattern.create("/test/**/*") bound_start_sub_py = Pattern.create("/test/**/*.py") bound_end_all = Pattern.create("**/test/*") bound_end_py = Pattern.create("**/test/*.py") bound_start_end = Pattern.create("/test/**/test/*.py") unbound_all = Pattern.create("**/*") unbound_py = Pattern.create("**/*.py") all = [ bound_start_top_all, bound_start_top_py, bound_start_sub_all, bound_start_sub_py, bound_end_all, bound_end_py, bound_start_end, unbound_all, unbound_py ] # Test matches for the root directory fst = FileSetState("Label", "nottest", None, all) file_set_state_location(fst, bound_start_top_all, NOT_PRESENT) file_set_state_location(fst, bound_start_top_py, NOT_PRESENT) file_set_state_location(fst, bound_start_sub_all, NOT_PRESENT) file_set_state_location(fst, bound_start_sub_py, NOT_PRESENT) file_set_state_location(fst, bound_end_all, UNMATCHED) file_set_state_location(fst, bound_end_py, UNMATCHED) file_set_state_location(fst, bound_start_end, NOT_PRESENT) file_set_state_location(fst, unbound_all, MATCHED_INHERIT) file_set_state_location(fst, unbound_py, MATCHED_INHERIT) assert fst.no_possible_matches_in_subdirs() is False assert fst.matches_all_files_all_subdirs() is True
def test_patterns_inherit_with_file(self): pattern1 = Pattern.create("/a/**/*.a") pattern2 = Pattern.create("**/b/**/*.b") pattern3 = Pattern.create("/a/b/c/*.c") all_files = ["not", "a.a", "b.b", "c.c"] a_files = ["a.a", "aa.a"] # Test matches for the root directory root = FileSetState("Label", "", None, [pattern1, pattern2, pattern3]) file_set_state_location(root, pattern1, UNMATCHED) file_set_state_location(root, pattern2, UNMATCHED) file_set_state_location(root, pattern3, UNMATCHED) assert not root.match([]) assert not root.match(all_files) assert not root.match(a_files) a = FileSetState("Label", "a", root) file_set_state_location(a, pattern1, MATCHED_INHERIT) file_set_state_location(a, pattern2, UNMATCHED) file_set_state_location(a, pattern3, UNMATCHED) assert not a.match([]) assert {"a.a"} == a.match(all_files) assert {"a.a", "aa.a"} == a.match(a_files) b = FileSetState("Label", os.path.join("a", "b"), a) file_set_state_location(b, pattern1, NOT_PRESENT) # In parent file_set_state_location(b, pattern2, MATCHED_INHERIT) file_set_state_location(b, pattern3, UNMATCHED) assert not b.match([]) assert {"a.a", "b.b"} == b.match(all_files) assert {"a.a", "aa.a"} == b.match(a_files) c = FileSetState("Label", os.path.join("a", "b", "c"), b) file_set_state_location(c, pattern1, NOT_PRESENT) # In grandparent file_set_state_location(c, pattern2, NOT_PRESENT) # In parent file_set_state_location(c, pattern3, MATCHED_NO_SUBDIR) assert not c.match([]) assert {"a.a", "b.b", "c.c"} == c.match(all_files) assert {"a.a", "aa.a"} == c.match(a_files) d = FileSetState("Label", os.path.join("a", "b", "c", "d"), b) file_set_state_location(d, pattern1, NOT_PRESENT) # In great-grandparent file_set_state_location(d, pattern2, NOT_PRESENT) # In grandparent file_set_state_location(d, pattern3, NOT_PRESENT) # Not applicable assert not d.match([]) assert {"a.a", "b.b"} == d.match(all_files) assert {"a.a", "aa.a"} == b.match(a_files)
def test_match_single_bound_start_no_sub(self): p = Pattern.create("/test/*.py") assert p.match_directory([]) == MatchType.NO_MATCH assert p.match_directory( "test".split("/")) == MatchType.MATCH_BUT_NO_SUBDIRECTORIES assert p.match_directory( "some/where/".split("/")) == MatchType.NO_MATCH_NO_SUBDIRECTORIES
def test_match_twin_directories(self): p_dir, p_file = create_starstar("/test/**/test/") assert p_dir.match_directory("test/test/test/test".split( "/")) == MatchType.MATCH_ALL_SUBDIRECTORIES assert p_dir.match_directory( "test/where/test".split("/")) == MatchType.MATCH_ALL_SUBDIRECTORIES assert p_dir.match_directory("test/a/very/long/way/apart/test".split( "/")) == MatchType.MATCH_ALL_SUBDIRECTORIES assert p_dir.match_directory([]) == MatchType.NO_MATCH assert p_dir.match_directory("test".split("/")) == MatchType.NO_MATCH assert p_dir.match_directory( "not/a/hope".split("/")) == MatchType.NO_MATCH_NO_SUBDIRECTORIES assert p_dir.match_directory("a/test/where/test/b".split( "/")) == MatchType.NO_MATCH_NO_SUBDIRECTORIES assert p_dir.match_directory("some/some/some".split( "/")) == MatchType.NO_MATCH_NO_SUBDIRECTORIES p = Pattern.create("/test/**/test/*.py") assert p.match_directory( "test/test/test/test".split("/")) == MatchType.MATCH assert p.match_directory( "test/where/test".split("/")) == MatchType.MATCH assert p.match_directory( "test/a/very/long/way/apart/test".split("/")) == MatchType.MATCH assert p.match_directory([]) == MatchType.NO_MATCH assert p.match_directory("test".split("/")) == MatchType.NO_MATCH assert p.match_directory( "not/a/hope".split("/")) == MatchType.NO_MATCH_NO_SUBDIRECTORIES assert p.match_directory("a/test/where/test/b".split( "/")) == MatchType.NO_MATCH_NO_SUBDIRECTORIES assert p.match_directory("some/some/some".split( "/")) == MatchType.NO_MATCH_NO_SUBDIRECTORIES
def test_no_file_pattern(self): p = Pattern.create("") assert p.file_pattern == "*" match(p, [], []) s = ["a.py", "b.py"] match(p, s, s) s = ["x.px", "a.py", "a.pz", "b.py", "py"] match(p, s, s) p = Pattern.create("*") assert p.file_pattern == "*" match(p, [], []) s = ["a.py", "b.py"] match(p, s, s) s = ["x.px", "a.py", "a.pz", "b.py", "py"] match(p, s, s)
def create_starstar(glob): ps = Pattern.create(glob) for pattern in ps.patterns: if pattern.file_pattern == "*": pattern_dir = pattern else: pattern_file = pattern return (pattern_dir, pattern_file)
def test_match_pure_file_pattern(self): # No sections - all must match p = Pattern.create("test.py") assert p.match_directory([]) == MatchType.MATCH_ALL_SUBDIRECTORIES assert p.match_directory( "test".split("/")) == MatchType.MATCH_ALL_SUBDIRECTORIES assert p.match_directory( "some/where/".split("/")) == MatchType.MATCH_ALL_SUBDIRECTORIES
def test_extend(self): py = Pattern.create("*.py") cvs = Pattern.create("**/CVS/*") pycache = Pattern.create("__pycache__/**/*") ps1 = PatternSet() ps1.extend([py, cvs, pycache]) assert [pat for pat in ps1.iter()] == [py, cvs, pycache] ps2 = PatternSet() ps2.extend(ps1) assert [pat for pat in ps2.iter()] == [py, cvs, pycache] ps1 = PatternSet() ps1.extend([py]) assert [pat for pat in ps1.iter()] == [py] ps1.extend([cvs, pycache]) assert [pat for pat in ps1.iter()] == [py, cvs, pycache]
def test_match_single_bound_end_directory(self): p = Pattern.create("test/*") assert p.match_directory([]) == MatchType.NO_MATCH assert p.match_directory("test".split("/")) == MatchType.MATCH assert p.match_directory( "some/where/test".split("/")) == MatchType.MATCH assert p.match_directory( "middle/test/middle".split("/")) == MatchType.NO_MATCH assert p.match_directory("not/a/hope".split("/")) == MatchType.NO_MATCH
def test_compilation_and_str_starstar(self): for glob in [ "test/", "/test/", "/test/**/", "/test/**", "/1/**/test/" ]: patternset = Pattern.create(glob) assert isinstance(patternset, PatternSet) assert len(patternset.patterns) == 2 for pattern in patternset.patterns: assert pattern.file_pattern == "test" or pattern.sections[ -1].elements[-1].pattern == "test"
def test_patterns_inherit_all_files(self): pattern1 = Pattern.create("/a/**/*") all_files = ["not", "a.a", "b.b", "c.c"] # Test matches for the root directory root = FileSetState("Label", "", None, [pattern1]) file_set_state_location(root, pattern1, UNMATCHED) assert not root.match([]) assert not root.match(all_files) a = FileSetState("Label", "a", root) file_set_state_location(a, pattern1, MATCHED_INHERIT) assert not a.match([]) assert set(all_files) == a.match(all_files) b = FileSetState("Label", os.path.join("a", "b"), a) file_set_state_location(b, pattern1, NOT_PRESENT) # In parent file_set_state_location(a, pattern1, MATCHED_INHERIT) assert not b.match([]) assert b.parent == a assert set(all_files) == b.match(all_files)
def test_compilation_and_str(self): # Syntax for the dictionary: # The key is the normative string, and the value is a list of patterns that generate the normative patterns = { "/*.py": ["/*.py"], "/test/*.py": ["/test/*.py"], "/test/dir/**/*": ["/test/dir/**/*"], "/start/**/test/*.py": ["/start/**/test/*.py"], "/start/**/test/**/*.py": ["/start/**/test/**/*.py"], "**/test/*.py": ["test/*.py", "**/test/*.py"], "**/test/*": ["test/*", "**/test/*"], "**/test/**/*": ["test/**/*", "**/test/**/*"], "**/test/**/*.py": ["test/**/*.py", "**/test/**/*.py"], "**/start/**/test/**/*.py": ["start/**/test/**/*.py", "**/start/**/test/**/*.py"], } for normative, options in patterns.items(): for option in options: print("Testing that Pattern.create('{0}') == '{1}'".format( option, normative)) assert normative == str(Pattern.create(option))
def test_illegal_glob(self): with pytest.raises(FormicError): Pattern.create("/test/../**")