Skip to content

Instantly share code, notes, and snippets.

@hsyed
Created August 15, 2017 19:49
Show Gist options
  • Save hsyed/77e8e63e5d3ba8d91dfa566c15a271fa to your computer and use it in GitHub Desktop.
Save hsyed/77e8e63e5d3ba8d91dfa566c15a271fa to your computer and use it in GitHub Desktop.
def canonacalize_package_name(prefix, pkg_name):
return pkg_name.replace(prefix, "").replace("/", ".")
def junit_suite_test(name, srcs, deps, prefix = "src/test/java", size="small", resources=[], classpath_resources=[], jvm_flags=[], tags=[], data=[]):
tests = []
if not prefix.endswith("/"):
prefix = prefix + "/"
for src in srcs:
if src.endswith("Test.java"):
tests += [canonacalize_package_name(prefix, src.replace(".java", ".class"))]
package = canonacalize_package_name(prefix, PACKAGE_NAME).replace("-", "_")
native.genrule(
name = name + "-AllTests-gen",
outs = ["AllTests.java"],
cmd = """
cat <<EOF >> $@
package %s;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
@RunWith(Suite.class)
@Suite.SuiteClasses({
%s
})
public class AllTests {
}
EOF
""" % (package, ",\n ".join(tests))
)
native.java_test(
name = name,
srcs = srcs + ["AllTests.java"],
test_class = package + ".AllTests",
resources = resources,
classpath_resources = classpath_resources,
data = data,
size = size,
tags = tags,
jvm_flags = jvm_flags,
deps = deps
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment