Skip to content

Commit c40b57a

Browse files
the-mikedavismichaelklishin
authored andcommitted
Add Bazel test for compiling rabbitmqctl with warnings-as-errors
This test should fail when `mix compile --warnings-as-errors` gives any warnings.
1 parent 9caee7c commit c40b57a

File tree

2 files changed

+249
-0
lines changed

2 files changed

+249
-0
lines changed

deps/rabbitmq_cli/BUILD.bazel

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
load("@rules_erlang//:dialyze.bzl", "dialyze", "plt")
22
load(":rabbitmqctl.bzl", "rabbitmqctl")
33
load(":rabbitmqctl_check_formatted.bzl", "rabbitmqctl_check_formatted_test")
4+
load(":rabbitmqctl_compile_warnings_as_errors.bzl", "rabbitmqctl_compile_warnings_as_errors_test")
45
load(":rabbitmqctl_test.bzl", "rabbitmqctl_test")
56
load("//:rabbitmq_home.bzl", "rabbitmq_home")
67
load("//:rabbitmq_run.bzl", "rabbitmq_run")
@@ -97,6 +98,52 @@ test_suite(
9798
tests = ["check_formatted"],
9899
)
99100

101+
rabbitmqctl_compile_warnings_as_errors_test(
102+
name = "compile_warnings_as_errors",
103+
size = "small",
104+
srcs = [
105+
".formatter.exs",
106+
"config/config.exs",
107+
"mix.exs",
108+
] + glob([
109+
"lib/**/*.ex",
110+
"test/**/*.exs",
111+
]),
112+
data = glob(["test/fixtures/**/*"]),
113+
archives = [
114+
"@hex//:archive",
115+
],
116+
source_deps = {
117+
"@amqp//:sources": "amqp",
118+
"@csv//:sources": "csv",
119+
"@json//:sources": "json",
120+
"@temp//:sources": "temp",
121+
"@x509//:sources": "x509",
122+
},
123+
deps = [
124+
"//deps/amqp_client:erlang_app",
125+
"//deps/rabbit:erlang_app",
126+
"//deps/rabbit_common:erlang_app",
127+
"@observer_cli//:erlang_app",
128+
"@stdout_formatter//:erlang_app",
129+
],
130+
target_compatible_with = select({
131+
"@platforms//os:macos": [
132+
"@platforms//os:macos",
133+
"@elixir_config//:elixir_1_15",
134+
],
135+
"//conditions:default": [
136+
"@platforms//os:linux",
137+
"@elixir_config//:elixir_1_15",
138+
],
139+
}),
140+
)
141+
142+
test_suite(
143+
name = "rabbitmqctl_compile_warnings_as_errors",
144+
tests = ["compile_warnings_as_errors"],
145+
)
146+
100147
plt(
101148
name = "deps_plt",
102149
apps = [
Lines changed: 202 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
1+
load("@bazel_skylib//lib:shell.bzl", "shell")
2+
load(
3+
"@rules_erlang//:erlang_app_info.bzl",
4+
"ErlangAppInfo",
5+
)
6+
load(
7+
"@rules_erlang//:util.bzl",
8+
"path_join",
9+
"windows_path",
10+
)
11+
load(
12+
"@rules_erlang//private:util.bzl",
13+
"additional_file_dest_relative_path",
14+
)
15+
load(
16+
"//bazel/elixir:elixir_toolchain.bzl",
17+
"elixir_dirs",
18+
"erlang_dirs",
19+
"maybe_install_erlang",
20+
)
21+
load(
22+
":rabbitmqctl.bzl",
23+
"deps_dir_contents",
24+
)
25+
26+
def _impl(ctx):
27+
(erlang_home, _, erlang_runfiles) = erlang_dirs(ctx)
28+
(elixir_home, elixir_runfiles) = elixir_dirs(ctx, short_path = True)
29+
30+
deps_dir = ctx.label.name + "_deps"
31+
32+
deps_dir_files = deps_dir_contents(
33+
ctx,
34+
ctx.attr.deps,
35+
deps_dir,
36+
)
37+
38+
for dep, app_name in ctx.attr.source_deps.items():
39+
for src in dep.files.to_list():
40+
if not src.is_directory:
41+
rp = additional_file_dest_relative_path(dep.label, src)
42+
f = ctx.actions.declare_file(path_join(
43+
deps_dir,
44+
app_name,
45+
rp,
46+
))
47+
ctx.actions.symlink(
48+
output = f,
49+
target_file = src,
50+
)
51+
deps_dir_files.append(f)
52+
53+
package_dir = path_join(
54+
ctx.label.workspace_root,
55+
ctx.label.package,
56+
)
57+
58+
precompiled_deps = " ".join([
59+
dep[ErlangAppInfo].app_name
60+
for dep in ctx.attr.deps
61+
])
62+
63+
if not ctx.attr.is_windows:
64+
output = ctx.actions.declare_file(ctx.label.name)
65+
script = """set -euo pipefail
66+
67+
{maybe_install_erlang}
68+
69+
if [[ "{elixir_home}" == /* ]]; then
70+
ABS_ELIXIR_HOME="{elixir_home}"
71+
else
72+
ABS_ELIXIR_HOME=$PWD/{elixir_home}
73+
fi
74+
75+
export PATH="$ABS_ELIXIR_HOME"/bin:"{erlang_home}"/bin:${{PATH}}
76+
77+
export LANG="en_US.UTF-8"
78+
export LC_ALL="en_US.UTF-8"
79+
80+
INITIAL_DIR="$(pwd)"
81+
82+
if [ ! -f ${{INITIAL_DIR}}/{package_dir}/test/test_helper.exs ]; then
83+
echo "test_helper.exs cannot be found. 'bazel clean' might fix this."
84+
exit 1
85+
fi
86+
87+
cp -r ${{INITIAL_DIR}}/{package_dir}/config ${{TEST_UNDECLARED_OUTPUTS_DIR}}
88+
cp -r ${{INITIAL_DIR}}/{package_dir}/lib ${{TEST_UNDECLARED_OUTPUTS_DIR}}
89+
cp -r ${{INITIAL_DIR}}/{package_dir}/test ${{TEST_UNDECLARED_OUTPUTS_DIR}}
90+
cp ${{INITIAL_DIR}}/{package_dir}/mix.exs ${{TEST_UNDECLARED_OUTPUTS_DIR}}
91+
cp ${{INITIAL_DIR}}/{package_dir}/.formatter.exs ${{TEST_UNDECLARED_OUTPUTS_DIR}}
92+
93+
cd ${{TEST_UNDECLARED_OUTPUTS_DIR}}
94+
95+
export IS_BAZEL=true
96+
export HOME=${{PWD}}
97+
export DEPS_DIR=$TEST_SRCDIR/$TEST_WORKSPACE/{package_dir}/{deps_dir}
98+
export MIX_ENV=test
99+
export ERL_COMPILER_OPTIONS=deterministic
100+
for archive in {archives}; do
101+
"${{ABS_ELIXIR_HOME}}"/bin/mix archive.install --force $INITIAL_DIR/$archive
102+
done
103+
set -x
104+
"${{ABS_ELIXIR_HOME}}"/bin/mix deps.compile
105+
"${{ABS_ELIXIR_HOME}}"/bin/mix compile --warnings-as-errors
106+
""".format(
107+
maybe_install_erlang = maybe_install_erlang(ctx, short_path = True),
108+
erlang_home = erlang_home,
109+
elixir_home = elixir_home,
110+
package_dir = package_dir,
111+
deps_dir = deps_dir,
112+
archives = " ".join([shell.quote(a.short_path) for a in ctx.files.archives]),
113+
precompiled_deps = precompiled_deps,
114+
)
115+
else:
116+
output = ctx.actions.declare_file(ctx.label.name + ".bat")
117+
script = """@echo off
118+
:: set LANG="en_US.UTF-8"
119+
:: set LC_ALL="en_US.UTF-8"
120+
121+
set PATH="{elixir_home}\\bin";"{erlang_home}\\bin";%PATH%
122+
123+
set OUTPUTS_DIR=%TEST_UNDECLARED_OUTPUTS_DIR:/=\\%
124+
125+
:: robocopy exits non-zero when files are copied successfully
126+
:: https://social.msdn.microsoft.com/Forums/en-US/d599833c-dcea-46f5-85e9-b1f028a0fefe/robocopy-exits-with-error-code-1?forum=tfsbuild
127+
robocopy {package_dir}\\config %OUTPUTS_DIR%\\config /E /NFL /NDL /NJH /NJS /nc /ns /np
128+
robocopy {package_dir}\\lib %OUTPUTS_DIR%\\lib /E /NFL /NDL /NJH /NJS /nc /ns /np
129+
robocopy {package_dir}\\test %OUTPUTS_DIR%\\test /E /NFL /NDL /NJH /NJS /nc /ns /np
130+
copy {package_dir}\\mix.exs %OUTPUTS_DIR%\\mix.exs || goto :error
131+
copy {package_dir}\\.formatter.exs %OUTPUTS_DIR%\\.formatter.exs || goto :error
132+
133+
cd %OUTPUTS_DIR% || goto :error
134+
135+
set DEPS_DIR=%TEST_SRCDIR%/%TEST_WORKSPACE%/{package_dir}/{deps_dir}
136+
set DEPS_DIR=%DEPS_DIR:/=\\%
137+
set ERL_COMPILER_OPTIONS=deterministic
138+
set MIX_ENV=test
139+
for %%a in ({archives}) do (
140+
set ARCH=%TEST_SRCDIR%/%TEST_WORKSPACE%/%%a
141+
set ARCH=%ARCH:/=\\%
142+
"{elixir_home}\\bin\\mix" archive.install --force %ARCH% || goto :error
143+
)
144+
"{elixir_home}\\bin\\mix" deps.compile || goto :error
145+
"{elixir_home}\\bin\\mix" compile --warnings-as-errors || goto :error
146+
goto :EOF
147+
:error
148+
exit /b 1
149+
""".format(
150+
erlang_home = windows_path(erlang_home),
151+
elixir_home = windows_path(elixir_home),
152+
package_dir = windows_path(ctx.label.package),
153+
deps_dir = deps_dir,
154+
archives = " ".join([shell.quote(a.short_path) for a in ctx.files.archives]),
155+
precompiled_deps = precompiled_deps,
156+
)
157+
158+
ctx.actions.write(
159+
output = output,
160+
content = script,
161+
)
162+
163+
runfiles = ctx.runfiles(
164+
files = ctx.files.srcs + ctx.files.data + ctx.files.archives,
165+
transitive_files = depset(deps_dir_files),
166+
).merge_all([
167+
erlang_runfiles,
168+
elixir_runfiles,
169+
])
170+
171+
return [DefaultInfo(
172+
runfiles = runfiles,
173+
executable = output,
174+
)]
175+
176+
rabbitmqctl_compile_warnings_as_errors_private_test = rule(
177+
implementation = _impl,
178+
attrs = {
179+
"is_windows": attr.bool(mandatory = True),
180+
"srcs": attr.label_list(allow_files = [".ex", ".exs"]),
181+
"data": attr.label_list(allow_files = True),
182+
"deps": attr.label_list(providers = [ErlangAppInfo]),
183+
"archives": attr.label_list(
184+
allow_files = [".ez"],
185+
),
186+
"source_deps": attr.label_keyed_string_dict(),
187+
},
188+
toolchains = [
189+
"//bazel/elixir:toolchain_type",
190+
],
191+
test = True,
192+
)
193+
194+
def rabbitmqctl_compile_warnings_as_errors_test(**kwargs):
195+
rabbitmqctl_compile_warnings_as_errors_private_test(
196+
is_windows = select({
197+
"@bazel_tools//src/conditions:host_windows": True,
198+
"//conditions:default": False,
199+
}),
200+
**kwargs
201+
)
202+

0 commit comments

Comments
 (0)