Skip to content

Add test_dependency_versions, update test_runner #1729

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
19 changes: 19 additions & 0 deletions .bazelci/presubmit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,22 @@ tasks:
- echo "build --tool_java_language_version=21" >> .bazelrc
- echo "build --tool_java_runtime_version=21" >> .bazelrc
- "./test_rules_scala.sh"
dependency_versions_linux:
name: "./test_dependency_versions"
platform: ubuntu2004
shell_commands:
- "./test_dependency_versions.sh"
dependency_versions_macos:
name: "./test_dependency_versions"
platform: macos
shell_commands:
- "./test_dependency_versions.sh"
dependency_versions_windows:
name: "./test_dependency_versions"
platform: windows
environment:
MSYS2_ARG_CONV_EXCL: "*"
batch_commands:
- "set PATH=/usr/bin;%PATH%" #Make sure bash uses msys commands over windows commands. (i.e. find).
- "bash -lc \"pacman --noconfirm --needed -S libxml2\"" #tests require xmllint
- "bash ./test_dependency_versions.sh" # script removes ./ from BASH_SOURCE
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,8 @@ by default](#protoc-msvc) section below for details.
#### Minimum dependency versions

These are the minimum dependency versions required to enable the precompiled
protocol compiler toolchain.
protocol compiler toolchain. These are validated by
[`test_dependency_versions.sh`](./test/shell/test_dependency_versions.sh).

Note that `rules_java` can be as low as 8.3.0, compared to `rules_java` 8.5.0
specified in [Compatible Bazel versions](#compatible-bazel-versions).
Expand Down
129 changes: 129 additions & 0 deletions deps/test/BUILD.bazel.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
"""Test targets to ensure dependency version compatibility.

Copied and adapted targets from the main repo as noted.
"""
load(
":defs.bzl",
"default_outputs_test",
"scalafmt_scala_test",
"scrooge_transitive_outputs_test",
)
load("@rules_proto//proto:defs.bzl", "proto_library")
load("@rules_scala//jmh:jmh.bzl", "scala_benchmark_jmh")
load("@rules_scala//scala/scalafmt:phase_scalafmt_ext.bzl", "ext_scalafmt")
load("@rules_scala//scala:advanced_usage/scala.bzl", "make_scala_test")
load(
"@rules_scala//scala:scala.bzl",
"scala_binary",
"scala_doc",
"scala_junit_test",
"scala_library",
"scala_specs2_junit_test",
"scala_test",
)
load("@rules_scala//scala_proto:scala_proto.bzl", "scala_proto_library")
load("@rules_scala//thrift:thrift.bzl", "thrift_library")
load(
"@rules_scala//twitter_scrooge:twitter_scrooge.bzl",
"scrooge_java_library",
"scrooge_scala_library",
)

# From: `test/BUILD`
scala_binary(
name = "ScalaBinary",
srcs = ["ScalaBinary.scala"],
main_class = "scalarules.test.ScalaBinary",
deps = [
":HelloLib",
],
)

scala_library(
name = "HelloLib",
srcs = ["HelloLib.scala"],
)

scala_doc(
name = "ScalaDoc",
deps = [":HelloLib"],
)

# From: `examples/testing/multi_frameworks_toolchain/example/BUILD`
scala_test(
name = "scalatest_example",
srcs = ["ScalaTestExampleTest.scala"],
)

scala_specs2_junit_test(
name = "specs2_example",
srcs = ["Specs2ExampleTest.scala"],
suffixes = ["Test"],
)

# Manufactured based on `docs/phase_scalafmt.md` and `test/scalafmt/BUILD`.
scalafmt_scala_test(
name = "ScalafmtTest",
srcs = ["ScalaTestExampleTest.scala"],
format = True,
)

# From: `test/proto/BUILD`
proto_library(
name = "standalone_proto",
srcs = ["standalone.proto"],
)

scala_proto_library(
name = "standalone_scala_proto",
deps = [":standalone_proto"],
)

default_outputs_test(
name = "standalone_scala_proto_outs_test",
expected_outs = [
"standalone_proto_scalapb-src.jar",
"standalone_proto_scalapb.jar",
],
target_under_test = ":standalone_scala_proto",
)

# From: `test/jmh/BUILD`
scala_benchmark_jmh(
name = "test_benchmark",
srcs = ["TestBenchmark.scala"],
data = ["data.txt"],
deps = ["@rules_scala//test/jmh:add_numbers"],
)

# From: `test/src/main/scala/scalarules/test/twitter_scrooge/BUILD`

thrift_library(
name = "thrift3",
srcs = ["Thrift3.thrift"],
visibility = ["//visibility:public"],
)

scrooge_scala_library(
name = "scrooge3",
visibility = ["//visibility:public"],
deps = [":thrift3"],
)

scrooge_java_library(
name = "scrooge3_java",
visibility = ["//visibility:public"],
deps = [":thrift3"],
)

scrooge_transitive_outputs_test(
name = "scrooge_test_scala",
dep = ":scrooge3",
expected_jars = ["thrift3_scrooge_scala.jar"],
)

scrooge_transitive_outputs_test(
name = "scrooge_test_java",
dep = ":scrooge3_java",
expected_jars = ["thrift3_scrooge_java.jar"],
)
22 changes: 22 additions & 0 deletions deps/test/HelloLib.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright 2016 The Bazel Authors. All rights reserved.
//
// 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.

package scalarules.test

object HelloLib {
def printMessage(arg: String) {
println(arg)
}
}

71 changes: 71 additions & 0 deletions deps/test/MODULE.bazel.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
"""Bazel module template for test/shell/test_deps_versions.sh tests."""

module(
name = "rules_scala_deps_versions_test",
bazel_compatibility = [">=${bazelversion}"],
)

bazel_dep(name = "rules_scala")
local_path_override(
module_name = "rules_scala",
path = "../..",
)

bazel_dep(name = "bazel_skylib")
single_version_override(
module_name = "bazel_skylib",
version = "${skylib_version}",
)

bazel_dep(name = "platforms")
single_version_override(
module_name = "platforms",
version = "${platforms_version}",
)

bazel_dep(name = "rules_java")
single_version_override(
module_name = "rules_java",
version = "${rules_java_version}",
)

bazel_dep(name = "rules_proto")
single_version_override(
module_name = "rules_proto",
version = "${rules_proto_version}",
)

# Requires the patch for `protoc` toolchainization until resolution of
# protocolbuffers/protobuf#19679.
bazel_dep(name = "protobuf")
single_version_override(
module_name = "protobuf",
patch_strip = 1,
patches = ["//:protobuf.patch"],
version = "${protobuf_version}",
)

scala_protoc = use_extension(
"@rules_scala//scala/extensions:protoc.bzl",
"scala_protoc",
dev_dependency = True,
)
use_repo(scala_protoc, "rules_scala_protoc_toolchains")

register_toolchains(
"@rules_scala_protoc_toolchains//...:all",
dev_dependency = True,
)

scala_deps = use_extension(
"@rules_scala//scala/extensions:deps.bzl",
"scala_deps",
)
scala_deps.scala()
scala_deps.jmh()
scala_deps.junit()
scala_deps.scala_proto()
scala_deps.scalafmt()
scala_deps.scalatest()
scala_deps.specs2()
scala_deps.twitter_scrooge()
21 changes: 21 additions & 0 deletions deps/test/ScalaBinary.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright 2016 The Bazel Authors. All rights reserved.
//
// 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.

package scalarules.test

object ScalaBinary {
def main(args: Array[String]) {
HelloLib.printMessage("Hello");
}
}
46 changes: 46 additions & 0 deletions deps/test/defs.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
load("@bazel_skylib//lib:unittest.bzl", "analysistest", "asserts", "unittest")
load("@bazel_skylib//lib:collections.bzl", "collections")
load("@rules_scala//scala:advanced_usage/scala.bzl", "make_scala_test")
load("@rules_scala//scala/scalafmt:phase_scalafmt_ext.bzl", "ext_scalafmt")

# From //test/scalafmt:phase_scalafmt_test.bzl
scalafmt_scala_test = make_scala_test(ext_scalafmt)

# From //test/proto:default_outputs_test.bzl
def _default_outputs_test(ctx):
env = analysistest.begin(ctx)

target_under_test = analysistest.target_under_test(env)
actual_outs = [f.basename for f in target_under_test[DefaultInfo].files.to_list()]

asserts.equals(env, sorted(ctx.attr.expected_outs), sorted(actual_outs))

return analysistest.end(env)

default_outputs_test = analysistest.make(
_default_outputs_test,
attrs = {
"expected_outs": attr.string_list(),
},
)

# From
# //test/src/main/scala/scalarules/test/twitter_scrooge:twitter_scrooge_test.bzl
def _scrooge_transitive_outputs(ctx):
env = unittest.begin(ctx)

asserts.equals(
env,
sorted(ctx.attr.expected_jars),
sorted(collections.uniq([out.class_jar.basename for out in ctx.attr.dep[JavaInfo].outputs.jars])),
)

return unittest.end(env)

scrooge_transitive_outputs_test = unittest.make(
_scrooge_transitive_outputs,
attrs = {
"dep": attr.label(),
"expected_jars": attr.string_list(),
},
)
35 changes: 6 additions & 29 deletions test/shell/test_bzlmod_macros.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ test_source="${dir}/test/shell/${BASH_SOURCE[0]#*test/shell/}"
# shellcheck source=./test_runner.sh
. "${dir}"/test/shell/test_runner.sh
. "${dir}"/test/shell/test_helper.sh
runner=$(get_test_runner "${1:-local}")
export USE_BAZEL_VERSION=${USE_BAZEL_VERSION:-$(cat $dir/.bazelversion)}

# Setup and teardown
Expand All @@ -23,14 +22,11 @@ setup_suite() {
fi

original_dir="$PWD"
test_tmpdir="${dir}/tmp/${BASH_SOURCE[0]##*/}"
test_tmpdir="${test_tmpdir%.*}"
test_srcs_dir="${dir}/scala/private/macros/test"

mkdir -p "$test_tmpdir"
cd "$test_tmpdir"
setup_test_tmpdir_for_file "$original_dir" "$test_source"
test_tmpdir="$PWD"

rules_scala_dir="../.."
rules_scala_dir="$(relative_path_to_parent "$original_dir" "$test_tmpdir")"
test_srcs_dir="${dir}/scala/private/macros/test"
test_tmpdir_base="${test_tmpdir##*/}"
test_module_bazel_regex="[^ ]+${test_tmpdir_base}/MODULE.bazel"

Expand All @@ -44,10 +40,7 @@ setup_suite() {
}

teardown_suite() {
# Make sure bazel isn't still running for this workspace.
bazel clean --expunge_async 2>/dev/null
cd "$original_dir"
rm -rf "$test_tmpdir"
teardown_test_tmpdir "$original_dir" "$test_tmpdir"
}

setup_test_module() {
Expand Down Expand Up @@ -223,22 +216,6 @@ test_bzlmod_repeated_tag_values_fails_on_duplicate_key() {
"${bazel_run_args[@]}" "$print_repeated_test_tag_values_target"
}

# Run tests
# To skip a test, add a `_` prefix to its function name.
# To run a specific test, set the `RULES_SCALA_TEST_ONLY` env var to its name.

setup_suite

while IFS= read -r line; do
if [[ "$line" =~ ^_?(test_[A-Za-z0-9_]+)\(\)\ ?\{$ ]]; then
test_name="${BASH_REMATCH[1]}"

if [[ "${line:0:1}" == '_' ]]; then
echo -e "${YELLOW}skipping ${test_name}${NC}"
else
"$runner" "$test_name"
fi
fi
done <"$test_source"

run_tests "$test_source" "$(get_test_runner "${1:-local}")"
teardown_suite
Loading