1#!/bin/bash 2 3# Runs Bazel build commands over rustfmt rules, where some are expected 4# to fail. 5# 6# Can be run from anywhere within the rules_rust workspace. 7 8set -euo pipefail 9 10if [[ -z "${BUILD_WORKSPACE_DIRECTORY:-}" ]]; then 11 echo "This script should be run under Bazel" 12 exit 1 13fi 14 15cd "${BUILD_WORKSPACE_DIRECTORY}" 16 17# Executes a bazel build command and handles the return value, exiting 18# upon seeing an error. 19# 20# Takes two arguments: 21# ${1}: The expected return code. 22# ${2}: The target within "//test/rustfmt" to be tested. 23function check_build_result() { 24 local ret=0 25 echo -n "Testing ${2}... " 26 (bazel test //test/rustfmt:"${2}") || ret="$?" && true 27 if [[ "${ret}" -ne "${1}" ]]; then 28 >&2 echo "FAIL: Unexpected return code [saw: ${ret}, want: ${1}] building target //test/rustfmt:${2}" 29 >&2 echo " Run \"bazel test //test/rustfmt:${2}\" to see the output" 30 exit 1 31 else 32 echo "OK" 33 fi 34} 35 36function test_all_and_apply() { 37 local -r TEST_OK=0 38 local -r TEST_FAILED=3 39 local -r VARIANTS=(rust_binary rust_library rust_shared_library rust_static_library) 40 41 temp_dir="$(mktemp -d -t ci-XXXXXXXXXX)" 42 new_workspace="${temp_dir}/rules_rust_test_rustfmt" 43 44 mkdir -p "${new_workspace}/test/rustfmt" && \ 45 cp -r test/rustfmt/* "${new_workspace}/test/rustfmt/" && \ 46 cat << EOF > "${new_workspace}/WORKSPACE.bazel" 47workspace(name = "rules_rust_test_rustfmt") 48local_repository( 49 name = "rules_rust", 50 path = "${BUILD_WORKSPACE_DIRECTORY}", 51) 52load("@rules_rust//rust:repositories.bzl", "rust_repositories") 53rust_repositories() 54EOF 55 # See github.com/bazelbuild/rules_rust/issues/2317. 56 echo "build --noincompatible_sandbox_hermetic_tmp" > "${new_workspace}/.bazelrc" 57 58 # Drop the 'norustfmt' tags 59 if [ "$(uname)" == "Darwin" ]; then 60 SEDOPTS=(-i '' -e) 61 else 62 SEDOPTS=(-i) 63 fi 64 sed ${SEDOPTS[@]} 's/"norustfmt"//' "${new_workspace}/test/rustfmt/rustfmt_integration_test_suite.bzl" 65 sed ${SEDOPTS[@]} 's/"manual"//' "${new_workspace}/test/rustfmt/rustfmt_integration_test_suite.bzl" 66 67 pushd "${new_workspace}" 68 69 for variant in ${VARIANTS[@]}; do 70 check_build_result $TEST_FAILED ${variant}_unformatted_2015_test 71 check_build_result $TEST_FAILED ${variant}_unformatted_2018_test 72 check_build_result $TEST_OK ${variant}_formatted_2015_test 73 check_build_result $TEST_OK ${variant}_formatted_2018_test 74 check_build_result $TEST_OK ${variant}_generated_test 75 done 76 77 # Format a specific target 78 for variant in ${VARIANTS[@]}; do 79 bazel run @rules_rust//tools/rustfmt -- //test/rustfmt:${variant}_unformatted_2018 80 done 81 82 for variant in ${VARIANTS[@]}; do 83 check_build_result $TEST_FAILED ${variant}_unformatted_2015_test 84 check_build_result $TEST_OK ${variant}_unformatted_2018_test 85 check_build_result $TEST_OK ${variant}_formatted_2015_test 86 check_build_result $TEST_OK ${variant}_formatted_2018_test 87 check_build_result $TEST_OK ${variant}_generated_test 88 done 89 90 # Format all targets 91 bazel run @rules_rust//tools/rustfmt --@rules_rust//:rustfmt.toml=//test/rustfmt:test_rustfmt.toml 92 93 # Ensure all tests pass 94 check_build_result $TEST_OK "*" 95 96 popd 97 98 rm -rf "${temp_dir}" 99} 100 101test_all_and_apply 102