Skip to content

Commit

Permalink
[bazel] Define a macro to generate test cases
Browse files Browse the repository at this point in the history
The `gen_java_tests` macro expands all the `srcs` into
individual `java_test` targets. In order to reduce
compilation time, a `java_library` is generated. Should
you wish to use a test suite, then the original `java_test`
rule is still available and should be used.
  • Loading branch information
shs96c committed Nov 19, 2018
1 parent 82e41c4 commit 005395d
Show file tree
Hide file tree
Showing 12 changed files with 173 additions and 26 deletions.
49 changes: 49 additions & 0 deletions java/bazel-rules.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
_PREFIXES = ("com", "io", "net", "org")

def _contains(list, value):
for v in list:
if v == value:
return True
return False

def _shortName(file):
base = file.rpartition("/")[-1]
return base.rpartition(".")[0]

# We assume that package name matches directory structure, which may not
# actually be true, but is for Selenium.
def _className(file):
dir = native.package_name()

segments = dir.split('/')
idx = len(segments) - 1
for i, segment in enumerate(segments):
if _contains(_PREFIXES, segment):
idx = i
break
return ".".join(segments[idx:]) + "." + _shortName(file)

def _impl(ctx):
for src in ctx.files.srcs:
test = native.java_test(
name = _shortName(src),
test_class = _className(src),
srcs = ctx.attr.srcs,
size = ctx.attr.size,
deps = ctx.attr.deps)
print(test)

def gen_java_tests(name, srcs=[], deps=[], **kwargs):
native.java_library(
name = "%s-lib" % name,
srcs = srcs,
deps = deps)

deps.append(":%s-lib" % name)

for src in srcs:
native.java_test(
name = _shortName(src),
test_class = _className(src),
runtime_deps = deps,
**kwargs)
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ java_library(
],
visibility = [
"//java/server/src/org/openqa/selenium/grid:__subpackages__",
"//java/server/test/org/openqa/selenium/grid:__subpackages__",
],
)
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ java_library(
],
visibility = [
"//java/server/src/org/openqa/selenium/grid:__subpackages__",
"//java/server/test/org/openqa/selenium/grid:__subpackages__",
],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
java_library(
name = "remote",
srcs = glob(["*.java"]),
deps = [
"//java/server/src/org/openqa/selenium/grid/sessionmap",
],
visibility = [
"//java/server/src/org/openqa/selenium/grid:__subpackages__",
"//java/server/test/org/openqa/selenium/grid:__subpackages__",
],
)
1 change: 1 addition & 0 deletions java/server/src/org/openqa/selenium/grid/web/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ java_library(
],
visibility = [
"//java/server/src/org/openqa/selenium/grid:__subpackages__",
"//java/server/test/org/openqa/selenium/grid:__subpackages__",
],
)
1 change: 1 addition & 0 deletions java/server/src/org/openqa/selenium/injector/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ java_library(
],
visibility = [
"//java/server/src/org/openqa/selenium:__subpackages__",
"//java/server/test/org/openqa/selenium:__subpackages__",
],
)
15 changes: 15 additions & 0 deletions java/server/test/org/openqa/selenium/grid/sessionmap/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
load("//java:bazel-rules.bzl", "gen_java_tests")

gen_java_tests(
name = "sessionmap",
srcs = glob(["*.java"]),
size = "small",
deps = [
"//java/server/src/org/openqa/selenium/grid/sessionmap",
"//java/server/src/org/openqa/selenium/grid/sessionmap/local",
"//java/server/src/org/openqa/selenium/grid/sessionmap/remote",
"//java/server/test/org/openqa/selenium/grid/web:utils",
"//third_party/java/assertj",
"//third_party/java/junit",
],
)
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package org.openqa.selenium.grid.sessionmap;

import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.catchThrowableOfType;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
Expand Down Expand Up @@ -77,8 +78,8 @@ public void shouldBeAbleToRetrieveASessionUri() {

@Test
public void shouldThrowANoSuchSessionExceptionIfSessionCannotBeFound() {
catchThrowableOfType(() -> local.get(id), NoSuchSessionException.class);
catchThrowableOfType(() -> remote.get(id), NoSuchSessionException.class);
assertThatExceptionOfType(NoSuchSessionException.class).isThrownBy(() -> local.get(id));
assertThatExceptionOfType(NoSuchSessionException.class).isThrownBy(() -> remote.get(id));
}

@Test
Expand All @@ -89,8 +90,8 @@ public void shouldAllowSessionsToBeRemoved() {

remote.remove(id);

catchThrowableOfType(() -> local.get(id), NoSuchSessionException.class);
catchThrowableOfType(() -> remote.get(id), NoSuchSessionException.class);
assertThatExceptionOfType(NoSuchSessionException.class).isThrownBy(() -> local.get(id));
assertThatExceptionOfType(NoSuchSessionException.class).isThrownBy(() -> remote.get(id));
}

/**
Expand Down
27 changes: 27 additions & 0 deletions java/server/test/org/openqa/selenium/grid/web/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
load("//java:bazel-rules.bzl", "gen_java_tests")

gen_java_tests(
name = "web",
size = "small",
srcs = glob(["*Test.java"]),
deps = [
":utils",
"//java/server/src/org/openqa/selenium/injector",
"//java/server/src/org/openqa/selenium/grid/web",
"//third_party/java/assertj",
"//third_party/java/guava:guava",
"//third_party/java/junit",
],
)

java_library(
name = "utils",
srcs = glob(["*.java"], exclude = ["*Test.java"]),
deps = [
"//java/client/src/org/openqa/selenium/remote",
"//java/server/src/org/openqa/selenium/grid/web",
],
visibility = [
"//java/server/test/org/openqa/selenium/grid:__subpackages__",
],
)
24 changes: 13 additions & 11 deletions third_party/java/assertj/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
java_import(
name = 'assertj',
licenses = [
"notice", # Apache 2
],
jars = [
'assertj-core-3.11.1.jar',
],
srcjar = 'assertj-core-3.11.1-sources.jar',
visibility = [
'//java/client/test:__subpackages__',
],
name = "assertj",
tags = [
"maven_coordinates=org.assertj:assertj-core:jar:3.11.1",
],
licenses = [
"notice", # Apache 2
],
jars = ["assertj-core-3.11.1.jar"],
srcjar = "assertj-core-3.11.1-sources.jar",
visibility = [
"//java/client/test:__subpackages__",
"//java/server/test:__subpackages__",
],
)
33 changes: 33 additions & 0 deletions third_party/java/hamcrest/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
java_import(
name = "hamcrest-core",
tags = [
"maven_coordinates=org.hamcrest:hamcrest-core:jar:1.3",
],
licenses = [
"notice", # BSD
],
jars = ["hamcrest-core-1.3.jar"],
srcjar = "hamcrest-core-1.3-sources.jar",
visibility = [
"//third_party/java/junit:__pkg__",
],
)

java_import(
name = "hamcrest-library",
tags = [
"maven_coordinates=org.hamcrest:hamcrest-library:jar:1.3",
],
licenses = [
"notice", # BSD
],
jars = ["hamcrest-library-1.3.jar"],
srcjar = "hamcrest-library-1.3-sources.jar",
deps = [
":hamcrest-core",
],
visibility = [
"//java/client/test:__subpackages__",
"//java/server/test:__subpackages__",
],
)
27 changes: 16 additions & 11 deletions third_party/java/junit/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
java_import(
name = 'junit',
licenses = [
"notice", # Eclipse Public License - v 1.0
],
jars = [
'junit-4.12.jar',
],
srcjar = 'junit-4.12-sources.jar',
visibility = [
'//java/client/test:__subpackages__',
],
name = "junit",
tags = [
"maven_coordinates=junit:junit:jar:4.12",
],
licenses = [
"reciprocal", # EPL
],
jars = ["junit-4.12.jar"],
srcjar = "junit-4.12-sources.jar",
deps = [
"//third_party/java/hamcrest:hamcrest-core",
],
visibility = [
"//java/client/test:__subpackages__",
"//java/server/test:__subpackages__",
],
)

0 comments on commit 005395d

Please sign in to comment.