forked from govim/govim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
govim_test.go
267 lines (232 loc) · 7.19 KB
/
govim_test.go
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
// Copyright 2018 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package govim_test
import (
"encoding/json"
"flag"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"strconv"
"strings"
"sync"
"testing"
"github.com/govim/govim"
"github.com/govim/govim/internal/plugin"
"github.com/govim/govim/testdriver"
"github.com/govim/govim/testsetup"
"github.com/rogpeppe/go-internal/testscript"
)
var (
fDebugLog = flag.Bool("debugLog", false, "whether to log debugging info from vim, govim and the test shim")
)
func TestMain(m *testing.M) {
os.Exit(testscript.RunMain(m, map[string]func() int{
"vim": testdriver.Vim,
}))
}
func TestScripts(t *testing.T) {
var waitLock sync.Mutex
var waitList []func() error
t.Run("scripts", func(t *testing.T) {
testscript.Run(t, testscript.Params{
Dir: "testdata",
Cmds: map[string]func(ts *testscript.TestScript, neg bool, args []string){
"sleep": testdriver.Sleep,
"errlogmatch": testdriver.ErrLogMatch,
},
Condition: testdriver.Condition,
Setup: func(e *testscript.Env) error {
home := filepath.Join(e.WorkDir, "home")
e.Vars = append(e.Vars,
"HOME="+home,
)
testPluginPath := filepath.Join(e.WorkDir, "home", ".vim", "pack", "plugins", "start", "govim")
var vimDebugLogPath, govimDebugLogPath string
errLog := new(testdriver.LockingBuffer)
outputs := []io.Writer{
errLog,
}
e.Values[testdriver.KeyErrLog] = errLog
if os.Getenv(testsetup.EnvTestscriptStderr) == "true" {
outputs = append(outputs, os.Stderr)
}
if *fDebugLog {
fmt.Printf("Vim home path is at %s\n", home)
vimDebugLog, err := ioutil.TempFile("", "govim_test_script_vim_debug_log*")
if err != nil {
t.Fatalf("failed to create govim log file: %v", err)
}
vimDebugLogPath = vimDebugLog.Name()
fmt.Printf("Vim debug logging enabled for %v at %v\n", filepath.Base(e.WorkDir), vimDebugLog.Name())
tf, err := ioutil.TempFile("", "govim_test_script_govim_log*")
if err != nil {
t.Fatalf("failed to create govim log file: %v", err)
}
outputs = append(outputs, tf)
govimDebugLogPath = tf.Name()
fmt.Printf("logging %v to %v\n", filepath.Base(e.WorkDir), tf.Name())
}
d := newTestPlugin(plugin.NewDriver(""))
config := &testdriver.Config{
Name: filepath.Base(e.WorkDir),
GovimPath: ".",
TestHomePath: home,
TestPluginPath: testPluginPath,
Env: e,
Plugin: d,
Log: io.MultiWriter(outputs...),
Debug: testdriver.Debug{
Enabled: *fDebugLog,
// FYI increasing this to 8 or above seems to cause Vim to do something weird with stdout, which means some tests fail
VimLogLevel: 7,
VimLogPath: vimDebugLogPath,
GovimLogPath: govimDebugLogPath,
},
}
td, err := testdriver.NewTestDriver(config)
if err != nil {
t.Fatalf("failed to create new driver: %v", err)
}
if err := td.Run(); err != nil {
t.Fatalf("failed to run TestDriver: %v", err)
}
waitLock.Lock()
waitList = append(waitList, td.Wait)
waitLock.Unlock()
e.Defer(func() {
td.Close()
})
return nil
},
})
})
var errLock sync.Mutex
var errors []error
var wg sync.WaitGroup
for _, w := range waitList {
w := w
wg.Add(1)
go func() {
if err := w(); err != nil {
errLock.Lock()
errors = append(errors, err)
errLock.Unlock()
}
wg.Done()
}()
}
wg.Wait()
if len(errors) > 0 {
var msgs []string
for _, e := range errors {
msgs = append(msgs, e.Error())
}
t.Fatalf("got some errors:\n%v\n", strings.Join(msgs, "\n"))
}
}
type testplugin struct {
plugin.Driver
*testpluginvim
}
type testpluginvim struct {
plugin.Driver
*testplugin
}
func newTestPlugin(d plugin.Driver) *testplugin {
res := &testplugin{
Driver: d,
testpluginvim: &testpluginvim{
Driver: d,
},
}
res.testpluginvim.testplugin = res
return res
}
func (t *testplugin) Init(g govim.Govim, errCh chan error) (err error) {
t.Driver.Govim = g
t.testpluginvim.Driver.Govim = g.Scheduled()
t.DefineFunction("HelloNil", nil, t.hello)
t.DefineFunction("Hello", []string{}, t.hello)
t.DefineFunction("HelloWithArg", []string{"target"}, t.helloWithArg)
t.DefineFunction("HelloWithVarArgs", []string{"target", "..."}, t.helloWithVarArgs)
t.DefineFunction("Bad", []string{}, t.bad)
t.DefineRangeFunction("Echo", []string{}, t.echo)
t.DefineCommand("HelloComm", t.helloComm, govim.AttrBang)
t.DefineAutoCommand("", govim.Events{govim.EventBufRead}, govim.Patterns{"*.go"}, false, t.bufRead, "expand('<afile>')")
t.DefineFunction("Func1", []string{}, t.func1)
t.DefineFunction("Func2", []string{}, t.func2)
t.DefineFunction("TriggerUnscheduled", []string{}, t.triggerUnscheduled)
t.DefineFunction("VersionCheck", []string{}, t.versionCheck)
return nil
}
func (t *testplugin) Shutdown() error {
return nil
}
func (t *testpluginvim) bufRead(args ...json.RawMessage) error {
// we are expecting the expanded result of <afile> as our first argument
fn := t.ParseString(args[0])
t.ChannelExf(`echom "Hello from BufRead %v"`, fn)
return nil
}
func (t *testpluginvim) helloComm(flags govim.CommandFlags, args ...string) error {
t.ChannelExf(`echom "Hello world (%v)"`, *flags.Bang)
return nil
}
func (t *testpluginvim) hello(args ...json.RawMessage) (interface{}, error) {
return "World", nil
}
func (t *testpluginvim) helloWithArg(args ...json.RawMessage) (interface{}, error) {
// Params: (target string)
return t.ParseString(args[0]), nil
}
func (t *testpluginvim) helloWithVarArgs(args ...json.RawMessage) (interface{}, error) {
// Params: (target string, others ...string)
parts := []string{t.ParseString(args[0])}
varargs := t.ParseJSONArgSlice(args[1])
for _, a := range varargs {
parts = append(parts, t.ParseString(a))
}
return strings.Join(parts, " "), nil
}
func (t *testpluginvim) bad(args ...json.RawMessage) (interface{}, error) {
return nil, fmt.Errorf("this is a bad function")
}
func (t *testpluginvim) echo(first, last int, jargs ...json.RawMessage) (interface{}, error) {
args := make([]interface{}, len(jargs))
for i, a := range jargs {
if err := json.Unmarshal(a, &args[i]); err != nil {
return nil, fmt.Errorf("failed to unmarshal arg %v: %v", i+1, err)
}
}
var lines []string
for i := first; i <= last; i++ {
line := t.ParseString(t.ChannelExprf("getline(%v)", i))
lines = append(lines, line)
}
t.ChannelExf("echom %v", strconv.Quote(strings.Join(lines, "\n")))
return nil, nil
}
func (t *testpluginvim) func1(args ...json.RawMessage) (interface{}, error) {
res := t.ParseString(t.ChannelCall("Func2"))
return res, nil
}
func (t *testpluginvim) func2(args ...json.RawMessage) (interface{}, error) {
return "World from Func2", nil
}
func (t *testpluginvim) triggerUnscheduled(args ...json.RawMessage) (interface{}, error) {
go func() {
t.testplugin.Schedule(func(g govim.Govim) error {
g.ChannelNormal("iHello Gophers")
g.ChannelEx("w out")
return nil
})
}()
return nil, nil
}
func (t *testpluginvim) versionCheck(args ...json.RawMessage) (interface{}, error) {
return fmt.Sprintf("%v %v", t.Flavor(), t.Version()), nil
}