Provide option to auto-correct cmdlineTests expectations

This commit is contained in:
mingchuan 2019-04-08 00:24:14 +08:00
parent ef3a18999c
commit f705b09b21
No known key found for this signature in database
GPG Key ID: 607CD25FA2D03651

View File

@ -93,6 +93,27 @@ function compileFull()
fi fi
} }
function ask_expectation_update()
{
local newExpectation="${1}"
local expectationFile="${2}"
while true;
do
set +e
read -t10 -p "(u)pdate expectation/(q)uit? "
if [ $? -gt 128 ];
then
echo -e "\nUser input timed out."
exit 1
fi
set -e
case $REPLY in
u* ) echo "$newExpectation" > $expectationFile ; break;;
q* ) exit 1;;
esac
done
}
# General helper function for testing SOLC behaviour, based on file name, compile opts, exit code, stdout and stderr. # General helper function for testing SOLC behaviour, based on file name, compile opts, exit code, stdout and stderr.
# An failure is expected. # An failure is expected.
function test_solc_behaviour() function test_solc_behaviour()
@ -103,17 +124,22 @@ function test_solc_behaviour()
local stdout_expected="${4}" local stdout_expected="${4}"
local exit_code_expected="${5}" local exit_code_expected="${5}"
local stderr_expected="${6}" local stderr_expected="${6}"
local stdout_expectation_file="${7}" # the file to write to when user chooses to update stdout expectation
local stderr_expectation_file="${8}" # the file to write to when user chooses to update stderr expectation
local stdout_path=`mktemp` local stdout_path=`mktemp`
local stderr_path=`mktemp` local stderr_path=`mktemp`
trap "rm -f $stdout_path $stderr_path" EXIT
if [[ "$exit_code_expected" = "" ]]; then exit_code_expected="0"; fi if [[ "$exit_code_expected" = "" ]]; then exit_code_expected="0"; fi
local solc_command="$SOLC ${filename} ${solc_args}"
if [[ -n "$solc_stdin" ]]; then solc_command+=" <$solc_stdin" ; fi
if [[ -n "$stdout_path" ]]; then solc_command+=" 1>$stdout_path"; fi
if [[ -n "$stderr_path" ]]; then solc_command+=" 2>$stderr_path"; fi
set +e set +e
if [[ "$solc_stdin" = "" ]] eval "$solc_command"
then
"$SOLC" "${filename}" ${solc_args} 1>$stdout_path 2>$stderr_path
else
"$SOLC" "${filename}" ${solc_args} <$solc_stdin 1>$stdout_path 2>$stderr_path
fi
exitCode=$? exitCode=$?
set -e set -e
@ -133,37 +159,44 @@ function test_solc_behaviour()
if [[ $exitCode -ne "$exit_code_expected" ]] if [[ $exitCode -ne "$exit_code_expected" ]]
then then
printError "Incorrect exit code. Expected $exit_code_expected but got $exitCode." printError "Incorrect exit code. Expected $exit_code_expected but got $exitCode."
rm -f $stdout_path $stderr_path
exit 1 exit 1
fi fi
if [[ "$(cat $stdout_path)" != "${stdout_expected}" ]] if [[ "$(cat $stdout_path)" != "${stdout_expected}" ]]
then then
printError "Incorrect output on stdout received. Expected:" printError "Incorrect output on stdout received. Expected:"
echo -e "${stdout_expected}" echo "${stdout_expected}"
printError "But got:" printError "But got:"
cat $stdout_path cat $stdout_path
printError "When running $SOLC ${filename} ${solc_args} <$solc_stdin"
rm -f $stdout_path $stderr_path printError "When running $solc_command"
exit 1
if [ -n "$stdout_expectation_file" ]
then
ask_expectation_update "$(cat $stdout_path)" "$stdout_expectation_file"
else
exit 1
fi
fi fi
if [[ "$(cat $stderr_path)" != "${stderr_expected}" ]] if [[ "$(cat $stderr_path)" != "${stderr_expected}" ]]
then then
printError "Incorrect output on stderr received. Expected:" printError "Incorrect output on stderr received. Expected:"
echo -e "${stderr_expected}" echo "${stderr_expected}"
printError "But got:" printError "But got:"
cat $stderr_path cat $stderr_path
printError "When running $SOLC ${filename} ${solc_args} <$solc_stdin"
rm -f $stdout_path $stderr_path printError "When running $solc_command"
exit 1
if [ -n "$stderr_expectation_file" ]
then
ask_expectation_update "$(cat $stderr_path)" "$stderr_expectation_file"
else
exit 1
fi
fi fi
rm -f $stdout_path $stderr_path
} }
@ -210,14 +243,14 @@ printTask "Testing unknown options..."
printTask "Testing passing files that are not found..." printTask "Testing passing files that are not found..."
test_solc_behaviour "file_not_found.sol" "" "" "" 1 "\"file_not_found.sol\" is not found." test_solc_behaviour "file_not_found.sol" "" "" "" 1 "\"file_not_found.sol\" is not found." "" ""
printTask "Testing passing files that are not files..." printTask "Testing passing files that are not files..."
test_solc_behaviour "." "" "" "" 1 "\".\" is not a valid file." test_solc_behaviour "." "" "" "" 1 "\".\" is not a valid file." "" ""
printTask "Testing passing empty remappings..." printTask "Testing passing empty remappings..."
test_solc_behaviour "${0}" "=/some/remapping/target" "" "" 1 "Invalid remapping: \"=/some/remapping/target\"." test_solc_behaviour "${0}" "=/some/remapping/target" "" "" 1 "Invalid remapping: \"=/some/remapping/target\"." "" ""
test_solc_behaviour "${0}" "ctx:=/some/remapping/target" "" "" 1 "Invalid remapping: \"ctx:=/some/remapping/target\"." test_solc_behaviour "${0}" "ctx:=/some/remapping/target" "" "" 1 "Invalid remapping: \"ctx:=/some/remapping/target\"." "" ""
printTask "Running general commandline tests..." printTask "Running general commandline tests..."
( (
@ -228,18 +261,28 @@ printTask "Running general commandline tests..."
then then
inputFile="" inputFile=""
stdin="${tdir}/input.json" stdin="${tdir}/input.json"
stdout=$(cat ${tdir}/output.json 2>/dev/null || true) stdout="$(cat ${tdir}/output.json 2>/dev/null || true)"
stdoutExpectationFile="$(pwd)/${tdir}/output.json"
args="--standard-json "$(cat ${tdir}/args 2>/dev/null || true) args="--standard-json "$(cat ${tdir}/args 2>/dev/null || true)
else else
inputFile="${tdir}input.sol" inputFile="${tdir}input.sol"
stdin="" stdin=""
stdout=$(cat ${tdir}/output 2>/dev/null || true) stdout="$(cat ${tdir}/output 2>/dev/null || true)"
stdoutExpectationFile="$(pwd)/${tdir}/output"
args=$(cat ${tdir}/args 2>/dev/null || true) args=$(cat ${tdir}/args 2>/dev/null || true)
fi fi
exitCode=$(cat ${tdir}/exit 2>/dev/null || true) exitCode=$(cat ${tdir}/exit 2>/dev/null || true)
err=$(cat ${tdir}/err 2>/dev/null || true) err="$(cat ${tdir}/err 2>/dev/null || true)"
stderrExpectationFile="${tdir}/err"
printTask " - ${tdir}" printTask " - ${tdir}"
test_solc_behaviour "$inputFile" "$args" "$stdin" "$stdout" "$exitCode" "$err" test_solc_behaviour "$inputFile" \
"$args" \
"$stdin" \
"$stdout" \
"$exitCode" \
"$err" \
"$stdoutExpectationFile" \
"$stderrExpectationFile"
done done
) )