Skip to content

Commit

Permalink
CI - add job for shellcheck (quantumlib#5948)
Browse files Browse the repository at this point in the history
- Add check/shellcheck for syntax checking of shell scripts
- Add CI job to run shellcheck on our shell scripts

Resolves quantumlib#5923.
  • Loading branch information
pavoljuhas authored and rht committed May 1, 2023
1 parent 4ecd298 commit 878d439
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,15 @@ jobs:
run: pip install -r dev_tools/requirements/deps/tensorflow-docs.txt
- name: Doc check
run: check/nbformat
shellcheck:
name: Shell check
runs-on: ubuntu-20.04
steps:
- uses: actions/checkout@v2
with:
fetch-depth: 0
- name: Run shellcheck
run: check/shellcheck
isolated-modules:
name: Isolated pytest Ubuntu
runs-on: ubuntu-20.04
Expand Down
71 changes: 71 additions & 0 deletions check/shellcheck
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#!/usr/bin/env bash

###############################################################################
# Execute shellcheck for all shell scripts in this repository
#
# Usage:
# check/shellcheck [--dry-run] [shellcheck-arguments]
#
# Use the '--dry-run' option to print out the shellcheck command line without
# running it. This displays all shell script files found in the repository.
#
################################################################################

# Change working directory to the repository root.
thisdir="$(dirname "${BASH_SOURCE[0]}")" || exit $?
topdir="$(git -C "${thisdir}" rev-parse --show-toplevel)" || exit $?
cd "${topdir}" || exit $?

# Process command line arguments
opt_dry_run=0
typeset -a shellcheck_options
typeset -a our_shell_scripts

for arg in "$@"; do
if [[ "${arg}" == "--dry-run" ]]; then
opt_dry_run=1
else
shellcheck_options+=( "${arg}" )
fi
done

# Find all shell scripts in this repository.
IFS=$'\n' read -r -d '' -a our_shell_scripts < \
<(git ls-files -z -- \
':(exclude)*.'{py,json,json_inward,repr,repr_inward,ipynb,txt,md} \
':(exclude)*.'{yaml,ts,tst,rst,pyi,cfg} | \
xargs -0 file | grep -i 'shell script' | cut -d: -f1)

# Verify our_shell_scripts array - require it must contain files below.
typeset -a required_shell_scripts
required_shell_scripts=(
# items below must be sorted
check/all
check/doctest
check/format-incremental
check/mypy
check/nbformat
check/pylint
check/pytest
dev_tools/pypath
)

scripts_not_found="$(comm -13 \
<(printf "%s\n" "${our_shell_scripts[@]}") \
<(printf "%s\n" "${required_shell_scripts[@]}") )"

if [[ -n "${scripts_not_found}" ]]; then
echo "Identification of shell scripts failed - files not found:" >&2
printf "\n%s\n\n" "${scripts_not_found}" >&2
echo "Please fix $0." >&2
exit 2
fi

# Ready to run here.
if (( opt_dry_run )); then
printf '%s ' '>>' 'shellcheck' "${shellcheck_options[@]}"
printf '\\\n %s ' "${our_shell_scripts[@]}"
printf '\\\n;\n'
else
shellcheck "${shellcheck_options[@]}" "${our_shell_scripts[@]}"
fi

0 comments on commit 878d439

Please sign in to comment.