xref: /aosp_15_r20/external/bazelbuild-rules_rust/test/clippy/clippy_failure_test.sh (revision d4726bddaa87cc4778e7472feed243fa4b6c267f)
1#!/bin/bash
2
3# Runs Bazel build commands over clippy 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  >&2 echo "This script should be run under Bazel"
12  exit 1
13fi
14
15cd "${BUILD_WORKSPACE_DIRECTORY}"
16
17TEMP_DIR="$(mktemp -d -t ci-XXXXXXXXXX)"
18NEW_WORKSPACE="${TEMP_DIR}/rules_rust_test_clippy"
19
20# Executes a bazel build command and handles the return value, exiting
21# upon seeing an error.
22#
23# Takes two arguments:
24# ${1}: The expected return code.
25# ${2}: The target within "//test/clippy" to be tested.
26#
27# Any additional arguments are passed to `bazel build`.
28function check_build_result() {
29  local ret=0
30  echo -n "Testing ${2}... "
31  (bazel build ${@:3} //test/clippy:"${2}" &> /dev/null) || ret="$?" && true
32  if [[ "${ret}" -ne "${1}" ]]; then
33    >&2 echo "FAIL: Unexpected return code [saw: ${ret}, want: ${1}] building target //test/clippy:${2}"
34    >&2 echo "  Run \"bazel build //test/clippy:${2}\" to see the output"
35    exit 1
36  elif [[ $# -ge 3 ]] && [[ "${@:3}" == *"capture_clippy_output"* ]]; then
37    # Make sure that content was written to the output file
38    if [ "$(uname)" == "Darwin" ]; then
39      STATOPTS=(-f%z)
40    else
41      STATOPTS=(-c%s)
42    fi
43    if [[ $(stat ${STATOPTS[@]} "${NEW_WORKSPACE}/bazel-bin/test/clippy/${2%_clippy}.clippy.out") == 0 ]]; then
44      >&2 echo "FAIL: Output wasn't written to out file building target //test/clippy:${2}"
45      >&2 echo "  Output file: ${NEW_WORKSPACE}/bazel-bin/test/clippy/${2%_clippy}.clippy.out"
46      >&2 echo "  Run \"bazel build //test/clippy:${2}\" to see the output"
47      exit 1
48    else
49      echo "OK"
50    fi
51  else
52    echo "OK"
53  fi
54}
55
56function test_all() {
57  local -r BUILD_OK=0
58  local -r BUILD_FAILED=1
59  local -r CAPTURE_OUTPUT="--@rules_rust//:capture_clippy_output=True --@rules_rust//:error_format=json"
60  local -r BAD_CLIPPY_TOML="--@rules_rust//:clippy.toml=//too_many_args:clippy.toml"
61
62  mkdir -p "${NEW_WORKSPACE}/test/clippy" && \
63  cp -r test/clippy/* "${NEW_WORKSPACE}/test/clippy/" && \
64  cat << EOF > "${NEW_WORKSPACE}/WORKSPACE.bazel"
65workspace(name = "rules_rust_test_clippy")
66local_repository(
67    name = "rules_rust",
68    path = "${BUILD_WORKSPACE_DIRECTORY}",
69)
70load("@rules_rust//rust:repositories.bzl", "rust_repositories")
71rust_repositories()
72EOF
73
74  # Drop the 'noclippy' tags
75  if [ "$(uname)" == "Darwin" ]; then
76    SEDOPTS=(-i '' -e)
77  else
78    SEDOPTS=(-i)
79  fi
80  sed ${SEDOPTS[@]} 's/"noclippy"//' "${NEW_WORKSPACE}/test/clippy/BUILD.bazel"
81
82  pushd "${NEW_WORKSPACE}"
83
84  check_build_result $BUILD_OK ok_binary_clippy
85  check_build_result $BUILD_OK ok_library_clippy
86  check_build_result $BUILD_OK ok_shared_library_clippy
87  check_build_result $BUILD_OK ok_static_library_clippy
88  check_build_result $BUILD_OK ok_test_clippy
89  check_build_result $BUILD_OK ok_proc_macro_clippy
90  check_build_result $BUILD_FAILED bad_binary_clippy
91  check_build_result $BUILD_FAILED bad_library_clippy
92  check_build_result $BUILD_FAILED bad_shared_library_clippy
93  check_build_result $BUILD_FAILED bad_static_library_clippy
94  check_build_result $BUILD_FAILED bad_test_clippy
95  check_build_result $BUILD_FAILED bad_proc_macro_clippy
96
97  # When capturing output, clippy errors are treated as warnings and the build
98  # should succeed.
99  check_build_result $BUILD_OK bad_binary_clippy $CAPTURE_OUTPUT
100  check_build_result $BUILD_OK bad_library_clippy $CAPTURE_OUTPUT
101  check_build_result $BUILD_OK bad_shared_library_clippy $CAPTURE_OUTPUT
102  check_build_result $BUILD_OK bad_static_library_clippy $CAPTURE_OUTPUT
103  check_build_result $BUILD_OK bad_test_clippy $CAPTURE_OUTPUT
104  check_build_result $BUILD_OK bad_proc_macro_clippy $CAPTURE_OUTPUT
105  check_build_result $BUILD_OK ok_library_clippy $CAPTURE_OUTPUT --@rules_rust//:clippy_flags=-Dclippy::pedantic
106
107  # Test that we can make the ok_library_clippy fail when using an extra config file.
108  # Proves that the config file is used and overrides default settings.
109  check_build_result $BUILD_FAILED ok_library_clippy $BAD_CLIPPY_TOML
110}
111
112test_all
113