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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
|
package main
import (
"fmt"
"io"
"io/ioutil"
"log"
"os"
"sort"
"strconv"
"strings"
"testing"
"bou.ke/monkey"
"github.com/posener/complete/v2"
)
func TestPredictions(t *testing.T) {
t.Parallel()
tests := []struct {
name string
predictor complete.Predictor
prefix string
want []string
}{
{
name: "predict tests ok",
predictor: predictTest,
want: []string{
"TestPredictions",
"Example",
"TestErrorSupression",
},
},
{
name: "predict benchmark ok",
predictor: predictBenchmark,
want: []string{"BenchmarkFake"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := tt.predictor.Predict(tt.prefix)
if !equal(got, tt.want) {
t.Errorf("Failed %s: got: %q, want: %q", t.Name(), got, tt.want)
}
})
}
}
func BenchmarkFake(b *testing.B) {}
func Example() {
p := monkey.Patch(os.Exit, func(int) {})
defer p.Unpatch()
os.Setenv("COMP_LINE", "go ru")
os.Setenv("COMP_POINT", "5")
main()
// output: run
}
func equal(s1, s2 []string) bool {
sort.Strings(s1)
sort.Strings(s2)
if len(s1) != len(s2) {
return false
}
for i := range s1 {
if s1[i] != s2[i] {
return false
}
}
return true
}
func TestErrorSupression(t *testing.T) {
defer monkey.Patch(os.Exit, func(int) {}).Unpatch()
// Completion API environment variable names.
const envLine, envPoint = "COMP_LINE", "COMP_POINT"
// line should work out to
//
// * on most POSIX:
// go test /tmp/
// * on MacOS X:
// go test /var/folders/<randomized_pathname>/T//
// * on Windows:
// go test C:\Users\<username>\AppData\Local\Temp\
//
// which should trigger "failed importing directory: ... no
// buildable Go source files..." error messages.
var line = "go test " + os.TempDir() + string(os.PathSeparator)
defer os.Unsetenv(envLine)
defer os.Unsetenv(envPoint)
os.Setenv(envLine, line)
os.Setenv(envPoint, strconv.Itoa(len(line)))
tests := []struct {
verbose string
wantErr bool
}{{
verbose: "",
wantErr: false,
}, {
verbose: "1",
wantErr: true,
}}
for _, tt := range tests {
t.Run(fmt.Sprintf(
"%s=%q", envVerbose, tt.verbose,
), func(t *testing.T) {
// Discard completion (stdout).
r, w, err := os.Pipe()
if err != nil {
t.Fatal(err)
}
defer w.Close()
defer func(o *os.File) { os.Stdout = o }(os.Stdout)
os.Stdout = w
go io.Copy(ioutil.Discard, r)
// "Redirect" stderr into a buffer.
b := &strings.Builder{}
log.SetOutput(b)
defer os.Unsetenv(envVerbose)
os.Setenv(envVerbose, tt.verbose)
main()
gotErr := b.Len() != 0
if tt.wantErr && !gotErr {
t.Fatal("want something in stderr, got nothing")
} else if !tt.wantErr && gotErr {
t.Fatalf("want nothing in stderr, got %d bytes",
b.Len())
}
})
}
}
|