diff --git a/.travis.yml b/.travis.yml index b2ac958..eded049 100644 --- a/.travis.yml +++ b/.travis.yml @@ -12,13 +12,11 @@ env: before_script: - rustc --version - cargo --version - - rustup component add rustfmt - - rustup component add clippy + - sh install_rustfmt_clippy.sh script: - - cargo fmt --all -- --check - - cargo clippy --all-targets --all-features -- -D warnings - cargo build --verbose --all - - cargo test --verbose --all + - sh run_tests.sh + - sh run_sanitizers.sh jobs: allow_failures: - rust: nightly diff --git a/install_rustfmt_clippy.sh b/install_rustfmt_clippy.sh new file mode 100644 index 0000000..5063059 --- /dev/null +++ b/install_rustfmt_clippy.sh @@ -0,0 +1,17 @@ +# https://github.com/rust-lang/rustup-components-history/blob/master/README.md +# https://github.com/rust-lang/rust-clippy/blob/27acea0a1baac6cf3ac6debfdbce04f91e15d772/.travis.yml#L40-L46 +if ! rustup component add rustfmt; then + TARGET=$(rustc -Vv | awk '/host/{print $2}') + NIGHTLY=$(curl -s "https://rust-lang.github.io/rustup-components-history/${TARGET}/rustfmt") + curl -sSL "https://static.rust-lang.org/dist/${NIGHTLY}/rustfmt-nightly-${TARGET}.tar.xz" | \ + tar -xJf - --strip-components=3 -C ~/.cargo/bin + rm -rf ~/.cargo/bin/doc +fi + +if ! rustup component add clippy; then + TARGET=$(rustc -Vv | awk '/host/{print $2}') + NIGHTLY=$(curl -s "https://rust-lang.github.io/rustup-components-history/${TARGET}/clippy") + rustup default nightly-${NIGHTLY} + rustup component add rustfmt + rustup component add clippy +fi diff --git a/run_sanitizers.sh b/run_sanitizers.sh new file mode 100644 index 0000000..12795bd --- /dev/null +++ b/run_sanitizers.sh @@ -0,0 +1,40 @@ +# The option `Z` is only accepted on the nightly compiler +# so changing to nightly toolchain by `rustup default nightly` is required. + +# See: https://github.com/rust-lang/rust/issues/39699 for more sanitizer support. + +toolchain=$(rustup default) +echo "\nUse Rust toolchain: $toolchain" + +NIGHTLY_PREFIX="nightly-" +if [ ! -z "${toolchain##$NIGHTLY_PREFIX*}" ]; then + echo "The sanitizer is only available on Rust Nightly only. Skip." + exit +fi + +# Display verbose backtrace for debugging if backtrace is unset +if [ -z "${RUST_BACKTRACE}" ]; then + export RUST_BACKTRACE=1 +fi +echo "RUST_BACKTRACE is set to ${RUST_BACKTRACE}\n" + +# {Address, Thread}Sanitizer cannot build with `criterion` crate, so `criterion` will be removed +# from the `Cargo.toml` temporarily during the sanitizer tests. +ORIGINAL_MANIFEST="Cargo_ori.toml" +mv Cargo.toml $ORIGINAL_MANIFEST +# Delete lines that contain a `criterion` from Cargo.toml. +sed '/criterion/d' $ORIGINAL_MANIFEST > Cargo.toml + +sanitizers=("address" "leak" "memory" "thread") +for san in "${sanitizers[@]}" +do + San="$(tr '[:lower:]' '[:upper:]' <<< ${san:0:1})${san:1}" + echo "\n\nRun ${San}Sanitizer\n------------------------------" + if [ $san = "leak" ]; then + echo "Skip the test that panics. Leaking memory when the program drops out abnormally is ok." + options="-- --Z unstable-options --exclude-should-panic" + fi + RUSTFLAGS="-Z sanitizer=${san}" cargo test $options +done + +mv $ORIGINAL_MANIFEST Cargo.toml \ No newline at end of file diff --git a/run_tests.sh b/run_tests.sh new file mode 100644 index 0000000..2035bcf --- /dev/null +++ b/run_tests.sh @@ -0,0 +1,14 @@ +# Display verbose backtrace for debugging if backtrace is unset +if [ -z "${RUST_BACKTRACE}" ]; then + export RUST_BACKTRACE=1 +fi +echo "RUST_BACKTRACE is set to ${RUST_BACKTRACE}\n" + +# Format check +cargo fmt --all -- --check + +# Lints check +cargo clippy --all-targets --all-features -- -D warnings + +# Regular Tests +cargo test --verbose --all