xref: /aosp_15_r20/external/bazelbuild-rules_rust/examples/bazel_env/rustfmt_test.sh (revision d4726bddaa87cc4778e7472feed243fa4b6c267f)
1#!/usr/bin/env bash
2
3# --- begin runfiles.bash initialization v3 ---
4# Copy-pasted from the Bazel Bash runfiles library v3.
5set -uo pipefail; set +e; f=bazel_tools/tools/bash/runfiles/runfiles.bash
6source "${RUNFILES_DIR:-/dev/null}/$f" 2>/dev/null || \
7  source "$(grep -sm1 "^$f " "${RUNFILES_MANIFEST_FILE:-/dev/null}" | cut -f2- -d' ')" 2>/dev/null || \
8  source "$0.runfiles/$f" 2>/dev/null || \
9  source "$(grep -sm1 "^$f " "$0.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \
10  source "$(grep -sm1 "^$f " "$0.exe.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \
11  { echo>&2 "ERROR: cannot find $f"; exit 1; }; f=; set -e
12# --- end runfiles.bash initialization v3 ---
13
14
15set -euo pipefail
16
17# MARK - Functions
18
19fail() {
20  echo >&2 "$@"
21  exit 1
22}
23
24# MARK - Args
25
26if [[ "$#" -lt 1 ]]; then
27  fail "Usage: $0 /path/to/bin <args>"
28fi
29RUSTFMT="$(rlocation "$1")"
30
31# MARK - Test
32
33# simulate bazel run with setting BUILD_WORKING_DIRECTORY
34export BUILD_WORKING_DIRECTORY="$PWD"
35
36OUTPUT="$(echo -e "fn main() {\n\n}" | "$RUSTFMT")"
37
38# without newlines in body
39EXPECTED_OUTPUT="fn main() {}"
40[[ "${OUTPUT}" == "${EXPECTED_OUTPUT}" ]] ||
41  fail 'Expected "'"${EXPECTED_OUTPUT}"'", but was' "${OUTPUT}"