robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
f907614088
master
1local t =require ( 'test.testutil' ) 2local n =require ( 'test.functional.testnvim' )() 3 4local describe , it , before_each , finally =t . describe , t . it , t . before_each , t . finally 5local command =n . command 6local insert =n . insert 7local eq =t . eq 8local pcall_err =t . pcall_err 9local clear =n . clear 10local api =n . api 11local fn =n . fn 12local feed =n . feed 13local feed_command =n . feed_command 14local write_file =t . write_file 15local tmpname =t . tmpname 16local exec =n . exec 17local exec_lua =n . exec_lua 18local eval =n . eval 19local exec_capture =n . exec_capture 20local neq =t . neq 21local matches =t . matches 22local mkdir =t . mkdir 23local rmdir =n . rmdir 24local is_os =t . is_os 25 26describe ( ':source' , function () 27before_each ( function () 28clear () 29end ) 30 31it ( 'sourcing a file that is deleted and recreated is consistent vim-patch:8.1.0151' , function () 32local test_file ='Xfile.vim' 33local other_file ='Xfoobar' 34local script =[[ 35func Func() 36endfunc 37]] 38write_file ( test_file , script ) 39command ( 'source ' .. test_file ) 40os . remove ( test_file ) 41write_file ( test_file , script ) 42command ( 'source ' .. test_file ) 43os . remove ( test_file ) 44write_file ( other_file , '' ) 45write_file ( test_file , script ) 46command ( 'source ' .. test_file ) 47os . remove ( other_file ) 48os . remove ( test_file ) 49end ) 50 51it ( "changing 'shellslash' changes the result of expand()" , function () 52t . skip ( not is_os ( 'win' ), "N/A: 'shellslash' only works on Windows" ) 53 54api . nvim_set_option_value ( 'shellslash' , false , {} ) 55mkdir ( 'Xshellslash' ) 56 57write_file ( 58[[Xshellslash/Xstack.vim]] , 59[[ 60let g:stack1 = expand('<stack>') 61set shellslash 62let g:stack2 = expand('<stack>') 63set noshellslash 64let g:stack3 = expand('<stack>') 65]] 66) 67 68for _ =1 , 2 do 69command ( [[source Xshellslash/Xstack.vim]] ) 70matches ( [[Xshellslash\Xstack%.vim]] , api . nvim_get_var ( 'stack1' )) 71matches ( [[Xshellslash/Xstack%.vim]] , api . nvim_get_var ( 'stack2' )) 72matches ( [[Xshellslash\Xstack%.vim]] , api . nvim_get_var ( 'stack3' )) 73end 74 75write_file ( 76[[Xshellslash/Xstack.lua]] , 77[[ 78vim.g.stack1 = vim.fn.expand('<stack>') 79vim.o.shellslash = true 80vim.g.stack2 = vim.fn.expand('<stack>') 81vim.o.shellslash = false 82vim.g.stack3 = vim.fn.expand('<stack>') 83]] 84) 85 86for _ =1 , 2 do 87command ( [[source Xshellslash/Xstack.lua]] ) 88matches ( [[Xshellslash\Xstack%.lua]] , api . nvim_get_var ( 'stack1' )) 89matches ( [[Xshellslash/Xstack%.lua]] , api . nvim_get_var ( 'stack2' )) 90matches ( [[Xshellslash\Xstack%.lua]] , api . nvim_get_var ( 'stack3' )) 91end 92 93rmdir ( 'Xshellslash' ) 94end ) 95 96it ( 'current buffer' , function () 97insert ( [[ 98let a = 2 99let b = #{ 100\ k: "v" 101"\ (o_o) 102\ } 103let c = expand("<SID>") 104let s:s = 0zbeef.cafe 105let d = s:s]] ) 106 107command ( 'source' ) 108eq ( '2' , exec_capture ( 'echo a' )) 109eq ( "{'k': 'v'}" , exec_capture ( 'echo b' )) 110eq ( '<SNR>1_' , exec_capture ( 'echo c' )) 111eq ( '0zBEEFCAFE' , exec_capture ( 'echo d' )) 112 113exec ( 'set cpoptions+=C' ) 114matches ( "Vim%(let%):E723: Missing end of Dictionary '%}'" , pcall_err ( command , 'source' )) 115end ) 116 117it ( 'selection in current buffer' , function () 118insert ( [[ 119let a = 2 120let a = 3 121let a = 4 122let b = #{ 123"\ (>_<) 124\ K: "V" 125\ } 126function! s:C() abort 127return expand("<SID>") .. "C()" 128endfunction 129let D = {-> s:C()}]] ) 130 131-- Source the 2nd line only 132feed ( 'ggjV' ) 133feed_command ( ':source' ) 134eq ( '3' , exec_capture ( 'echo a' )) 135 136-- Source last line only 137feed_command ( ':$source' ) 138matches ( 'Vim%(echo%):E117: Unknown function: s:C' , pcall_err ( command , 'echo D()' )) 139 140-- Source from 2nd line to end of file 141feed ( 'ggjVG' ) 142feed_command ( ':source' ) 143eq ( '4' , exec_capture ( 'echo a' )) 144eq ( "{'K': 'V'}" , exec_capture ( 'echo b' )) 145eq ( '<SNR>1_C()' , exec_capture ( 'echo D()' )) 146 147-- Source last line after the lines that define s:C() have been sourced 148feed_command ( ':$source' ) 149eq ( '<SNR>1_C()' , exec_capture ( 'echo D()' )) 150 151exec ( 'set cpoptions+=C' ) 152matches ( "Vim%(let%):E723: Missing end of Dictionary '%}'" , pcall_err ( command , "'<,'>source" )) 153end ) 154 155it ( 'does not break if current buffer is modified while sourced' , function () 156insert [[ 157bwipeout! 158let a = 123 159]] 160command ( 'source' ) 161eq ( '123' , exec_capture ( 'echo a' )) 162end ) 163 164it ( 'multiline heredoc command' , function () 165insert ( [[ 166lua << EOF 167y = 4 168EOF]] ) 169 170command ( 'source' ) 171eq ( '4' , exec_capture ( 'echo luaeval("y")' )) 172end ) 173 174--- @param verbose boolean 175local function test_source_lua_file ( verbose ) 176local test_file ='Xtest.lua' 177write_file ( 178test_file , 179[[ 180vim.g.sourced_lua = 1 181vim.g.sfile_value = vim.fn.expand('<sfile>') 182vim.g.stack_value = vim.fn.expand('<stack>') 183vim.g.script_value = vim.fn.expand('<script>') 184vim.g.script_id = tonumber(vim.fn.expand('<SID>'):match('<SNR>(%d+)_')) 185vim.o.mouse = 'nv' 186]] 187) 188 189command ( 'set shellslash' ) 190command (( '%ssource %s' ): format ( verbose and 'verbose ' or '' , test_file )) 191eq ( 1 , eval ( 'g:sourced_lua' )) 192matches ( [[/Xtest%.lua$]] , api . nvim_get_var ( 'sfile_value' )) 193matches ( [[/Xtest%.lua$]] , api . nvim_get_var ( 'stack_value' )) 194matches ( [[/Xtest%.lua$]] , api . nvim_get_var ( 'script_value' )) 195 196local expected_sid =fn . getscriptinfo ( { name =test_file } )[ 1 ]. sid 197local sid =api . nvim_get_var ( 'script_id' ) 198eq ( expected_sid , sid ) 199eq ( sid , api . nvim_get_option_info2 ( 'mouse' , {} ). last_set_sid ) 200 201os . remove ( test_file ) 202end 203 204it ( 'can source lua files' , function () 205test_source_lua_file ( false ) 206end ) 207 208it ( 'with :verbose modifier can source lua files' , function () 209test_source_lua_file ( true ) 210end ) 211 212describe ( 'can source current buffer' , function () 213local function test_source_lua_curbuf () 214it ( 'selected region' , function () 215insert ( [[ 216vim.g.b = 5 217vim.g.b = 6 218vim.g.b = 7 219a = [=[ 220"\ a 221\ b]=] 222]] ) 223feed ( 'dd' ) 224 225feed ( 'ggjV' ) 226feed_command ( ':source' ) 227eq ( 6 , eval ( 'g:b' )) 228 229feed ( 'GVkk' ) 230feed_command ( ':source' ) 231eq ( ' "\\ a\n \\ b' , exec_lua ( 'return _G.a' )) 232end ) 233 234it ( 'whole buffer' , function () 235insert ( [[ 236vim.g.c = 10 237vim.g.c = 11 238vim.g.c = 12 239a = [=[ 240\ 1 241"\ 2]=] 242vim.g.sfile_value = vim.fn.expand('<sfile>') 243vim.g.stack_value = vim.fn.expand('<stack>') 244vim.g.script_value = vim.fn.expand('<script>') 245]] ) 246feed ( 'dd' ) 247 248feed_command ( ':source' ) 249local filepath =fn . expand ( '%:p' ) 250if filepath == '' then 251filepath =':source buffer=1' 252end 253eq ( 12 , eval ( 'g:c' )) 254eq ( ' \\ 1\n "\\ 2' , exec_lua ( 'return _G.a' )) 255eq ( filepath , api . nvim_get_var ( 'sfile_value' )) 256eq ( filepath , api . nvim_get_var ( 'stack_value' )) 257eq ( filepath , api . nvim_get_var ( 'script_value' )) 258end ) 259end 260 261describe ( 'with ft=lua' , function () 262before_each ( function () 263command ( 'setlocal ft=lua' ) 264end ) 265test_source_lua_curbuf () 266end ) 267 268describe ( 'with .lua extension' , function () 269before_each ( function () 270command ( 'edit ' .. tmpname () .. '.lua' ) 271end ) 272test_source_lua_curbuf () 273end ) 274end ) 275 276it ( "doesn't throw E484 for lua parsing/runtime errors" , function () 277local test_file ='Xtest.lua' 278 279-- Does throw E484 for unreadable files 280local ok , result =pcall ( exec_capture , ':source ' .. test_file .. 'noexisting' ) 281eq ( false , ok ) 282neq ( nil , result : find ( 'E484' )) 283 284-- Doesn't throw for parsing error 285write_file ( test_file , 'vim.g.c = ' ) 286ok , result =pcall ( exec_capture , ':source ' .. test_file ) 287eq ( false , ok ) 288eq ( nil , result : find ( 'E484' )) 289os . remove ( test_file ) 290 291-- Doesn't throw for runtime error 292write_file ( test_file , "error('Cause error anyway :D')" ) 293ok , result =pcall ( exec_capture , ':source ' .. test_file ) 294eq ( false , ok ) 295eq ( nil , result : find ( 'E484' )) 296os . remove ( test_file ) 297end ) 298 299it ( 'sources Lua/Vimscript codeblocks based on treesitter injection' , function () 300insert ( [[ 301*test.txt* Test help file 302 303Lua example: >lua 304vim.g.test_lua = 42 305< 306 307Vim example: >vim 308let g:test_vim = 99 309<]] ) 310command ( 'setlocal filetype=help' ) 311 312-- Source Lua codeblock (line 4 contains the Lua code) 313command ( ':4source' ) 314eq ( 42 , eval ( 'g:test_lua' )) 315 316-- Source Vimscript codeblock (line 8 contains the Vim code) 317command ( ':8source' ) 318eq ( 99 , eval ( 'g:test_vim' )) 319 320-- Test fallback without treesitter 321command ( 'enew' ) 322insert ( [[let g:test_no_ts = 123]] ) 323command ( 'setlocal filetype=' ) 324command ( 'source' ) 325eq ( 123 , eval ( 'g:test_no_ts' )) 326end ) 327end ) 328 329it ( '$HOME is not shortened in filepath in v:stacktrace from sourced file' , function () 330local xhome =t . fix_slashes ( assert ( vim . uv . cwd ())) .. '/Xhome' 331mkdir ( xhome ) 332clear ( { env ={ HOME =xhome } } ) 333finally ( function () 334rmdir ( xhome ) 335end ) 336local filepath =xhome .. '/Xstacktrace.vim' 337local script =[[ 338func Xfunc() 339throw 'Exception from Xfunc' 340endfunc 341]] 342write_file ( filepath , script ) 343exec ( 'source ' .. filepath ) 344exec ( [[ 345try 346call Xfunc() 347catch 348let g:stacktrace = v:stacktrace 349endtry 350]] ) 351eq ( filepath , n . eval ( 'g:stacktrace[-1].filepath' )) 352end )