Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move scala_test/scala_test_suite to their own file #815

Merged
merged 5 commits into from
Aug 18, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions scala/private/common.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -162,3 +162,13 @@ def _provider_of_dependency_label_of(dependency, path):
return dependency[JarsToLabelsInfo].jars_to_labels.get(path)
else:
return None

def sanitize_string_for_usage(s):
res_array = []
for idx in range(len(s)):
c = s[idx]
if c.isalnum() or c == ".":
res_array.append(c)
else:
res_array.append("_")
return "".join(res_array)
14 changes: 14 additions & 0 deletions scala/private/common_attributes.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,17 @@ resolve_deps = {
allow_files = False,
),
}

test_resolve_deps = {
long-stripe marked this conversation as resolved.
Show resolved Hide resolved
"_scala_toolchain": attr.label_list(
default = [
Label(
"//external:io_bazel_rules_scala/dependency/scala/scala_library",
),
Label(
"//external:io_bazel_rules_scala/dependency/scalatest/scalatest",
),
],
allow_files = False,
),
}
130 changes: 5 additions & 125 deletions scala/private/rule_impls.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ touch {statsfile}
arguments = [],
)

def _expand_location(ctx, flags):
def expand_location(ctx, flags):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we move every public method to common.bzl or maybe interim_common.bzl?
It's somewhat surprising to find a public method in private/rule_impls.bzl

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I plan to eventually, basically the common functions in rule_impls.bzl will get moved to common.bzl or some other sort of private lib organization. I didn't in these PRs to reduce the size of the incidental diffs during the refactoring process.

if hasattr(ctx.attr, "data"):
data = ctx.attr.data
else:
Expand All @@ -138,7 +138,7 @@ def _join_path(args, sep = ","):
return sep.join([f.path for f in args])

# Return the first non-empty arg. If all are empty, return the last.
def _first_non_empty(*args):
def first_non_empty(*args):
for arg in args:
if arg:
return arg
Expand Down Expand Up @@ -307,7 +307,7 @@ StatsfileOutput: {statsfile_output}

# scalac_jvm_flags passed in on the target override scalac_jvm_flags passed in on the
# toolchain
final_scalac_jvm_flags = _first_non_empty(
final_scalac_jvm_flags = first_non_empty(
scalac_jvm_flags,
ctx.toolchains["@io_bazel_rules_scala//scala:toolchain_type"].scalac_jvm_flags
)
Expand All @@ -330,7 +330,7 @@ StatsfileOutput: {statsfile_output}
# consume the flags on startup.
arguments = [
"--jvm_flag=%s" % f
for f in _expand_location(ctx, final_scalac_jvm_flags)
for f in expand_location(ctx, final_scalac_jvm_flags)
] + ["@" + argfile.path],
)

Expand Down Expand Up @@ -372,7 +372,7 @@ def try_to_compile_java_jar(
source_jars = all_srcjars.to_list(),
source_files = java_srcs,
output = full_java_jar,
javac_opts = _expand_location(
javac_opts = expand_location(
ctx,
ctx.attr.javacopts + ctx.attr.javac_jvm_flags +
java_common.default_javac_opts(
Expand Down Expand Up @@ -1079,126 +1079,6 @@ trap finish EXIT

return out

def _scala_test_flags(ctx):
# output report test duration
flags = "-oD"
if ctx.attr.full_stacktraces:
flags += "F"
else:
flags += "S"
if not ctx.attr.colors:
flags += "W"
return flags

def scala_test_impl(ctx):
if len(ctx.attr.suites) != 0:
print("suites attribute is deprecated. All scalatest test suites are run")

scalac_provider = get_scalac_provider(ctx)

unused_dependency_checker_mode = get_unused_dependency_checker_mode(ctx)
unused_dependency_checker_ignored_targets = [
target.label
for target in scalac_provider.default_classpath +
ctx.attr.unused_dependency_checker_ignored_targets
]
unused_dependency_checker_is_off = unused_dependency_checker_mode == "off"

scalatest_base_classpath = scalac_provider.default_classpath + [ctx.attr._scalatest]
jars = collect_jars_from_common_ctx(
ctx,
scalatest_base_classpath,
extra_runtime_deps = [
ctx.attr._scalatest_reporter,
ctx.attr._scalatest_runner,
],
unused_dependency_checker_is_off = unused_dependency_checker_is_off,
)
(
cjars,
transitive_rjars,
transitive_compile_jars,
jars_to_labels,
) = (
jars.compile_jars,
jars.transitive_runtime_jars,
jars.transitive_compile_jars,
jars.jars2labels,
)

args = "\n".join([
"-R",
ctx.outputs.jar.short_path,
_scala_test_flags(ctx),
"-C",
"io.bazel.rules.scala.JUnitXmlReporter",
])

argsFile = ctx.actions.declare_file("%s.args" % ctx.label.name)
ctx.actions.write(argsFile, args)

executable = declare_executable(ctx)

wrapper = write_java_wrapper(ctx, "", "")
out = scala_binary_common(
ctx,
executable,
cjars,
transitive_rjars,
transitive_compile_jars,
jars_to_labels,
wrapper,
unused_dependency_checker_ignored_targets =
unused_dependency_checker_ignored_targets,
unused_dependency_checker_mode = unused_dependency_checker_mode,
runfiles_ext = [argsFile],
deps_providers = jars.deps_providers,
)

rjars = out.transitive_rjars

coverage_runfiles = []
if ctx.configuration.coverage_enabled and _coverage_replacements_provider.is_enabled(ctx):
coverage_replacements = _coverage_replacements_provider.from_ctx(
ctx,
base = out.coverage.replacements,
).replacements

rjars = depset([
coverage_replacements[jar] if jar in coverage_replacements else jar
for jar in rjars.to_list()
])
coverage_runfiles = ctx.files._jacocorunner + ctx.files._lcov_merger + coverage_replacements.values()

# jvm_flags passed in on the target override scala_test_jvm_flags passed in on the
# toolchain
final_jvm_flags = _first_non_empty(
ctx.attr.jvm_flags,
ctx.toolchains["@io_bazel_rules_scala//scala:toolchain_type"].scala_test_jvm_flags
)

coverage_runfiles.extend(write_executable(
ctx = ctx,
executable = executable,
jvm_flags = [
"-DRULES_SCALA_MAIN_WS_NAME=%s" % ctx.workspace_name,
"-DRULES_SCALA_ARGS_FILE=%s" % argsFile.short_path,
] + _expand_location(ctx, final_jvm_flags),
main_class = ctx.attr.main_class,
rjars = rjars,
use_jacoco = ctx.configuration.coverage_enabled,
wrapper = wrapper,
))

return struct(
executable = executable,
files = out.files,
instrumented_files = out.instrumented_files,
providers = out.providers,
runfiles = ctx.runfiles(coverage_runfiles, transitive_files = out.runfiles.files),
scala = out.scala,
)

def _gen_test_suite_flags_based_on_prefixes_and_suffixes(ctx, archives):
return struct(
archiveFlag = "-Dbazel.discover.classes.archives.file.paths=%s" %
Expand Down
Loading