From d905b4f8705d72714198a53739c9df254c39844d Mon Sep 17 00:00:00 2001 From: jrfnl Date: Wed, 22 Jan 2025 10:43:51 +0100 Subject: [PATCH 1/4] GH Actions: add shellcheck job Shellcheck is a static analysis tool for shell scripts, which can help find common mistakes, simplifications and point out best practices. This commit adds a new GH Actions job to run shellcheck on all shell scripts in this repo. At this moment, that means it will run over the `tests/utils/proxy` start/stop scripts, but more scripts are expected to be added in the near future. I've researched the available action runners and found the `ludeeus/action-shellcheck` one to be the most popular as well as suitable for our needs. Additionally, the `lumaxis/shellcheck-problem-matchers` will allow for showing any issues found inline in the GH code view via annotations. Refs: * https://www.shellcheck.net/ * https://github.com/koalaman/shellcheck/wiki/Integration * https://github.com/ludeeus/action-shellcheck * https://github.com/lumaxis/shellcheck-problem-matchers --- .github/workflows/cs.yml | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/.github/workflows/cs.yml b/.github/workflows/cs.yml index e738b12c0..dc6f5b1c5 100644 --- a/.github/workflows/cs.yml +++ b/.github/workflows/cs.yml @@ -67,3 +67,21 @@ jobs: - name: Show PHPCS results in PR if: ${{ always() && steps.phpcs.outcome == 'failure' }} run: cs2pr ./phpcs-report.xml + + shellcheck: #---------------------------------------------------------------------- + name: 'ShellCheck' + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Set up problem matcher + uses: lumaxis/shellcheck-problem-matchers@v2 + with: + format: gcc + + - name: Run ShellCheck + uses: ludeeus/action-shellcheck@2.0.0 + with: + format: gcc From d156661482fb7afa4cc06340a8cc134fce1eef03 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Wed, 22 Jan 2025 10:44:45 +0100 Subject: [PATCH 2/4] [INCOMPLETE] Shell scripts: various tweaks Various tweaks to allow the scripts to pass the shellcheck checks. --- scripts/proxy/start.sh | 8 ++++---- scripts/proxy/stop.sh | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/scripts/proxy/start.sh b/scripts/proxy/start.sh index e1e30e4b7..9d8bc0fb3 100755 --- a/scripts/proxy/start.sh +++ b/scripts/proxy/start.sh @@ -5,15 +5,15 @@ PORT=${PORT:-9000} PROXYBIN=${PROXYBIN:-"$(which mitmdump)"} ARGS="-s '$PROXYDIR/proxy.py' -p $PORT" -if [[ ! -z "$AUTH" ]]; then +if [[ -n "$AUTH" ]]; then ARGS="$ARGS --proxyauth $AUTH" fi PIDFILE="$PROXYDIR/proxy-$PORT.pid" set -x -start-stop-daemon --verbose --start --background --pidfile $PIDFILE --make-pidfile --exec $PROXYBIN -- $ARGS +start-stop-daemon --verbose --start --background --pidfile "$PIDFILE" --make-pidfile --exec "$PROXYBIN" -- $ARGS -ps -p $(cat $PIDFILE) u +ps -p "$(cat "$PIDFILE")" u sleep 2 -ps -p $(cat $PIDFILE) u +ps -p "$(cat "$PIDFILE")" u diff --git a/scripts/proxy/stop.sh b/scripts/proxy/stop.sh index 8cb0eb92f..805db7e25 100755 --- a/scripts/proxy/stop.sh +++ b/scripts/proxy/stop.sh @@ -1,10 +1,10 @@ #!/usr/bin/env bash -PROXYDIR="$PWD/$(dirname $0)" +PROXYDIR="$PWD/$(dirname "$0")" PORT=${PORT:-9000} PIDFILE="$PROXYDIR/proxy-$PORT.pid" set -x -start-stop-daemon --verbose --stop --pidfile $PIDFILE --remove-pidfile +start-stop-daemon --verbose --stop --pidfile "$PIDFILE" --remove-pidfile From 197e540acc4d9e8bdc93344448e27d3681e72d6f Mon Sep 17 00:00:00 2001 From: Alain Schlesser Date: Mon, 3 Feb 2025 10:23:48 +0100 Subject: [PATCH 3/4] Turn $ARGS into array to fix shellcheck issue --- scripts/proxy/start.sh | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/scripts/proxy/start.sh b/scripts/proxy/start.sh index 9d8bc0fb3..77c868602 100755 --- a/scripts/proxy/start.sh +++ b/scripts/proxy/start.sh @@ -4,15 +4,16 @@ PROXYDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" PORT=${PORT:-9000} PROXYBIN=${PROXYBIN:-"$(which mitmdump)"} -ARGS="-s '$PROXYDIR/proxy.py' -p $PORT" + +ARGS=(-s "$PROXYDIR/proxy.py" -p "$PORT") if [[ -n "$AUTH" ]]; then - ARGS="$ARGS --proxyauth $AUTH" + ARGS+=("--proxyauth" "$AUTH") fi PIDFILE="$PROXYDIR/proxy-$PORT.pid" set -x -start-stop-daemon --verbose --start --background --pidfile "$PIDFILE" --make-pidfile --exec "$PROXYBIN" -- $ARGS +start-stop-daemon --verbose --start --background --pidfile "$PIDFILE" --make-pidfile --exec "$PROXYBIN" -- "${ARGS[@]}" ps -p "$(cat "$PIDFILE")" u sleep 2 From 1d5b105ad4ae10d307c45658eadd61be95fd2cce Mon Sep 17 00:00:00 2001 From: jrfnl Date: Tue, 11 Feb 2025 11:27:50 +0100 Subject: [PATCH 4/4] WIP --- .github/CONTRIBUTING.md | 15 ++++++++++++++- .shellcheckrc | 9 +++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 .shellcheckrc diff --git a/.github/CONTRIBUTING.md b/.github/CONTRIBUTING.md index 56bef6024..df47cbdbc 100644 --- a/.github/CONTRIBUTING.md +++ b/.github/CONTRIBUTING.md @@ -62,7 +62,7 @@ The linter can be run locally using `composer lint`. [PHP Parallel Lint]: https://github.com/php-parallel-lint/PHP-Parallel-Lint -## Coding Style +## PHP Coding Style Please follow the existing coding style and best practices. This project uses [PHP_CodeSniffer][] to detect coding standard violations and apply automated fixes (whenever possible). @@ -72,6 +72,19 @@ This project uses [PHP_CodeSniffer][] to detect coding standard violations and a [PHP_CodeSniffer]: https://github.com/squizlabs/PHP_CodeSniffer +## Shell script QA & Coding Style + +All shell scripts used in this project are, and should be, located in the `/scripts` directory. + +Please follow the existing coding style and best practices. +This project uses [ShellCheck][] to detect violations. + +* Shellcheck can be [installed on all platforms][shellcheck-install] +* All files can be checked for coding standard violations by running `shellcheck`. + +[ShellCheck]: https://github.com/koalaman/shellcheck +[shellcheck-install]: https://github.com/koalaman/shellcheck#user-content-installing + ## Unit Tests PRs should include unit tests for all changes. diff --git a/.shellcheckrc b/.shellcheckrc new file mode 100644 index 000000000..02cbe4b93 --- /dev/null +++ b/.shellcheckrc @@ -0,0 +1,9 @@ +shell=bash +color=always + +external-sources=false +source-path=/scripts +source-path=/scripts/proxy + +# Turn on all checks. +enable=all