fixing auto-completion
This commit is contained in:
1 parent
f1e20856c8
commit
847b6cd5c6
2 files changed
+191
-135
No files matched your search
@@ -5,6 +5,8 @@ import (
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"sort"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
@@ -781,3 +783,68 @@ func TestFilePathCompleter_NoMatches(t *testing.T) {
|
||||
t.Errorf("expected no matches for non-existent prefix, got: %v", matches)
|
||||
}
|
||||
}
|
||||
|
||||
// completeLine runs the completer on line (cursor at the end) and returns the
|
||||
// candidates as strings together with the reported length.
|
||||
func completeLine(line string) ([]string, int) {
|
||||
cands, length := newShellCompleter().Do([]rune(line), len([]rune(line)))
|
||||
var out []string
|
||||
for _, c := range cands {
|
||||
out = append(out, string(c))
|
||||
}
|
||||
sort.Strings(out)
|
||||
return out, length
|
||||
}
|
||||
|
||||
func TestShellCompleter_Do(t *testing.T) {
|
||||
origDir, _ := os.Getwd()
|
||||
defer os.Chdir(origDir)
|
||||
|
||||
tmpDir := t.TempDir()
|
||||
os.Chdir(tmpDir)
|
||||
os.Create("main.go")
|
||||
os.Create("my file.txt")
|
||||
os.Create(".hidden")
|
||||
os.Mkdir("subdir", 0755)
|
||||
os.Create("subdir/inner.txt")
|
||||
|
||||
// Controlled PATH with one executable, one non-executable and a
|
||||
// duplicate of a builtin
|
||||
binDir := t.TempDir()
|
||||
os.WriteFile(filepath.Join(binDir, "gitty"), nil, 0755)
|
||||
os.WriteFile(filepath.Join(binDir, "girl"), nil, 0644)
|
||||
os.WriteFile(filepath.Join(binDir, "echo"), nil, 0755)
|
||||
os.Mkdir(filepath.Join(binDir, "gidir"), 0755)
|
||||
t.Setenv("PATH", binDir)
|
||||
|
||||
tests := []struct {
|
||||
line string
|
||||
want []string
|
||||
length int
|
||||
}{
|
||||
{"c", []string{"d "}, 1},
|
||||
{"e", []string{"cho ", "xit ", "xport "}, 1},
|
||||
{"gi", []string{"tty "}, 2},
|
||||
{" gi", []string{"tty "}, 2},
|
||||
{"ls gi", nil, 2},
|
||||
{"ls ma", []string{"in.go"}, 2},
|
||||
{"ls su", []string{"bdir/"}, 2},
|
||||
{"ls subdir/", []string{"inner.txt"}, 0},
|
||||
{"ls subdir/in", []string{"ner.txt"}, 2},
|
||||
{"ls my", []string{`\ file.txt`}, 2},
|
||||
{`ls my\ f`, []string{"ile.txt"}, 4},
|
||||
{`ls "my f`, []string{"ile.txt"}, 4},
|
||||
{"ls ./ma", []string{"in.go"}, 2},
|
||||
{"ls .h", []string{"idden"}, 2},
|
||||
{"ls ", []string{"main.go", `my\ file.txt`, "subdir/"}, 0},
|
||||
{"ls " + tmpDir + "/ma", []string{"in.go"}, 2},
|
||||
{"ls xyz", nil, 3},
|
||||
{"ls ~", []string{"/"}, 1},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
got, length := completeLine(tt.line)
|
||||
if !reflect.DeepEqual(got, tt.want) || length != tt.length {
|
||||
t.Errorf("Do(%q) = %q, %d; want %q, %d", tt.line, got, length, tt.want, tt.length)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user