Skip to content

Commit

Permalink
Reorganize test rules under //rules package. Add compose unit test …
Browse files Browse the repository at this point in the history
…support (#117)

Fixes #117
  • Loading branch information
arunkumar9t2 committed Oct 20, 2023
1 parent 5d70380 commit dcbf0cf
Show file tree
Hide file tree
Showing 42 changed files with 176 additions and 192 deletions.
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,10 +182,10 @@ name.
#### Kotlin Unit tests

```python
load("@grab_bazel_common//tools/test:test.bzl", "grab_kt_jvm_test")
load("@grab_bazel_common//rules:defs.bzl", "kotlin_test")

grab_kt_jvm_test(
name = "binding-adapter-processor-test",
kotlin_test(
name = "kotlin_test",
srcs = glob([
"src/test/java/**/*.kt",
]),
Expand All @@ -200,17 +200,17 @@ grab_kt_jvm_test(
```

This will generate a single build target for all Kotlin files and individual `*Test` targets for each `*Test`
class. [Reference](tools/binding-adapter-bridge/BUILD.bazel).
class

#### Android Unit tests

Similarly for android unit tests, use `grab_android_local_test` to build and execute tests. [Reference](tools/test/android/BUILD.bazel).
Similarly for android unit tests, use `android_unit_test` to build and execute tests

```python
load("@grab_bazel_common//tools/test:test.bzl", "grab_android_local_test")
load("@grab_bazel_common//rules:defs.bzl", "android_unit_test")

grab_android_local_test(
name = "grab_android_local_test",
android_unit_test(
name = "android_unit_test",
srcs = glob([
"src/test/java/**/*.kt",
]),
Expand Down
3 changes: 3 additions & 0 deletions rules/android/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
load("@grab_bazel_common//tools/android_mock:andorid_mock.bzl", "mock_android_jar")

mock_android_jar()
File renamed without changes.
63 changes: 63 additions & 0 deletions rules/android/test.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
load("@grab_bazel_common//rules/test:test.bzl", "gen_test_targets")
load("@grab_bazel_common//rules/android:runtime_resources.bzl", "runtime_resources")

def android_unit_test(
name,
deps,
srcs,
additional_src_sets = [],
associates = [],
resources = [],
enable_compose = False,
**kwargs):
"""A macro that executes all android library unit tests.
Usage:
The macro creates a single build target to compile all Android unit test classes and then loads
all Test class onto a test suite for execution.
The macro adds a mocked Android jar to compile classpath similar to Android Gradle Plugin's
testOptions.unitTests.returnDefaultValues = true feature.
The macro assumes Kotlin is used and will use rules_kotlin's kt_jvm_test for execution with
mocked android.jar on the classpath.
Executing via Robolectric is currently not supported.
Args:
name: name for the test target,
srcs: the test sources under test.
src_sets: The root source set path of all test sources
deps: the build dependencies to use for the generated the android local test target
and all valid arguments that you want to pass to the android_local_test target
associates: associates target to allow access to internal members from the main Kotlin target
resources: A list of files that should be include in a Java jar.
enable_compose: Enable Jetpack Compose compiler on Kotlin sources
"""

runtime_resources_name = name + "-runtime-resources"
runtime_resources(
name = runtime_resources_name,
deps = deps,
)

if enable_compose:
deps.extend(["@grab_bazel_common//rules/android/compose:compose-plugin"])

gen_test_targets(
name = name,
srcs = srcs,
additional_src_sets = additional_src_sets,
associates = associates,
deps = deps,
test_compile_deps = [
"@grab_bazel_common//rules/android:mock_android_jar",
],
test_runtime_deps = [
":" + runtime_resources_name,
"@grab_bazel_common//rules/android:mock_android_jar",
"@com_github_jetbrains_kotlin//:kotlin-reflect",
],
resources = resources,
**kwargs
)
4 changes: 4 additions & 0 deletions rules/defs.bzl
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
load("@grab_bazel_common//rules/android:android_binary.bzl", _android_binary = "android_binary")
load("@grab_bazel_common//rules/android:android_library.bzl", _android_library = "android_library")
load("@grab_bazel_common//rules/android:android_instrumentation.bzl", _android_instrumentation_binary = "android_instrumentation_binary")
load("@grab_bazel_common//rules/android:test.bzl", _android_unit_test = "android_unit_test")
load(
"@grab_bazel_common//rules/kotlin:kotlin.bzl",
_kt_compiler_plugin = "kt_compiler_plugin",
_kt_jvm_library = "kt_jvm_library",
)
load("@grab_bazel_common//rules/kotlin:test.bzl", _kotlin_test = "kotlin_test")

# Android
android_binary = _android_binary
android_library = _android_library
android_instrumentation_binary = _android_instrumentation_binary
android_unit_test = _android_unit_test

# Kotlin
kt_jvm_library = _kt_jvm_library
kt_compiler_plugin = _kt_compiler_plugin
kotlin_test = _kotlin_test
35 changes: 35 additions & 0 deletions rules/kotlin/test.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
load("@grab_bazel_common//rules/test:test.bzl", "gen_test_targets")

def kotlin_test(
name,
srcs,
deps,
additional_src_sets = [],
associates = [],
**kwargs):
"""A macro that generates test targets to execute all Kotlin unit tests.
Usage:
The macro creates a single build target to compile all unit test classes and then creates a test target containing each Test class.
The name of the test target is derived from test class name and location of the file on disk.
Args:
name: name for the test target,
srcs: the test sources under test.
src_sets: The root source set path of all test sources
deps: the build dependencies to use for the generated the android local test target
and all valid arguments that you want to pass to the android_local_test target
associates: associates target to allow access to internal members from the main Kotlin target
"""
gen_test_targets(
name = name,
srcs = srcs,
additional_src_sets = additional_src_sets,
associates = associates,
deps = deps,
test_compile_deps = [],
test_runtime_deps = [
"@com_github_jetbrains_kotlin//:kotlin-reflect",
],
**kwargs
)
File renamed without changes.
2 changes: 2 additions & 0 deletions tools/test/multi_test.bzl → rules/test/multi_test.bzl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
load("@io_bazel_rules_kotlin//kotlin:jvm.bzl", "kt_jvm_library")
load(":runtime_resources.bzl", "runtime_resources")

# Unused
def grab_android_local_test(
name,
srcs,
Expand Down Expand Up @@ -53,6 +54,7 @@ def grab_android_local_test(
**kwargs
)

# Unused
def grab_kt_jvm_test(
name,
srcs,
Expand Down
132 changes: 20 additions & 112 deletions tools/test/test.bzl → rules/test/test.bzl
Original file line number Diff line number Diff line change
@@ -1,100 +1,32 @@
load("@io_bazel_rules_kotlin//kotlin:jvm.bzl", "kt_jvm_test")
load(":runtime_resources.bzl", "runtime_resources")

_DEFAULT_SRC_SETS = ["src/test/java", "src/test/kotlin"]

def grab_android_local_test(
name,
deps,
srcs,
additional_src_sets = [],
associates = [],
resources = [],
**kwargs):
"""A macro that executes all android library unit tests.
Usage:
The macro creates a single build target to compile all Android unit test classes and then loads
all Test class onto a test suite for execution.
The macro adds a mocked Android jar to compile classpath similar to Android Gradle Plugin's
testOptions.unitTests.returnDefaultValues = true feature.
The macro assumes Kotlin is used and will use rules_kotlin's kt_jvm_test for execution with
mocked android.jar on the classpath.
Executing via Robolectric is currently not supported.
def _unique_test_packages(packages):
"""Extract unique base package names from list of provided package names
Args:
name: name for the test target,
srcs: the test sources under test.
src_sets: The root source set path of all test sources
deps: the build dependencies to use for the generated the android local test target
and all valid arguments that you want to pass to the android_local_test target
associates: associates target to allow access to internal members from the main Kotlin target
resources: A list of files that should be include in a Java jar.
packages: List of package name in the format ["com.grab.test", "com.grab"]
"""
packages = sorted(packages)
unique_packages = []
unique_packages.append(packages[0])

runtime_resources_name = name + "-runtime-resources"
runtime_resources(
name = runtime_resources_name,
deps = deps,
)

_gen_test_targets(
name = name,
srcs = srcs,
additional_src_sets = additional_src_sets,
associates = associates,
deps = deps,
test_compile_deps = [
"@grab_bazel_common//tools/test:mockable-android-jar",
],
test_runtime_deps = [
":" + runtime_resources_name,
"@grab_bazel_common//tools/test:mockable-android-jar",
"@com_github_jetbrains_kotlin//:kotlin-reflect",
],
resources = resources,
**kwargs
)

def grab_kt_jvm_test(
name,
srcs,
deps,
additional_src_sets = [],
associates = [],
**kwargs):
"""A macro that generates test targets to execute all Kotlin unit tests.
for package in packages:
if package not in unique_packages:
not_in_unique_packages = True
for unique_package in unique_packages:
# ensure that package is not a subpackage of unique_package
if package.startswith("{}.".format(unique_package)):
not_in_unique_packages = False
break

Usage:
The macro creates a single build target to compile all unit test classes and then creates
multiple parallel test targets for each Test class. The name of the test class is derived from
test class name and location of the file disk.
if not_in_unique_packages:
unique_packages.append(package)

Args:
name: name for the test target,
srcs: the test sources under test.
src_sets: The root source set path of all test sources
deps: the build dependencies to use for the generated the android local test target
and all valid arguments that you want to pass to the android_local_test target
associates: associates target to allow access to internal members from the main Kotlin target
"""
_gen_test_targets(
name = name,
srcs = srcs,
additional_src_sets = additional_src_sets,
associates = associates,
deps = deps,
test_compile_deps = [],
test_runtime_deps = [
"@com_github_jetbrains_kotlin//:kotlin-reflect",
],
**kwargs
)

def _gen_test_targets(
return unique_packages

def gen_test_targets(
name,
srcs,
additional_src_sets,
Expand Down Expand Up @@ -169,7 +101,7 @@ EOF""".format(unique_base_packages = unique_packages_str),
kt_jvm_test(
name = test_build_target,
srcs = srcs + test_package_file,
deps = deps + test_compile_deps + ["@grab_bazel_common//tools/test-suite:test-suite"],
deps = deps + test_compile_deps + ["@grab_bazel_common//tools/test_suite:test_suite"],
associates = associates,
test_class = "com.grab.test.AllTests",
jvm_flags = [
Expand All @@ -181,27 +113,3 @@ EOF""".format(unique_base_packages = unique_packages_str),
runtime_deps = test_runtime_deps,
resources = resources,
)

def _unique_test_packages(packages):
"""Extract unique base package names from list of provided package names
Args:
packages: List of package name in the format ["com.grab.test", "com.grab"]
"""
packages = sorted(packages)
unique_packages = []
unique_packages.append(packages[0])

for package in packages:
if package not in unique_packages:
not_in_unique_packages = True
for unique_package in unique_packages:
# ensure that package is not a subpackage of unique_package
if package.startswith("{}.".format(unique_package)):
not_in_unique_packages = False
break

if not_in_unique_packages:
unique_packages.append(package)

return unique_packages
Empty file added tests/BUILD.bazel
Empty file.
12 changes: 6 additions & 6 deletions tools/test/android/BUILD.bazel → tests/android/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
load("@grab_bazel_common//tools/kotlin:android.bzl", "kt_android_library")
load("@grab_bazel_common//tools/test:test.bzl", "grab_android_local_test")
load("@grab_bazel_common//rules:defs.bzl", "android_unit_test")

kt_android_library(
name = "grab_android_local_test_lib",
name = "android_test_sample",
srcs = glob([
"src/main/java/**/*.kt",
]),
Expand All @@ -11,16 +11,16 @@ kt_android_library(
],
)

grab_android_local_test(
name = "grab_android_local_test",
android_unit_test(
name = "android_unit_test",
srcs = glob([
"src/test/java/**/*.kt",
]),
associates = [
":grab_android_local_test_lib_kt",
":android_test_sample_kt",
],
deps = [
":grab_android_local_test_lib",
":android_test_sample",
"@maven//:junit_junit",
"@maven//:org_json_json",
],
Expand Down
10 changes: 5 additions & 5 deletions tools/test/jvm/BUILD.bazel → tests/kotlin/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
load("@grab_bazel_common//tools/test:test.bzl", "grab_kt_jvm_test")
load("@io_bazel_rules_kotlin//kotlin:jvm.bzl", "kt_jvm_library")
load("@grab_bazel_common//rules:defs.bzl", "kotlin_test")

kt_jvm_library(
name = "grab_kt_jvm",
name = "kotlin_test_lib",
srcs = glob([
"src/main/java/**/*.kt",
]),
)

grab_kt_jvm_test(
name = "grab_kt_jvm_test",
kotlin_test(
name = "kotlin_test",
srcs = glob([
"src/test/java/**/*.kt",
]),
associates = [
":grab_kt_jvm",
":kotlin_test_lib",
],
deps = [
"@maven//:junit_junit",
Expand Down
Loading

0 comments on commit dcbf0cf

Please sign in to comment.