From 6bf25749a2b3831cf79731709f02c56b439fcbee Mon Sep 17 00:00:00 2001 From: Bor Kae Hwang Date: Fri, 25 Oct 2019 15:21:58 -0600 Subject: [PATCH] Customizable phases tests --- test/phase/BUILD | 30 +++++++++++++++ test/phase/HelloBinary.scala | 7 ++++ test/phase/HelloLibrary.scala | 5 +++ test/phase/HelloTest.scala | 10 +++++ test/phase/customizability_test.bzl | 45 ++++++++++++++++++++++ test/phase/phase_customizability_test.bzl | 10 +++++ test/shell/test_phase.sh | 47 +++++++++++++++++++++++ test_rules_scala.sh | 1 + 8 files changed, 155 insertions(+) create mode 100644 test/phase/BUILD create mode 100644 test/phase/HelloBinary.scala create mode 100644 test/phase/HelloLibrary.scala create mode 100644 test/phase/HelloTest.scala create mode 100644 test/phase/customizability_test.bzl create mode 100644 test/phase/phase_customizability_test.bzl create mode 100755 test/shell/test_phase.sh diff --git a/test/phase/BUILD b/test/phase/BUILD new file mode 100644 index 0000000000..7c47a162bf --- /dev/null +++ b/test/phase/BUILD @@ -0,0 +1,30 @@ +load( + "//test/phase:customizability_test.bzl", + "add_phase_customizability_test_singleton", + "customizability_test_scala_binary", + "customizability_test_scala_library", + "customizability_test_scala_test", +) + +add_phase_customizability_test_singleton( + name = "phase_customizability_test", + visibility = ["//visibility:public"], +) + +customizability_test_scala_binary( + name = "HelloBinary", + srcs = ["HelloBinary.scala"], + main_class = "scalarules.test.phase.HelloBinary", +) + +customizability_test_scala_library( + name = "HelloLibrary", + srcs = ["HelloLibrary.scala"], + custom_content = "This is custom content in library", +) + +customizability_test_scala_test( + name = "HelloTest", + srcs = ["HelloTest.scala"], + custom_content = "This is custom content in test", +) diff --git a/test/phase/HelloBinary.scala b/test/phase/HelloBinary.scala new file mode 100644 index 0000000000..4ca6fa9a56 --- /dev/null +++ b/test/phase/HelloBinary.scala @@ -0,0 +1,7 @@ +package scalarules.test.phase + +object HelloBinary { + def main(args: Array[String]) { + val message = "You can customize binary phases!" + } +} diff --git a/test/phase/HelloLibrary.scala b/test/phase/HelloLibrary.scala new file mode 100644 index 0000000000..58737692ab --- /dev/null +++ b/test/phase/HelloLibrary.scala @@ -0,0 +1,5 @@ +package scalarules.test.phase + +object HelloLibrary { + val message = "You can customize library phases!" +} diff --git a/test/phase/HelloTest.scala b/test/phase/HelloTest.scala new file mode 100644 index 0000000000..2df1d58795 --- /dev/null +++ b/test/phase/HelloTest.scala @@ -0,0 +1,10 @@ +package scalarules.test.phase + +import org.scalatest._ + +class HelloTest extends FlatSpec { + val message = "You can customize test phases!" + "HelloTest" should "be able to customize test phases!" in { + assert(message.equals("You can customize test phases!")) + } +} diff --git a/test/phase/customizability_test.bzl b/test/phase/customizability_test.bzl new file mode 100644 index 0000000000..90e17d15f0 --- /dev/null +++ b/test/phase/customizability_test.bzl @@ -0,0 +1,45 @@ +load( + "//scala:providers.bzl", + _ScalaRulePhase = "ScalaRulePhase", +) +load( + "//test/phase:phase_customizability_test.bzl", + _phase_customizability_test = "phase_customizability_test", +) +load( + "//scala:scala.bzl", + _make_scala_binary = "make_scala_binary", + _make_scala_library = "make_scala_library", + _make_scala_test = "make_scala_test", +) + +ext_add_phase_customizability_test = { + "attrs": { + "custom_content": attr.string( + default = "This is custom content", + ), + }, + "outputs": { + "custom_output": "%{name}.custom-output" + }, + "phase_providers": [ + "//test/phase:phase_customizability_test", + ], +} + +def _add_phase_customizability_test_singleton_implementation(ctx): + return [ + _ScalaRulePhase( + phases = [ + ("-", "final", "customizability_test", _phase_customizability_test), + ], + ), + ] + +add_phase_customizability_test_singleton = rule( + implementation = _add_phase_customizability_test_singleton_implementation, +) + +customizability_test_scala_binary = _make_scala_binary(ext_add_phase_customizability_test) +customizability_test_scala_library = _make_scala_library(ext_add_phase_customizability_test) +customizability_test_scala_test = _make_scala_test(ext_add_phase_customizability_test) diff --git a/test/phase/phase_customizability_test.bzl b/test/phase/phase_customizability_test.bzl new file mode 100644 index 0000000000..15f01e09e7 --- /dev/null +++ b/test/phase/phase_customizability_test.bzl @@ -0,0 +1,10 @@ +# +# PHASE: customizability test +# +# A dummy test phase to make sure rules are customizable +# +def phase_customizability_test(ctx, p): + ctx.actions.write( + output = ctx.outputs.custom_output, + content = ctx.attr.custom_content, + ) diff --git a/test/shell/test_phase.sh b/test/shell/test_phase.sh new file mode 100755 index 0000000000..ac5844a1ef --- /dev/null +++ b/test/shell/test_phase.sh @@ -0,0 +1,47 @@ +# shellcheck source=./test_runner.sh +dir=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd ) +. "${dir}"/test_runner.sh +. "${dir}"/test_helper.sh +runner=$(get_test_runner "${1:-local}") + +output_file_should_contain_message() { + set +e + MSG=$1 + TEST_ARG=${@:2} + OUTPUT_FILE=$(echo ${@:3} | sed 's#//#/#g;s#:#/#g') + OUTPUT_PATH=$(bazel info bazel-bin)/$OUTPUT_FILE + bazel $TEST_ARG + RESPONSE_CODE=$? + cat $OUTPUT_PATH | grep -- "$MSG" + GREP_RES=$? + if [ $RESPONSE_CODE -ne 0 ]; then + echo -e "${RED} \"bazel $TEST_ARG\" should pass but failed. $NC" + exit 1 + elif [ $GREP_RES -ne 0 ]; then + echo -e "${RED} \"bazel $TEST_ARG\" should pass with \"$MSG\" in file \"$OUTPUT_FILE\" but did not. $NC" + exit 1 + else + exit 0 + fi +} +test_scala_binary_with_extra_phase() { + output_file_should_contain_message \ + "This is custom content" \ + build //test/phase:HelloBinary.custom-output +} + +test_scala_library_with_extra_phase_and_custom_content() { + output_file_should_contain_message \ + "This is custom content in library" \ + build //test/phase:HelloLibrary.custom-output +} + +test_scala_test_with_extra_phase_and_custom_content() { + output_file_should_contain_message \ + "This is custom content in test" \ + build //test/phase:HelloTest.custom-output +} + +$runner test_scala_binary_with_extra_phase +$runner test_scala_library_with_extra_phase_and_custom_content +$runner test_scala_test_with_extra_phase_and_custom_content diff --git a/test_rules_scala.sh b/test_rules_scala.sh index 04b1ced740..e8456fabfe 100755 --- a/test_rules_scala.sh +++ b/test_rules_scala.sh @@ -29,6 +29,7 @@ $runner bazel test //test/... --extra_toolchains="//test_expect_failure/plus_one . "${test_dir}"/test_javac_jvm_flags.sh . "${test_dir}"/test_junit.sh . "${test_dir}"/test_misc.sh +. "${test_dir}"/test_phase.sh . "${test_dir}"/test_scala_binary.sh . "${test_dir}"/test_scalac_jvm_flags.sh . "${test_dir}"/test_scala_classpath.sh