grader updates from CS431 (#11)

* If any of linters fail, don't run the tests.
* If build fails, echo the error message to stderr.
* Fix sanitizer options.
* Don't use colored output (it messes up gg log).
* $@ → "$@"
* $@ → $* in string
This commit is contained in:
Jaehwang Jung
2022-09-25 12:11:24 +09:00
committed by Seungmin Jeon
parent e91413b506
commit 7913e6774e
5 changed files with 37 additions and 20 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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] [-- <args>...]
# 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