[Java. Tests. JUnit] Escape characters for child processes when arg-file shortening is present

#IDEA-323007 fixed

GitOrigin-RevId: f2c75dfb983873e6182f93b80749ad0dfc43be28
This commit is contained in:
Georgii Ustinov
2025-04-23 16:37:02 +03:00
committed by intellij-monorepo-bot
parent ef8a49bea1
commit 4ca91861f0
2 changed files with 38 additions and 1 deletions

View File

@@ -75,7 +75,8 @@ public abstract class ForkedByModuleSplitter {
File argFile = File.createTempFile("arg_file", null);
argFile.deleteOnExit();
try (FileOutputStream writer = new FileOutputStream(argFile)) {
writer.write(classpath.getBytes(Charset.defaultCharset()));
String quotedArg = quoteArg(classpath);
writer.write(quotedArg.getBytes(Charset.defaultCharset()));
}
builder.add("@" + argFile.getAbsolutePath());
}
@@ -108,6 +109,38 @@ public abstract class ForkedByModuleSplitter {
return exec.waitFor();
}
/**
* WARNING: Due to compatibility reasons, this method has duplicate: {@link com.intellij.execution.CommandLineWrapperUtil#quoteArg(String)}
* If you modify this method, consider also changing its copy.
*/
private static String quoteArg(String arg) {
String specialCharacters = " #'\"\n\r\t\f";
boolean containsSpecialCharacter = false;
for (int i = 0; i < arg.length(); i++ ) {
char ch = arg.charAt(i);
if (specialCharacters.indexOf(ch) >= 0) {
containsSpecialCharacter = true;
break;
}
}
if (!containsSpecialCharacter) return arg;
StringBuilder sb = new StringBuilder(arg.length() * 2);
for (int i = 0; i < arg.length(); i++) {
char c = arg.charAt(i);
if (c == ' ' || c == '#' || c == '\'') sb.append('"').append(c).append('"');
else if (c == '"') sb.append("\"\\\"\"");
else if (c == '\n') sb.append("\"\\n\"");
else if (c == '\r') sb.append("\"\\r\"");
else if (c == '\t') sb.append("\"\\t\"");
else if (c == '\f') sb.append("\"\\f\"");
else sb.append(c);
}
return sb.toString();
}
private static Runnable createInputReader(final InputStream inputStream, final PrintStream outputStream) {
return new Runnable() {
@Override