def ast_test_code(): source_path = os.path.join(os.path.dirname(__file__), "test_code.txt") with open(source_path) as f: source = f.read() walker = astutils.ASTWalker([], []) return walker._walk_ast(ast.parse(source), top=True)
def test_read(tmpdir): source = tmpdir.join("test.py") source.write("testing") walker = astutils.ASTWalker([], []) assert walker._read(str(source)) == "testing"
def test_walk_files_one(tmpdir): tmpdir.join("test.py").write("") tmpdir.join("test.not-py").write("") walker = astutils.ASTWalker([], []) assert len(list(walker._walk_files(tmpdir.dirname))) == 1
def test_walk_syntax_error(tmpdir): tmpdir = tmpdir.mkdir("sub") tmpdir.join("test.py").write("import import") walker = astutils.ASTWalker([ tmpdir.dirname, ], []) result = list(walker.walk()) assert len(result) == 0
def test_walk(tmpdir): tmpdir = tmpdir.mkdir("sub") tmpdir.join("test.py").write("import os") tmpdir.join("test.not-py").write("") walker = astutils.ASTWalker([ tmpdir.dirname, ], []) result = list(walker.walk()) name, ast = result[0] assert len(result) == 1 assert name.endswith("sub/test.py") assert list(ast), [] # TODO: Wat. How does this pass?
def search(args): start = time.time() ignore_dirs = [pattern.compile(i_d) for i_d in args.ignore_dir] ast_walker = astutils.ASTWalker(args.paths, ignore_dirs) files_with_matches = set() activated_filters = filters.get_active_filters(args) if len(activated_filters) == 0: LOG.info("No filters were provided. Using all filters") activated_filters = filters.get_all_filters() patterns = pattern.matchers(args) node_count = 0 for file_count, (file_path, nodes) in enumerate(ast_walker.walk()): for file_node_count, node in enumerate(nodes): for f in activated_filters: if f.match(node, patterns): if file_path not in files_with_matches: files_with_matches.add(file_path) if args.files_with_matches: print(file_path) else: first = len(files_with_matches) == 1 print_file_path(file_path, first=first) if not args.files_with_matches: display_result(f, file_path, node) node_count += file_node_count if args.show_stats: seconds = round(time.time() - start, 3) print("**STATS**") print("Ran for {} seconds".format(seconds)) print("Parsed {} Python files ({}/s)".format( file_count, round(file_count / seconds, 2))) print("Visited {} AST nodes ({}/s)".format( node_count, round(node_count / seconds, 2))) for pat in patterns: print("Ran regular expression '{}' {} times.".format( pat.pat, pat.count))
def test_walk_files_none(tmpdir): walker = astutils.ASTWalker([], []) assert list(walker._walk_files(tmpdir.dirname)) == []
def test_is_not_python(): walker = astutils.ASTWalker([], []) assert not walker._is_python("fake_python.yp")
def test_is_python(): walker = astutils.ASTWalker([], []) assert walker._is_python("real_python.py")