robin/laufey

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

robinmerge: 1dc9728daa1d8a2702

master
7.1 KiB220 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 eq = t.eq
6local ok = t.ok
7local exec_lua = n.exec_lua
8local clear = n.clear
9local feed = n.feed
10local eval = n.eval
11local is_ci = t.is_ci
12local is_os = t.is_os
13local poke_eventloop = n.poke_eventloop
14
15describe('vim.ui', function()
16  before_each(function()
17    clear({ args_rm = { '-u' }, args = { '--clean' } })
18  end)
19
20  describe('input()', function()
21    it('can input text', function()
22      local result = exec_lua [[
23        local opts = {
24            prompt = 'Input: ',
25        }
26        local input
27        local cb = function(item)
28          input = item
29        end
30        -- input would require input and block the test;
31        local prompt
32        vim.fn.input = function(opts)
33          prompt = opts.prompt
34          return "Inputted text"
35        end
36        vim.ui.input(opts, cb)
37        vim.wait(100, function() return input ~= nil end)
38        return {input, prompt}
39      ]]
40      eq('Inputted text', result[1])
41      eq('Input: ', result[2])
42    end)
43
44    it('can input text on nil opt', function()
45      feed(':lua vim.ui.input(nil, function(input) result = input end)<cr>')
46      eq('', eval('v:errmsg'))
47      feed('Inputted text<cr>')
48      eq('Inputted text', exec_lua('return result'))
49    end)
50
51    it('can input text on {} opt', function()
52      feed(':lua vim.ui.input({}, function(input) result = input end)<cr>')
53      eq('', eval('v:errmsg'))
54      feed('abcdefg<cr>')
55      eq('abcdefg', exec_lua('return result'))
56    end)
57
58    it('can input empty text #18144', function()
59      feed(':lua vim.ui.input({}, function(input) result = input end)<cr>')
60      feed('<cr>')
61      eq('', exec_lua('return result'))
62    end)
63
64    it('can input empty text with cancelreturn opt #18144', function()
65      feed(':lua vim.ui.input({ cancelreturn = "CANCEL" }, function(input) result = input end)<cr>')
66      feed('<cr>')
67      eq('', exec_lua('return result'))
68    end)
69
70    it('can return nil when aborted with ESC #18144', function()
71      feed(':lua result = "on_confirm not called"<cr>')
72      feed(':lua vim.ui.input({}, function(input) result = input end)<cr>')
73      feed('Inputted Text<esc>')
74      -- Note: When `result == nil`, exec_lua('returns result') returns vim.NIL
75      eq(true, exec_lua('return (nil == result)'))
76    end)
77
78    it('can return opts.cancelreturn when aborted with ESC with cancelreturn opt #18144', function()
79      feed(':lua result = "on_confirm not called"<cr>')
80      feed(':lua vim.ui.input({ cancelreturn = "CANCEL" }, function(input) result = input end)<cr>')
81      feed('Inputted Text<esc>')
82      eq('CANCEL', exec_lua('return result'))
83    end)
84
85    it('can return nil when interrupted with Ctrl-C #18144', function()
86      feed(':lua result = "on_confirm not called"<cr>')
87      feed(':lua vim.ui.input({}, function(input) result = input end)<cr>')
88      poke_eventloop() -- This is needed because Ctrl-C flushes input
89      feed('Inputted Text<c-c>')
90      eq(true, exec_lua('return (nil == result)'))
91    end)
92
93    it(
94      'can return the identical object when an arbitrary opts.cancelreturn object is given',
95      function()
96        feed(':lua fn = function() return 42 end<CR>')
97        eq(42, exec_lua('return fn()'))
98        feed(':lua vim.ui.input({ cancelreturn = fn }, function(input) result = input end)<cr>')
99        feed('cancel<esc>')
100        eq(true, exec_lua('return (result == fn)'))
101        eq(42, exec_lua('return result()'))
102      end
103    )
104  end)
105
106  describe('open()', function()
107    it('validation', function()
108      if is_os('win') or not is_ci('github') then
109        exec_lua [[vim.system = function() return { wait=function() return { code=3 } end } end]]
110      end
111      if not is_os('bsd') then
112        local rv = exec_lua([[
113          local cmd, err = vim.ui.open('non-existent-file')
114          if err and err:find('no handler found') then
115            return -1
116          end
117          return cmd:wait(100).code
118        ]])
119        ok(type(rv) == 'number' and rv ~= 0, 'nonzero exit code', rv)
120      end
121
122      exec_lua [[
123        vim.fn.has = function() return 0 end
124        vim.fn.executable = function() return 0 end
125      ]]
126      eq(
127        'vim.ui.open: no handler found (tried: wslview, explorer.exe, xdg-open, lemonade)',
128        exec_lua [[local _, err = vim.ui.open('foo') ; return err]]
129      )
130    end)
131
132    it('escapes cmd.exe metacharacters in URIs #41337', function()
133      eq(
134        { 'cmd.exe', '/c', 'start', '', 'https://example.com/?q=^&^|^<^>^^^%^!' },
135        exec_lua(function()
136          vim.fn.has = function(feat)
137            return feat == 'win32' and 1 or 0
138          end
139          local captured --- @type string[]
140          vim.system = function(cmd)
141            captured = cmd
142            return {
143              wait = function()
144                return { code = 0 }
145              end,
146            }
147          end
148          vim.ui.open('https://example.com/?q=&|<>^%!')
149          return captured
150        end)
151      )
152    end)
153
154    it('opt.cmd #29490', function()
155      t.matches(
156        'ENOENT: no such file or directory',
157        t.pcall_err(exec_lua, function()
158          vim.ui.open('foo', { cmd = { 'non-existent-tool' } })
159        end)
160      )
161
162      eq(
163        {
164          code = 0,
165          signal = 0,
166          stderr = '',
167          stdout = 'arg1=arg1;arg2=https://example.com;',
168        },
169        exec_lua(function(cmd_)
170          local cmd, err = vim.ui.open('https://example.com', { cmd = cmd_ })
171          assert(cmd and not err)
172          return cmd:wait()
173        end, { n.testprg('printargs-test'), 'arg1' })
174      )
175    end)
176
177    it('can reuse opt.cmd without accumulating paths', function()
178      eq(
179        'arg1=arg1;arg2=https://example.com/second;',
180        exec_lua(function(opener)
181          local opts = { cmd = { opener, 'arg1' } }
182          vim.ui.open('https://example.com/first', opts):wait()
183          return vim.ui.open('https://example.com/second', opts):wait().stdout
184        end, n.testprg('printargs-test'))
185      )
186    end)
187
188    it('gx on a help tag opens URL', function()
189      n.command('helptags $VIMRUNTIME/doc')
190      n.command('help nvim.txt')
191
192      local link_ns = n.api.nvim_create_namespace('nvim.help.urls')
193      local tag = n.api.nvim_buf_get_extmarks(0, link_ns, 0, -1, {
194        limit = 1,
195        details = true,
196      })[1]
197
198      local url = tag[4].url
199      assert(url)
200
201      --- points to the neovim.io site
202      eq(true, vim.startswith(url, 'https://neovim.io/doc'))
203
204      -- tag is URI encoded
205      local param = url:match('%?tag=(.*)')
206      local tagname =
207        n.api.nvim_buf_get_text(0, tag[2], tag[3], tag[4].end_row, tag[4].end_col, {})[1]
208      eq(vim.uri_encode(tagname), param)
209
210      -- non-nvim tags are ignored
211      local buf = n.api.nvim_create_buf(false, false)
212      n.api.nvim_buf_set_lines(buf, 0, 0, false, {
213        '|nonexisting|',
214      })
215      n.api.nvim_set_option_value('filetype', 'help', { buf = buf, scope = 'local' })
216      local tags = n.api.nvim_buf_get_extmarks(buf, link_ns, 0, -1, {})
217      eq(0, #tags)
218    end)
219  end)
220end)