|
| 1 | +package nvimtest |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/neovim/go-client/nvim" |
| 10 | +) |
| 11 | + |
| 12 | +// NewChildProcess returns the new *Nvim, and registers cleanup to tb.Cleanup. |
| 13 | +func NewChildProcess(tb testing.TB) *nvim.Nvim { |
| 14 | + tb.Helper() |
| 15 | + |
| 16 | + ctx, cancel := context.WithCancel(context.Background()) |
| 17 | + tb.Cleanup(func() { |
| 18 | + cancel() |
| 19 | + }) |
| 20 | + |
| 21 | + tmpdir := tb.TempDir() |
| 22 | + envs := os.Environ() |
| 23 | + envs = append(envs, []string{ |
| 24 | + fmt.Sprintf("XDG_CONFIG_HOME=%s", tmpdir), |
| 25 | + fmt.Sprintf("XDG_DATA_HOME=%s", tmpdir), |
| 26 | + fmt.Sprintf("NVIM_LOG_FILE=%s", os.DevNull), |
| 27 | + }...) |
| 28 | + opts := []nvim.ChildProcessOption{ |
| 29 | + nvim.ChildProcessCommand(nvim.BinaryName), |
| 30 | + nvim.ChildProcessArgs( |
| 31 | + "--clean", // Mimics a fresh install of Nvim. See :help --clean |
| 32 | + "--embed", // Use stdin/stdout as a msgpack-RPC channel, so applications can embed and control Nvim via the RPC API. |
| 33 | + "--headless", // Start without UI, and do not wait for nvim_ui_attach |
| 34 | + "-c", "set packpath=", // Clean packpath |
| 35 | + ), |
| 36 | + nvim.ChildProcessContext(ctx), |
| 37 | + nvim.ChildProcessEnv(envs), |
| 38 | + nvim.ChildProcessServe(true), |
| 39 | + nvim.ChildProcessLogf(tb.Logf), |
| 40 | + } |
| 41 | + n, err := nvim.NewChildProcess(opts...) |
| 42 | + if err != nil { |
| 43 | + tb.Fatal(err) |
| 44 | + } |
| 45 | + |
| 46 | + tb.Cleanup(func() { |
| 47 | + if err := n.Close(); err != nil { |
| 48 | + tb.Fatal(err) |
| 49 | + } |
| 50 | + }) |
| 51 | + |
| 52 | + return n |
| 53 | +} |
0 commit comments