Validate shell completion across supported shells
This commit is contained in:
@@ -67,8 +67,10 @@ sys.stdout.write(os.environ['PROBE_REPLY'])
|
||||
if err := os.WriteFile(filepath.Join(dir, "probe"), []byte(fixture), 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(dir, "probe.app"), []byte(fixture), 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
for _, executable := range []string{"probe.app", "café"} {
|
||||
if err := os.WriteFile(filepath.Join(dir, executable), []byte(fixture), 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
if err := os.Mkdir(filepath.Join(dir, "partial dir"), 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
@@ -110,6 +112,7 @@ sys.stdout.write(os.environ['PROBE_REPLY'])
|
||||
{"short cluster directory", "probe -sc./partial", pathReply(commandmeta.CompletionDirectories, "-sc"), []string{"__complete", "-sc./partial"}, []string{"-sc./partial dir"}},
|
||||
{"short path with equals", "probe -cfoo=ba", pathReply(commandmeta.CompletionFiles, "-c"), []string{"__complete", "-cfoo=ba"}, []string{"-cfoo=bar"}},
|
||||
{"short attached choice", "probe -cbl", choiceReply("-c", "blue"), []string{"__complete", "-cbl"}, []string{"-cblue"}},
|
||||
{"short equals choice", "probe -c=bl", choiceReply("-c=", "blue"), []string{"__complete", "-c=bl"}, []string{"-c=blue"}},
|
||||
{"short choice with equals", "probe -cfoo=ba", choiceReply("-c", "foo=bar"), []string{"__complete", "-cfoo=ba"}, []string{"-cfoo=bar"}},
|
||||
{"short no suggestions", "probe -cnomatch", pathReply(commandmeta.CompletionChoices, "-c"), []string{"__complete", "-cnomatch"}, []string{"-cnomatch"}},
|
||||
{"literal prefix marker", "probe __commandkit_path_pre", choiceReply("", completionPathPrefixMarker+"-c"), []string{"__complete", "__commandkit_path_pre"}, []string{completionPathPrefixMarker + "-c"}},
|
||||
@@ -158,28 +161,30 @@ sys.stdout.write(os.environ['PROBE_REPLY'])
|
||||
}
|
||||
})
|
||||
}
|
||||
t.Run(shell+"/dotted executable", func(t *testing.T) {
|
||||
var generated bytes.Buffer
|
||||
if err := CompletionScript(&generated, "probe.app", shell); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(script, generated.Bytes(), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
log := filepath.Join(dir, "log")
|
||||
if err := os.WriteFile(log, nil, 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
cmd := exec.Command("python3", "testdata/completion_terminal.py", shell, script, "probe.app --config=./par")
|
||||
cmd.Env = append(os.Environ(), "PATH="+dir+string(os.PathListSeparator)+os.Getenv("PATH"), "HOME="+dir, "TERM=xterm", "PROBE_LOG="+log, "PROBE_REPLY="+reply("./partial file\tdescription\n", spfcobra.ShellCompDirectiveNoFileComp), "PROBE_DIR="+dir)
|
||||
if out, err := cmd.CombinedOutput(); err != nil {
|
||||
t.Fatalf("shell completion: %v\n%s", err, out)
|
||||
}
|
||||
contents, err := os.ReadFile(log)
|
||||
if err != nil || !strings.Contains(string(contents), `["__complete", "--config=./par"]`) || !strings.Contains(string(contents), `["--config=./partial file"]`) {
|
||||
t.Fatalf("query and edited line: %q; read error: %v", contents, err)
|
||||
}
|
||||
})
|
||||
for _, executable := range []string{"probe.app", "café"} {
|
||||
t.Run(shell+"/executable "+executable, func(t *testing.T) {
|
||||
var generated bytes.Buffer
|
||||
if err := CompletionScript(&generated, executable, shell); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(script, generated.Bytes(), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
log := filepath.Join(dir, "log")
|
||||
if err := os.WriteFile(log, nil, 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
cmd := exec.Command("python3", "testdata/completion_terminal.py", shell, script, executable+" --config=./par")
|
||||
cmd.Env = append(os.Environ(), "PATH="+dir+string(os.PathListSeparator)+os.Getenv("PATH"), "HOME="+dir, "TERM=xterm", "PROBE_LOG="+log, "PROBE_REPLY="+reply("./partial file\tdescription\n", spfcobra.ShellCompDirectiveNoFileComp), "PROBE_DIR="+dir)
|
||||
if out, err := cmd.CombinedOutput(); err != nil {
|
||||
t.Fatalf("shell completion: %v\n%s", err, out)
|
||||
}
|
||||
contents, err := os.ReadFile(log)
|
||||
if err != nil || !strings.Contains(string(contents), `["__complete", "--config=./par"]`) || !strings.Contains(string(contents), `["--config=./partial file"]`) {
|
||||
t.Fatalf("query and edited line: %q; read error: %v", contents, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
14
internal/cobra/testdata/completion_terminal.py
vendored
14
internal/cobra/testdata/completion_terminal.py
vendored
@@ -39,7 +39,17 @@ try:
|
||||
child.expect_exact("PROMPT> ")
|
||||
child.send(line)
|
||||
child.send("\t")
|
||||
child.sendline("")
|
||||
child.expect_exact("PROMPT> ")
|
||||
if os.environ.get("PROBE_LIST") == "1":
|
||||
child.send("\t")
|
||||
remaining = ["Readable text", "YAML output"]
|
||||
captured = []
|
||||
while remaining:
|
||||
index = child.expect_exact(remaining)
|
||||
captured.append(child.before + child.after)
|
||||
remaining.pop(index)
|
||||
print("".join(captured))
|
||||
else:
|
||||
child.sendline("")
|
||||
child.expect_exact("PROMPT> ")
|
||||
finally:
|
||||
child.close(force=True)
|
||||
|
||||
Reference in New Issue
Block a user