aboutsummaryrefslogtreecommitdiff
path: root/test/functional/shell/viml_system_spec.lua
blob: 00b16e9158f6e6cc804560f5b5a363ea3d867c82 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
-- Specs for
-- - `system()`
-- - `systemlist()`

local helpers = require('test.functional.helpers')
local eq, clear, eval, feed, nvim =
  helpers.eq, helpers.clear, helpers.eval, helpers.feed, helpers.nvim

local Screen = require('test.functional.ui.screen')


local function create_file_with_nuls(name)
  return function()
    feed('ipart1<C-V>000part2<C-V>000part3<ESC>:w '..name..'<CR>')
    eval('1')  -- wait for the file to be created
  end
end

local function delete_file(name)
  return function()
    eval("delete('"..name.."')")
  end
end

-- Some tests require the xclip program and a x server.
local xclip = nil
do
  if os.getenv('DISPLAY') then
    xclip = (os.execute('command -v xclip > /dev/null 2>&1') == 0)
  end
end

describe('system()', function()
  before_each(clear)

  it('sets the v:shell_error variable', function()
    eval([[system("sh -c 'exit'")]])
    eq(0, eval('v:shell_error'))
    eval([[system("sh -c 'exit 1'")]])
    eq(1, eval('v:shell_error'))
    eval([[system("sh -c 'exit 5'")]])
    eq(5, eval('v:shell_error'))
    eval([[system('this-should-not-exist')]])
    eq(127, eval('v:shell_error'))
  end)

  describe('executes shell function if passed a string', function()
    local screen

    before_each(function()
        clear()
        screen = Screen.new()
        screen:attach()
    end)

    after_each(function()
        screen:detach()
    end)

    it('`echo` and waits for its return', function()
      feed(':call system("echo")<cr>')
      screen:expect([[
        ^                                                     |
        ~                                                    |
        ~                                                    |
        ~                                                    |
        ~                                                    |
        ~                                                    |
        ~                                                    |
        ~                                                    |
        ~                                                    |
        ~                                                    |
        ~                                                    |
        ~                                                    |
        ~                                                    |
        :call system("echo")                                 |
      ]])
    end)

    it('`yes` and is interrupted with CTRL-C', function()
      feed(':call system("yes")<cr>')
      screen:expect([[
                                                             |
        ~                                                    |
        ~                                                    |
        ~                                                    |
        ~                                                    |
        ~                                                    |
        ~                                                    |
        ~                                                    |
        ~                                                    |
        ~                                                    |
        ~                                                    |
        ~                                                    |
        ~                                                    |
        :call system("yes")                                  |
      ]])
      feed('<c-c>')
      screen:expect([[
        ^                                                     |
        ~                                                    |
        ~                                                    |
        ~                                                    |
        ~                                                    |
        ~                                                    |
        ~                                                    |
        ~                                                    |
        ~                                                    |
        ~                                                    |
        ~                                                    |
        ~                                                    |
        ~                                                    |
        Type  :quit<Enter>  to exit Nvim                     |
      ]])
    end)
  end)

  describe('passing no input', function()
    it('returns the program output', function()
      eq("echoed", eval('system("echo -n echoed")'))
    end)
  end)

  describe('passing input', function()
    it('returns the program output', function()
      eq("input", eval('system("cat -", "input")'))
    end)
  end)

  describe('passing a lot of input', function()
    it('returns the program output', function()
      local input = {}
      -- write more than 1mb of data, which should be enough to overcome
      -- the os buffer limit and force multiple event loop iterations to write
      -- everything
      for _ = 1, 0xffff do
        input[#input + 1] = '01234567890ABCDEFabcdef'
      end
      input = table.concat(input, '\n')
      nvim('set_var', 'input', input)
      eq(input, eval('system("cat -", g:input)'))
    end)
  end)

  describe('passing number as input', function()
    it('stringifies the input', function()
      eq('1', eval('system("cat", 1)'))
    end)
  end)

  describe('with output containing NULs', function()
    local fname = 'Xtest'

    setup(create_file_with_nuls(fname))
    teardown(delete_file(fname))

    it('replaces NULs by SOH characters', function()
      eq('part1\001part2\001part3\n', eval('system("cat '..fname..'")'))
    end)
  end)

  describe('passing list as input', function()
    it('joins list items with linefeed characters', function()
      eq('line1\nline2\nline3',
        eval("system('cat -', ['line1', 'line2', 'line3'])"))
    end)

    -- Notice that NULs are converted to SOH when the data is read back. This
    -- is inconsistent and is a good reason for the existence of the
    -- `systemlist()` function, where input and output map to the same
    -- characters(see the following tests with `systemlist()` below)
    describe('with linefeed characters inside list items', function()
      it('converts linefeed characters to NULs', function()
        eq('l1\001p2\nline2\001a\001b\nl3',
          eval([[system('cat -', ["l1\np2", "line2\na\nb", 'l3'])]]))
      end)
    end)

    describe('with leading/trailing whitespace characters on items', function()
      it('preserves whitespace, replacing linefeeds by NULs', function()
        eq('line \nline2\001\n\001line3',
          eval([[system('cat -', ['line ', "line2\n", "\nline3"])]]))
      end)
    end)
  end)

  describe("with a program that doesn't close stdout", function()
    if not xclip then
      pending('skipped (missing xclip)', function() end)
    else
      it('will exit properly after passing input', function()
        eq('', eval([[system('xclip -i -selection clipboard', 'clip-data')]]))
        eq('clip-data', eval([[system('xclip -o -selection clipboard')]]))
      end)
    end
  end)

  describe('command passed as a list', function()
    it('does not execute &shell', function()
      eq('* $NOTHING ~/file',
         eval("system(['echo', '-n', '*', '$NOTHING', '~/file'])"))
    end)
  end)
end)

describe('systemlist()', function()
  -- behavior is similar to `system()` but it returns a list instead of a
  -- string.
  before_each(clear)

  it('sets the v:shell_error variable', function()
    eval([[systemlist("sh -c 'exit'")]])
    eq(0, eval('v:shell_error'))
    eval([[systemlist("sh -c 'exit 1'")]])
    eq(1, eval('v:shell_error'))
    eval([[systemlist("sh -c 'exit 5'")]])
    eq(5, eval('v:shell_error'))
    eval([[systemlist('this-should-not-exist')]])
    eq(127, eval('v:shell_error'))
  end)

  describe('exectues shell function', function()
    local screen

    before_each(function()
        clear()
        screen = Screen.new()
        screen:attach()
    end)

    after_each(function()
        screen:detach()
    end)

    it('`echo` and waits for its return', function()
      feed(':call systemlist("echo")<cr>')
      screen:expect([[
        ^                                                     |
        ~                                                    |
        ~                                                    |
        ~                                                    |
        ~                                                    |
        ~                                                    |
        ~                                                    |
        ~                                                    |
        ~                                                    |
        ~                                                    |
        ~                                                    |
        ~                                                    |
        ~                                                    |
        :call systemlist("echo")                             |
      ]])
    end)

    it('`yes` and is interrupted with CTRL-C', function()
      feed(':call systemlist("yes | xargs")<cr>')
      screen:expect([[
                                                             |
        ~                                                    |
        ~                                                    |
        ~                                                    |
        ~                                                    |
        ~                                                    |
        ~                                                    |
        ~                                                    |
        ~                                                    |
        ~                                                    |
        ~                                                    |
        ~                                                    |
        ~                                                    |
        :call systemlist("yes | xargs")                      |
      ]])
      feed('<c-c>')
      screen:expect([[
        ^                                                     |
        ~                                                    |
        ~                                                    |
        ~                                                    |
        ~                                                    |
        ~                                                    |
        ~                                                    |
        ~                                                    |
        ~                                                    |
        ~                                                    |
        ~                                                    |
        ~                                                    |
        ~                                                    |
        Type  :quit<Enter>  to exit Nvim                     |
      ]])
    end)
  end)

  describe('passing string with linefeed characters as input', function()
    it('splits the output on linefeed characters', function()
      eq({'abc', 'def', 'ghi'}, eval([[systemlist("cat -", "abc\ndef\nghi")]]))
    end)
  end)

  describe('passing a lot of input', function()
    it('returns the program output', function()
      local input = {}
      for _ = 1, 0xffff do
        input[#input + 1] = '01234567890ABCDEFabcdef'
      end
      nvim('set_var', 'input', input)
      eq(input, eval('systemlist("cat -", g:input)'))
    end)
  end)

  describe('with output containing NULs', function()
    local fname = 'Xtest'

    setup(create_file_with_nuls(fname))
    teardown(delete_file(fname))

    it('replaces NULs by newline characters', function()
      eq({'part1\npart2\npart3'}, eval('systemlist("cat '..fname..'")'))
    end)
  end)

  describe('passing list as input', function()
    it('joins list items with linefeed characters', function()
      eq({'line1', 'line2', 'line3'},
        eval("systemlist('cat -', ['line1', 'line2', 'line3'])"))
    end)

    -- Unlike `system()` which uses SOH to represent NULs, with `systemlist()`
    -- input and ouput are the same
    describe('with linefeed characters inside list items', function()
      it('converts linefeed characters to NULs', function()
        eq({'l1\np2', 'line2\na\nb', 'l3'},
          eval([[systemlist('cat -', ["l1\np2", "line2\na\nb", 'l3'])]]))
      end)
    end)

    describe('with leading/trailing whitespace characters on items', function()
      it('preserves whitespace, replacing linefeeds by NULs', function()
        eq({'line ', 'line2\n', '\nline3'},
          eval([[systemlist('cat -', ['line ', "line2\n", "\nline3"])]]))
      end)
    end)
  end)

  describe('handles empty lines', function()
    it('in the middle', function()
      eq({'line one','','line two'}, eval("systemlist('cat',['line one','','line two'])"))
    end)

    it('in the beginning', function()
      eq({'','line one','line two'}, eval("systemlist('cat',['','line one','line two'])"))
    end)
  end)

  describe('when keepempty option is', function()
    it('0, ignores trailing newline', function()
      eq({'aa','bb'}, eval("systemlist('cat',['aa','bb'],0)"))
      eq({'aa','bb'}, eval("systemlist('cat',['aa','bb',''],0)"))
    end)

    it('1, preserves trailing newline', function()
      eq({'aa','bb'}, eval("systemlist('cat',['aa','bb'],1)"))
      eq({'aa','bb',''}, eval("systemlist('cat',['aa','bb',''],2)"))
    end)
  end)

  describe("with a program that doesn't close stdout", function()
    if not xclip then
      pending('skipped (missing xclip)', function() end)
    else
      it('will exit properly after passing input', function()
        eq({}, eval(
          "systemlist('xclip -i -selection clipboard', ['clip', 'data'])"))
        eq({'clip', 'data'}, eval(
          "systemlist('xclip -o -selection clipboard')"))
      end)
    end
  end)
end)