diff --git a/scripts/grade-01.sh b/scripts/grade-01.sh index 6505296..5579366 100755 --- a/scripts/grade-01.sh +++ b/scripts/grade-01.sh @@ -18,8 +18,7 @@ RUNNERS=( ) # Lints. -cargo fmt --check -cargo clippy +run_linters || exit 1 # Executes test for each runner. for RUNNER in "${RUNNERS[@]}"; do diff --git a/scripts/grade-02.sh b/scripts/grade-02.sh index cc6ada1..570f0e4 100755 --- a/scripts/grade-02.sh +++ b/scripts/grade-02.sh @@ -18,8 +18,7 @@ RUNNERS=( ) # Lints. -cargo fmt --check -cargo clippy +run_linters || exit 1 # Executes test for each runner. for RUNNER in "${RUNNERS[@]}"; do diff --git a/scripts/grade-03.sh b/scripts/grade-03.sh index 4223bcb..bce3607 100755 --- a/scripts/grade-03.sh +++ b/scripts/grade-03.sh @@ -18,8 +18,7 @@ RUNNERS=( ) # Lints. -cargo fmt --check -cargo clippy +run_linters || exit 1 # Executes test for each runner. for RUNNER in "${RUNNERS[@]}"; do diff --git a/scripts/grade-06.sh b/scripts/grade-06.sh index 02ab6f0..d139dcb 100755 --- a/scripts/grade-06.sh +++ b/scripts/grade-06.sh @@ -18,8 +18,7 @@ RUNNERS=( ) # Lints. -cargo fmt --check -cargo clippy +run_linters || exit 1 # Executes test for each runner. for RUNNER in "${RUNNERS[@]}"; do diff --git a/scripts/grade-utils.sh b/scripts/grade-utils.sh index 4bc2b1e..8244ee5 100755 --- a/scripts/grade-utils.sh +++ b/scripts/grade-utils.sh @@ -10,12 +10,12 @@ rustup toolchain update stable nightly echo_err() { - echo -e "\033[0;31m\033[1m$@\033[0m" 1>&2 + echo "$@" 1>&2 } export -f echo_err # check_diff FILE TEST_LINES_FROM_TAIL -# Abort if tests are modified. +# Abort if "--lib" tests are modified. # Uses global variable TEMPLATE_REV. check_diff() { local FILE=$1 @@ -25,13 +25,26 @@ check_diff() { } export -f check_diff +# Returns non-zero exit code if any of the linters have failed. +run_linters() { + cargo fmt -- --check + local FMT_ERR=$? + cargo clippy -- -D warnings + local CLIPPY_ERR=$? + [ "$FMT_ERR" -ne 0 ] && echo_err 'Please format your code with `cargo fmt` first.' + [ "$CLIPPY_ERR" -ne 0 ] && echo_err 'Please fix the issues from `cargo clippy` first.' + return $(( FMT_ERR || CLIPPY_ERR )) +} +export -f run_linters + # usage: cargo_asan [SUBCOMMAND] [OPTIONS] [-- ...] # example: cargo_asan test --release TEST_NAME -- --skip SKIPPED +# NOTE: sanitizer documentation at https://doc.rust-lang.org/beta/unstable-book/compiler-flags/sanitizer.html cargo_asan() { local SUBCOMMAND=$1; shift RUSTFLAGS="-Z sanitizer=address" \ RUSTDOCFLAGS="-Z sanitizer=address" \ - cargo +nightly $SUBCOMMAND --target x86_64-unknown-linux-gnu $@ + cargo +nightly $SUBCOMMAND -Z build-std --target x86_64-unknown-linux-gnu "$@" } export -f cargo_asan @@ -41,24 +54,32 @@ cargo_tsan() { TSAN_OPTIONS="suppressions=suppress_tsan.txt" \ RUSTDOCFLAGS="-Z sanitizer=thread" \ RUST_TEST_THREADS=1 \ - cargo +nightly $SUBCOMMAND --target x86_64-unknown-linux-gnu $@ + cargo +nightly $SUBCOMMAND -Z build-std --target x86_64-unknown-linux-gnu "$@" } export -f cargo_tsan # usage: _run_tests_with CARGO [OPTIONS] # example: _run_tests_with cargo_tsan --release -# echos number of failed tests -# Uses global variable TESTS, TIMEOUT -# [OPTIONS] must not contain " -- " (cargo options only) +# Echos number of failed tests to stdout. +# Echos error message to stderr. +# Uses global variables TESTS, TIMEOUT. +# [OPTIONS] must not contain " -- " (cargo options only). _run_tests_with() { local CARGO=$1; shift - $CARGO test --no-run $@ &>/dev/null \ - || (echo_err "Build failed!"; exit 1) + local MSGS # https://mywiki.wooledge.org/BashPitfalls#local_var.3D.24.28cmd.29 + MSGS=$($CARGO test --no-run "$@" 2>&1) + if [ $? -ne 0 ]; then + echo_err "Build failed! Error message:" + echo_err "${MSGS}" + echo_err "--------------------------------------------------------------------------------" + echo ${#TESTS[@]} # failed all tests + exit 1 + fi local FAILED=0 for TEST in "${TESTS[@]}"; do - local TEST_CMD="$CARGO test $@ $TEST" - timeout ${TIMEOUT:-10s} bash -c "$TEST_CMD 2>/dev/null" 1>&2 + local TEST_CMD="$CARGO test $* $TEST" + timeout ${TIMEOUT:-20s} bash -c "$TEST_CMD 2>/dev/null" 1>&2 case $? in 0) ;; 124) echo_err "Test timed out: $TEST_CMD"; FAILED=$((FAILED + 1));; @@ -73,6 +94,6 @@ _run_tests_with() { run_tests() { # "cargo --release" should be split into "cargo" and "--release" local IFS=' ' - echo $(_run_tests_with $RUNNER) + _run_tests_with $RUNNER } export -f run_tests