[llvm] [Code Coverage] Add a tool to check test coverage of a patch (PR #71841)
Shivam Gupta via llvm-commits
llvm-commits at lists.llvm.org
Sun Nov 26 07:15:06 PST 2023
xgupta wrote:
I realize that it is not working for unit tests.
So wrote a separate function to run them but their profraw file is not generating at the right place/path, and has a name of default.profraw which overrides on every run of other unit tests.
And unlike unit tests of clang, llvm unit tests are not showing up with `build/bin/llvm-lit --show-tests llvm/test` which is another thing to investigate.
But I think it should be review and commit this version and then proceed for unit tests coverage in the next step.
```
def run_modified_unit_tests(build_dir, patch_path, tests):
"""Read the patch file, identify modified and added unit test cases, and
then execute each of these test cases."""
try:
# Get the list of modified and added test cases from the patch
with open(patch_path, "r") as patch_file:
patch_diff = patch_file.read()
modified_tests = []
custom_print()
# Use regular expressions to find modified test cases with ".ll|.c|.cpp" extension
for match in re.finditer(
r"^\+\+\+ [ab]/(.*\.(ll|mir|mlir|fir|c|cpp|f90|s|test))$",
patch_diff,
re.MULTILINE,
):
test_file = match.group(1)
# Get the current working directory
cwd = os.getcwd()
# Build the full file path dynamically by going two steps back from cwd
full_test_file = os.path.join(
os.path.dirname(cwd), "llvm-project", test_file
)
# Extract the first three directories from the test_file path
first_three_dirs = os.path.join(*test_file.split(os.path.sep)[:3])
matching_test_paths = [
test_path for test_path in tests if first_three_dirs in test_path
]
if matching_test_paths:
custom_print()
custom_print("Unit test file in the patch:", test_file)
custom_print("Full unit test file path:", full_test_file)
custom_print("Matching unit test in tests:", matching_test_paths[0])
# Capture the first matching test path
modified_test_path = os.path.dirname(
os.path.dirname(matching_test_paths[0])
)
# Extract the file name (excluding the extension) from test_file
file_name = os.path.splitext(os.path.basename(test_file))[0]
# Extract the last directory name (excluding the extension) from test_file
last_directory = os.path.splitext(
os.path.basename(os.path.dirname(test_file))
)[0]
# Add "Tests" to the last_directory
last_directory_with_tests = f"{last_directory}Tests"
cwd = os.getcwd()
os.chdir(build_dir)
subprocess.check_call(["ninja", last_directory_with_tests])
os.chdir(cwd)
# Check if the file name is starts with +++
if match.group(0).startswith("+++"):
modified_tests.append(full_test_file)
# Run each modified test case
custom_print("")
custom_print("Running modified test cases:")
subprocess.check_call(
[modified_test_path, f"--gtest_filter={file_name}*"]
)
custom_print("Test case executed:", full_test_file)
if not modified_tests:
custom_print("No modified unit tests found in the patch.")
return
except subprocess.CalledProcessError as e:
custom_print("Error while running modified tests:", e)
sys.exit(1)
except Exception as ex:
custom_print("Error:", ex)
sys.exit(1)
```
https://github.com/llvm/llvm-project/pull/71841
More information about the llvm-commits
mailing list