robin/laufey

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

Justin M. KeyesMerge #40865 from justinmk/fixcif907614088

master
3.7 KiB123 linesraw
1local t = require('test.testutil')
2local n = require('test.functional.testnvim')()
3local tt = require('test.functional.testterm')
4
5local describe, it, after_each = t.describe, t.it, t.after_each
6local assert_log = t.assert_log
7local clear = n.clear
8local command = n.command
9local eq = t.eq
10local exec_lua = n.exec_lua
11local expect_exit = n.expect_exit
12local request = n.request
13
14describe('log', function()
15  local testlog = 'Xtest_logging'
16
17  after_each(function()
18    expect_exit(command, 'qa!')
19    vim.uv.sleep(10) -- Wait for Nvim to fully exit
20    os.remove(testlog)
21  end)
22
23  it('skipped before log_init', function()
24    -- This test is for _visibility_: adjust as needed, after checking for regression.
25    --
26    -- During startup some components may try to log before logging is setup.
27    -- That should be uncommon (ideally never)--and if there are MANY such
28    -- calls, that needs investigation.
29    clear()
30    eq(0, request('nvim__stats').log_skip)
31    clear { env = { CDPATH = '~doesnotexist' } }
32    assert(request('nvim__stats').log_skip <= 13)
33  end)
34
35  it('TUI client name is "ui"', function()
36    local function setup(env)
37      clear()
38      -- Start Nvim with builtin UI.
39      local screen = tt.setup_child_nvim({
40        '-u',
41        'NONE',
42        '-i',
43        'NONE',
44        '--cmd',
45        n.nvim_set,
46      }, {
47        env = env,
48      })
49      screen:expect([[
50        ^                                                  |
51        ~                                                 |*4
52                                                          |
53        {5:-- TERMINAL --}                                    |
54      ]])
55    end
56
57    -- Without $NVIM parent.
58    setup({
59      NVIM = '',
60      NVIM_LISTEN_ADDRESS = '',
61      NVIM_LOG_FILE = testlog,
62      __NVIM_TEST_LOG = '1',
63    })
64    -- Example:
65    --    ERR 2024-09-11T16:40:02.421 ui.47056   ui_client_run:165: test log message
66    assert_log(' ui%.%d+% +ui_client_run:%d+: test log message', testlog, 100)
67
68    -- With $NVIM parent.
69    setup({
70      NVIM_LOG_FILE = testlog,
71      __NVIM_TEST_LOG = '1',
72    })
73    -- Example:
74    --    ERR 2024-09-11T16:41:17.539 ui/c/T2.47826.0 ui_client_run:165: test log message
75    local tid = _G._nvim_test_id
76    assert_log(' ui/c/' .. tid .. '%.%d+%.%d +ui_client_run:%d+: test log message', testlog, 100)
77  end)
78
79  it('formats messages with session name or test id', function()
80    -- Examples:
81    --    ERR 2024-09-11T16:44:33.794 T3.49429.0 server_init:58: test log message
82    --    ERR 2024-09-11T16:44:33.823 c/T3.49429.0 server_init:58: test log message
83
84    clear({
85      env = {
86        NVIM_LOG_FILE = testlog,
87        -- TODO: remove this after nvim_log #7062 is merged.
88        __NVIM_TEST_LOG = '1',
89      },
90    })
91
92    local tid = _G._nvim_test_id
93    assert_log(tid .. '%.%d+%.%d +server_init:%d+: test log message', testlog, 100)
94
95    exec_lua([[
96      local j1 = vim.fn.jobstart({ vim.v.progpath, '-es', '-V1', '+foochild', '+qa!' }, vim.empty_dict())
97      vim.fn.jobwait({ j1 }, 5000)
98    ]])
99
100    -- Child Nvim spawned by jobstart() prepends "c/" to parent name.
101    assert_log('c/' .. tid .. '%.%d+%.%d +server_init:%d+: test log message', testlog, 100)
102  end)
103
104  it('warns when $NVIM_LOG_FILE is inaccessible', function()
105    clear({
106      args_rm = { '-u' },
107      args = { '--clean' },
108      env = { NVIM_LOG_FILE = '/foo/bar' },
109    })
110    t.retry(nil, nil, function()
111      t.matches('log: "/foo/bar" not accessible, logging to', n.exec_capture('messages'))
112    end)
113
114    clear({
115      args_rm = { '-u' },
116      args = { '--clean' },
117      env = { NVIM_LOG_FILE = '/foo/bar', XDG_STATE_HOME = '/foo2/bar2' },
118    })
119    t.retry(nil, nil, function()
120      t.matches('log: "/foo/bar" not accessible, logging to', n.exec_capture('messages'))
121    end)
122  end)
123end)