Skip to content

Commit

Permalink
picocli fish completion tests
Browse files Browse the repository at this point in the history
  • Loading branch information
serg-v committed Jan 22, 2023
1 parent 1af8274 commit 5e210f0
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 10 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,7 @@ picocli.iml
/src/test/dejagnu.tests/testrun.log
/src/test/dejagnu.tests/testrun.sum
/src/test/dejagnu.tests/tmp/
/src/test/dejagnu.fishtests/log/completion.sum
/src/test/dejagnu.fishtests/log/completion.log
/picocli-legacy-tests/gradle/wrapper/dists/**/*.lck
/picocli-legacy-tests/gradle/wrapper/dists/**/*.ok
32 changes: 22 additions & 10 deletions src/main/java/picocli/AutoComplete.java
Original file line number Diff line number Diff line change
Expand Up @@ -554,22 +554,18 @@ public static String fish(String scriptName, CommandLine commandLine) {
continue;
}
if (!descriptor.parentFunctionName.equals(parentFunction)) {
if (!currentLevelCommands.isEmpty()) {
processLevel(scriptName, result, currentLevel, currentLevelCommands, parentFunction, rootDescriptor);
rootDescriptor = null;
processLevel(scriptName, result, currentLevel, currentLevelCommands, parentFunction, rootDescriptor);
rootDescriptor = null;

currentLevel.clear();
currentLevelCommands.clear();
}
currentLevel.clear();
currentLevelCommands.clear();
parentFunction = descriptor.parentFunctionName;
}

currentLevel.add(descriptor);
currentLevelCommands.add(descriptor.commandName);
}
if (!currentLevelCommands.isEmpty()) {
processLevel(scriptName, result, currentLevel, currentLevelCommands, parentFunction, rootDescriptor);
}
processLevel(scriptName, result, currentLevel, currentLevelCommands, parentFunction, rootDescriptor);


return result.toString();
Expand All @@ -578,17 +574,27 @@ public static String fish(String scriptName, CommandLine commandLine) {
private static void processLevel(String scriptName, StringBuilder result, List<CommandDescriptor> currentLevel,
List<String> currentLevelCommands, String levelName,
CommandDescriptor rootDescriptor) {
result.append("\n# ").append(levelName).append(" completion \n");
if (levelName.equals("")) {
levelName = "root";
}
result.append("\n# ").append(levelName).append(" completion\n");
result.append("set -l ").append(levelName).append(" ").append(String.join(" ", currentLevelCommands)).append(
"\n");
if (rootDescriptor != null) {
for (OptionSpec optionSpec : rootDescriptor.commandLine.getCommandSpec().options()) {
result.append("complete -c ").append(scriptName);
result.append(" -n \"not __fish_seen_subcommand_from $").append(levelName).append("\"");
result.append(" -l ").append(optionSpec.longestName().replace("--", ""));

if (optionSpec.completionCandidates() != null) {
result.append(" -f -a '").append(String.join(" ", extract(optionSpec.completionCandidates()))).append("' ");
}

String optionDescription = sanitizeDescription(optionSpec.description().length > 0 ? optionSpec.description()[0] : "");
result.append(" -d '").append(optionDescription).append("'\n");



if (!optionSpec.shortestName().equals(optionSpec.longestName())) {
result.append("complete -c ").append(scriptName);
result.append(" -n \"not __fish_seen_subcommand_from $").append(levelName).append("\"");
Expand Down Expand Up @@ -617,9 +623,15 @@ private static void processLevel(String scriptName, StringBuilder result, List<C
commandDescriptor.parentWithoutTopLevelCommand).append("'");
}
result.append(" -l ").append(optionSpec.longestName().replace("--", ""));

if (optionSpec.completionCandidates() != null) {
result.append(" -a '").append(String.join(" ", extract(optionSpec.completionCandidates()))).append("' ");
}

String optionDescription = sanitizeDescription(optionSpec.description().length > 0 ? optionSpec.description()[0] : "");
result.append(" -d '").append(optionDescription).append("'\n");


if (!optionSpec.shortestName().equals(optionSpec.longestName())) {
result.append("complete -c ").append(scriptName);
result.append(" -n \"__fish_seen_subcommand_from ").append(commandDescriptor.commandName).append("\"");
Expand Down
4 changes: 4 additions & 0 deletions src/test/dejagnu.fishtests/README.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Run
```
runtest --outdir log --tool completion
```
30 changes: 30 additions & 0 deletions src/test/dejagnu.fishtests/completion/simple.exp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
exp_internal 1
set timeout 1

proc run_completion_test {cmd test candidates} {
send "${cmd}\t"
expect {
-re "(\n${candidates}\u001b)" { pass $test }
timeout { fail $test }
}
puts "###Output"
puts "$expect_out(1,string)"
send "\x03"
expect ">"
}

set cmd "basicExample -"
set test "Tab should show options for '$cmd'"
set candidates "-t -u --timeout --timeUnit --timeUnit="
run_completion_test $cmd $test $candidates

set cmd "basicExample --"
set test "Tab should show options for '$cmd'"
set candidates "--timeout --timeUnit --timeUnit="
run_completion_test $cmd $test $candidates

set cmd "basicExample --timeUnit="
set test "Tab should show time unit enum values for '$cmd'"
#set candidates "1 2 3"
set candidates "\u2026timeUnit=DAYS \u2026timeUnit=MICROSECONDS \u2026timeUnit=MINUTES \u2026timeUnit=SECONDS\r\n\u2026timeUnit=HOURS \u2026timeUnit=MILLISECONDS \u2026timeUnit=NANOSECONDS "
run_completion_test $cmd $test $candidates
8 changes: 8 additions & 0 deletions src/test/dejagnu.fishtests/lib/completion.exp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
exp_spawn fish --no-config
expect -re "(.+>)"

send "source ../resources/basic.fish\r"
expect -re "(.+>)"

send "function basicExample; echo 'do'; end\r"
expect -re "(.+>)"
Empty file modified src/test/dejagnu.tests/runCompletion
100644 → 100755
Empty file.
8 changes: 8 additions & 0 deletions src/test/java/picocli/AutoCompleteTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,14 @@ public void basic() throws Exception {
assertEquals(expected, script);
}

@Test
public void basicFish() throws Exception {
String script = AutoComplete.fish("basicExample", new CommandLine(new BasicExample()));
System.out.println(script);
String expected = loadTextFromClasspath("/basic.fish");
assertEquals(expected, script);
}

public static class TopLevel {
@Option(names = {"-V", "--version"}, help = true) boolean versionRequested;
@Option(names = {"-h", "--help"}, help = true) boolean helpRequested;
Expand Down
7 changes: 7 additions & 0 deletions src/test/resources/basic.fish
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

# root completion
set -l root
complete -c basicExample -n "not __fish_seen_subcommand_from $root" -l timeUnit -f -a 'NANOSECONDS MICROSECONDS MILLISECONDS SECONDS MINUTES HOURS DAYS' -d ''
complete -c basicExample -n "not __fish_seen_subcommand_from $root" -s u -d ''
complete -c basicExample -n "not __fish_seen_subcommand_from $root" -l timeout -d ''
complete -c basicExample -n "not __fish_seen_subcommand_from $root" -s t -d ''

0 comments on commit 5e210f0

Please sign in to comment.