17
17
18
18
import static org .assertj .core .api .Assertions .assertThat ;
19
19
20
- import java .io .ByteArrayOutputStream ;
21
20
import java .io .File ;
22
21
import java .io .IOException ;
23
- import java .io .InputStream ;
24
- import java .nio .charset .Charset ;
25
22
import java .util .Arrays ;
26
23
import java .util .HashMap ;
27
- import java .util .List ;
28
24
import java .util .Map ;
29
25
import java .util .Objects ;
30
26
31
- import com .diffplug .common .base .Throwables ;
32
- import com .diffplug .common .io .ByteStreams ;
33
- import com .diffplug .spotless .FileSignature ;
34
27
import com .diffplug .spotless .Jvm ;
28
+ import com .diffplug .spotless .ProcessRunner ;
35
29
36
30
/**
37
31
* Harness for running a maven build, same idea as the
@@ -47,6 +41,7 @@ private MavenRunner() {}
47
41
private File projectDir ;
48
42
private String [] args ;
49
43
private Map <String , String > environment = new HashMap <>();
44
+ private ProcessRunner runner ;
50
45
51
46
public MavenRunner withProjectDir (File projectDir ) {
52
47
this .projectDir = Objects .requireNonNull (projectDir );
@@ -58,110 +53,36 @@ public MavenRunner withArguments(String... args) {
58
53
return this ;
59
54
}
60
55
56
+ public MavenRunner withRunner (ProcessRunner runner ) {
57
+ this .runner = runner ;
58
+ return this ;
59
+ }
60
+
61
61
public MavenRunner withRemoteDebug (int port ) {
62
62
String address = (Jvm .version () < 9 ? "" : "*:" ) + port ;
63
63
environment .put ("MAVEN_OPTS" , "-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=" + address );
64
64
return this ;
65
65
}
66
66
67
- private Result run () throws IOException , InterruptedException {
67
+ private ProcessRunner . Result run () throws IOException , InterruptedException {
68
68
Objects .requireNonNull (projectDir , "Need to call withProjectDir() first" );
69
69
Objects .requireNonNull (args , "Need to call withArguments() first" );
70
70
// run maven with the given args in the given directory
71
- String argsString = String .join (" " , Arrays .asList (args ));
72
- List <String > cmds = getPlatformCmds ("-e " + argsString );
73
- ProcessBuilder builder = new ProcessBuilder (cmds );
74
- builder .directory (projectDir );
75
- builder .environment ().putAll (environment );
76
- Process process = builder .start ();
77
- // slurp and return the stdout, stderr, and exitValue
78
- Slurper output = new Slurper (process .getInputStream ());
79
- Slurper error = new Slurper (process .getErrorStream ());
80
- int exitValue = process .waitFor ();
81
- output .join ();
82
- error .join ();
83
- return new Result (exitValue , output .result (), error .result ());
71
+ String argsString = "-e " + String .join (" " , Arrays .asList (args ));
72
+ return runner .shellWinUnix (projectDir , "mvnw " + argsString , "./mvnw " + argsString );
84
73
}
85
74
86
75
/** Runs the command and asserts that exit code is 0. */
87
- public Result runNoError () throws IOException , InterruptedException {
88
- Result result = run ();
89
- assertThat (result .exitValue ()).as ("Run without error %s" , result ).isEqualTo (0 );
76
+ public ProcessRunner . Result runNoError () throws IOException , InterruptedException {
77
+ ProcessRunner . Result result = run ();
78
+ assertThat (result .exitCode ()).as ("Run without error %s" , result ).isEqualTo (0 );
90
79
return result ;
91
80
}
92
81
93
82
/** Runs the command and asserts that exit code is not 0. */
94
- public Result runHasError () throws IOException , InterruptedException {
95
- Result result = run ();
96
- assertThat (result .exitValue ()).as ("Run with error %s" , result ).isNotEqualTo (0 );
83
+ public ProcessRunner . Result runHasError () throws IOException , InterruptedException {
84
+ ProcessRunner . Result result = run ();
85
+ assertThat (result .exitCode ()).as ("Run with error %s" , result ).isNotEqualTo (0 );
97
86
return result ;
98
87
}
99
-
100
- public static class Result {
101
- private final int exitValue ;
102
- private final String output ;
103
- private final String error ;
104
-
105
- public Result (int exitValue , String output , String error ) {
106
- super ();
107
- this .exitValue = exitValue ;
108
- this .output = Objects .requireNonNull (output );
109
- this .error = Objects .requireNonNull (error );
110
- }
111
-
112
- public int exitValue () {
113
- return exitValue ;
114
- }
115
-
116
- public String output () {
117
- return output ;
118
- }
119
-
120
- public String error () {
121
- return error ;
122
- }
123
-
124
- @ Override
125
- public String toString () {
126
- return "Result{" +
127
- "exitValue=" + exitValue +
128
- ", output='" + output + '\'' +
129
- ", error='" + error + '\'' +
130
- '}' ;
131
- }
132
- }
133
-
134
- /** Prepends any arguments necessary to run a console command. */
135
- private static List <String > getPlatformCmds (String cmd ) {
136
- if (FileSignature .machineIsWin ()) {
137
- return Arrays .asList ("cmd" , "/c" , "mvnw " + cmd );
138
- } else {
139
- return Arrays .asList ("/bin/sh" , "-c" , "./mvnw " + cmd );
140
- }
141
- }
142
-
143
- private static class Slurper extends Thread {
144
- private final InputStream input ;
145
- private volatile String result ;
146
-
147
- Slurper (InputStream input ) {
148
- this .input = Objects .requireNonNull (input );
149
- start ();
150
- }
151
-
152
- @ Override
153
- public void run () {
154
- try {
155
- ByteArrayOutputStream output = new ByteArrayOutputStream ();
156
- ByteStreams .copy (input , output );
157
- result = output .toString (Charset .defaultCharset ().name ());
158
- } catch (Exception e ) {
159
- result = Throwables .getStackTraceAsString (e );
160
- }
161
- }
162
-
163
- public String result () {
164
- return result ;
165
- }
166
- }
167
88
}
0 commit comments