Skip to content

Commit 1324234

Browse files
committed
Customizable phases tests
1 parent 7ed8b33 commit 1324234

File tree

8 files changed

+116
-0
lines changed

8 files changed

+116
-0
lines changed

test/phase/BUILD

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
load(
2+
"//test/phase:customizability_test.bzl",
3+
"add_phase_customizability_test_singleton",
4+
"customizability_test_scala_binary",
5+
"customizability_test_scala_library",
6+
)
7+
8+
add_phase_customizability_test_singleton(
9+
name = "phase_customizability_test",
10+
visibility = ["//visibility:public"],
11+
)
12+
13+
customizability_test_scala_binary(
14+
name = "HelloBinary",
15+
srcs = ["HelloBinary.scala"],
16+
main_class = "scalarules.test.phase.HelloBinary",
17+
)
18+
19+
customizability_test_scala_library(
20+
name = "HelloLibrary",
21+
srcs = ["HelloLibrary.scala"],
22+
)

test/phase/HelloBinary.scala

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package scalarules.test.phase
2+
3+
object HelloBinary {
4+
def main(args: Array[String]) {
5+
println("You can customize binary phases!")
6+
}
7+
}

test/phase/HelloLibrary.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package scalarules.test.phase
2+
3+
object HelloLibrary {
4+
println("You can customize library phases!")
5+
}

test/phase/customizability_test.bzl

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
load(
2+
"//scala:providers.bzl",
3+
_ScalaRulePhase = "ScalaRulePhase",
4+
)
5+
load(
6+
"//test/phase:phase_customizability_test.bzl",
7+
_phase_customizability_test = "phase_customizability_test",
8+
)
9+
load(
10+
"//scala:scala.bzl",
11+
_make_scala_binary = "make_scala_binary",
12+
_make_scala_library = "make_scala_library",
13+
)
14+
15+
ext_add_phase_customizability_test = {
16+
"phase_providers": [
17+
"//test/phase:phase_customizability_test",
18+
],
19+
}
20+
21+
def _add_phase_customizability_test_singleton_implementation(ctx):
22+
return [
23+
_ScalaRulePhase(
24+
phases = [
25+
("-", "coda", "customizability_test", _phase_customizability_test),
26+
],
27+
),
28+
]
29+
30+
add_phase_customizability_test_singleton = rule(
31+
implementation = _add_phase_customizability_test_singleton_implementation,
32+
)
33+
34+
customizability_test_scala_binary = _make_scala_binary(ext_add_phase_customizability_test)
35+
customizability_test_scala_library = _make_scala_library(ext_add_phase_customizability_test)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#
2+
# PHASE: customizability test
3+
#
4+
# A dummy test phase to make sure rules are customizable
5+
#
6+
def phase_customizability_test(ctx, p):
7+
print("customizable phase")

test/shell/test_helper.sh

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,25 @@ action_should_fail_with_message() {
6060
fi
6161
}
6262

63+
action_should_contain_message() {
64+
set +e
65+
MSG=$1
66+
TEST_ARG=${@:2}
67+
RES=$(bazel $TEST_ARG 2>&1)
68+
RESPONSE_CODE=$?
69+
echo $RES | grep -- "$MSG"
70+
GREP_RES=$?
71+
if [ $RESPONSE_CODE -ne 0 ]; then
72+
echo -e "${RED} \"bazel $TEST_ARG\" should pass but failed. $NC"
73+
exit 1
74+
elif [ $GREP_RES -ne 0 ]; then
75+
echo -e "${RED} \"bazel $TEST_ARG\" should pass with message \"$MSG\" but did not. $NC"
76+
exit 1
77+
else
78+
exit 0
79+
fi
80+
}
81+
6382
test_expect_failure_or_warning_on_missing_direct_deps_with_expected_message() {
6483
set +e
6584

test/shell/test_phase.sh

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# shellcheck source=./test_runner.sh
2+
dir=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
3+
. "${dir}"/test_runner.sh
4+
. "${dir}"/test_helper.sh
5+
runner=$(get_test_runner "${1:-local}")
6+
7+
test_binary_with_extra_phase() {
8+
action_should_contain_message \
9+
"customizable phase" \
10+
build //test/phase:HelloBinary
11+
}
12+
13+
test_library_with_extra_phase() {
14+
action_should_contain_message \
15+
"customizable phase" \
16+
build //test/phase:HelloLibrary
17+
}
18+
19+
$runner test_binary_with_extra_phase
20+
$runner test_library_with_extra_phase

test_rules_scala.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ $runner bazel test //test/... --extra_toolchains="//test_expect_failure/plus_one
2929
. "${test_dir}"/test_javac_jvm_flags.sh
3030
. "${test_dir}"/test_junit.sh
3131
. "${test_dir}"/test_misc.sh
32+
. "${test_dir}"/test_phase.sh
3233
. "${test_dir}"/test_scala_binary.sh
3334
. "${test_dir}"/test_scalac_jvm_flags.sh
3435
. "${test_dir}"/test_scala_classpath.sh

0 commit comments

Comments
 (0)