Skip to content

Commit 972fdf2

Browse files
authored
Put -s in front of other parameters for the ScalaTest runner. (bazelbuild#1412)
* Put -s in front of other parameters for the ScalaTest runner. ScalaTest cares about the ordering between -s (class selection) and -t (test name selection). "-s -t" will select the test in the specified class with the specified name. "-t -s" will select all tests in the specified class, and all tests with the specified name. Putting the -s we derive from --test_filter at the front of the argument list rather than at the back is more likely to do what people want when they specify both --test_filter and other selector flags. It is still possible to specify -s last by passing the arguments as --test_arg=-t --test_arg=testName --test_arg=-s --test_arg=className and leaving --test_filter out. * Add tests * Don't run the test target automatically * formatting * comments * Remove wrong import added by intellij * add to test_rules_scala
1 parent 71d7135 commit 972fdf2

File tree

6 files changed

+48
-4
lines changed

6 files changed

+48
-4
lines changed

src/java/io/bazel/rulesscala/scala_test/Runner.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public static void main(String[] args) throws IOException {
3535

3636
private static String[] extendArgs(String[] args, Map<String, String> env) throws IOException {
3737
args = extendFromFileArgs(args);
38-
args = extendFromEnvVar(args, env, TESTBRIDGE_TEST_ONLY, "-s");
38+
args = prependFromEnvVar(args, env, TESTBRIDGE_TEST_ONLY, "-s");
3939
return args;
4040
}
4141

@@ -65,16 +65,16 @@ private static String[] extendFromFileArgs(String[] args) throws IOException {
6565
return result;
6666
}
6767

68-
private static String[] extendFromEnvVar(
68+
private static String[] prependFromEnvVar(
6969
String[] args, Map<String, String> env, String varName, String flagName) {
7070
String value = env.get(varName);
7171
if (value == null) {
7272
return args;
7373
}
7474
String[] flag = new String[] {flagName, value};
7575
String[] result = new String[args.length + flag.length];
76-
System.arraycopy(args, 0, result, 0, args.length);
77-
System.arraycopy(flag, 0, result, args.length, flag.length);
76+
System.arraycopy(flag, 0, result, 0, flag.length);
77+
System.arraycopy(args, 0, result, flag.length, args.length);
7878

7979
return result;
8080
}
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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_scala_test_testfilter_class_selection() {
8+
bazel test --test_output=errors --test_filter=A //test_expect_failure/scala_test_testfilter:tests
9+
}
10+
11+
test_scala_test_testfilter_method_selection() {
12+
bazel test --test_output=errors --test_filter=A --test_arg=-t --test_arg="test 1" //test_expect_failure/scala_test_testfilter:tests
13+
}
14+
15+
$runner test_scala_test_testfilter_class_selection
16+
$runner test_scala_test_testfilter_method_selection
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import org.scalatest.funsuite.AnyFunSuite
2+
3+
class A extends AnyFunSuite {
4+
5+
test("test 1") {
6+
7+
}
8+
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import org.scalatest.funsuite.AnyFunSuite
2+
3+
class B extends AnyFunSuite {
4+
5+
test("test 1") {
6+
fail("This test should not be selected")
7+
}
8+
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
load("@io_bazel_rules_scala//scala:scala.bzl", "scala_test")
2+
3+
scala_test(
4+
name = "tests",
5+
srcs = [
6+
"A.scala",
7+
"B.scala",
8+
],
9+
)

test_rules_scala.sh

+1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ $runner bazel build //test_statsfile:SimpleNoStatsFile_statsfile --extra_toolcha
4848
. "${test_dir}"/test_scala_proto_library.sh
4949
. "${test_dir}"/test_scala_library.sh
5050
. "${test_dir}"/test_scala_specs2.sh
51+
. "${test_dir}"/test_scala_test_testfilter.sh
5152
. "${test_dir}"/test_toolchain.sh
5253
. "${test_dir}"/test_strict_dependency.sh
5354
. "${test_dir}"/test_unused_dependency.sh

0 commit comments

Comments
 (0)