Skip to content

Commit 85bf58a

Browse files
authored
Merge pull request #137 from rabbitmq/eunit-rule-target
Generate more compact eunit rules with gazelle
2 parents 70ac45a + 895e18b commit 85bf58a

File tree

9 files changed

+71
-60
lines changed

9 files changed

+71
-60
lines changed

eunit2.bzl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
load("//private:eunit.bzl", "eunit_test")
22

3-
# maybe just call this eunit_test, and rename this file?
43
def eunit(
54
name = "eunit",
65
**kwargs):

gazelle/erlang_app.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -703,7 +703,7 @@ func (erlangApp *ErlangApp) dialyzeRule() *rule.Rule {
703703
return r
704704
}
705705

706-
func (erlangApp *ErlangApp) eunitRule() *rule.Rule {
706+
func (erlangApp *ErlangApp) EunitRule() *rule.Rule {
707707
// eunit_mods is the list of source modules, plus any test module which is
708708
// not among the source modules with a "_tests" suffix appended
709709
modMap := make(map[string]string)
@@ -736,9 +736,10 @@ func (erlangApp *ErlangApp) eunitRule() *rule.Rule {
736736
}
737737

738738
eunit := rule.NewRule(eunitKind, "eunit")
739-
eunit.SetAttr("compiled_suites", compiled_suites.Values(strings.Compare))
740-
eunit.SetAttr("eunit_mods", eunit_mods.Values(strings.Compare))
741-
eunit.SetAttr("deps", []string{":test_erlang_app"})
739+
if !compiled_suites.IsEmpty() {
740+
eunit.SetAttr("compiled_suites", compiled_suites.Values(strings.Compare))
741+
}
742+
eunit.SetAttr("target", ":test_erlang_app")
742743

743744
return eunit
744745
}

gazelle/generate.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -619,7 +619,7 @@ func (erlang *erlangLang) GenerateRules(args language.GenerateArgs) language.Gen
619619
maybeAppendRule(erlangConfig, dialyzeRule, &result)
620620

621621
if erlangApp.hasTestSuites() {
622-
eunitRule := erlangApp.eunitRule()
622+
eunitRule := erlangApp.EunitRule()
623623
maybeAppendRule(erlangConfig, eunitRule, &result)
624624

625625
ctSuiteRules := erlangApp.CtSuiteRules(testDirBeamFilesRules)

gazelle/kinds.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,13 +157,13 @@ func (l *erlangLang) Kinds() map[string]rule.KindInfo {
157157
MatchAny: true,
158158
NonEmptyAttrs: map[string]bool{
159159
"compiled_suites": true,
160-
"eunit_mods": true,
160+
"target": true,
161161
"visibility": true,
162162
},
163163
SubstituteAttrs: map[string]bool{},
164164
MergeableAttrs: map[string]bool{
165165
"compiled_suites": true,
166-
"eunit_mods": true,
166+
"target": true,
167167
},
168168
ResolveAttrs: map[string]bool{
169169
"deps": true,

private/eunit.bzl

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ load(
44
"path_join",
55
"windows_path",
66
)
7-
load(":util.bzl", "erl_libs_contents")
7+
load(":util.bzl", "erl_libs_contents2")
88
load(
99
"//tools:erlang_toolchain.bzl",
1010
"erlang_dirs",
@@ -35,13 +35,37 @@ def package_relative_dirnames(package, files):
3535
dirs.append(rel)
3636
return dirs
3737

38-
def _to_atom_list(l):
39-
return "[" + ",".join(["'{}'".format(i) for i in l]) + "]"
38+
def _to_atom_list(atoms):
39+
return "[" + ",".join(["'%s'" % a for a in atoms]) + "]"
4040

4141
def _impl(ctx):
42+
if ctx.attr.eunit_mods == [] and ctx.attr.target == None:
43+
fail("Either eunit_mods or target must be set")
44+
if ctx.attr.eunit_mods != [] and ctx.attr.target != None:
45+
fail("eunit_mods and target cannot be set simultaneously")
46+
47+
deps = list(ctx.attr.deps)
48+
eunit_mods = list(ctx.attr.eunit_mods)
49+
if ctx.attr.target != None:
50+
lib_info = ctx.attr.target[ErlangAppInfo]
51+
deps.extend(lib_info.deps)
52+
for m in lib_info.beam:
53+
if m.extension == "beam":
54+
module_name = m.basename.removesuffix(".beam")
55+
if not module_name.endswith("_tests"):
56+
eunit_mods.append(module_name)
57+
for s in ctx.files.compiled_suites:
58+
module_name = s.basename.removesuffix(".beam")
59+
if not module_name.endswith("_tests"):
60+
eunit_mods.append(module_name)
61+
4262
erl_libs_dir = ctx.label.name + "_deps"
4363

44-
erl_libs_files = erl_libs_contents(ctx, dir = erl_libs_dir)
64+
erl_libs_files = erl_libs_contents2(
65+
ctx,
66+
deps = deps,
67+
dir = erl_libs_dir,
68+
)
4569

4670
package = ctx.label.package
4771

@@ -66,7 +90,9 @@ def _impl(ctx):
6690
{maybe_install_erlang}
6791
6892
export HOME=${{TEST_TMPDIR}}
69-
export ERL_LIBS=$TEST_SRCDIR/$TEST_WORKSPACE/{erl_libs_path}
93+
if [ -n "{erl_libs_path}" ]; then
94+
export ERL_LIBS=$TEST_SRCDIR/$TEST_WORKSPACE/{erl_libs_path}
95+
fi
7096
7197
{test_env}
7298
@@ -81,10 +107,10 @@ set -x
81107
""".format(
82108
maybe_install_erlang = maybe_install_erlang(ctx, short_path = True),
83109
erlang_home = erlang_home,
84-
erl_libs_path = erl_libs_path,
110+
erl_libs_path = erl_libs_path if len(erl_libs_files) > 0 else "",
85111
package = package,
86112
pa_args = " ".join(pa_args),
87-
eunit_mods_term = _to_atom_list(ctx.attr.eunit_mods),
113+
eunit_mods_term = _to_atom_list(eunit_mods),
88114
eunit_opts_term = eunit_opts_term,
89115
test_env = "\n".join(test_env_commands),
90116
)
@@ -95,9 +121,11 @@ set -x
95121

96122
output = ctx.actions.declare_file(ctx.label.name + ".bat")
97123
script = """@echo off
124+
if [{erl_libs_path}] == [] goto :env
98125
REM TEST_SRCDIR is provided by bazel but with unix directory separators
99126
set ERL_LIBS=%TEST_SRCDIR%/%TEST_WORKSPACE%/{erl_libs_path}
100127
set ERL_LIBS=%ERL_LIBS:/=\\%
128+
:env
101129
102130
{test_env}
103131
@@ -110,9 +138,9 @@ echo on
110138
""".format(
111139
package = package,
112140
erlang_home = windows_path(erlang_home),
113-
erl_libs_path = erl_libs_path,
141+
erl_libs_path = erl_libs_path if len(erl_libs_files) > 0 else "",
114142
pa_args = " ".join(pa_args),
115-
eunit_mods_term = _to_atom_list(ctx.attr.eunit_mods),
143+
eunit_mods_term = _to_atom_list(eunit_mods),
116144
eunit_opts_term = eunit_opts_term,
117145
test_env = "\n".join(test_env_commands),
118146
)
@@ -131,6 +159,8 @@ echo on
131159
for tool in ctx.attr.tools
132160
],
133161
)
162+
if ctx.attr.target != None:
163+
runfiles = runfiles.merge(ctx.attr.target[DefaultInfo].default_runfiles)
134164

135165
return [DefaultInfo(
136166
runfiles = runfiles,
@@ -143,9 +173,9 @@ eunit_test = rule(
143173
"is_windows": attr.bool(mandatory = True),
144174
"compiled_suites": attr.label_list(
145175
allow_files = [".beam"],
146-
mandatory = True,
147176
),
148-
"eunit_mods": attr.string_list(mandatory = True),
177+
"eunit_mods": attr.string_list(),
178+
"target": attr.label(providers = [ErlangAppInfo]),
149179
"eunit_opts": attr.string_list(),
150180
"data": attr.label_list(allow_files = True),
151181
"deps": attr.label_list(providers = [ErlangAppInfo]),

test/gazelle/erlang_test.go

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,10 @@ var _ = Describe("an ErlangApp", func() {
170170

171171
BeforeEach(func() {
172172
app.AddFile("src/foo.erl")
173+
app.AddFile("src/bar.erl")
173174
app.AddFile("test/foo_SUITE.erl")
174175
app.AddFile("test/foo_helper.erl")
176+
app.AddFile("test/bar_tests.erl")
175177

176178
fakeParser = fakeErlParser(map[string]*erlang.ErlAttrs{
177179
"src/foo.erl": &erlang.ErlAttrs{},
@@ -194,13 +196,26 @@ var _ = Describe("an ErlangApp", func() {
194196

195197
Describe("TestDirBeamFilesRules", func() {
196198
It("Adds runtime deps to the suite", func() {
197-
Expect(testDirRules).To(HaveLen(2))
199+
Expect(testDirRules).To(HaveLen(3))
200+
201+
Expect(testDirRules[0].Name()).To(Equal("test_bar_tests_beam"))
198202

199-
Expect(testDirRules[0].Name()).To(Equal("foo_SUITE_beam_files"))
200-
Expect(testDirRules[0].AttrStrings("beam")).To(
203+
Expect(testDirRules[1].Name()).To(Equal("foo_SUITE_beam_files"))
204+
Expect(testDirRules[1].AttrStrings("beam")).To(
201205
ContainElements("ebin/foo.beam"))
202206

203-
Expect(testDirRules[1].Name()).To(Equal("test_foo_helper_beam"))
207+
Expect(testDirRules[2].Name()).To(Equal("test_foo_helper_beam"))
208+
})
209+
})
210+
211+
Describe("EunitRule", func() {
212+
It("Adds runtime deps to the suite", func() {
213+
r := app.EunitRule()
214+
215+
Expect(r.Name()).To(Equal("eunit"))
216+
Expect(r.AttrStrings("compiled_suites")).To(
217+
ContainElements(":test_foo_helper_beam"))
218+
Expect(r.AttrString("target")).To(Equal(":test_erlang_app"))
204219
})
205220
})
206221

test/gazelle/testdata/basic_rebar/BUILD.out

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -255,15 +255,7 @@ eunit(
255255
":test_seshat_counters_server_test_beam",
256256
":test_seshat_test_beam",
257257
],
258-
eunit_mods = [
259-
"seshat",
260-
"seshat_app",
261-
"seshat_counters_server",
262-
"seshat_counters_server_test",
263-
"seshat_sup",
264-
"seshat_test",
265-
],
266-
deps = [":test_erlang_app"],
258+
target = ":test_erlang_app",
267259
)
268260

269261
assert_suites2()

test/gazelle/testdata/basic_rebar_gen_macro/BUILD.out

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -110,15 +110,7 @@ eunit(
110110
":test_seshat_counters_server_test_beam",
111111
":test_seshat_test_beam",
112112
],
113-
eunit_mods = [
114-
"seshat",
115-
"seshat_app",
116-
"seshat_counters_server",
117-
"seshat_counters_server_test",
118-
"seshat_sup",
119-
"seshat_test",
120-
],
121-
deps = [":test_erlang_app"],
113+
target = ":test_erlang_app",
122114
)
123115

124116
assert_suites2()

test/gazelle/testdata/rebar_with_ct_suites/BUILD.out

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -91,25 +91,7 @@ dialyze(
9191

9292
eunit(
9393
name = "eunit",
94-
compiled_suites = [],
95-
eunit_mods = [
96-
"osiris",
97-
"osiris_app",
98-
"osiris_bench",
99-
"osiris_counters",
100-
"osiris_log",
101-
"osiris_log_shared",
102-
"osiris_replica",
103-
"osiris_replica_reader",
104-
"osiris_replica_reader_sup",
105-
"osiris_retention",
106-
"osiris_server_sup",
107-
"osiris_sup",
108-
"osiris_tracking",
109-
"osiris_util",
110-
"osiris_writer",
111-
],
112-
deps = [":test_erlang_app"],
94+
target = ":test_erlang_app",
11395
)
11496

11597
ct_test(

0 commit comments

Comments
 (0)