From 3ff29b93f09ed633377b4597df3bbaa314f46458 Mon Sep 17 00:00:00 2001 From: David Date: Thu, 27 Aug 2020 09:25:34 -0700 Subject: [PATCH 1/3] Support for monorepos with multiple projects (#51) --- groovy/groovy.bzl | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/groovy/groovy.bzl b/groovy/groovy.bzl index 3599769..e44b08d 100644 --- a/groovy/groovy.bzl +++ b/groovy/groovy.bzl @@ -195,7 +195,10 @@ def groovy_binary(name, main_class, srcs = [], testonly = 0, deps = [], **kwargs **kwargs ) -def path_to_class(path): +def path_to_class(path, project_path): + if path.startswith(project_path): + path = path[len(project_path):] + if path.startswith("src/test/groovy/"): return path[len("src/test/groovy/"):path.index(".groovy")].replace("/", ".") elif path.startswith("src/test/java/"): @@ -222,7 +225,8 @@ def _groovy_test_impl(ctx): ) # Infer a class name from each src file - classes = [path_to_class(src.path) for src in ctx.files.srcs] + project_path = ctx.attr.generator_location[:ctx.attr.generator_location.index("BUILD:")] + classes = [path_to_class(src.path, project_path) for src in ctx.files.srcs] # Write a file that executes JUnit on the inferred classes cmd = "external/local_jdk/bin/java %s -cp %s org.junit.runner.JUnitCore %s\n" % ( From 4ac12c5860b7cb1cc7a417cb24925eeda633af1e Mon Sep 17 00:00:00 2001 From: David Date: Thu, 17 Sep 2020 12:45:49 -0700 Subject: [PATCH 2/3] Fix jdk paths --- groovy/groovy.bzl | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/groovy/groovy.bzl b/groovy/groovy.bzl index e44b08d..6e494f3 100644 --- a/groovy/groovy.bzl +++ b/groovy/groovy.bzl @@ -12,6 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. +load("@bazel_skylib//lib:paths.bzl", "paths") load("@rules_java//java:defs.bzl", "java_binary", "java_import", "java_library") def _groovy_jar_impl(ctx): @@ -206,6 +207,19 @@ def path_to_class(path, project_path): else: fail("groovy_test sources must be under src/test/java or src/test/groovy") +def runfiles_root(ctx): + return "${TEST_SRCDIR}/%s" % ctx.workspace_name + +def _java_bin(ctx): + java_path = str(ctx.attr._jdk[java_common.JavaRuntimeInfo].java_home) + + if paths.is_absolute(java_path): + javabin = java_path + else: + runfiles_root_var = runfiles_root(ctx) + javabin = "%s/%s" % (runfiles_root_var, java_path) + return javabin + "/bin/java" + def _groovy_test_impl(ctx): # Collect jars from the Groovy sdk groovy_sdk_jars = [ @@ -225,15 +239,20 @@ def _groovy_test_impl(ctx): ) # Infer a class name from each src file + java_bin = _java_bin(ctx) + project_path = ctx.attr.generator_location[:ctx.attr.generator_location.index("BUILD:")] classes = [path_to_class(src.path, project_path) for src in ctx.files.srcs] - + # Write a file that executes JUnit on the inferred classes - cmd = "external/local_jdk/bin/java %s -cp %s org.junit.runner.JUnitCore %s\n" % ( + cmd = "%s %s -cp %s org.junit.runner.JUnitCore %s\n" % ( + java_bin, " ".join(ctx.attr.jvm_flags), ":".join([dep.short_path for dep in all_deps.to_list()]), " ".join(classes), ) + + # Return all dependencies needed to run the tests ctx.actions.write( output = ctx.outputs.executable, content = cmd, From 209a359de6a5aad79874ce6a4d53b895ce78ad95 Mon Sep 17 00:00:00 2001 From: David Date: Fri, 18 Sep 2020 11:44:48 -0700 Subject: [PATCH 3/3] Add an option to not include spock and junit as external deps --- groovy/groovy.bzl | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/groovy/groovy.bzl b/groovy/groovy.bzl index 6e494f3..ba2aaed 100644 --- a/groovy/groovy.bzl +++ b/groovy/groovy.bzl @@ -374,15 +374,19 @@ def spock_test( resources = [], jvm_flags = [], size = "small", - tags = []): + tags = [], + include_external_deps = 1): groovy_lib_deps = deps + [ "//external:junit", "//external:spock", ] - test_deps = deps + [ - "//external:junit", - "//external:spock", - ] + + test_deps = deps + if include_external_deps: + test_deps = deps + [ + "//external:junit", + "//external:spock", + ] if len(specs) == 0: fail("Must provide at least one file in specs") @@ -393,10 +397,7 @@ def spock_test( name = name + "-javalib", srcs = java_srcs, testonly = 1, - deps = deps + [ - "//external:junit", - "//external:spock", - ], + deps = test_deps, ) groovy_lib_deps += [name + "-javalib"] test_deps += [name + "-javalib"]