aboutsummaryrefslogtreecommitdiff
path: root/test/functional/core/main_spec.lua
blob: ab11e14a67a12a8a40cc85eccf90b8a1a30dfc2d (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
local lfs = require('lfs')
local helpers = require('test.functional.helpers')(after_each)
local Screen = require('test.functional.ui.screen')

local eq = helpers.eq
local feed = helpers.feed
local eval = helpers.eval
local clear = helpers.clear
local funcs = helpers.funcs
local nvim_prog_abs = helpers.nvim_prog_abs
local write_file = helpers.write_file
local is_os = helpers.is_os
local skip = helpers.skip

describe('Command-line option', function()
  describe('-s', function()
    local fname = 'Xtest-functional-core-main-s'
    local fname_2 = fname .. '.2'
    local nonexistent_fname = fname .. '.nonexistent'
    local dollar_fname = '$' .. fname
    before_each(function()
      clear()
      os.remove(fname)
      os.remove(dollar_fname)
    end)
    after_each(function()
      os.remove(fname)
      os.remove(dollar_fname)
    end)
    it('treats - as stdin', function()
      eq(nil, lfs.attributes(fname))
      funcs.system(
        {nvim_prog_abs(), '-u', 'NONE', '-i', 'NONE', '--headless',
         '--cmd', 'set noswapfile shortmess+=IFW fileformats=unix',
         '-s', '-', fname},
        {':call setline(1, "42")', ':wqall!', ''})
      eq(0, eval('v:shell_error'))
      local attrs = lfs.attributes(fname)
      eq(#('42\n'), attrs.size)
    end)
    it('does not expand $VAR', function()
      eq(nil, lfs.attributes(fname))
      eq(true, not not dollar_fname:find('%$%w+'))
      write_file(dollar_fname, ':call setline(1, "100500")\n:wqall!\n')
      funcs.system(
        {nvim_prog_abs(), '-u', 'NONE', '-i', 'NONE', '--headless',
         '--cmd', 'set noswapfile shortmess+=IFW fileformats=unix',
         '-s', dollar_fname, fname})
      eq(0, eval('v:shell_error'))
      local attrs = lfs.attributes(fname)
      eq(#('100500\n'), attrs.size)
    end)
    it('does not crash after reading from stdin in non-headless mode', function()
      skip(is_os('win'))
      local screen = Screen.new(40, 8)
      screen:attach()
      local args = {
        nvim_prog_abs(), '-u', 'NONE', '-i', 'NONE',
        '--cmd', '"set noswapfile shortmess+=IFW fileformats=unix"',
        '-s', '-'
      }

      -- Need to explicitly pipe to stdin so that the embedded Nvim instance doesn't try to read
      -- data from the terminal #18181
      funcs.termopen(string.format([[echo "" | %s]], table.concat(args, " ")))
      screen:expect([[
        ^                                        |
        {1:~                                       }|
        {1:~                                       }|
        {1:~                                       }|
        {1:~                                       }|
        {2:[No Name]             0,0-1          All}|
                                                |
                                                |
      ]], {
        [1] = {foreground = tonumber('0x4040ff'), fg_indexed=true},
        [2] = {bold = true, reverse = true}
      })
      feed('i:cq<CR>')
      screen:expect([[
                                                |
        [Process exited 1]                      |
                                                |
                                                |
                                                |
                                                |
                                                |
        -- TERMINAL --                          |
      ]])
      --[=[ Example of incorrect output:
      screen:expect([[
        ^nvim: /var/tmp/portage/dev-libs/libuv-1.|
        10.2/work/libuv-1.10.2/src/unix/core.c:5|
        19: uv__close: Assertion `fd > STDERR_FI|
        LENO' failed.                           |
                                                |
        [Process exited 6]                      |
                                                |
                                                |
      ]])
      ]=]
    end)
    it('errors out when trying to use nonexistent file with -s', function()
      eq(
        'Cannot open for reading: "'..nonexistent_fname..'": no such file or directory\n',
        funcs.system(
          {nvim_prog_abs(), '-u', 'NONE', '-i', 'NONE', '--headless',
           '--cmd', 'set noswapfile shortmess+=IFW fileformats=unix',
           '--cmd', 'language C',
           '-s', nonexistent_fname}))
      eq(2, eval('v:shell_error'))
    end)
    it('errors out when trying to use -s twice', function()
      write_file(fname, ':call setline(1, "1")\n:wqall!\n')
      write_file(dollar_fname, ':call setline(1, "2")\n:wqall!\n')
      eq(
        'Attempt to open script file again: "-s '..dollar_fname..'"\n',
        funcs.system(
          {nvim_prog_abs(), '-u', 'NONE', '-i', 'NONE', '--headless',
           '--cmd', 'set noswapfile shortmess+=IFW fileformats=unix',
           '--cmd', 'language C',
           '-s', fname, '-s', dollar_fname, fname_2}))
      eq(2, eval('v:shell_error'))
      eq(nil, lfs.attributes(fname_2))
    end)
  end)
end)