Add test clang-format automatic test

This commit is contained in:
Bartosz Podrygajlo
2025-08-18 21:51:33 +02:00
parent 9e37a2cf94
commit b2f6be0424
3 changed files with 119 additions and 0 deletions

View File

@@ -0,0 +1,98 @@
#!/bin/bash
#/*
# * Licensed to the OpenAirInterface (OAI) Software Alliance under one or more
# * contributor license agreements. See the NOTICE file distributed with
# * this work for additional information regarding copyright ownership.
# * The OpenAirInterface Software Alliance licenses this file to You under
# * the OAI Public License, Version 1.1 (the "License"); you may not use this file
# * except in compliance with the License.
# * You may obtain a copy of the License at
# *
# * http://www.openairinterface.org/?page_id=698
# *
# * Unless required by applicable law or agreed to in writing, software
# * distributed under the License is distributed on an "AS IS" BASIS,
# * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# * See the License for the specific language governing permissions and
# * limitations under the License.
# *-------------------------------------------------------------------------------
# * For more information about the OpenAirInterface (OAI) Software Alliance:
# * contact@openairinterface.org
# */
# ==============================================================================
# SCRIPT CONFIGURATION
# ==============================================================================
# Set the maximum number of files allowed to have formatting violations.
# The script will exit with a non-zero status if this number is exceeded.
MAX_FORMAT_VIOLATIONS=869
# Define the file types to check.
# The script will search for these file extensions recursively from the current directory.
# You can add or remove file extensions as needed.
FILE_EXTENSIONS=("*.cpp" "*.cxx" "*.cc" "*.hpp" "*.h" "*.hxx")
# ==============================================================================
# SCRIPT LOGIC
# ==============================================================================
# Initialize a counter for files with formatting violations.
files_with_violations=0
# A temporary variable to track the exit status of commands.
exit_status=0
echo "Starting clang-format check..."
echo "Maximum allowed violations: $MAX_FORMAT_VIOLATIONS"
echo "----------------------------------------"
# Loop through all specified file types and check them.
for ext in "${FILE_EXTENSIONS[@]}"; do
# Use 'find' to get a list of all files with the current extension.
# The 'eval' command is used to correctly handle the globbing of file extensions.
# List of directories to exclude from the search
EXCLUDE_DIRS=("cmake_targets/build" "cmake_targets/ran_build" "openair2/E2AP/flexric") # Replace with actual directory names
# Build the prune expression for excluded directories
prune_expr=""
for exclude in "${EXCLUDE_DIRS[@]}"; do
prune_expr="$prune_expr -path \"./$exclude\" -prune -o"
done
# Construct the find command with exclusions
find_command="find . $prune_expr -type f -name \"$ext\" -print"
# Execute the find command and read each file path.
while IFS= read -r file; do
# Use clang-format to format the file and pipe the output to 'diff'.
# The 'diff -q' command compares the file with the formatted output.
# It exits with status 1 if a difference is found (i.e., a violation).
# We redirect stdout and stderr to /dev/null to keep the output clean.
clang-format -style=file "$file" | diff -q "$file" - >/dev/null 2>&1
exit_status=$?
# Check if 'diff' found a difference.
if [ $exit_status -eq 1 ]; then
echo " [VIOLATION] $file"
# Increment the violation counter if a difference is found.
files_with_violations=$((files_with_violations + 1))
fi
done < <(eval "$find_command")
done
echo "----------------------------------------"
echo "Check complete."
echo "Total files with formatting violations: $files_with_violations"
echo "Maximum allowed violations: $MAX_FORMAT_VIOLATIONS"
# Check if the number of violations exceeds the allowed maximum.
if [ "$files_with_violations" -gt "$MAX_FORMAT_VIOLATIONS" ]; then
echo "----------------------------------------"
echo "FAILURE: The number of formatting violations ($files_with_violations) " \
"exceeds the maximum allowed ($MAX_FORMAT_VIOLATIONS)."
exit 1
else
echo "----------------------------------------"
echo "SUCCESS: The number of formatting violations satisfy requirement"
exit 0
fi

View File

@@ -203,6 +203,15 @@ class StaticCodeAnalysis():
cmd.run('docker image rm oai-formatting-check:latest')
cmd.run(f'docker build --target oai-formatting-check --tag oai-formatting-check:latest {check_options} --file {lSourcePath}/ci-scripts/docker/Dockerfile.formatting.ubuntu {lSourcePath} > {logDir}/oai-formatting-check.txt 2>&1')
finalStatus = 0
res = cmd.run(f'docker compose -f {lSourcePath}/ci-scripts/docker/clang-format.yaml up > {logDir}/oai-clang-format-check.txt')
if res.returncode == 0:
HTML.CreateHtmlTestRowQueue('clang-format', 'OK', ['Files satisfy clang-format requirements.'])
else:
finalStatus = -1
HTML.CreateHtmlTestRowQueue('clang-format', 'KO', ['Too many clang-format violations. If you have added files, make sure they are formatted with clang-format.'])
archiveArtifact(cmd, ctx, f'{logDir}/oai-clang-format-check.txt')
cmd.run('docker image rm oai-formatting-check:latest')
cmd.run('docker image prune --force')
cmd.run('docker volume prune --force')

View File

@@ -0,0 +1,12 @@
services:
clang-format:
image: clang-format
build:
context: ../../
dockerfile_inline: |
FROM ubuntu:24.04
ENV DEBIAN_FRONTEND=noninteractive
RUN apt-get update && apt-get install -y clang-format
volumes:
- ../../:/oai-src/
command: bash -c "cd /oai-src/ && ./ci-scripts/checkClangFormatErrors.sh"