Skip to content

Commit

Permalink
Use POSIX syntax
Browse files Browse the repository at this point in the history
Reduce bashisms. Downgrade shebangs from Bash to Dash (sh) in
POSIX-compliant shell scripts. Use the most portable shebang
(`#!/usr/bin/env <shell>`) to include the user $PATH in the
search path.
  • Loading branch information
jngrad committed Nov 19, 2019
1 parent df1758a commit 0f11fcc
Show file tree
Hide file tree
Showing 10 changed files with 40 additions and 41 deletions.
17 changes: 8 additions & 9 deletions maintainer/CI/build_cmake.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
# notice and this notice are preserved. This file is offered as-is,
# without any warranty.

abort()
{
abort() {
echo "An error occurred. Exiting..." >&2
echo "Command that failed: ${BASH_COMMAND}" >&2
exit 1
Expand All @@ -20,29 +19,29 @@ set -e
# HELPER FUNCTIONS

# output value of env variables
function outp {
outp() {
for p in ${*}; do
echo " ${p}=${!p}"
done
}

# start a block
function start {
start() {
echo "=================================================="
echo "START ${1}"
echo "=================================================="
}

# end a block
function end {
end() {
echo "=================================================="
echo "END ${1}"
echo "=================================================="
}

# set a default value to empty environment variables
# cast boolean values to true/false
function set_default_value {
set_default_value() {
if [ "${#}" != 2 ]; then
echo "set_default_value() takes 2 arguments (varname, default), got ${#}"
exit 1
Expand Down Expand Up @@ -127,7 +126,7 @@ if [ -z "${cxx_flags}" ]; then
fi

if [ "${with_coverage}" = true ]; then
bash <(curl -s https://codecov.io/env) &> /dev/null
bash <(curl -s https://codecov.io/env) 1>/dev/null 2>&1
fi

cmake_params="-DCMAKE_BUILD_TYPE=${build_type} -DWARNINGS_ARE_ERRORS=ON -DTEST_NP:INT=${check_procs} ${cmake_params}"
Expand Down Expand Up @@ -176,8 +175,8 @@ fi

# load MPI module if necessary
if [ -f "/etc/os-release" ]; then
grep -q suse /etc/os-release && source /etc/profile.d/modules.sh && module load gnu-openmpi
grep -q 'rhel\|fedora' /etc/os-release && for f in /etc/profile.d/*module*.sh; do source "${f}"; done && module load mpi
grep -q suse /etc/os-release && . /etc/profile.d/modules.sh && module load gnu-openmpi
grep -q 'rhel\|fedora' /etc/os-release && for f in /etc/profile.d/*module*.sh; do . "${f}"; done && module load mpi
fi

# CONFIGURE
Expand Down
2 changes: 1 addition & 1 deletion maintainer/CI/dox_warnings.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env bash
#!/usr/bin/env sh
# Copyright (C) 2019 The ESPResSo project
#
# This file is part of ESPResSo.
Expand Down
6 changes: 3 additions & 3 deletions maintainer/benchmarks/suite.sh
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,15 @@ mkdir "${build_dir}"
cd "${build_dir}"

# check for unstaged changes
if [ "$(git diff-index HEAD -- ${directories})" ]; then
if [ ! -z "$(git status --porcelain -- ${directories})" ]; then
echo "fatal: you have unstaged changes, please commit or stash them:"
git diff-index --name-only HEAD -- ${directories}
exit 1
fi

cleanup() {
# restore files in source directory
git checkout HEAD ${directories}
git checkout HEAD -- ${directories}
}

# prepare output files
Expand All @@ -69,7 +69,7 @@ EOF
# run benchmarks
for commit in ${commits}; do
echo "### commit ${commit}" >> benchmarks.log
git checkout ${commit} ${directories}
git checkout ${commit} -- ${directories}
bash ../maintainer/benchmarks/runner.sh
sed -ri "s/^/\"${commit}\",/" benchmarks.csv
tail -n +2 benchmarks.csv >> benchmarks_suite.csv
Expand Down
2 changes: 1 addition & 1 deletion maintainer/find_missing_header.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env bash
#!/usr/bin/env sh
# Copyright (C) 2012-2019 The ESPResSo project
# Copyright (C) 2012 Olaf Lenz
#
Expand Down
2 changes: 1 addition & 1 deletion maintainer/gh_close_issue.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env bash
#!/usr/bin/env sh
# Copyright (C) 2019 The ESPResSo project
#
# This file is part of ESPResSo.
Expand Down
2 changes: 1 addition & 1 deletion maintainer/gh_create_issue.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env bash
#!/usr/bin/env sh
# Copyright (C) 2019 The ESPResSo project
#
# This file is part of ESPResSo.
Expand Down
2 changes: 1 addition & 1 deletion maintainer/gh_post_status.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env bash
#!/usr/bin/env sh
# Copyright (C) 2017,2019 The ESPResSo project
#
# This file is part of ESPResSo.
Expand Down
8 changes: 4 additions & 4 deletions maintainer/missing_tests.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env bash
#!/usr/bin/env sh
# Copyright (C) 2017-2019 The ESPResSo project
#
# This file is part of ESPResSo.
Expand All @@ -18,14 +18,14 @@


# Detect python tests that are not included in CMakeLists.txt. Run this script
# in the folders for python, sample, tutorial and benchmark testsuite folders.
# in the testsuite folders for python, sample, tutorial and benchmark tests.

for T in *.py; do
if grep -qF " ${T}" CMakeLists.txt; then
continue
else
GIT_STATUS=$(git status --porcelain -- "${T}")
if [[ ${GIT_STATUS} == ??* ]]; then
tracked_status=$(git ls-files -- "${T}")
if [ -z "${tracked_status}" ]; then
echo "File '${T}' is not tracked."
continue
else
Expand Down
2 changes: 1 addition & 1 deletion src/python/pypresso.cmakein
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/sh
#!/usr/bin/env sh
# Copyright (C) 2010-2019 The ESPResSo project
#
# Copying and distribution of this file, with or without modification,
Expand Down
38 changes: 19 additions & 19 deletions testsuite/cmake/BashUnitTests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@

## @brief Print message to stderr
## @param $1 Message to display
function stderr() {
stderr() {
local message=$1
echo "${message}" 1>&2
}
Expand All @@ -116,7 +116,7 @@ function stderr() {
##
## If functions named `test_*` are already declared in the current environment,
## stop unit testing now. We don't want to run them and create side effects.
function check_namespace() {
check_namespace() {
if [ ! -z "$(declare -F | sed -r 's/declare +-f +//' | grep -P '^test_')" ]; then
stderr 'Functions named test_* already exist:'
for test in $(declare -F | sed -r 's/declare +-f +//' | grep -P "^test_"); do
Expand Down Expand Up @@ -148,7 +148,7 @@ check_namespace
## variables](https://www.open-mpi.org/faq/?category=running#mpi-environmental-variables)
## that can easily be checked for.
## @returns 1 if MPI is used, 0 otherwise
function detect_open_mpi() {
detect_open_mpi() {
if [ -z "${OMPI_COMM_WORLD_SIZE}" ] && [ -z "${OMPI_COMM_WORLD_RANK}" ] \
&& [ -z "${OMPI_COMM_WORLD_LOCAL_RANK}" ] && [ -z "${OMPI_UNIVERSE_SIZE}" ] \
&& [ -z "${OMPI_COMM_WORLD_LOCAL_SIZE}" ] && [ -z "${OMPI_COMM_WORLD_NODE_RANK}" ]; then
Expand All @@ -164,7 +164,7 @@ function detect_open_mpi() {
##
## @param $@ Command to run, possibly with modifiers
## @returns Error code returned by the command
function try_catch() {
try_catch() {
detect_open_mpi || return 1
(
set -e # exit from current subshell on first error
Expand All @@ -178,7 +178,7 @@ function try_catch() {
## Run a command in a subshell, exiting the subshell on first error.
## @param $@ Command to run, possibly with modifiers
## @returns Error code returned by the command
function try_catch_silent() {
try_catch_silent() {
detect_open_mpi || return 1
(
set -e # exit from current subshell on first error
Expand All @@ -193,7 +193,7 @@ function try_catch_silent() {
## logging stdout/stderr to the temporary file at #TMPNAME.
## @param $@ Command to run, possibly with modifiers
## @returns Error code returned by the command
function try_catch_capture_output() {
try_catch_capture_output() {
detect_open_mpi || return 1
rm -f "${TMPNAME}"
(
Expand All @@ -207,7 +207,7 @@ function try_catch_capture_output() {

## @brief Run the set_up() function if it exists
## @returns Error code returned by setUp()
function run_set_up() {
run_set_up() {
if [ "$(type -t set_up)" = "function" ]; then
try_catch set_up
local -r retcode=${?}
Expand All @@ -222,7 +222,7 @@ function run_set_up() {

## @brief Run the tear_down() function if it exists
## @returns Error code returned by tear_down()
function run_tear_down() {
run_tear_down() {
rm -f "${TMPNAME}"
if [ "$(type -t tear_down)" = "function" ]; then
try_catch tear_down
Expand All @@ -236,7 +236,7 @@ function run_tear_down() {
}

## @brief Run the tests
function run_tests() {
run_tests() {
for test in $(declare -F | sed -r 's/declare +-f +//' | grep -P "^test_"); do
start_test_block "${test#test_}"
${test}
Expand Down Expand Up @@ -273,22 +273,22 @@ function start_test_block() {
## @brief End a test
##
## Print a newline character and print all error messages in @ref error_log
function end_test_block() {
end_test_block() {
echo ""
for (( i=0; i<${#error_log[@]}; i++ )); do
stderr " ${error_log[i]}"
done
}

## @brief Log a successful assertion
function log_success() {
log_success() {
echo -n '.'
total_tests=$((total_tests + 1))
}

## @brief Log a failed assertion
## @param $* Description of the failure
function log_failure() {
log_failure() {
local message="${*}"
echo -n 'x'
error_log+=("${message}")
Expand All @@ -307,7 +307,7 @@ function log_failure() {
## @{

## @brief Run the setup, tests, teardown
function run_test_suite() {
run_test_suite() {
run_set_up
run_tests
run_tear_down
Expand All @@ -329,7 +329,7 @@ function run_test_suite() {
## @brief Check if a file exists
## @param $1 Filepath
## @param $2 Message on failure (optional)
function assert_file_exists() {
assert_file_exists() {
local -r filepath="${1}"
local message="${2}"
if [ -z "${message}" ]; then
Expand All @@ -346,7 +346,7 @@ function assert_file_exists() {
## @param $1 Obtained result
## @param $2 Expected result
## @param $3 Message on failure (optional)
function assert_string_equal() {
assert_string_equal() {
local -r result="${1}"
local -r expected="${2}"
local message="${3}"
Expand All @@ -364,7 +364,7 @@ function assert_string_equal() {
## @param $1 Obtained result
## @param $2 Expected result
## @param $3 Message on failure (optional)
function assert_equal() {
assert_equal() {
local -r result="${1}"
local -r expected="${2}"
local message="${3}"
Expand All @@ -381,7 +381,7 @@ function assert_equal() {
## @brief Check if a variable is non-zero
## @param $1 Obtained result
## @param $2 Message on failure (optional)
function assert_non_zero() {
assert_non_zero() {
local -r result="${1}"
local message="${2}"
if [ -z "${message}" ]; then
Expand All @@ -397,7 +397,7 @@ function assert_non_zero() {
## @brief Check if a variable is zero
## @param $1 Obtained result
## @param $2 Message on failure (optional)
function assert_zero() {
assert_zero() {
local -r result="${1}"
local message="${2}"
if [ -z "${message}" ]; then
Expand All @@ -414,7 +414,7 @@ function assert_zero() {
##
## Cannot be used in a script run by Open MPI (see \ref TryCatch).
## @param $@ Command to run, possibly with modifiers
function assert_return_code() {
assert_return_code() {
try_catch_capture_output "${@}"
local -r retcode=${?}
if [ "${retcode}" -eq "0" ]; then
Expand Down

0 comments on commit 0f11fcc

Please sign in to comment.