Skip to content

Commit

Permalink
Adjust terminal tests to new behavior in JDK 22. (#103614) (#106051)
Browse files Browse the repository at this point in the history
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 <mmack@apache.org>
  • Loading branch information
rjernst and mosche committed Mar 7, 2024
1 parent 63eeed8 commit 226ee28
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
5 changes: 4 additions & 1 deletion libs/cli/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@ apply plugin: 'elasticsearch.publish'
dependencies {
api 'net.sf.jopt-simple:jopt-simple:5.0.2'
api project(':libs:elasticsearch-core')

testImplementation(project(":test:framework")) {
exclude group: 'org.elasticsearch', module: 'elasticsearch-cli'
}
}

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

Expand Down
24 changes: 22 additions & 2 deletions libs/cli/src/main/java/org/elasticsearch/cli/Terminal.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
package org.elasticsearch.cli;

import org.elasticsearch.core.Nullable;
import org.elasticsearch.jdk.JavaVersion;

import java.io.BufferedReader;
import java.io.Console;
Expand All @@ -17,6 +18,8 @@
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.Reader;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.Locale;
Expand Down Expand Up @@ -217,8 +220,8 @@ public boolean isHeadless() {
}

private static class ConsoleTerminal extends Terminal {

private static final Console CONSOLE = System.console();
private static final int JDK_VERSION_WITH_IS_TERMINAL = 22;
private static final Console CONSOLE = detectTerminal();

ConsoleTerminal() {
super(System.lineSeparator());
Expand All @@ -228,6 +231,23 @@ static boolean isSupported() {
return CONSOLE != null;
}

static Console detectTerminal() {
// JDK >= 22 returns a console even if the terminal is redirected unless using -Djdk.console=java.base
// https://bugs.openjdk.org/browse/JDK-8308591
Console console = System.console();
if (console != null && JavaVersion.current().getVersion().get(0) >= JDK_VERSION_WITH_IS_TERMINAL) {
try {
// verify the console is a terminal using isTerminal() on JDK >= 22
// TODO: Remove reflection once Java 22 sources are supported, e.g. using a MRJAR
Method isTerminal = Console.class.getMethod("isTerminal");
return Boolean.TRUE.equals(isTerminal.invoke(console)) ? console : null;
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
throw new AssertionError(e);
}
}
return console;
}

@Override
public PrintWriter getWriter() {
return CONSOLE.writer();
Expand Down

0 comments on commit 226ee28

Please sign in to comment.