From 681616a10366c401735c93684841442e3e4491be Mon Sep 17 00:00:00 2001 From: Mia Boloix <36868381+miaboloix@users.noreply.github.com> Date: Sun, 9 Aug 2020 12:53:37 -0400 Subject: [PATCH] Fixes part of #59: Testing module builds with both Bazel + Gradle [BLOCKED: #1482] (#1488) * Working on having one module build with bazel * Created initial app-level WORKSPACE file * Added proto_library rule to build model app module * Added newline at end of WORKSPACE file * Created macro to process proto files * Both Bazel and Gradle now build /model successfully * Fixed typo in BUILD * Added missing end of file empty lines * Added Robolectric dependencies and general build rule * Source files building in both systems - added a TODO for test files * Turned two kt_android_library rules into one * Added dependencies for test files * Created kt_android_library rule for source files * Add TODO for android_local_test rule * Added a test manifest for android_local_test() * Added Firebase dependencies and git_repository for tools_android * Refactored google-services.json, imported new dependencies * Fixed Bens nits * Fixed space in WORKSPACE comment * Got a demo test working in both Java and Kotlin * Added rules_java dependencies for protocol buffers * Added java_proto_library rules, each proto file now has its own rule * Remove unnecessary srcs attribute for android_library * Rename bzl file macro * Added Robolectric dependencies and general build rule * Source files building in both systems - added a TODO for test files * Turned two kt_android_library rules into one * Added dependencies for test files * Created kt_android_library rule for source files * Add TODO for android_local_test rule * Changed model visability, added model and util dependencies to testing * Added dependencies to testing_main_lib * Testing source files now build * Test files are building * Changed to java_lite * Added EOF newlines * Each library now has its own build rule * Utility is now one rule * Changed visibility for utility_lib * Added duplicate google-services.json file to please Gradle * Changed event_logger.proto to oppia_logger.proto * Added missing EOF newlines * Fixed bug in import statements for exploration.proto, topic.proto, and question.proto * Deleted DemoJava * CHanged rules_kotlin version * Added Firebase dependencies * Removed AsyncResultTest example * Removed AsynchResultTest example * Removed unnecessary comments in WORKSPACE and moved rules_kotlin * Added re-naming TODOs * Renamed java_proto rules java_proto_lite * Added doc comments to model/BUILD.bazel and format_import_proto_library * Added comments to WORKSPACE file * Fixed nits * Added comment to kt_android_library() rule * Fixed more nits * Added Firebase comment * Fixed merge conflicts and nits * Deleted unused dependencies from testing_main_lib * Changed library name to model * Formatted WORKSPACE comment * Formatted TODO statement * Changed format_import_proto_library comment * Added EOF newline * Created Issue and linked TODO in WORKSPACE * Edited model BUILD file top comment * Addressed nits * Fixed nits and added comments * Fixing nits * Deleted unnecessary dependencies and testing example file * Fixed manifest issues * Move google json file * Restore app version of json * Edit TODO * Deleted TODO * Changed Firebase comment * Added testing functionality from later PR * Added comments to bzl files * Added targetSDK to manifests * Changed crashlytics_lib to crashlytics * Changed testing_tests visibility * Changed type of comment ticks * Fixed nits and added TODOs * Added Args to document comment * Changed targetSDK to 29 * Formatted TODO * Moved google-services.json * Added gogle-services.json back to app * Trying to remove combined testing library * Changed targetSDK to 29 * Tests pass now with deps passed in. * Created TEST_DEPS list to avoid duplicate list of dependencies * Removed unused resources and manifest * Updated comments to reflect changes * Updated comment for testing_test * Formatted dependencies and removed unused dependency * Updated dependency list * Header for oppia_android_test now fits on one line * Edited documentation for oppia_android_test * Updated documentation for utility and testing BUILD files * Changed src to srcs * Fixed the duplicate google-services.json issue * Created Issue and added TODO * Added minSDK back * Alphabetized dependencies * Changed targetSdk to 28 --- BUILD.bazel | 0 WORKSPACE | 30 +++++++++-------- oppia_android_test.bzl | 33 ++++++++++++++++++ testing/BUILD.bazel | 50 ++++++++++++++++++++++++++++ testing/src/test/AndroidManifest.xml | 5 +++ testing/testing_test.bzl | 22 ++++++++++++ utility/BUILD.bazel | 4 --- 7 files changed, 126 insertions(+), 18 deletions(-) create mode 100644 BUILD.bazel create mode 100644 oppia_android_test.bzl create mode 100644 testing/BUILD.bazel create mode 100644 testing/src/test/AndroidManifest.xml create mode 100644 testing/testing_test.bzl diff --git a/BUILD.bazel b/BUILD.bazel new file mode 100644 index 00000000000..e69de29bb2d diff --git a/WORKSPACE b/WORKSPACE index b63c57dc6a4..3d6b42663bc 100644 --- a/WORKSPACE +++ b/WORKSPACE @@ -108,32 +108,34 @@ load("@rules_jvm_external//:defs.bzl", "maven_install") maven_install( artifacts = DAGGER_ARTIFACTS + [ - "org.robolectric:robolectric:4.3", - "org.robolectric:annotations:4.3", + "androidx.annotation:annotation:1.1.0", "androidx.appcompat:appcompat:1.0.2", "androidx.core:core-ktx:1.0.1", "androidx.lifecycle:lifecycle-livedata-ktx:2.2.0-alpha03", "androidx.test.ext:junit:1.1.1", "com.android.support:support-annotations:28.0.0", - "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.2", - "org.jetbrains.kotlinx:kotlinx-coroutines-test:1.2.2", - "org.jetbrains.kotlin:kotlin-stdlib-jdk7:jar:1.3.72", - "org.jetbrains.kotlin:kotlin-test-junit:1.3.72", - "com.google.truth:truth:0.43", + "com.caverock:androidsvg-aar:1.4", + "com.crashlytics.sdk.android:crashlytics:2.9.8", + "com.github.bumptech.glide:glide:4.11.0", "com.google.firebase:firebase-analytics:17.4.4", "com.google.firebase:firebase-crashlytics:17.1.1", - "com.crashlytics.sdk.android:crashlytics:2.9.8", + "com.google.truth:truth:0.43", "io.fabric.sdk.android:fabric:1.4.7", - "com.github.bumptech.glide:glide:4.11.0", - "com.caverock:androidsvg-aar:1.4", - "org.mockito:mockito-core:2.19.0", "junit:junit:4.12", + "org.jetbrains.kotlin:kotlin-reflect:1.3.41", + "org.jetbrains.kotlin:kotlin-stdlib-jdk7:jar:1.3.72", + "org.jetbrains.kotlin:kotlin-test-junit:1.3.72", + "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.2", + "org.jetbrains.kotlinx:kotlinx-coroutines-test:1.2.2", + "org.mockito:mockito-core:2.19.0", + "org.robolectric:annotations:4.3", + "org.robolectric:robolectric:4.3", ], repositories = DAGGER_REPOSITORIES + [ - "https://maven.google.com", - "https://repo1.maven.org/maven2", - "https://jcenter.bintray.com/", "https://bintray.com/bintray/jcenter", + "https://jcenter.bintray.com/", "https://maven.fabric.io/public", + "https://maven.google.com", + "https://repo1.maven.org/maven2", ], ) diff --git a/oppia_android_test.bzl b/oppia_android_test.bzl new file mode 100644 index 00000000000..b65be3406e1 --- /dev/null +++ b/oppia_android_test.bzl @@ -0,0 +1,33 @@ +load("@rules_jvm_external//:defs.bzl", "artifact") +load("@io_bazel_rules_kotlin//kotlin:kotlin.bzl", "kt_android_library") + +def oppia_android_test(name, srcs, test_manifest, custom_package, test_class, deps): + ''' + Creates an Oppia test target for running the specified test as an Android local test with Kotlin + support. Note that this creates an additional, internal library. + + Args: + name: str. The name of the Kotlin test file without the '.kt' suffix. + srcs: list of str. The name of the Kotlin test files to be run. + test_manifest: str. The path to the test manifest file. + custom_package: str. The module's package. Example: 'org.oppia.utility'. + test_class: The package of the src file. For example, if the src is 'FakeEventLoggerTest.kt', + then the test_class would be "org.oppia.testing.FakeEventLoggerTest". + deps: list of str. The list of dependencies needed to run the tests. + ''' + + kt_android_library( + name = name + "_lib", + custom_package = custom_package, + srcs = srcs, + deps = deps, + testonly = True, + ) + + native.android_local_test( + name = name, + custom_package = custom_package, + test_class = test_class, + manifest = test_manifest, + deps = [ ":" + name + "_lib",] + deps, + ) diff --git a/testing/BUILD.bazel b/testing/BUILD.bazel new file mode 100644 index 00000000000..c226c15fc6b --- /dev/null +++ b/testing/BUILD.bazel @@ -0,0 +1,50 @@ +# TODO(#1532): Rename file to 'BUILD' post-Gradle. +''' +This library contains fake objects used for testing as well as tests for these objects. +''' + +load("@io_bazel_rules_kotlin//kotlin:kotlin.bzl", "kt_android_library") +load("@rules_jvm_external//:defs.bzl", "artifact") +load("@dagger//:workspace_defs.bzl", "dagger_rules") +load("//:oppia_android_test.bzl", "oppia_android_test") +load("//testing:testing_test.bzl", "testing_test") + +# Library for general-purpose testing fakes. +kt_android_library( + name = "testing", + custom_package = "org.oppia.testing", + srcs = glob(["src/main/java/org/oppia/testing/*.kt"]), + manifest = "src/main/AndroidManifest.xml", + deps = [ + ":dagger", + "//utility", + artifact("org.jetbrains.kotlinx:kotlinx-coroutines-test"), + artifact("org.robolectric:robolectric"), + ], + visibility = ["//visibility:public"], + ) + +TEST_DEPS = [ + ":testing", + ":dagger", + "@robolectric//bazel:android-all", + artifact("org.jetbrains.kotlin:kotlin-reflect"), + artifact("com.google.truth:truth"), + artifact("androidx.test.ext:junit"), +] + +testing_test( + name = "FakeEventLoggerTest", + srcs = ["src/test/java/org/oppia/testing/FakeEventLoggerTest.kt"], + test_class = "org.oppia.testing.FakeEventLoggerTest", + deps = TEST_DEPS, +) + +testing_test( + name = "FakeExceptionLoggerTest", + srcs = ["src/test/java/org/oppia/testing/FakeExceptionLoggerTest.kt"], + test_class = "org.oppia.testing.FakeExceptionLoggerTest", + deps = TEST_DEPS, +) + +dagger_rules() diff --git a/testing/src/test/AndroidManifest.xml b/testing/src/test/AndroidManifest.xml new file mode 100644 index 00000000000..866b17b8a84 --- /dev/null +++ b/testing/src/test/AndroidManifest.xml @@ -0,0 +1,5 @@ + + + diff --git a/testing/testing_test.bzl b/testing/testing_test.bzl new file mode 100644 index 00000000000..fd3532adcb5 --- /dev/null +++ b/testing/testing_test.bzl @@ -0,0 +1,22 @@ +load("//:oppia_android_test.bzl", "oppia_android_test") + +def testing_test(name, srcs, test_class, deps): + ''' + Creates individual tests for test files in the testing module. + + Args: + name: str. The name of the Kotlin test file without the '.kt' suffix. + src: list of str. The list of test files to be run. + test_class: str. The package of the src file. Example: If the src is 'FakeEventLoggerTest.kt', + then the test_class would be "org.oppia.testing.FakeEventLoggerTest". + deps: list of str. The list of dependencies needed to build and run this test. + ''' + + oppia_android_test( + name = name, + srcs = srcs, + custom_package = "org.oppia.testing", + test_class = test_class, + test_manifest = "src/test/AndroidManifest.xml", + deps = deps, + ) diff --git a/utility/BUILD.bazel b/utility/BUILD.bazel index b511f7fc1de..71b1be94cef 100644 --- a/utility/BUILD.bazel +++ b/utility/BUILD.bazel @@ -1,10 +1,6 @@ # TODO(#1532): Rename file to 'BUILD' post-Gradle. ''' This library contains utilities that all other modules, minus model, depend on. -It also contains Robolectric and JUnit tests for it's utilities. -In Bazel, Kotlin source files are built into a library using the kt_android_library() rule. -Kotlin test files must be built in their own kt_android_library() rule and added as a dependency to -an android_local_test() rule which configures instrumentation tests in Bazel. ''' load("@io_bazel_rules_kotlin//kotlin:kotlin.bzl", "kt_android_library")