Skip to content

Commit

Permalink
Customizable phases tests
Browse files Browse the repository at this point in the history
  • Loading branch information
borkaehw committed Oct 29, 2019
1 parent c22d3c0 commit 7fd8f4c
Show file tree
Hide file tree
Showing 8 changed files with 155 additions and 0 deletions.
30 changes: 30 additions & 0 deletions test/phase/BUILD
Original file line number Diff line number Diff line change
@@ -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",
)
7 changes: 7 additions & 0 deletions test/phase/HelloBinary.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package scalarules.test.phase

object HelloBinary {
def main(args: Array[String]) {
val message = "You can customize binary phases!"
}
}
5 changes: 5 additions & 0 deletions test/phase/HelloLibrary.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package scalarules.test.phase

object HelloLibrary {
val message = "You can customize library phases!"
}
10 changes: 10 additions & 0 deletions test/phase/HelloTest.scala
Original file line number Diff line number Diff line change
@@ -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!"))
}
}
45 changes: 45 additions & 0 deletions test/phase/customizability_test.bzl
Original file line number Diff line number Diff line change
@@ -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)
10 changes: 10 additions & 0 deletions test/phase/phase_customizability_test.bzl
Original file line number Diff line number Diff line change
@@ -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,
)
47 changes: 47 additions & 0 deletions test/shell/test_phase.sh
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions test_rules_scala.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 7fd8f4c

Please sign in to comment.