2
2
3
3
import com .google .devtools .build .runfiles .Runfiles ;
4
4
import java .io .IOException ;
5
+ import java .io .File ;
5
6
import java .util .List ;
6
7
import java .util .Map ;
7
8
import java .nio .charset .Charset ;
8
9
import java .nio .file .Files ;
9
10
import java .nio .file .Paths ;
10
11
11
- /** This exists only as a proxy for scala tests's runner to provide access to env variables */
12
+ /** This exists only as a proxy for scala tests's runner to:
13
+ * - provide access to env variables
14
+ * - unwrap runner's arguments from a file (passed via file to overcome command-line string limitation on Windows)
15
+ **/
12
16
public class Runner {
13
17
/**
14
18
* This is the name of the env var set by bazel when a user provides a `--test_filter` test option
15
19
*/
16
20
private static final String TESTBRIDGE_TEST_ONLY = "TESTBRIDGE_TEST_ONLY" ;
17
21
18
22
/**
19
- * This is the name of the system property used to pass Bazel's workspace name
23
+ * This is the name of the system property used to pass the main workspace name
20
24
*/
21
- private static final String RULES_SCALA_WS = "RULES_SCALA_WS " ;
25
+ private static final String RULES_SCALA_MAIN_WS_NAME = "RULES_SCALA_MAIN_WS_NAME " ;
22
26
23
27
/**
24
28
* This is the name of the system property used to pass a short path of the file, which includes
@@ -31,32 +35,26 @@ public static void main(String[] args) throws IOException {
31
35
}
32
36
33
37
private static String [] extendArgs (String [] args , Map <String , String > env ) throws IOException {
34
- args = extendFromSystemPropArgs (args );
38
+ args = extendFromFileArgs (args );
35
39
args = extendFromEnvVar (args , env , TESTBRIDGE_TEST_ONLY , "-s" );
36
40
return args ;
37
41
}
38
42
39
- private static String [] extendFromSystemPropArgs (String [] args ) throws IOException {
40
- String rulesWorkspace = System .getProperty (RULES_SCALA_WS );
41
- if (rulesWorkspace == null || rulesWorkspace .trim ().isEmpty ())
42
- throw new IllegalArgumentException (RULES_SCALA_WS + " is null or empty." );
43
-
44
- String rulesArgsKey = System .getProperty (RULES_SCALA_ARGS_FILE );
45
- if (rulesArgsKey == null || rulesArgsKey .trim ().isEmpty ())
43
+ private static String [] extendFromFileArgs (String [] args ) throws IOException {
44
+ String runnerArgsFileKey = System .getProperty (RULES_SCALA_ARGS_FILE );
45
+ if (runnerArgsFileKey == null || runnerArgsFileKey .trim ().isEmpty ())
46
46
throw new IllegalArgumentException (RULES_SCALA_ARGS_FILE + " is null or empty." );
47
47
48
- String rulesArgsPath = Runfiles . create (). rlocation ( rulesWorkspace + "/" + rulesArgsKey );
49
- if (rulesArgsPath == null )
50
- throw new IllegalArgumentException ("rlocation value is null for key: " + rulesArgsKey );
48
+ String workspace = System . getProperty ( RULES_SCALA_MAIN_WS_NAME );
49
+ if (workspace == null || workspace . trim (). isEmpty () )
50
+ throw new IllegalArgumentException (RULES_SCALA_MAIN_WS_NAME + " is null or empty." );
51
51
52
- List <String > runnerArgs = Files .readAllLines (Paths .get (rulesArgsPath ), Charset .forName ("UTF-8" ));
52
+ String runnerArgsFilePath = Runfiles .create ().rlocation (workspace + "/" + runnerArgsFileKey );
53
+ if (runnerArgsFilePath == null )
54
+ throw new IllegalArgumentException ("rlocation value is null for key: " + runnerArgsFileKey );
53
55
54
- int runpathFlag = runnerArgs .indexOf ("-R" );
55
- if (runpathFlag >= 0 ) {
56
- String runpathKey = runnerArgs .get (runpathFlag + 1 );
57
- String runpath = Runfiles .create ().rlocation (rulesWorkspace + "/" + runpathKey );
58
- runnerArgs .set (runpathFlag + 1 , runpath );
59
- }
56
+ List <String > runnerArgs = Files .readAllLines (Paths .get (runnerArgsFilePath ), Charset .forName ("UTF-8" ));
57
+ rlocateRunpathValue (workspace , runnerArgs );
60
58
61
59
String [] runnerArgsArray = runnerArgs .toArray (new String [runnerArgs .size ()]);
62
60
@@ -73,12 +71,28 @@ private static String[] extendFromEnvVar(
73
71
if (value == null ) {
74
72
return args ;
75
73
}
76
- ;
77
74
String [] flag = new String [] {flagName , value };
78
75
String [] result = new String [args .length + flag .length ];
79
76
System .arraycopy (args , 0 , result , 0 , args .length );
80
77
System .arraycopy (flag , 0 , result , args .length , flag .length );
81
78
82
79
return result ;
83
80
}
81
+
82
+ /**
83
+ * Replaces ScalaTest Runner's runpath elements paths (see http://www.scalatest.org/user_guide/using_the_runner)
84
+ * with values from Bazel's runfiles
85
+ */
86
+ private static void rlocateRunpathValue (String rulesWorkspace , List <String > runnerArgs ) throws IOException {
87
+ int runpathFlag = runnerArgs .indexOf ("-R" );
88
+ if (runpathFlag >= 0 ) {
89
+ String [] runpathElements = runnerArgs .get (runpathFlag + 1 ).split (File .pathSeparator );
90
+ Runfiles runfiles = Runfiles .create ();
91
+ for (int i = 0 ; i < runpathElements .length ; i ++) {
92
+ runpathElements [i ] = runfiles .rlocation (rulesWorkspace + "/" + runpathElements [i ]);
93
+ }
94
+ String runpath = String .join (File .separator , runpathElements );
95
+ runnerArgs .set (runpathFlag + 1 , runpath );
96
+ }
97
+ }
84
98
}
0 commit comments