Skip to content

Commit

Permalink
8277328: jdk/jshell/CommandCompletionTest.java failures on Windows
Browse files Browse the repository at this point in the history
Backport-of: 3955b037da8a0981d8efc67f28caaacdef7dfb31
  • Loading branch information
GoeLin committed Jan 14, 2022
1 parent 3a96302 commit 96c8499
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ public int read() throws IOException {
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;
Expand Down Expand Up @@ -1523,13 +1523,13 @@ private static CompletionProvider skipWordThenCompletion(CompletionProvider comp
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...
Expand Down
25 changes: 21 additions & 4 deletions test/langtools/jdk/jshell/CommandCompletionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -331,24 +331,41 @@ public void testClassPath() throws IOException {
);
}

@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;
Path home = Paths.get(System.getProperty("user.home"));
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) {
throw new SkipException("No suitable file(s) found for this test in " + home);
}
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());
}
Expand Down

1 comment on commit 96c8499

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.