Ejemplo n.º 1
0
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)
Ejemplo n.º 2
0
def test_read(tmpdir):
    source = tmpdir.join("test.py")
    source.write("testing")

    walker = astutils.ASTWalker([], [])

    assert walker._read(str(source)) == "testing"
Ejemplo n.º 3
0
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
Ejemplo n.º 4
0
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
Ejemplo n.º 5
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?
Ejemplo n.º 6
0
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))
Ejemplo n.º 7
0
def test_walk_files_none(tmpdir):
    walker = astutils.ASTWalker([], [])
    assert list(walker._walk_files(tmpdir.dirname)) == []
Ejemplo n.º 8
0
def test_is_not_python():
    walker = astutils.ASTWalker([], [])
    assert not walker._is_python("fake_python.yp")
Ejemplo n.º 9
0
def test_is_python():
    walker = astutils.ASTWalker([], [])
    assert walker._is_python("real_python.py")