robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

robinmerge: 1dc9728daa1d8a2702

master
5.2 KiB178 linesraw
1local t = require('test.testutil')
2local n = require('test.functional.testnvim')()
3
4local describe, it, before_each = t.describe, t.it, t.before_each
5local clear = n.clear
6local exec_lua = n.exec_lua
7local eq = t.eq
8
9describe('vim.system', function()
10  before_each(function()
11    clear()
12  end)
13
14  for name, system in pairs { sync = n.system_sync, async = n.system_async } do
15    describe('(' .. name .. ')', function()
16      it('failure modes', function()
17        t.matches('cmd: expected non%-empty list of strings', t.pcall_err(system, {}))
18        t.matches('cmd: expected non%-empty list of strings', t.pcall_err(system, { 42 }))
19
20        t.matches(
21          "ENOENT%: no such file .* %(cmd%): 'non%-existent%-cmd'",
22          t.pcall_err(system, { 'non-existent-cmd', 'arg1', 'arg2' }, { text = true })
23        )
24
25        t.matches(
26          "ENOENT%: no such file .* %(cwd%): 'non%-existent%-cwd'",
27          t.pcall_err(system, { 'echo', 'hello' }, { cwd = 'non-existent-cwd', text = true })
28        )
29      end)
30
31      it('can run simple commands', function()
32        eq('hello\n', system({ 'echo', 'hello' }, { text = true }).stdout)
33      end)
34
35      it('handle input', function()
36        eq('hellocat', system({ 'cat' }, { stdin = 'hellocat', text = true }).stdout)
37      end)
38
39      it('uses real pipes for stdin/stdout #35984', function()
40        if t.is_os('win') then
41          return -- Not applicable for Windows.
42        end
43        local res = system(
44          { 'bash', '-c', 'wc /dev/stdin > /dev/stdout' },
45          { stdin = 'pipe\npipy text\n' }
46        )
47        eq({ '2', '3', '15', '/dev/stdin' }, vim.split(res.stdout, '%s+', { trimempty = true }))
48      end)
49
50      it('can set environment', function()
51        local function test_env(opt)
52          eq('TESTVAL', system({ n.testprg('printenv-test'), 'TEST' }, opt).stdout)
53        end
54
55        test_env({ env = { TEST = 'TESTVAL' }, text = true })
56        test_env({ clear_env = true, env = { TEST = 'TESTVAL' }, text = true })
57      end)
58
59      it('can set environment with clear_env = true and env = nil', function()
60        exec_lua(function()
61          vim.env.TEST = 'TESTVAL'
62        end)
63
64        -- Not passing env with clear_env should not clear the environment
65        eq(
66          'TESTVAL',
67          system({ n.testprg('printenv-test'), 'TEST' }, { clear_env = true, text = true }).stdout
68        )
69
70        -- Passing env = {} with clear_env should clear the environment
71        eq(
72          '',
73          system(
74            { n.testprg('printenv-test'), 'TEST' },
75            { env = {}, clear_env = true, text = true }
76          ).stdout
77        )
78      end)
79
80      it('supports timeout', function()
81        eq({
82          code = 124,
83          signal = 15,
84          stdout = '',
85          stderr = '',
86        }, system({ 'sleep', '10' }, { timeout = 1000 }))
87      end)
88    end)
89  end
90
91  it('kill processes', function()
92    exec_lua(function()
93      local signal --- @type integer?
94      local cmd = vim.system({ 'cat', '-' }, { stdin = true }, function(r)
95        signal = r.signal
96      end) -- run forever
97
98      cmd:kill('sigint')
99
100      -- wait for the process not to exist
101      local done = vim.wait(2000, function()
102        return signal ~= nil
103      end)
104
105      assert(done, 'process did not exit')
106
107      -- Check the process is no longer running
108      local proc = vim.api.nvim_get_proc(cmd.pid)
109      assert(not proc, 'process still exists')
110
111      assert(signal == 2)
112    end)
113  end)
114
115  it('SystemObj:wait() does not process non-fast events #27292', function()
116    eq(
117      false,
118      exec_lua(function()
119        _G.processed = false
120        local cmd = vim.system({ 'sleep', '1' })
121        vim.schedule(function()
122          _G.processed = true
123        end)
124        cmd:wait()
125        return _G.processed
126      end)
127    )
128    eq(true, exec_lua([[return _G.processed]]))
129  end)
130
131  if t.is_os('win') then
132    it('can resolve windows command extensions', function()
133      t.write_file('test.bat', 'echo hello world')
134      n.system_sync({ 'chmod', '+x', 'test.bat' })
135      n.system_sync({ './test' })
136    end)
137
138    it('launches cmd.exe when shellslash is set', function()
139      n.command('set shellslash')
140      local path = exec_lua([[return vim.fn.exepath('cmd.exe')]])
141      t.neq(nil, path:find('/', 1, true))
142
143      local result = n.system_sync({ 'cmd.exe', '/c', 'echo OK' })
144      eq(0, result.code)
145      eq('OK\r\n', result.stdout)
146      eq('', result.stderr)
147    end)
148  end
149
150  it('always captures all content of stdout/stderr #30846', function()
151    t.skip(n.fn.executable('git') == 0, 'N/A: missing "git" command')
152    t.skip(n.fn.isdirectory('.git') == 0, 'N/A: missing ".git" directory')
153    eq(
154      0,
155      exec_lua(function()
156        local done = 0
157        local fail = 0
158        for _ = 1, 200 do
159          vim.system(
160            { 'git', 'show', ':0:test/functional/plugin/lsp_spec.lua' },
161            { text = true },
162            function(o)
163              if o.code ~= 0 or #o.stdout == 0 then
164                fail = fail + 1
165              end
166              done = done + 1
167            end
168          )
169        end
170
171        local ok = vim.wait(10000, function()
172          return done == 200
173        end, 200)
174        return fail + (ok and 0 or 1)
175      end)
176    )
177  end)
178end)