log_spec.lua 3.1 KB

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