samedi 17 avril 2021

How to resolve a Bazel label to its jar in a custom test runner, so to be able to extract the packaged unit tests?

Using Bazel we have a macro that we call from our BUILD files to invoke our custom test runner. This macro makes a native call to java_test similar to:

native.java_test(
    name = name,
    srcs = srcs,
    resources = resources,
    use_testrunner = False,
    main_class = "com.example.CustomTestRunner",
    args = srcs,
    deps = deps,
    runtime_deps = runtime_deps + ["//bazel/tools/testng:testng_runner"],
    **kwargs
)

In our BUILD file the target to invoke the macro looks similar to:

java_custom_test(
    name = "test",
    srcs = glob(["src/test/java/**/*.java"]) + [
        ":sdk_gen_model_test",
        ":sdk_gen_controller_test",
    ],
    resources = glob(["src/test/resources/**/*"]),
    deps = [
        "@maven//:org_mockito_mockito_core",
        "@maven//:org_testng_testng",
    ],
)

Passing srcs to args in the macro, the hope is that in CustomTestRunner I can get access to all the unit test files, which is true for those identified by glob(["src/test/java/**/*.java"]).

However, in the case of ":sdk_gen_model_test" and ":sdk_gen_controller_test", these two refer to targets that generate unit tests which are packaged into a jar. Unfortunately, these two label strings are passed as are to the CustomTestRunner and I'm struggle to work out how to resolve the generated jars from them.

So the question is, how can I get Bazel to pass the list of files generated by those two targets to CustomTestRunner, or at least have some way to resolve those labels to the jars, so I can extract the files in CustomTestRunner?

Aucun commentaire:

Enregistrer un commentaire