-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcompletions_test.go
More file actions
97 lines (84 loc) · 2.31 KB
/
Copy pathcompletions_test.go
File metadata and controls
97 lines (84 loc) · 2.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package main
import (
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"testing"
)
func TestBashCompletion(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("bash completion not supported on Windows")
}
if _, err := exec.LookPath("bash"); err != nil {
t.Skip("bash not on PATH")
}
if err := gitConfigGlobalFixture(t); err != nil {
t.Fatalf("setup: %v", err)
}
bin, getpath := buildCompletionFixture(t)
cmd := exec.Command("bash", "-c", `source completions/git-get.bash
COMP_WORDS=("git-get" "github.com/arbourd/")
COMP_CWORD=1
_git_get
printf '%s\n' "${COMPREPLY[@]}"`)
cmd.Env = completionEnv(t, bin, getpath)
out, err := cmd.Output()
if err != nil {
t.Fatalf("bash: %v", err)
}
if got := string(out); !strings.Contains(got, "github.com/arbourd/git-get") {
t.Errorf("unexpected output: %q", got)
}
}
func TestFishCompletion(t *testing.T) {
if _, err := exec.LookPath("fish"); err != nil {
t.Skip("fish not on PATH")
}
if err := gitConfigGlobalFixture(t); err != nil {
t.Fatalf("setup: %v", err)
}
bin, getpath := buildCompletionFixture(t)
cmd := exec.Command("fish", "-c", `source completions/git-get.fish
complete --do-complete "git-get github.com/arbourd/"`)
cmd.Env = completionEnv(t, bin, getpath)
out, err := cmd.Output()
if err != nil {
t.Fatalf("fish: %v", err)
}
if got := string(out); !strings.Contains(got, "github.com/arbourd/git-get") {
t.Errorf("unexpected output: %q", got)
}
}
func buildCompletionFixture(t *testing.T) (bin, getpath string) {
t.Helper()
bin = filepath.Join(t.TempDir(), "git-get")
if runtime.GOOS == "windows" {
bin += ".exe"
}
if out, err := exec.Command("go", "build", "-o", bin, ".").CombinedOutput(); err != nil {
t.Fatalf("go build: %v\n%s", err, out)
}
getpath = t.TempDir()
seedRepos(t, getpath, []string{"github.com/arbourd/git-get"})
return bin, getpath
}
func completionEnv(t *testing.T, bin, getpath string) []string {
t.Helper()
pathSep := ":"
if runtime.GOOS == "windows" {
pathSep = ";"
}
env := make([]string, 0, len(os.Environ())+3)
for _, e := range os.Environ() {
if !strings.HasPrefix(e, "PATH=") && !strings.HasPrefix(e, "GETPATH=") && !strings.HasPrefix(e, "HOME=") {
env = append(env, e)
}
}
return append(env,
"PATH="+filepath.Dir(bin)+pathSep+os.Getenv("PATH"),
"GETPATH="+getpath,
"HOME="+t.TempDir(),
)
}