Skip to content

Commit

Permalink
fix(check-formatting.sh): use relative directory (#835)
Browse files Browse the repository at this point in the history
The `bin/check-formatting.sh` script changed its directory to `bin` as
the first step, consequently expecting paths of files to check to be
relative to `bin`, when typically they would be given relative to the
working directory. When not finding a file, the script would just print
a warning, but still exit with error code 0, which is probably the
reason why this went unnoticed for quite a while.

This commit fixes the relative directory issue. Further, when a given
path cannot be checked, we return a non-zero code now. Lastly, the
script would count the number of files with formatting issues and return
that number as error code. This has the issue that if the number of
violating files is a multiple of 256, error code would falsly be returned.
  • Loading branch information
ahans authored Apr 9, 2024
1 parent 5041ca5 commit fc46fcd
Showing 1 changed file with 9 additions and 26 deletions.
35 changes: 9 additions & 26 deletions bin/check-formatting.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env bash
set -e

print_help() {
echo "This script verifies if one or more files are formatted using the provided"
Expand All @@ -13,8 +14,6 @@ print_help() {
echo " warnings are returned for each invalid path."
}

cd "$(dirname "$0")"

# Get files list
FILES=()
while [[ $# -gt 0 ]]
Expand All @@ -31,40 +30,24 @@ do
shift
done

# Verifies a single file is formatted following .clang-format style
run_check() {
local FILE="$1"
CHECK_RESULT=0
# Get diff between current content and formatted content
local OUTPUT=$(diff -u <(cat "$FILE") <(clang-format --style=file "$FILE"))
# Shows diff output only if files are different
if [[ $OUTPUT != "" ]]; then
echo "$OUTPUT"
CHECK_RESULT=1
fi
}

# Result of this script, incremented by one for each unformatted file
# Result of this script
RESULT=0
# List of warnings
WARNINGS=()

# Iterate files to run format check
for INDEX in "${!FILES[@]}"; do
FILE="${FILES[$INDEX]}"
if [[ -f "$FILE" ]]; then
run_check $FILE
RESULT=$(( RESULT + CHECK_RESULT ))
if ! clang-format --dry-run --Werror "$FILE"; then
RESULT=1
fi
elif [[ -d "$FILE" ]]; then
WARNINGS+=("Path is a directory: $FILE")
echo "Path is a directory: $FILE"
RESULT=1
else
WARNINGS+=("File not found: $FILE")
echo "File not found: $FILE"
RESULT=1
fi
done

for INDEX in "${!WARNINGS[@]}"; do
echo "${WARNINGS[$INDEX]}"
done

echo "Done"
exit $RESULT

0 comments on commit fc46fcd

Please sign in to comment.