Skip to content

Commit

Permalink
Add unit tests for build from single C file
Browse files Browse the repository at this point in the history
  • Loading branch information
PLukas2018 committed Sep 21, 2023
1 parent 41a52b8 commit 79c024b
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions tests/unit_tests/build_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
"""Unit tests for 'build' sub-command - specifically for making snapshots
from single C file."""
from diffkemp.llvm_ir.single_c_builder import SingleCBuilder
from diffkemp.diffkemp import run_from_cli
from diffkemp.utils import get_functions_from_llvm
import filecmp
import os
import yaml

SINGLE_C_FILE_DIR = "kernel/linux-4.18.0-240.el8/tools/build/tests/ex/"
SINGLE_C_FILE_FILENAME = "b.c"


def test_single_c_builder():
builder = SingleCBuilder(SINGLE_C_FILE_DIR, SINGLE_C_FILE_FILENAME)
assert builder.str() == "single_c_file"
assert builder.get_function_list() == ["b"]
exp_llvm_file = os.path.abspath(os.path.join(SINGLE_C_FILE_DIR, "b.ll"))
assert builder.find_llvm_with_symbol_def("b") == exp_llvm_file
assert builder.find_llvm_with_symbol_use("b") == exp_llvm_file
builder.finalize()


def test_build_single_c_file(monkeypatch, tmp_path):
"""Testing 'build' sub-command - making snapshot from single c file"""
source_dir = os.path.join(SINGLE_C_FILE_DIR, SINGLE_C_FILE_FILENAME)
output_dir = str(tmp_path)
args = ["diffkemp", "build", source_dir, output_dir]
monkeypatch.setattr("sys.argv", args)
run_from_cli()

files = os.listdir(output_dir)

assert "b.c" in files
assert filecmp.cmp(
os.path.join(SINGLE_C_FILE_DIR, SINGLE_C_FILE_FILENAME),
os.path.join(output_dir, "b.c"),
), "c file in snapshot should be same as the original one"

assert "b.ll" in files
llvm_file_path = os.path.join(output_dir, "b.ll")
assert "b" in get_functions_from_llvm([llvm_file_path]), \
"function from source file should be also in llvm file"

assert "snapshot.yaml" in files
snapshot_yaml_path = os.path.join(output_dir, "snapshot.yaml")
with open(snapshot_yaml_path, "r") as file:
snapshot_yaml = yaml.safe_load(file)
function_list = [function["name"] for function in snapshot_yaml[0]["list"]]
assert "b" in function_list, "function name should be in snapshot.yaml"
assert snapshot_yaml[0]["llvm_source_finder"]["kind"] == "single_c_file"
assert snapshot_yaml[0]["source_dir"] == os.path.abspath(SINGLE_C_FILE_DIR)

0 comments on commit 79c024b

Please sign in to comment.