1""" 2Generates a side-car JUnit suite test runner class for each 3input src. 4""" 5_template = """import org.junit.runners.Suite; 6import org.junit.runner.RunWith; 7 8@RunWith(Suite.class) 9@Suite.SuiteClasses({%s}) 10public class %s {} 11""" 12 13def _as_classname(fname, pkg): 14 path_name = [x.path for x in fname.files.to_list()][0] 15 file_name = path_name.split("/")[-1] 16 return ".".join([pkg, file_name.split(".")[0]]) + ".class" 17 18def _gen_suite_impl(ctx): 19 classes = ",".join( 20 [_as_classname(x, ctx.attr.package_name) for x in ctx.attr.srcs], 21 ) 22 ctx.actions.write(output = ctx.outputs.out, content = _template % ( 23 classes, 24 ctx.attr.outname, 25 )) 26 27_gen_suite = rule( 28 attrs = { 29 "srcs": attr.label_list(allow_files = True), 30 "package_name": attr.string(), 31 "outname": attr.string(), 32 }, 33 outputs = {"out": "%{name}.java"}, 34 implementation = _gen_suite_impl, 35) 36 37def junit_tests(name, srcs, data = [], deps = [], package_name = "com.google.protobuf", test_prefix = None, **kwargs): 38 testlib_name = "%s_lib" % name 39 native.java_library( 40 name = testlib_name, 41 srcs = srcs, 42 deps = deps, 43 resources = data, 44 data = data, 45 ) 46 test_names = [] 47 prefix = name.replace("-", "_") + "TestSuite" 48 for src in srcs: 49 test_name = src.rsplit("/", 1)[1].split(".")[0] 50 if not test_name.endswith("Test") or test_name.startswith("Abstract"): 51 continue 52 if test_prefix: 53 test_name = "%s%s" % (test_prefix, test_name) 54 test_names = test_names + [test_name] 55 suite_name = prefix + '_' + test_name 56 _gen_suite( 57 name = suite_name, 58 srcs = [src], 59 package_name = package_name, 60 outname = suite_name, 61 ) 62 native.java_test( 63 name = test_name, 64 test_class = suite_name, 65 srcs = [src] + [":" + suite_name], 66 deps = deps + [":%s" % testlib_name], 67 **kwargs 68 ) 69 native.test_suite( 70 name = name, 71 tests = test_names, 72 ) 73