Files
q3alique-codeflow/tests/unit/test_generic_extractor.py
q3alique acc1b4f3e7 Initial release of codeflow
Static taint-analysis and visualization tool for source-code security review.
Supports deep analysis for Python, JavaScript/TypeScript, Java, Go, and C#,
with structural support for all other languages via the generic extractor.

Outputs: interactive HTML report, LLM-ready Markdown review document,
and optional Burp Suite JSON export. Self-bootstrapping launcher (run.py)
requires no virtual environment.
2026-06-08 00:55:14 +02:00

127 lines
4.4 KiB
Python

"""Task 012 tests: Generic language extractor."""
import pytest
from pathlib import Path
from codeflow.discovery.file_scanner import FileInfo
from codeflow.analysis.generic_extractor import extract, GenericExtractor
from codeflow.models.node import NodeType
class TestGenericExtractor:
def test_java_function_detection(self, tmp_path):
f = tmp_path / "Main.java"
f.write_text("""
public class Main {
public static void main(String[] args) {
System.out.println("hello");
}
}
""")
fi = FileInfo(path=str(f), language="java", size_bytes=0, relative_path="Main.java")
result = extract(fi)
funcs = [n for n in result.nodes if n.node_type == NodeType.FUNCTION]
assert len(funcs) >= 1
mods = [n for n in result.nodes if n.node_type == NodeType.MODULE]
assert len(mods) == 1
def test_go_function_detection(self, tmp_path):
f = tmp_path / "main.go"
f.write_text("""
package main
func main() {}
func helper() int { return 1 }
""")
fi = FileInfo(path=str(f), language="go", size_bytes=0, relative_path="main.go")
result = extract(fi)
funcs = [n for n in result.nodes if n.node_type == NodeType.FUNCTION]
assert len(funcs) >= 2
def test_ruby_function_detection(self, tmp_path):
f = tmp_path / "app.rb"
f.write_text("""
def hello
puts "hello"
end
""")
fi = FileInfo(path=str(f), language="ruby", size_bytes=0, relative_path="app.rb")
result = extract(fi)
funcs = [n for n in result.nodes if n.node_type == NodeType.FUNCTION]
assert len(funcs) >= 1
def test_rust_function_detection(self, tmp_path):
f = tmp_path / "main.rs"
f.write_text("""
fn main() {
println!("hello");
}
""")
fi = FileInfo(path=str(f), language="rust", size_bytes=0, relative_path="main.rs")
result = extract(fi)
funcs = [n for n in result.nodes if n.node_type == NodeType.FUNCTION]
assert len(funcs) >= 1
def test_c_function_detection(self, tmp_path):
f = tmp_path / "main.c"
f.write_text("""
int main() { return 0; }
void helper() {}
""")
fi = FileInfo(path=str(f), language="c", size_bytes=0, relative_path="main.c")
result = extract(fi)
funcs = [n for n in result.nodes if n.node_type == NodeType.FUNCTION]
assert len(funcs) >= 1
def test_cpp_function_detection(self, tmp_path):
f = tmp_path / "main.cpp"
f.write_text("""
int main() { return 0; }
void helper() {}
""")
fi = FileInfo(path=str(f), language="cpp", size_bytes=0, relative_path="main.cpp")
result = extract(fi)
funcs = [n for n in result.nodes if n.node_type == NodeType.FUNCTION]
assert len(funcs) >= 1
def test_php_function_detection(self, tmp_path):
f = tmp_path / "index.php"
f.write_text("""<?php
function hello() { echo "hi"; }
""")
fi = FileInfo(path=str(f), language="php", size_bytes=0, relative_path="index.php")
result = extract(fi)
funcs = [n for n in result.nodes if n.node_type == NodeType.FUNCTION]
assert len(funcs) >= 1
def test_bash_function_detection(self, tmp_path):
f = tmp_path / "script.sh"
f.write_text("""
function hello() {
echo "hello"
}
""")
fi = FileInfo(path=str(f), language="bash", size_bytes=0, relative_path="script.sh")
result = extract(fi)
funcs = [n for n in result.nodes if n.node_type == NodeType.FUNCTION]
assert len(funcs) >= 1
def test_extraction_quality(self, tmp_path):
f = tmp_path / "test.java"
f.write_text("class A {}")
fi = FileInfo(path=str(f), language="java", size_bytes=0, relative_path="test.java")
result = extract(fi)
assert result.metadata.get("extraction_quality") == "partial"
def test_unsupported_language(self, tmp_path):
f = tmp_path / "file.xyz"
f.write_text("some content")
fi = FileInfo(path=str(f), language="unknown", size_bytes=0, relative_path="file.xyz")
result = extract(fi)
assert result.metadata.get("extraction_quality") == "failed"
def test_class_detection_java(self, tmp_path):
f = tmp_path / "App.java"
f.write_text("class App {}")
fi = FileInfo(path=str(f), language="java", size_bytes=0, relative_path="App.java")
result = extract(fi)
classes = [n for n in result.nodes if n.node_type == NodeType.CLASS]
assert len(classes) >= 1