File: testing.bzl

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (76 lines) | stat: -rw-r--r-- 2,188 bytes parent folder | download | duplicates (6)
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
"""
Generates a side-car JUnit suite test runner class for each
input src.
"""

load("@rules_java//java:defs.bzl", "java_library", "java_test")

_template = """import org.junit.runners.Suite;
import org.junit.runner.RunWith;

@RunWith(Suite.class)
@Suite.SuiteClasses({%s})
public class %s {}
"""

def _as_classname(fname, pkg):
    path_name = [x.path for x in fname.files.to_list()][0]
    file_name = path_name.split("/")[-1]
    return ".".join([pkg, file_name.split(".")[0]]) + ".class"

def _gen_suite_impl(ctx):
    classes = ",".join(
        [_as_classname(x, ctx.attr.package_name) for x in ctx.attr.srcs],
    )
    ctx.actions.write(output = ctx.outputs.out, content = _template % (
        classes,
        ctx.attr.outname,
    ))

_gen_suite = rule(
    attrs = {
        "srcs": attr.label_list(allow_files = True),
        "package_name": attr.string(),
        "outname": attr.string(),
    },
    outputs = {"out": "%{name}.java"},
    implementation = _gen_suite_impl,
)

def junit_tests(name, srcs, data = [], deps = [], package_name = "com.google.protobuf", test_prefix = None, **kwargs):
    testlib_name = "%s_lib" % name
    java_library(
        name = testlib_name,
        srcs = srcs,
        deps = deps,
        resources = data,
        data = data,
        testonly = True,
    )
    test_names = []
    prefix = name.replace("-", "_") + "TestSuite"
    for src in srcs:
        test_name = src.rsplit("/", 1)[1].split(".")[0]
        if not test_name.endswith("Test") or test_name.startswith("Abstract"):
            continue
        if test_prefix:
            test_name = "%s%s" % (test_prefix, test_name)
        test_names = test_names + [test_name]
        suite_name = prefix + "_" + test_name
        _gen_suite(
            name = suite_name,
            srcs = [src],
            package_name = package_name,
            outname = suite_name,
        )
        java_test(
            name = test_name,
            test_class = suite_name,
            srcs = [src] + [":" + suite_name],
            deps = deps + [":%s" % testlib_name],
            **kwargs
        )
    native.test_suite(
        name = name,
        tests = test_names,
    )