From 1f25558c2143de8fa4afcc3ed16ac377c13191b3 Mon Sep 17 00:00:00 2001 From: Keith Bennett Date: Thu, 6 Jan 2022 16:02:02 -0600 Subject: [PATCH] make shellcheck clean `$ bash ./shellcheck.sh` ``` Checking: /home/kbennett/src/public/valijson/tests/fuzzing/oss-fuzz-build.sh Checking: /home/kbennett/src/public/valijson/shellcheck.sh All scripts found (listed above) passed shellcheck ``` --- shellcheck.sh | 62 +++++++++++++++++++++++++++++++++ tests/fuzzing/oss-fuzz-build.sh | 15 ++++---- 2 files changed, 71 insertions(+), 6 deletions(-) create mode 100644 shellcheck.sh diff --git a/shellcheck.sh b/shellcheck.sh new file mode 100644 index 0000000..4957df1 --- /dev/null +++ b/shellcheck.sh @@ -0,0 +1,62 @@ +#!/usr/bin/env bash + +# +# Shellcheck is a static analyzer for shell scripts: https://shellcheck.net/ +# It is available in several operating systems and also as a docker image. +# +# If it finds any issues, it will output a small blurb describing the affected +# line(s) and will have a generic issue ID. The issue ID can be opened on its +# website to learn more about what the underlying problem is, why it's a +# problem, and (usually) suggests a way to fix. +# Specific shellcheck issues can be disabled (aka silenced). Doing so is +# usually pretty loud during code review. +# https://github.com/koalaman/shellcheck/wiki/Directive + +# https://stackoverflow.com/a/2871034/1111557 +set -euo pipefail + +HERE="$(cd "$(dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" +SHELLCHECK="${SHELLCHECK:-"/usr/bin/shellcheck"}" +SEARCH_DIR="${SEARCH_DIR:-"$HERE"}" +cd "${SEARCH_DIR}" #so that we can call git + +# +# This block will: +# 1) `find` files under `SEARCH_DIR` +# 2) skip anything under `/thirdparty/`, `/.git/` +# 3) in a loop reading each path: +# 3a) ignore files that git also ignores +# 3b) use `file` to filter only script files +# 3c) run shellcheck against that script +# 4) if any paths are found to have an error, their paths are collated. +FAILED_PATHS=() +while read -r file_path +do + if git rev-parse --git-dir > /dev/null 2>&1; + then + git check-ignore --quiet "${file_path}" && continue + fi + file "${file_path}" | grep -q 'shell script' || continue + SCRIPT_PATH="${file_path}" + echo "Checking: ${SCRIPT_PATH}" + "${SHELLCHECK}" \ + "${SCRIPT_PATH}" \ + || FAILED_PATHS+=( "${SCRIPT_PATH}" ) +done < <( + find "${SEARCH_DIR}" -type f \ + | grep -v '/\.git/\|/thirdparty/' +) + +# +# If there are any failed paths, summarize them here. +# Then report a failing status to our caller. +if [[ 0 -lt "${#FAILED_PATHS[@]}" ]]; then + >&2 echo "These scripts aren't shellcheck-clean:" + for path in "${FAILED_PATHS[@]}"; do + >&2 echo "${path}" + done + exit 1 +fi + +# If we get here, then none of the scripts had any warnings. +echo "All scripts found (listed above) passed shellcheck" diff --git a/tests/fuzzing/oss-fuzz-build.sh b/tests/fuzzing/oss-fuzz-build.sh index 346dc72..dba41f4 100755 --- a/tests/fuzzing/oss-fuzz-build.sh +++ b/tests/fuzzing/oss-fuzz-build.sh @@ -9,13 +9,15 @@ cmake -Dvalijson_BUILD_EXAMPLES=FALSE \ -Dvalijson_EXCLUDE_BOOST=TRUE \ .. -make -j$(nproc) +make -j"$(nproc)" cd ../tests/fuzzing find ../.. -name "*.o" -exec ar rcs fuzz_lib.a {} \; -$CXX $CXXFLAGS -DVALIJSON_USE_EXCEPTIONS=1 \ +# CXXFLAGS may contain spaces +# shellcheck disable=SC2086 +"$CXX" $CXXFLAGS -DVALIJSON_USE_EXCEPTIONS=1 \ -I/src/valijson/thirdparty/rapidjson-48fbd8c/include \ -I/src/valijson/thirdparty/rapidjson-48fbd8c/include/rapidjson \ -I/src/valijson/include \ @@ -23,10 +25,11 @@ $CXX $CXXFLAGS -DVALIJSON_USE_EXCEPTIONS=1 \ -I/src/valijson/include/valijson/adapters \ -c fuzzer.cpp -o fuzzer.o -$CXX $CXXFLAGS $LIB_FUZZING_ENGINE \ +# shellcheck disable=SC2086 +"$CXX" $CXXFLAGS "$LIB_FUZZING_ENGINE" \ -DVALIJSON_USE_EXCEPTIONS=1 \ -rdynamic fuzzer.o \ - -o $OUT/fuzzer fuzz_lib.a + -o "${OUT}/fuzzer fuzz_lib.a" -zip $OUT/fuzzer_seed_corpus.zip \ - $SRC/valijson/doc/schema/draft-03.json +zip "${OUT}/fuzzer_seed_corpus.zip" \ + "${SRC}/valijson/doc/schema/draft-03.json"