From 4b576fde35028ab87f24e720ee1e01b8193012b9 Mon Sep 17 00:00:00 2001 From: Sam Gammon Date: Mon, 27 Jan 2020 22:47:12 -0800 Subject: [PATCH 1/2] Feature: Kotlin Support This changeset adds support for running tests written in Kotlin, via the same mechanism used to run Scala tests. The README is also updated with a test sample, and there is a unit test alongside `WebTestTest.java`, but written in Kotlin. --- README.md | 50 +++++++++++++++++++ WORKSPACE | 14 ++++++ javatests/com/google/testing/web/BUILD.bazel | 18 +++++++ .../com/google/testing/web/WebKotlinTest.kt | 19 +++++++ web/kotlin.bzl | 34 +++++++++++++ 5 files changed, 135 insertions(+) create mode 100644 javatests/com/google/testing/web/WebKotlinTest.kt create mode 100644 web/kotlin.bzl diff --git a/README.md b/README.md index 9c2d1a63..8e1269b2 100644 --- a/README.md +++ b/README.md @@ -68,6 +68,18 @@ maven_install( ) ``` +### Kotlin + +Follow the directions at [`bazelbuild/rules_kotlin`](https://github.com/bazelbuild/rules_kotlin) to setup the Kotlin toolchain. At the time of this writing, that is: + +```bzl +# add the `bazelbuild/rules_kotlin` repo to your WORKSPACE, and then... + +load("@io_bazel_rules_kotlin//kotlin:kotlin.bzl", "kotlin_repositories", "kt_register_toolchains") +kotlin_repositories() +kt_register_toolchains() +``` + ### Scala ```bzl @@ -169,6 +181,44 @@ public class BrowserTest { } ``` +### Example Kotlin Test + +```kotlin +package javatests.kotlinsample + +import com.google.testing.web.WebTest +import org.openqa.selenium.WebDriver + +import org.junit.Test as test +import org.junit.After as after +import org.junit.Before as before + + +class SomeTest { + private var driver: WebDriver? = null + + @before + fun createDriver() { + driver = WebTest().newWebDriverSession() + } + + @after + fun quitDriver() { + try { + driver!!.quit() + } finally { + driver = null + } + } + + @test + fun testWebDriverFromKotlin() { + val wt = WebTest() + driver?.get(wt.HTTPAddress().resolve("/healthz").toString()) + } +} +``` + ### Example Python Test ```python diff --git a/WORKSPACE b/WORKSPACE index 41ae02e3..b6159ce7 100644 --- a/WORKSPACE +++ b/WORKSPACE @@ -92,3 +92,17 @@ scala_repositories() load("@io_bazel_rules_scala//scala:toolchains.bzl", "scala_register_toolchains") scala_register_toolchains() + +rules_kotlin_version = "4512a83053489326a3643ef9d84e3e15420eb58e" +rules_kotlin_sha = "5108e1fa0ac9012a92e7a5825562284fa756f469b80020d0cd7fa03c44f6bb20" +http_archive( + name = "io_bazel_rules_kotlin", + urls = ["https://github.com/bazelbuild/rules_kotlin/archive/%s.zip" % rules_kotlin_version], + type = "zip", + strip_prefix = "rules_kotlin-%s" % rules_kotlin_version, + sha256 = rules_kotlin_sha, +) + +load("@io_bazel_rules_kotlin//kotlin:kotlin.bzl", "kotlin_repositories", "kt_register_toolchains") +kotlin_repositories() +kt_register_toolchains() diff --git a/javatests/com/google/testing/web/BUILD.bazel b/javatests/com/google/testing/web/BUILD.bazel index c1cefdc3..eed63354 100644 --- a/javatests/com/google/testing/web/BUILD.bazel +++ b/javatests/com/google/testing/web/BUILD.bazel @@ -15,6 +15,7 @@ ################################################################################ # load("//web:java.bzl", "java_web_test_suite") +load("//web:kotlin.bzl", "kotlin_web_test_suite") licenses(["notice"]) # Apache 2.0 @@ -33,3 +34,20 @@ java_web_test_suite( "@org_seleniumhq_selenium_selenium_api", ], ) + +kotlin_web_test_suite( + name = "WebKotlinTest", + srcs = ["WebKotlinTest.kt"], + test_class = "com.google.testing.web.WebKotlinTest", + browsers = [ + "//browsers:chromium-local", + "//browsers:firefox-local", + "//browsers/sauce:chrome-win10", + "//browsers/sauce:chrome-win10-connect", + ], + deps = [ + "//java/com/google/testing/web", + "@junit_junit", + "@org_seleniumhq_selenium_selenium_api", + ] +) diff --git a/javatests/com/google/testing/web/WebKotlinTest.kt b/javatests/com/google/testing/web/WebKotlinTest.kt new file mode 100644 index 00000000..aa1990e9 --- /dev/null +++ b/javatests/com/google/testing/web/WebKotlinTest.kt @@ -0,0 +1,19 @@ +package com.google.testing.web + +import com.google.testing.web.WebTest +import org.openqa.selenium.WebDriver + +import org.junit.runner.RunWith +import org.junit.runners.JUnit4 +import org.junit.Test as test + + +@RunWith(JUnit4::class) +class WebKotlinTest { + @test + fun testWebDriverFromKotlin() { + val wt = WebTest() + val driver = wt.newWebDriverSession() + driver.get(wt.HTTPAddress().resolve("/healthz").toString()) + } +} diff --git a/web/kotlin.bzl b/web/kotlin.bzl new file mode 100644 index 00000000..ea16dc55 --- /dev/null +++ b/web/kotlin.bzl @@ -0,0 +1,34 @@ +# Copyright 2016 Google Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +"""Web Test rules for Java.""" + +load("//web/internal:constants.bzl", "DEFAULT_WRAPPED_TEST_TAGS") +load("//web/internal:wrap_web_test_suite.bzl", "wrap_web_test_suite") +load("@io_bazel_rules_kotlin//kotlin:kotlin.bzl", "kt_jvm_test") + +def kotlin_web_test_suite(name, kotlin_test_tags = DEFAULT_WRAPPED_TEST_TAGS, **kwargs): + """Defines a test_suite of web_test targets that wrap a java_test target. + + Args: + name: The base name of the test. + kotlin_test_tags: A list of test tag strings to use for the scala_test + target. + **kwargs: Arguments for wrapped_web_test_suite + """ + wrap_web_test_suite( + name = name, + rule = kt_jvm_test, + wrapped_test_tags = kotlin_test_tags, + **kwargs + ) From c9672a4aa27e6fad22dbcdfc209efbb6f59574cc Mon Sep 17 00:00:00 2001 From: Sam Gammon Date: Mon, 27 Jan 2020 22:18:34 -0800 Subject: [PATCH 2/2] Fix: Scala HTTP Maven error This PR fixes and closes #407, wherein, the build breaks because it references a `rules_scala` version that accesses Maven Central via HTTP. After late Jan 2020, HTTP is no longer allowed on Maven Central, so it produces a build error for anyone who doesn't have the artifact cached already. The only change is upgrading `rules_scala` to the latest `HEAD`, which at this time is bazelbuild/rules_scala@d2e7e3b. --- WORKSPACE | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/WORKSPACE b/WORKSPACE index b6159ce7..6c4ccd28 100644 --- a/WORKSPACE +++ b/WORKSPACE @@ -78,10 +78,10 @@ py_repositories() http_archive( name = "io_bazel_rules_scala", - sha256 = "098fdf408759123e7955943deacd89ffc76e2ba55a3d112436551b60827299da", - strip_prefix = "rules_scala-1e258720c3adb66002310378128f29eb63f50dd1", + sha256 = "f4873029dcb725ae81e4a8995d89801cfba9969f25c69ecdf5a7c169be8f6ccd", + strip_prefix = "rules_scala-d2e7e3b720be8f118e3d4720fd4757abae3c7911", urls = [ - "https://github.com/bazelbuild/rules_scala/archive/1e258720c3adb66002310378128f29eb63f50dd1.tar.gz", + "https://github.com/bazelbuild/rules_scala/archive/d2e7e3b720be8f118e3d4720fd4757abae3c7911.tar.gz", ], )