44 "path_join" ,
55 "windows_path" ,
66)
7- load (":util.bzl" , "erl_libs_contents " )
7+ load (":util.bzl" , "erl_libs_contents2 " )
88load (
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
4141def _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
6892export 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
98125REM TEST_SRCDIR is provided by bazel but with unix directory separators
99126set ERL_LIBS=%TEST_SRCDIR%/%TEST_WORKSPACE%/{erl_libs_path}
100127set 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 ]),
0 commit comments