def glob_match_using_glob_walk(self, pattern_to_test, path_to_test, include_dotfiles=False): chunks = split_path(path_to_test) # Simulate a "file system" with only one path, that is path_to_test # Note: for the purpose of simulating glob_match, we do not treat empty names as special. def iglob(pattern): tokens = split_path(pattern) n = len(tokens) self.assertTrue(n > 0) if n > len(chunks): return self.assertEqual(chunks[:n-1], tokens[:n-1]) token = tokens[n-1] chunk = chunks[n-1] if not include_dotfiles and (not token or token[0] != '.') and chunk and chunk[0] == '.': return if fnmatch.fnmatch(chunk, token): yield os.path.sep.join(chunks[:n]) def isresult(path): if path is None: return False return path_to_test == path visited = set() tokens = split_path(pattern_to_test) return next(glob_walk_internal(path_join, iglob, isresult, visited, tokens, None, None), None) is not None
def test_split_path(self): self.assertEqual( ['', 'foo', 'bar', 'baz.java'], split_path('/foo/bar/baz.java')) self.assertEqual( ['foo', 'bar', 'baz.java'], split_path('foo/bar/baz.java')) self.assertEqual(['', 'foo', 'bar', ''], split_path('/foo/bar/'))
def iglob(pattern): tokens = split_path(pattern) n = len(tokens) self.assertTrue(n > 0) if n > len(chunks): return self.assertEqual(chunks[:n-1], tokens[:n-1]) token = tokens[n-1] chunk = chunks[n-1] if not include_dotfiles and (not token or token[0] != '.') and chunk and chunk[0] == '.': return if fnmatch.fnmatch(chunk, token): yield os.path.sep.join(chunks[:n])
def test_split_path(self): self.assertEqual(['', 'foo', 'bar', 'baz.java'], split_path('/foo/bar/baz.java')) self.assertEqual(['foo', 'bar', 'baz.java'], split_path('foo/bar/baz.java')) self.assertEqual(['', 'foo', 'bar', ''], split_path('/foo/bar/'))