-
Notifications
You must be signed in to change notification settings - Fork 442
/
Copy pathrust_analyzer_test_runner.sh
executable file
·149 lines (124 loc) · 4.32 KB
/
rust_analyzer_test_runner.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#!/bin/bash
# This script creates temporary workspaces and generates `rust-project.json`
# files unique to the set of targets defined in the generated workspace.
set -euo pipefail
if [[ -z "${BUILD_WORKSPACE_DIRECTORY:-}" ]]; then
>&2 echo "This script should be run under Bazel"
exit 1
fi
PACKAGE_NAME="$1"
if [[ -z "${PACKAGE_NAME:-}" ]]; then
>&2 echo "The first argument should be the package name of the test target"
exit 1
fi
function generate_workspace() {
local workspace_root="$1"
local package_dir="$2"
local temp_dir="$(mktemp -d -t rules_rust_test_rust_analyzer-XXXXXXXXXX)"
local new_workspace="${temp_dir}/rules_rust_test_rust_analyzer"
mkdir -p "${new_workspace}"
cat <<EOF >"${new_workspace}/MODULE.bazel"
module(
name = "rules_rust_test_rust_analyzer",
version = "0.0.0",
)
bazel_dep(name = "rules_rust", version = "0.0.0")
local_path_override(
module_name = "rules_rust",
path = "${BUILD_WORKSPACE_DIRECTORY}",
)
bazel_dep(
name = "bazel_skylib",
version = "1.7.1",
)
rust = use_extension("@rules_rust//rust:extensions.bzl", "rust")
use_repo(rust, "rust_toolchains")
register_toolchains("@rust_toolchains//:all")
rust_analyzer_test = use_extension("//test/rust_analyzer/3rdparty:extensions.bzl", "rust_analyzer_test", dev_dependency = True)
use_repo(
rust_analyzer_test,
"rtra",
"rtra__serde-1.0.217",
"rtra__serde_json-1.0.135",
)
EOF
cat <<EOF >"${new_workspace}/.bazelrc"
build --keep_going
test --test_output=errors
# The 'strict' config is used to ensure extra checks are run on the test
# targets that would otherwise not run due to them being tagged as "manual".
# Note that that tag is stripped for this test.
build:strict --aspects=@rules_rust//rust:defs.bzl%rustfmt_aspect
build:strict --output_groups=+rustfmt_checks
build:strict --aspects=@rules_rust//rust:defs.bzl%rust_clippy_aspect
build:strict --output_groups=+clippy_checks
EOF
if [[ -f "${workspace_root}/.bazelversion" ]]; then
cp "${workspace_root}/.bazelversion" "${new_workspace}/.bazelversion"
fi
mkdir -p "${new_workspace}/${package_dir}"
cp -r "${workspace_root}/${package_dir}/3rdparty" "${new_workspace}/${package_dir}/"
echo "${new_workspace}"
}
function rust_analyzer_test() {
local source_dir="$1"
local workspace="$2"
local generator_arg="$3"
echo "Testing '$(basename "${source_dir}")'"
rm -f "${workspace}"/*.rs "${workspace}"/*.json "${workspace}"/*.bzl "${workspace}/BUILD.bazel" "${workspace}/BUILD.bazel-e"
cp -r "${source_dir}"/* "${workspace}"
# Drop the 'manual' tags
if [ "$(uname)" == "Darwin" ]; then
SEDOPTS=(-i '' -e)
else
SEDOPTS=(-i)
fi
sed ${SEDOPTS[@]} 's/"manual"//' "${workspace}/BUILD.bazel"
pushd "${workspace}" &>/dev/null
echo "Generating rust-project.json..."
if [[ -n "${generator_arg}" ]]; then
bazel run "@rules_rust//tools/rust_analyzer:gen_rust_project" -- "${generator_arg}"
else
bazel run "@rules_rust//tools/rust_analyzer:gen_rust_project"
fi
echo "Building..."
bazel build //...
echo "Testing..."
bazel test //...
echo "Building with Aspects..."
bazel build //... --config=strict
popd &>/dev/null
}
function cleanup() {
local workspace="$1"
pushd "${workspace}" &>/dev/null
bazel clean --async
popd &>/dev/null
rm -rf "${workspace}"
}
function run_test_suite() {
local temp_workspace="$(generate_workspace "${BUILD_WORKSPACE_DIRECTORY}" "${PACKAGE_NAME}")"
echo "Generated workspace: ${temp_workspace}"
for test_dir in "${BUILD_WORKSPACE_DIRECTORY}/${PACKAGE_NAME}"/*; do
# Skip everything but directories
if [[ ! -d "${test_dir}" ]]; then
continue
fi
# Skip the 3rdparty directory
if [[ "${test_dir}" == *"3rdparty"* ]]; then
continue
fi
# Some tests have arguments that need to be passed to the rust-project.json generator.
if [[ "${test_dir}" = "aspect_traversal_test" ]]; then
test_arg="//mylib_test"
elif [[ "${test_dir}" = "merging_crates_test" ]]; then
test_arg="//mylib_test"
else
test_arg=""
fi
rust_analyzer_test "${test_dir}" "${temp_workspace}" "${test_arg}"
done
echo "Done"
cleanup "${temp_workspace}"
}
run_test_suite