|
1 | 1 | package plugin_test
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "fmt" |
4 | 7 | "os"
|
| 8 | + "path/filepath" |
5 | 9 | "strings"
|
6 | 10 | "testing"
|
7 | 11 |
|
8 | 12 | "github.com/neovim/go-client/nvim"
|
9 | 13 | "github.com/neovim/go-client/nvim/plugin"
|
10 | 14 | )
|
11 | 15 |
|
12 |
| -func newEmbeddedPlugin(t *testing.T) (*plugin.Plugin, func()) { |
| 16 | +func newChildProcess(tb testing.TB) (p *plugin.Plugin, cleanup func()) { |
| 17 | + tb.Helper() |
| 18 | + |
13 | 19 | env := []string{}
|
14 | 20 | if v := os.Getenv("VIM"); v != "" {
|
15 | 21 | env = append(env, "VIM="+v)
|
16 | 22 | }
|
17 | 23 |
|
| 24 | + ctx := context.Background() |
18 | 25 | opts := []nvim.ChildProcessOption{
|
19 | 26 | 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), |
21 | 30 | nvim.ChildProcessEnv(env),
|
22 |
| - nvim.ChildProcessLogf(t.Logf), |
23 | 31 | }
|
24 | 32 | v, err := nvim.NewChildProcess(opts...)
|
25 | 33 | if err != nil {
|
26 |
| - t.Fatal(err) |
| 34 | + tb.Fatal(err) |
27 | 35 | }
|
28 | 36 |
|
29 |
| - return plugin.New(v), func() { |
| 37 | + done := make(chan error, 1) |
| 38 | + go func() { |
| 39 | + done <- v.Serve() |
| 40 | + }() |
| 41 | + |
| 42 | + cleanup = func() { |
30 | 43 | 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))) |
32 | 75 | }
|
33 | 76 | }
|
| 77 | + |
| 78 | + return plugin.New(v), cleanup |
34 | 79 | }
|
35 | 80 |
|
36 | 81 | 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 | + }) |
39 | 86 |
|
40 | 87 | p.Handle("hello", func(s string) (string, error) {
|
41 | 88 | return "Hello, " + s, nil
|
|
0 commit comments