Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.StringTokenizer;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;
import java.util.concurrent.CountDownLatch;
Expand Down Expand Up @@ -218,7 +219,10 @@ private void execProcess(String[] process, ProcessResult processresult) throws H
// execute process
try {
if (!meta.isArgumentsInFields()) {
p = data.runtime.exec(new String[] {process[0]});
// Match historical Runtime.exec(String) whitespace tokenization without using the
// deprecated String overload. A single-element String[] would treat the whole command
// line (e.g. "/bin/echo hop-single") as the executable name.
p = data.runtime.exec(tokenizeCommandLine(process[0]));
} else {
p = data.runtime.exec(process);
}
Expand Down Expand Up @@ -323,6 +327,19 @@ private void execProcess(String[] process, ProcessResult processresult) throws H
}
}

/**
* Tokenize a command line the same way Runtime.exec(String) historically did (whitespace via
* {@link StringTokenizer}).
*/
static String[] tokenizeCommandLine(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;
}

private String getOutputString(BufferedReader b) throws IOException {
StringBuilder returnValueBuffer = new StringBuilder();
String line;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;

import java.util.Arrays;
import java.util.List;
import org.apache.hop.core.HopEnvironment;
import org.apache.hop.core.IRowSet;
import org.apache.hop.core.QueueRowSet;
Expand Down Expand Up @@ -215,6 +217,14 @@ void processRow_failWhenNotSuccess_setsErrors() throws HopException {
assertEquals(1, transform.getErrors());
}

@Test
void tokenizeCommandLine_splitsOnWhitespaceLikeRuntimeExecString() {
assertEquals(
Arrays.asList("/bin/echo", "hop-single"),
Arrays.asList(ExecProcess.tokenizeCommandLine("/bin/echo hop-single")));
assertEquals(List.of("cmd"), Arrays.asList(ExecProcess.tokenizeCommandLine("cmd")));
}

@Test
void processRow_emptyProcessField_throws() throws HopException {
HopEnvironment.init();
Expand Down
Loading