aboutsummaryrefslogtreecommitdiff
path: root/test/functional/autocmd/signal_spec.lua
blob: 03e991f21c8ccc6a395829dbbe565f20abe0f450 (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
local t = require('test.testutil')
local n = require('test.functional.testnvim')()

local clear = n.clear
local command = n.command
local eq = t.eq
local fn = n.fn
local next_msg = n.next_msg
local is_os = t.is_os
local skip = t.skip
local read_file = t.read_file
local feed = n.feed
local retry = t.retry

if skip(is_os('win'), 'Only applies to POSIX systems') then
  return
end

describe("'autowriteall' on signal exit", function()
  before_each(clear)

  local function test_deadly_sig(signame, awa, should_write)
    local testfile = 'Xtest_SIG' .. signame .. (awa and '_awa' or '_noawa')
    local teststr = 'Testaaaaaaa'
    finally(function()
      os.remove(testfile)
    end)

    if awa then
      command('set autowriteall')
    end

    command('edit ' .. testfile)
    feed('i' .. teststr)
    vim.uv.kill(fn.getpid(), signame)

    retry(nil, 5000, function()
      eq((should_write and (teststr .. '\n') or nil), read_file(testfile))
    end)
  end

  it('dont write if SIGTERM & awa on', function()
    test_deadly_sig('sigterm', true, false)
  end)
  it('dont write if SIGTERM & awa off', function()
    test_deadly_sig('sigterm', false, false)
  end)

  it('write if SIGHUP & awa on', function()
    test_deadly_sig('sighup', true, true)
  end)
  it('dont write if SIGHUP & awa off', function()
    test_deadly_sig('sighup', false, false)
  end)

  it('write if SIGTSTP & awa on', function()
    test_deadly_sig('sigtstp', true, true)
  end)
  it('dont write if SIGTSTP & awa off', function()
    test_deadly_sig('sigtstp', false, false)
  end)

  it('write if SIGQUIT & awa on', function()
    test_deadly_sig('sigquit', true, true)
  end)
  it('dont write if SIGQUIT & awa off', function()
    test_deadly_sig('sigquit', false, false)
  end)
end)

describe('autocmd Signal', function()
  before_each(clear)

  it('matches *', function()
    command('autocmd Signal * call rpcnotify(1, "foo")')
    vim.uv.kill(fn.getpid(), 'sigusr1')
    eq({ 'notification', 'foo', {} }, next_msg())
  end)

  it('matches SIGUSR1', function()
    command('autocmd Signal SIGUSR1 call rpcnotify(1, "foo")')
    vim.uv.kill(fn.getpid(), 'sigusr1')
    eq({ 'notification', 'foo', {} }, next_msg())
  end)

  it('matches SIGWINCH', function()
    command('autocmd Signal SIGWINCH call rpcnotify(1, "foo")')
    vim.uv.kill(fn.getpid(), 'sigwinch')
    eq({ 'notification', 'foo', {} }, next_msg())
  end)

  it('does not match unknown patterns', function()
    command('autocmd Signal SIGUSR2 call rpcnotify(1, "foo")')
    vim.uv.kill(fn.getpid(), 'sigusr2')
    eq(nil, next_msg(500))
  end)
end)