forked from facebook/buck
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProcessExecutorTest.java
130 lines (119 loc) · 5.82 KB
/
ProcessExecutorTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.facebook.buck.util;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import com.facebook.buck.testutil.TestConsole;
import com.facebook.buck.util.environment.Platform;
import com.google.common.collect.ImmutableSet;
import java.io.IOException;
import java.util.EnumSet;
import java.util.Optional;
import java.util.StringTokenizer;
import java.util.concurrent.atomic.AtomicBoolean;
import org.junit.Test;
public class ProcessExecutorTest {
@Test
public void testDontExpectStdout() throws IOException, InterruptedException {
CapturingPrintStream stdOut = new CapturingPrintStream();
CapturingPrintStream stdErr = new CapturingPrintStream();
Ansi ansi = Ansi.forceTty();
Console console = new Console(Verbosity.ALL, stdOut, stdErr, ansi);
ProcessExecutor executor = new DefaultProcessExecutor(console);
String cmd = Platform.detect() == Platform.WINDOWS ? "cmd /C echo Hello" : "echo Hello";
ProcessExecutorParams params = ProcessExecutorParams.ofCommand(makeCommandArray(cmd));
ProcessExecutor.Result result = executor.launchAndExecute(params);
assertEquals(
ansi.asHighlightedFailureText("Hello" + System.lineSeparator()), result.getStdout().get());
assertEquals("", result.getStderr().get());
}
@Test
public void testExpectStdout() throws IOException, InterruptedException {
String cmd = Platform.detect() == Platform.WINDOWS ? "cmd /C echo Hello" : "echo Hello";
CapturingPrintStream stdOut = new CapturingPrintStream();
CapturingPrintStream stdErr = new CapturingPrintStream();
Ansi ansi = Ansi.forceTty();
Console console = new Console(Verbosity.ALL, stdOut, stdErr, ansi);
ProcessExecutor executor = new DefaultProcessExecutor(console);
ProcessExecutorParams params = ProcessExecutorParams.ofCommand(makeCommandArray(cmd));
ProcessExecutor.Result result =
executor.launchAndExecute(
params,
EnumSet.of(ProcessExecutor.Option.EXPECTING_STD_OUT),
/* stdin */ Optional.empty(),
/* timeOutMs */ Optional.empty(),
/* timeOutHandler */ Optional.empty());
assertEquals("Hello" + System.lineSeparator(), result.getStdout().get());
assertEquals("", result.getStderr().get());
}
@Test
public void testProcessFailureDoesNotWriteEmptyString() throws IOException, InterruptedException {
String cmd = Platform.detect() == Platform.WINDOWS ? "cmd /C (exit 1)" : "false";
DirtyPrintStreamDecorator stdOut = new DirtyPrintStreamDecorator(new CapturingPrintStream());
DirtyPrintStreamDecorator stdErr = new DirtyPrintStreamDecorator(new CapturingPrintStream());
Ansi ansi = Ansi.forceTty();
Console console = new Console(Verbosity.ALL, stdOut, stdErr, ansi);
ProcessExecutor executor = new DefaultProcessExecutor(console);
ProcessExecutorParams params = ProcessExecutorParams.ofCommand(makeCommandArray(cmd));
executor.launchAndExecute(params);
assertFalse(stdOut.isDirty());
assertFalse(stdErr.isDirty());
}
@Test
public void testProcessTimeoutHandlerIsInvoked() throws IOException, InterruptedException {
@SuppressWarnings("PMD.PrematureDeclaration")
ProcessExecutor executor = new DefaultProcessExecutor(new TestConsole(Verbosity.ALL));
AtomicBoolean called = new AtomicBoolean(false);
String cmd = (Platform.detect() == Platform.WINDOWS) ? "ping -n 50 0.0.0.0" : "sleep 50";
ProcessExecutorParams params = ProcessExecutorParams.ofCommand(makeCommandArray(cmd));
ProcessExecutor.Result result =
executor.launchAndExecute(
params,
/* options */ ImmutableSet.<ProcessExecutor.Option>builder().build(),
/* stdin */ Optional.empty(),
/* timeOutMs */ Optional.of((long) 100),
/* timeOutHandler */ Optional.of(ignored -> called.set(true)));
assertTrue("process was reported as timed out", result.isTimedOut());
assertTrue("timeOutHandler was called when a timeout was hit", called.get());
}
@Test
public void testProcessTimeoutHandlerThrowsException() throws IOException, InterruptedException {
@SuppressWarnings("PMD.PrematureDeclaration")
ProcessExecutor executor = new DefaultProcessExecutor(new TestConsole(Verbosity.ALL));
String cmd = (Platform.detect() == Platform.WINDOWS) ? "ping -n 50 0.0.0.0" : "sleep 50";
ProcessExecutorParams params = ProcessExecutorParams.ofCommand(makeCommandArray(cmd));
ProcessExecutor.Result result =
executor.launchAndExecute(
params,
/* options */ ImmutableSet.<ProcessExecutor.Option>builder().build(),
/* stdin */ Optional.empty(),
/* timeOutMs */ Optional.of((long) 100),
/* timeOutHandler */ Optional.of(
ignored -> {
throw new RuntimeException("This shouldn't fail the test!");
}));
assertTrue("process was reported as timed out", result.isTimedOut());
}
private static String[] makeCommandArray(String command) {
StringTokenizer st = new StringTokenizer(command);
String[] cmdarray = new String[st.countTokens()];
for (int i = 0; st.hasMoreTokens(); i++) {
cmdarray[i] = st.nextToken();
}
return cmdarray;
}
}