Skip to content

Commit 77a0d17

Browse files
committed
nvim/plugin: use nvim.NewChildProcess
Signed-off-by: Koichi Shiraishi <[email protected]>
1 parent 45e74ee commit 77a0d17

File tree

1 file changed

+55
-8
lines changed

1 file changed

+55
-8
lines changed

nvim/plugin/register_test.go

Lines changed: 55 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,88 @@
11
package plugin_test
22

33
import (
4+
"context"
5+
"errors"
6+
"fmt"
47
"os"
8+
"path/filepath"
59
"strings"
610
"testing"
711

812
"github.com/neovim/go-client/nvim"
913
"github.com/neovim/go-client/nvim/plugin"
1014
)
1115

12-
func newEmbeddedPlugin(t *testing.T) (*plugin.Plugin, func()) {
16+
func newChildProcess(tb testing.TB) (p *plugin.Plugin, cleanup func()) {
17+
tb.Helper()
18+
1319
env := []string{}
1420
if v := os.Getenv("VIM"); v != "" {
1521
env = append(env, "VIM="+v)
1622
}
1723

24+
ctx := context.Background()
1825
opts := []nvim.ChildProcessOption{
1926
nvim.ChildProcessCommand(nvim.BinaryName),
20-
nvim.ChildProcessArgs("-u", "NONE", "-n", "--embed"),
27+
nvim.ChildProcessArgs("-u", "NONE", "-n", "-i", "NONE", "--embed", "--headless"),
28+
nvim.ChildProcessContext(ctx),
29+
nvim.ChildProcessLogf(tb.Logf),
2130
nvim.ChildProcessEnv(env),
22-
nvim.ChildProcessLogf(t.Logf),
2331
}
2432
v, err := nvim.NewChildProcess(opts...)
2533
if err != nil {
26-
t.Fatal(err)
34+
tb.Fatal(err)
2735
}
2836

29-
return plugin.New(v), func() {
37+
done := make(chan error, 1)
38+
go func() {
39+
done <- v.Serve()
40+
}()
41+
42+
cleanup = func() {
3043
if err := v.Close(); err != nil {
31-
t.Fatal(err)
44+
tb.Fatal(err)
45+
}
46+
47+
err := <-done
48+
if err != nil {
49+
tb.Fatal(err)
50+
}
51+
52+
const nvimlogFile = ".nvimlog"
53+
wd, err := os.Getwd()
54+
if err != nil {
55+
tb.Fatal(err)
56+
}
57+
if walkErr := filepath.Walk(wd, func(path string, info os.FileInfo, err error) error {
58+
if err != nil {
59+
return err
60+
}
61+
62+
if info.IsDir() {
63+
return nil
64+
}
65+
66+
if fname := info.Name(); fname == nvimlogFile {
67+
if err := os.RemoveAll(path); err != nil {
68+
return fmt.Errorf("failed to remove %s file: %w", path, err)
69+
}
70+
}
71+
72+
return nil
73+
}); walkErr != nil && !os.IsNotExist(err) {
74+
tb.Fatal(fmt.Errorf("walkErr: %w", errors.Unwrap(walkErr)))
3275
}
3376
}
77+
78+
return plugin.New(v), cleanup
3479
}
3580

3681
func TestRegister(t *testing.T) {
37-
p, cleanup := newEmbeddedPlugin(t)
38-
defer cleanup()
82+
p, cleanup := newChildProcess(t)
83+
t.Cleanup(func() {
84+
cleanup()
85+
})
3986

4087
p.Handle("hello", func(s string) (string, error) {
4188
return "Hello, " + s, nil

0 commit comments

Comments
 (0)