8277328: jdk/jshell/CommandCompletionTest.java failures on Windows

Reviewed-by: vromero
This commit is contained in:
Jan Lahoda 2021-12-07 18:15:25 +00:00
parent 5a036ace01
commit 3955b037da
2 changed files with 25 additions and 8 deletions
src/jdk.jshell/share/classes/jdk/internal/jshell/tool
test/langtools/jdk/jshell

@ -254,7 +254,7 @@ public class JShellTool implements MessageHandler {
Pattern.compile(OPTION_PRE_PATTERN.pattern() + "(?<dd>-??)(?<flag>-([a-z][a-z\\-]*)?)");
// match an option flag and a (possibly missing or incomplete) value
private static final Pattern OPTION_VALUE_PATTERN =
Pattern.compile(OPTION_PATTERN.pattern() + "\\s+(?<val>\\S*)");
Pattern.compile(OPTION_PATTERN.pattern() + "\\s+(?<val>(\\S|\\\\ )*)");
// Tool id (tid) mapping: the three name spaces
NameSpace mainNamespace;
@ -1561,13 +1561,13 @@ public class JShellTool implements MessageHandler {
private static CompletionProvider fileCompletions(Predicate<Path> accept) {
return (code, cursor, anchor) -> {
int lastSlash = code.lastIndexOf('/');
String path = code.substring(0, lastSlash + 1);
String prefix = lastSlash != (-1) ? code.substring(lastSlash + 1) : code;
String path = code.substring(0, lastSlash + 1).replace("\\ ", " ");
String prefix = (lastSlash != (-1) ? code.substring(lastSlash + 1) : code).replace("\\ ", " ");
Path current = toPathResolvingUserHome(path);
List<Suggestion> result = new ArrayList<>();
try (Stream<Path> dir = Files.list(current)) {
dir.filter(f -> accept.test(f) && f.getFileName().toString().startsWith(prefix))
.map(f -> new ArgSuggestion(f.getFileName() + (Files.isDirectory(f) ? "/" : "")))
.map(f -> new ArgSuggestion(f.getFileName().toString().replace(" ", "\\ ") + (Files.isDirectory(f) ? "/" : "")))
.forEach(result::add);
} catch (IOException ex) {
//ignore...

@ -23,7 +23,7 @@
/*
* @test
* @bug 8144095 8164825 8169818 8153402 8165405 8177079 8178013 8167554 8166232
* @bug 8144095 8164825 8169818 8153402 8165405 8177079 8178013 8167554 8166232 8277328
* @summary Test Command Completion
* @modules jdk.compiler/com.sun.tools.javac.api
* jdk.compiler/com.sun.tools.javac.main
@ -331,6 +331,22 @@ public class CommandCompletionTest extends ReplToolTesting {
);
}
@Test
public void testClassPathWithSpace() throws IOException {
Compiler compiler = new Compiler();
Path outDir = compiler.getPath("testClassPathWithSpace");
Path dirWithSpace = Files.createDirectories(outDir.resolve("dir with space"));
Files.createDirectories(dirWithSpace.resolve("nested with space"));
String[] pathArray = new String[] {"dir\\ with\\ space/"};
String[] pathArray2 = new String[] {"nested\\ with\\ space/"};
testNoStartUp(
a -> assertCompletion(a, "/env -class-path " + outDir + "/|", false, pathArray),
a -> assertCompletion(a, "/env -class-path " + outDir + "/dir|", false, pathArray),
a -> assertCompletion(a, "/env -class-path " + outDir + "/dir\\ with|", false, pathArray),
a -> assertCompletion(a, "/env -class-path " + outDir + "/dir\\ with\\ space/|", false, pathArray2)
);
}
@Test
public void testUserHome() throws IOException {
List<String> completions;
@ -338,8 +354,9 @@ public class CommandCompletionTest extends ReplToolTesting {
String selectedFile;
try (Stream<Path> content = Files.list(home)) {
selectedFile = content.filter(CLASSPATH_FILTER)
.filter(file -> file.getFileName().toString().contains(" "))
.findAny()
.map(file -> file.getFileName().toString())
.map(file -> file.getFileName().toString().replace(" ", "\\ "))
.orElse(null);
}
if (selectedFile == null) {
@ -347,8 +364,8 @@ public class CommandCompletionTest extends ReplToolTesting {
}
try (Stream<Path> content = Files.list(home)) {
completions = content.filter(CLASSPATH_FILTER)
.filter(file -> file.getFileName().toString().startsWith(selectedFile))
.map(file -> file.getFileName().toString() + (Files.isDirectory(file) ? "/" : ""))
.filter(file -> file.getFileName().toString().startsWith(selectedFile.replace("\\ ", " ")))
.map(file -> file.getFileName().toString().replace(" ", "\\ ") + (Files.isDirectory(file) ? "/" : ""))
.sorted()
.collect(Collectors.toList());
}