Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Bash arrays for argument lists in shell scripts #10877

Merged
merged 2 commits into from
Feb 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions .circleci/soltest.sh
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ OPTIMIZE=${OPTIMIZE:-"0"}
EVM=${EVM:-"invalid"}
REPODIR="$(realpath "$(dirname "$0")/..")"

IFS=" " read -r -a BOOST_TEST_ARGS <<< "$BOOST_TEST_ARGS"
IFS=" " read -r -a SOLTEST_FLAGS <<< "$SOLTEST_FLAGS"
cameel marked this conversation as resolved.
Show resolved Hide resolved

source "${REPODIR}/scripts/common.sh"
# Test result output directory (CircleCI is reading test results from here)
mkdir -p test_results
Expand All @@ -53,11 +56,12 @@ get_logfile_basename() {
echo -ne "${filename}"
}

BOOST_TEST_ARGS="--color_output=no --show_progress=yes --logger=JUNIT,error,test_results/`get_logfile_basename`.xml ${BOOST_TEST_ARGS}"
SOLTEST_ARGS="--evm-version=$EVM $SOLTEST_FLAGS"
test "${OPTIMIZE}" = "1" && SOLTEST_ARGS="${SOLTEST_ARGS} --optimize"
test "${ABI_ENCODER_V1}" = "1" && SOLTEST_ARGS="${SOLTEST_ARGS} --abiencoderv1"
BOOST_TEST_ARGS=("--color_output=no" "--show_progress=yes" "--logger=JUNIT,error,test_results/`get_logfile_basename`.xml" "${BOOST_TEST_ARGS[@]}")
SOLTEST_ARGS=("--evm-version=$EVM" "${SOLTEST_FLAGS[@]}")

test "${OPTIMIZE}" = "1" && SOLTEST_ARGS+=(--optimize)
test "${ABI_ENCODER_V1}" = "1" && SOLTEST_ARGS+=(--abiencoderv1)

echo "Running ${REPODIR}/build/test/soltest ${BOOST_TEST_ARGS} -- ${SOLTEST_ARGS}"
echo "Running ${REPODIR}/build/test/soltest ${BOOST_TEST_ARGS[*]} -- ${SOLTEST_ARGS[*]}"

"${REPODIR}/build/test/soltest" ${BOOST_TEST_ARGS} -- ${SOLTEST_ARGS}
"${REPODIR}/build/test/soltest" "${BOOST_TEST_ARGS[@]}" -- "${SOLTEST_ARGS[@]}"
7 changes: 5 additions & 2 deletions scripts/ASTImportTest.sh
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,13 @@ fi
# $1 name of the file to be exported and imported
# $2 any files needed to do so that might be in parent directories
function testImportExportEquivalence {
if $SOLC "$1" $2 > /dev/null 2>&1
local nth_input_file="$1"
IFS=" " read -r -a all_input_files <<< "$2"

if $SOLC "$nth_input_file" "${all_input_files[@]}" > /dev/null 2>&1
then
# save exported json as expected result (silently)
$SOLC --combined-json ast,compact-format --pretty-json "$1" $2 > expected.json 2> /dev/null
$SOLC --combined-json ast,compact-format --pretty-json "$nth_input_file" "${all_input_files[@]}" > expected.json 2> /dev/null
# import it, and export it again as obtained result (silently)
$SOLC --import-ast --combined-json ast,compact-format --pretty-json expected.json > obtained.json 2> /dev/null
if [ $? -ne 0 ]
Expand Down
1 change: 0 additions & 1 deletion scripts/chk_shellscripts/ignore.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
./test/cmdlineTests.sh
./scripts/soltest.sh
./scripts/wasm-rebuild/docker-scripts/rebuild_tags.sh
./scripts/wasm-rebuild/docker-scripts/rebuild_current.sh
./scripts/wasm-rebuild/docker-scripts/genbytecode.sh
Expand Down
14 changes: 7 additions & 7 deletions scripts/common_cmdline.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
# (c) 2016-2019 solidity contributors.
# ------------------------------------------------------------------------------

FULLARGS="--optimize --ignore-missing --combined-json abi,asm,ast,bin,bin-runtime,compact-format,devdoc,hashes,interface,metadata,opcodes,srcmap,srcmap-runtime,userdoc"
OLDARGS="--optimize --combined-json abi,asm,ast,bin,bin-runtime,devdoc,interface,metadata,opcodes,srcmap,srcmap-runtime,userdoc"
FULLARGS=(--optimize --ignore-missing --combined-json "abi,asm,ast,bin,bin-runtime,compact-format,devdoc,hashes,interface,metadata,opcodes,srcmap,srcmap-runtime,userdoc")
OLDARGS=(--optimize --combined-json "abi,asm,ast,bin,bin-runtime,devdoc,interface,metadata,opcodes,srcmap,srcmap-runtime,userdoc")
function compileFull()
{
local expected_exit_code=0
Expand All @@ -37,23 +37,23 @@ function compileFull()
expect_output=2
shift;
fi
local args=$FULLARGS
local args=("${FULLARGS[@]}")
if [[ $1 = '-v' ]]; then
if (echo "$2" | grep -Po '(?<=0.4.)\d+' >/dev/null); then
patch=$(echo "$2" | grep -Po '(?<=0.4.)\d+')
if (( patch < 22 )); then
args=$OLDARGS
args=("${OLDARGS[@]}")
fi
fi
shift 2
fi

local files="$*"
local files=("$@")

local stderr_path=$(mktemp)

set +e
"$SOLC" ${args} ${files} >/dev/null 2>"$stderr_path"
"$SOLC" "${args[@]}" "${files[@]}" >/dev/null 2>"$stderr_path"
local exit_code=$?
local errors=$(grep -v -E 'Warning: This is a pre-release compiler version|Warning: Experimental features are turned on|pragma experimental ABIEncoderV2|^ +--> |^ +\||^[0-9]+ +\|' < "$stderr_path")
set -e
Expand All @@ -70,7 +70,7 @@ function compileFull()
printError "Was failure: $exit_code"
echo "$errors"
printError "While calling:"
echo "\"$SOLC\" $args $files"
echo "\"$SOLC\" ${args[*]} ${files[*]}"
printError "Inside directory:"
pwd
false
Expand Down
10 changes: 5 additions & 5 deletions scripts/docs_version_pragma_check.sh
Original file line number Diff line number Diff line change
Expand Up @@ -145,17 +145,17 @@ SOLTMPDIR=$(mktemp -d)
fi
echo "$f"

opts=''
opts=()
# We expect errors if explicitly stated, or if imports
# are used (in the style guide)
if ( ! grep -E "This will not compile after" "$f" >/dev/null && \
grep -E "This will not compile|import \"" "$f" >/dev/null )
then
opts="-e"
opts=(-e)
fi

# ignore warnings in this case
opts="$opts -o"
opts+=(-o)

findMinimalVersion "$f"
if [[ "$version" == "" ]]
Expand All @@ -168,7 +168,7 @@ SOLTMPDIR=$(mktemp -d)
continue
fi

opts="$opts -v $version"
opts+=(-v "$version")

solc_bin="solc-$version"
echo "$solc_bin"
Expand All @@ -188,7 +188,7 @@ SOLTMPDIR=$(mktemp -d)
chmod a+x solc

SOLC="$SOLTMPDIR/solc"
compileFull $opts "$SOLTMPDIR/$f"
compileFull "${opts[@]}" "$SOLTMPDIR/$f"
done
)
rm -rf "$SOLTMPDIR"
Expand Down
22 changes: 13 additions & 9 deletions scripts/soltest.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ set -e
REPO_ROOT="$(dirname "$0")"/..
USE_DEBUGGER=0
DEBUGGER="gdb --args"
BOOST_OPTIONS=
SOLTEST_OPTIONS=
BOOST_OPTIONS=()
SOLTEST_OPTIONS=()
SOLIDITY_BUILD_DIR=${SOLIDITY_BUILD_DIR:-${REPO_ROOT}/build}

usage() {
Expand Down Expand Up @@ -41,27 +41,31 @@ do
;;
--boost-options)
shift
BOOST_OPTIONS="${BOOST_OPTIONS} $1"
BOOST_OPTIONS+=("$1")
;;
--help)
usage
exit 0
;;
--run_test | -t )
shift
BOOST_OPTIONS="${BOOST_OPTIONS} -t $1"
BOOST_OPTIONS+=(-t "$1")
;;
--show-progress | -p)
BOOST_OPTIONS="${BOOST_OPTIONS} $1"
BOOST_OPTIONS+=("$1")
;;
*)
SOLTEST_OPTIONS="${SOLTEST_OPTIONS} $1"
SOLTEST_OPTIONS+=("$1")
;;
esac
shift
done

SOLTEST_COMMAND=("${SOLIDITY_BUILD_DIR}/test/soltest" "${BOOST_OPTIONS[@]}" -- --testpath "${REPO_ROOT}/test" "${SOLTEST_OPTIONS[@]}")

if [ "$USE_DEBUGGER" -ne "0" ]; then
DEBUG_PREFIX=${DEBUGGER}
# shellcheck disable=SC2086
exec ${DEBUGGER} "${SOLTEST_COMMAND[@]}"
else
exec "${SOLTEST_COMMAND[@]}"
fi

exec ${DEBUG_PREFIX} "${SOLIDITY_BUILD_DIR}/test/soltest" ${BOOST_OPTIONS} -- --testpath "${REPO_ROOT}/test" ${SOLTEST_OPTIONS}
15 changes: 8 additions & 7 deletions scripts/tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ set -e

REPO_ROOT="$(dirname "$0")/.."
SOLIDITY_BUILD_DIR="${SOLIDITY_BUILD_DIR:-${REPO_ROOT}/build}"
IFS=" " read -r -a SMT_FLAGS <<< "$SMT_FLAGS"

source "${REPO_ROOT}/scripts/common.sh"

Expand Down Expand Up @@ -100,29 +101,29 @@ do
fi
for abiv1 in $FORCE_ABIV1_RUNS
do
force_abiv1_flag=""
force_abiv1_flag=()
if [[ "$abiv1" == "yes" ]]
then
force_abiv1_flag="--abiencoderv1"
force_abiv1_flag=(--abiencoderv1)
fi
printTask "--> Running tests using $optimize --evm-version $vm $force_abiv1_flag..."
printTask "--> Running tests using $optimize --evm-version $vm ${force_abiv1_flag[*]}..."

log=""
log=()
if [ -n "$log_directory" ]
then
if [ -n "$optimize" ]
then
log=--logger=JUNIT,error,$log_directory/opt_$vm.xml
log+=("--logger=JUNIT,error,$log_directory/opt_$vm.xml")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

= should work here as well, right?

Just asking, doesn't need any changes.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, for not getting back to this one earlier :)

Yeah, it would work and it would match the original code better but I think that += is more appropriate here. If log were initialized with a non-empty array, we'd likely want to add to it rather than ovewrite it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No worries!

else
log=--logger=JUNIT,error,$log_directory/noopt_$vm.xml
log+=("--logger=JUNIT,error,$log_directory/noopt_$vm.xml")
fi
fi

EWASM_ARGS=""
[ "${vm}" = "byzantium" ] && [ "${optimize}" = "" ] && EWASM_ARGS="--ewasm"

set +e
"${SOLIDITY_BUILD_DIR}"/test/soltest --show-progress $log -- ${EWASM_ARGS} --testpath "$REPO_ROOT"/test "$optimize" --evm-version "$vm" $SMT_FLAGS $force_abiv1_flag
"${SOLIDITY_BUILD_DIR}"/test/soltest --show-progress "${log[@]}" -- ${EWASM_ARGS} --testpath "$REPO_ROOT"/test "$optimize" --evm-version "$vm" "${SMT_FLAGS[@]}" "${force_abiv1_flag[@]}"

if test "0" -ne "$?"; then
exit 1
Expand Down
31 changes: 16 additions & 15 deletions test/cmdlineTests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ function ask_expectation_update
function test_solc_behaviour()
{
local filename="${1}"
local solc_args="${2}"
local solc_args
IFS=" " read -r -a solc_args <<< "${2}"
local solc_stdin="${3}"
[ -z "$solc_stdin" ] && solc_stdin="/dev/stdin"
local stdout_expected="${4}"
Expand All @@ -127,13 +128,13 @@ function test_solc_behaviour()

if [[ "$exit_code_expected" = "" ]]; then exit_code_expected="0"; fi

local solc_command="$SOLC ${filename} ${solc_args} <$solc_stdin"
local solc_command="$SOLC ${filename} ${solc_args[*]} <$solc_stdin"
set +e
"$SOLC" "${filename}" ${solc_args} <"$solc_stdin" >"$stdout_path" 2>"$stderr_path"
"$SOLC" "${filename}" "${solc_args[@]}" <"$solc_stdin" >"$stdout_path" 2>"$stderr_path"
exitCode=$?
set -e

if [[ "$solc_args" == *"--standard-json"* ]]
if [[ " ${solc_args[*]} " == *" --standard-json "* ]]
then
sed -i.bak -e 's/{[^{]*Warning: This is a pre-release compiler version[^}]*},\{0,1\}//' "$stdout_path"
sed -i.bak -E -e 's/ Consider adding \\"pragma solidity \^[0-9.]*;\\"//g' "$stdout_path"
Expand Down Expand Up @@ -221,17 +222,17 @@ function test_solc_assembly_output()
{
local input="${1}"
local expected="${2}"
local solc_args="${3}"
IFS=" " read -r -a solc_args <<< "${3}"

local expected_object="object \"object\" { code ${expected} }"

output=$(echo "${input}" | "$SOLC" - ${solc_args} 2>/dev/null)
output=$(echo "${input}" | "$SOLC" - "${solc_args[@]}" 2>/dev/null)
empty=$(echo "$output" | tr '\n' ' ' | tr -s ' ' | sed -ne "/${expected_object}/p")
if [ -z "$empty" ]
then
printError "Incorrect assembly output. Expected: "
echo -e "${expected}"
printError "with arguments ${solc_args}, but got:"
printError "with arguments ${solc_args[*]}, but got:"
echo "${output}"
exit 1
fi
Expand Down Expand Up @@ -302,19 +303,19 @@ printTask "Running general commandline tests..."
inputFile=""
stdout="$(cat "${tdir}/output.json" 2>/dev/null || true)"
stdoutExpectationFile="${tdir}/output.json"
args="--standard-json "$(cat "${tdir}/args" 2>/dev/null || true)
command_args="--standard-json "$(cat "${tdir}/args" 2>/dev/null || true)
else
stdin=""
stdout="$(cat "${tdir}/output" 2>/dev/null || true)"
stdoutExpectationFile="${tdir}/output"
args=$(cat "${tdir}/args" 2>/dev/null || true)
command_args=$(cat "${tdir}/args" 2>/dev/null || true)
fi
exitCodeExpectationFile="${tdir}/exit"
exitCode=$(cat "$exitCodeExpectationFile" 2>/dev/null || true)
err="$(cat "${tdir}/err" 2>/dev/null || true)"
stderrExpectationFile="${tdir}/err"
test_solc_behaviour "$inputFile" \
"$args" \
"$command_args" \
"$stdin" \
"$stdout" \
"$exitCode" \
Expand Down Expand Up @@ -354,22 +355,22 @@ SOLTMPDIR=$(mktemp -d)
fi
echo "$f"

opts=''
opts=()
# We expect errors if explicitly stated, or if imports
# are used (in the style guide)
if grep -E "This will not compile|import \"" "$f" >/dev/null
then
opts="-e"
opts=(-e)
fi
if grep "This will report a warning" "$f" >/dev/null
then
opts="$opts -w"
opts+=(-w)
fi
if grep "This may report a warning" "$f" >/dev/null
then
opts="$opts -o"
opts+=(-o)
fi
compileFull $opts "$SOLTMPDIR/$f"
compileFull "${opts[@]}" "$SOLTMPDIR/$f"
done
)
rm -rf "$SOLTMPDIR"
Expand Down