robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
f907614088
master
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 () 15local testlog ='Xtest_logging' 16 17after_each ( function () 18expect_exit ( command , 'qa!' ) 19vim . uv . sleep ( 10 ) -- Wait for Nvim to fully exit 20os . remove ( testlog ) 21end ) 22 23it ( '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. 29clear () 30eq ( 0 , request ( 'nvim__stats' ). log_skip ) 31clear { env ={ CDPATH ='~doesnotexist' } } 32assert ( request ( 'nvim__stats' ). log_skip <= 13 ) 33end ) 34 35it ( 'TUI client name is "ui"' , function () 36local function setup ( env ) 37clear () 38-- Start Nvim with builtin UI. 39local screen =tt . setup_child_nvim ( { 40'-u' , 41'NONE' , 42'-i' , 43'NONE' , 44'--cmd' , 45n . nvim_set , 46} , { 47env =env , 48} ) 49screen : expect ( [[ 50^ | 51~ |*4 52| 53{5:-- TERMINAL --} | 54]] ) 55end 56 57-- Without $NVIM parent. 58setup ( { 59NVIM ='' , 60NVIM_LISTEN_ADDRESS ='' , 61NVIM_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 66assert_log ( ' ui%.%d+% +ui_client_run:%d+: test log message' , testlog , 100 ) 67 68-- With $NVIM parent. 69setup ( { 70NVIM_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 75local tid =_G . _nvim_test_id 76assert_log ( ' ui/c/' .. tid .. '%.%d+%.%d +ui_client_run:%d+: test log message' , testlog , 100 ) 77end ) 78 79it ( '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 84clear ( { 85env ={ 86NVIM_LOG_FILE =testlog , 87-- TODO: remove this after nvim_log #7062 is merged. 88__NVIM_TEST_LOG ='1' , 89} , 90} ) 91 92local tid =_G . _nvim_test_id 93assert_log ( tid .. '%.%d+%.%d +server_init:%d+: test log message' , testlog , 100 ) 94 95exec_lua ( [[ 96local j1 = vim.fn.jobstart({ vim.v.progpath, '-es', '-V1', '+foochild', '+qa!' }, vim.empty_dict()) 97vim.fn.jobwait({ j1 }, 5000) 98]] ) 99 100-- Child Nvim spawned by jobstart() prepends "c/" to parent name. 101assert_log ( 'c/' .. tid .. '%.%d+%.%d +server_init:%d+: test log message' , testlog , 100 ) 102end ) 103 104it ( 'warns when $NVIM_LOG_FILE is inaccessible' , function () 105clear ( { 106args_rm ={ '-u' } , 107args ={ '--clean' } , 108env ={ NVIM_LOG_FILE ='/foo/bar' } , 109} ) 110t . retry ( nil , nil , function () 111t . matches ( 'log: "/foo/bar" not accessible, logging to' , n . exec_capture ( 'messages' )) 112end ) 113 114clear ( { 115args_rm ={ '-u' } , 116args ={ '--clean' } , 117env ={ NVIM_LOG_FILE ='/foo/bar' , XDG_STATE_HOME ='/foo2/bar2' } , 118} ) 119t . retry ( nil , nil , function () 120t . matches ( 'log: "/foo/bar" not accessible, logging to' , n . exec_capture ( 'messages' )) 121end ) 122end ) 123end )