Skip to content

Commit 70db9c9

Browse files
authored
Add strict error handling to bash scripts (#123)
* Add strict error handling to bash scripts Add "set -eu -o pipefail" to improve script reliability: - Exit on undefined variables (-u) - Exit on command failures (-e) - Fail on pipe errors (-o pipefail) * Remove unused, undefined variable * Fix unbound variable
1 parent 5c0eaae commit 70db9c9

File tree

10 files changed

+22
-1
lines changed

10 files changed

+22
-1
lines changed

commands/web/eslint

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
## Example: "ddev eslint"
88
## ExecRaw: true
99

10+
set -eu -o pipefail
11+
1012
if "$DDEV_DOCROOT/core/node_modules/.bin/eslint" --version >/dev/null ; then
1113
# Configure prettier
1214
test -e .prettierrc.json || ln -s $DDEV_DOCROOT/core/.prettierrc.json .

commands/web/expand-composer-json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@
77
## Example: "ddev expand-composer-json ctools"
88
## ExecRaw: true
99

10+
set -eu -o pipefail
11+
1012
export _WEB_ROOT=$DDEV_DOCROOT
1113
cd "$DDEV_COMPOSER_ROOT" || exit
1214
curl -OL https://git.drupalcode.org/project/gitlab_templates/-/raw/default-ref/scripts/expand_composer_json.php
15+
_ddev_drupal_contrib_empty_composer=false
1316
if [[ ! -f composer.json ]]; then
1417
echo "{}" > composer.json
1518
_ddev_drupal_contrib_empty_composer=true

commands/web/nightwatch

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,6 @@
77
## Example: "ddev nightwatch"
88
## ExecRaw: true
99

10+
set -eu -o pipefail
11+
1012
yarn --cwd "$DDEV_DOCROOT/core" test:nightwatch "$DDEV_APPROOT/$DDEV_DOCROOT/$DRUPAL_PROJECTS_PATH/" "$@"

commands/web/phpcbf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
## Example: "ddev phpcbf" or "ddev phpcbf -n"
88
## ExecRaw: true
99

10+
set -eu -o pipefail
11+
1012
if ! command -v phpcbf >/dev/null; then
1113
echo "phpcbf is not available. You may need to 'ddev composer install'"
1214
exit 1

commands/web/phpcs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
## Example: "ddev phpcs" or "ddev phpcs -n"
88
## ExecRaw: true
99

10+
set -eu -o pipefail
11+
1012
if ! command -v phpcs >/dev/null; then
1113
echo "phpcs is not available. You may need to 'ddev composer install'"
1214
exit 1

commands/web/phpstan

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
## Example: "ddev phpstan" or "ddev phpstan -n"
88
## ExecRaw: true
99

10+
set -eu -o pipefail
11+
1012
if ! command -v phpstan >/dev/null; then
1113
echo "phpstan is not available. You may need to 'ddev poser'"
1214
exit 1

commands/web/phpunit

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
## Example: "ddev phpunit" or "ddev phpunit --stop-on-failure"
88
## ExecRaw: true
99

10+
set -eu -o pipefail
11+
1012
if ! command -v phpunit >/dev/null; then
1113
echo "phpunit is not available. You may need to 'ddev composer install'"
1214
exit 1

commands/web/poser

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77
## Example: "ddev poser" or "ddev poser --prefer-source"
88
## ExecRaw: true
99

10+
set -eu -o pipefail
11+
1012
export COMPOSER=composer.contrib.json
11-
.ddev/commands/web/expand-composer-json "$DDEV_PROJECT_NAME"
13+
.ddev/commands/web/expand-composer-json
1214
composer install "$@"
1315
# The -f flag suppresses errors if lock file does not exist.
1416
rm -f composer.contrib.json composer.contrib.lock

commands/web/stylelint

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
## Example: "ddev stylelint"
88
## ExecRaw: true
99

10+
set -eu -o pipefail
11+
1012
if $DDEV_DOCROOT/core/node_modules/.bin/stylelint --version >/dev/null ; then
1113
# Change directory to the project root folder
1214
cd "$DDEV_DOCROOT/$DRUPAL_PROJECTS_PATH/${DDEV_SITENAME//-/_}" || exit

commands/web/symlink-project

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
## Example: "ddev symlink-project"
88
## ExecRaw: true
99

10+
set -eu -o pipefail
11+
1012
export _WEB_ROOT=$DDEV_DOCROOT
1113
#todo use more dynamic ref.
1214
cd "$DDEV_COMPOSER_ROOT" || exit

0 commit comments

Comments
 (0)