Reusable Android/Kotlin/Robolectric suite macro that scans many test files and generates per-class targets

Open
#579 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 6 days

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
38/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Active
Tech stack
android, kotlin

Research direction

Start by reading the existing android_local_test and kt_android_library interfaces referenced in the issue, along with the inline macro example. Determine where a reusable Bazel macro fits in rules_android and how it should handle source discovery and target generation. Done means the supported inputs and generated per-class or per-file targets are defined and covered by tests.

Written by the indexing model from the issue text.

Description

Hello!

I'm working with rules_android and rules_kotlin together and trying to automate discovery of test files.
Unfortunately, the existing rules only let me add a single test file at a time, which doesn't scale well.

Posted it here first, https://github.com/bazel-contrib/rules_kotlin/issues/1810, but decided to move this discussion to rules_android as it looks to me like Android-specific behavior.

I'm looking for a reusable Bazel macro/rule for Android/Kotlin/Robolectric tests that takes a glob of test files, scans them, and generates one target per test class (or per file) instead of one monolithic android_local_test target.

The patterns I found for doing this:

  1. A single @RunWith(Suite::class) or monolithic android_local_test target forces everything through one JVM
    this kills parallelism, increases Robolectric memory use, and destroys caching

  2. A custom macro that collects file and generates targets

While the custom macro is probably the structurally correct Bazel solution, it's awkward to write it in every project from scratch. Is there any possibility to add it as a reusable rule?

The macro I'm using:

load("@bazel_skylib//lib:paths.bzl", "paths")
load("@rules_android//rules:rules.bzl", "android_local_test")
load("@rules_kotlin//kotlin:android.bzl", "kt_android_library")

def granular_android_test_suite(
        name,
        srcs,
        custom_package,
        manifest,
        deps = [],
        associates = [],
        tags = [],
        visibility = None):
    test_targets = []

    for src in srcs:
        base_name = paths.split_extension(paths.basename(src))[0]

        # Automatically extracts the package root whether it uses /java/ or /kotlin/
        path_segments = src.split("/")
        root_index = -1
        for root in ["kotlin", "java"]:
            if root in path_segments:
                root_index = path_segments.index(root) + 1
                break

        if root_index == -1:
            fail("Source file {} must reside under a standard 'java' or 'kotlin' directory.".format(src))

        # Reconstruct the package layout into standard dot notation
        package_segments = path_segments[root_index:-1]
        package_segments.append(base_name)
        test_class = ".".join(package_segments)

        lib_target_name = "{}_{}_lib".format(name, base_name)
        test_target_name = "{}_{}".format(name, base_name)

        kt_android_library(
            name = lib_target_name,
            srcs = [src],
            custom_package = custom_package,
            manifest = manifest,
            associates = associates,
            deps = deps,
            visibility = ["//visibility:private"],
            tags = tags,
        )

        android_local_test(
            name = test_target_name,
            test_class = test_class,
            manifest = manifest,
            custom_package = custom_package,
            tags = tags,
            visibility = visibility,
            deps = [
                ":" + lib_target_name,
                "@rules_robolectric//bazel:android-all",
            ],
        )

        test_targets.append(":" + test_target_name)

    native.test_suite(
        name = name,
        tests = test_targets,
        tags = tags,
        visibility = visibility,
    )

Related:

https://github.com/bazel-contrib/rules_kotlin/issues/375

Dominant language
Java
Stars
205
Forks
113
PR merge metrics
No merged PRs in 30d

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from bazelbuild/rules_android

All issues in bazelbuild/rules_android

Similar issues

More Java issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.