Skip to content

Commit 226ee28

Browse files
rjernstmosche
andauthored
Adjust terminal tests to new behavior in JDK 22. (#103614) (#106051)
JDK 22 may return a console even if the terminal is redirected. These cases are detected using the new Console#isTerminal() to maintain the current behavior (closes #98033). Co-authored-by: Moritz Mack <[email protected]>
1 parent 63eeed8 commit 226ee28

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

libs/cli/build.gradle

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,12 @@ apply plugin: 'elasticsearch.publish'
1111
dependencies {
1212
api 'net.sf.jopt-simple:jopt-simple:5.0.2'
1313
api project(':libs:elasticsearch-core')
14+
15+
testImplementation(project(":test:framework")) {
16+
exclude group: 'org.elasticsearch', module: 'elasticsearch-cli'
17+
}
1418
}
1519

16-
tasks.named("test").configure { enabled = false }
1720
// Since CLI does not depend on :server, it cannot run the jarHell task
1821
tasks.named("jarHell").configure { enabled = false }
1922

libs/cli/src/main/java/org/elasticsearch/cli/Terminal.java

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
package org.elasticsearch.cli;
1010

1111
import org.elasticsearch.core.Nullable;
12+
import org.elasticsearch.jdk.JavaVersion;
1213

1314
import java.io.BufferedReader;
1415
import java.io.Console;
@@ -17,6 +18,8 @@
1718
import java.io.OutputStream;
1819
import java.io.PrintWriter;
1920
import java.io.Reader;
21+
import java.lang.reflect.InvocationTargetException;
22+
import java.lang.reflect.Method;
2023
import java.nio.charset.Charset;
2124
import java.util.Arrays;
2225
import java.util.Locale;
@@ -217,8 +220,8 @@ public boolean isHeadless() {
217220
}
218221

219222
private static class ConsoleTerminal extends Terminal {
220-
221-
private static final Console CONSOLE = System.console();
223+
private static final int JDK_VERSION_WITH_IS_TERMINAL = 22;
224+
private static final Console CONSOLE = detectTerminal();
222225

223226
ConsoleTerminal() {
224227
super(System.lineSeparator());
@@ -228,6 +231,23 @@ static boolean isSupported() {
228231
return CONSOLE != null;
229232
}
230233

234+
static Console detectTerminal() {
235+
// JDK >= 22 returns a console even if the terminal is redirected unless using -Djdk.console=java.base
236+
// https://bugs.openjdk.org/browse/JDK-8308591
237+
Console console = System.console();
238+
if (console != null && JavaVersion.current().getVersion().get(0) >= JDK_VERSION_WITH_IS_TERMINAL) {
239+
try {
240+
// verify the console is a terminal using isTerminal() on JDK >= 22
241+
// TODO: Remove reflection once Java 22 sources are supported, e.g. using a MRJAR
242+
Method isTerminal = Console.class.getMethod("isTerminal");
243+
return Boolean.TRUE.equals(isTerminal.invoke(console)) ? console : null;
244+
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
245+
throw new AssertionError(e);
246+
}
247+
}
248+
return console;
249+
}
250+
231251
@Override
232252
public PrintWriter getWriter() {
233253
return CONSOLE.writer();

0 commit comments

Comments
 (0)