startup_spec.lua 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705
  1. local helpers = require('test.functional.helpers')(after_each)
  2. local Screen = require('test.functional.ui.screen')
  3. local assert_alive = helpers.assert_alive
  4. local clear = helpers.clear
  5. local command = helpers.command
  6. local ok = helpers.ok
  7. local eq = helpers.eq
  8. local matches = helpers.matches
  9. local eval = helpers.eval
  10. local exec_lua = helpers.exec_lua
  11. local feed = helpers.feed
  12. local funcs = helpers.funcs
  13. local mkdir = helpers.mkdir
  14. local mkdir_p = helpers.mkdir_p
  15. local nvim_prog = helpers.nvim_prog
  16. local nvim_set = helpers.nvim_set
  17. local read_file = helpers.read_file
  18. local retry = helpers.retry
  19. local rmdir = helpers.rmdir
  20. local sleep = helpers.sleep
  21. local iswin = helpers.iswin
  22. local startswith = helpers.startswith
  23. local write_file = helpers.write_file
  24. local meths = helpers.meths
  25. describe('startup', function()
  26. before_each(function()
  27. clear()
  28. os.remove('Xtest_startup_ttyout')
  29. end)
  30. after_each(function()
  31. os.remove('Xtest_startup_ttyout')
  32. end)
  33. it('pipe at both ends: has("ttyin")==0 has("ttyout")==0', function()
  34. -- system() puts a pipe at both ends.
  35. local out = funcs.system({ nvim_prog, '-u', 'NONE', '-i', 'NONE', '--headless',
  36. '--cmd', nvim_set,
  37. '-c', [[echo has('ttyin') has('ttyout')]],
  38. '+q' })
  39. eq('0 0', out)
  40. end)
  41. it('with --embed: has("ttyin")==0 has("ttyout")==0', function()
  42. local screen = Screen.new(25, 3)
  43. -- Remote UI connected by --embed.
  44. screen:attach()
  45. command([[echo has('ttyin') has('ttyout')]])
  46. screen:expect([[
  47. ^ |
  48. ~ |
  49. 0 0 |
  50. ]])
  51. end)
  52. it('in a TTY: has("ttyin")==1 has("ttyout")==1', function()
  53. if helpers.pending_win32(pending) then return end
  54. local screen = Screen.new(25, 4)
  55. screen:attach()
  56. if iswin() then
  57. command([[set shellcmdflag=/s\ /c shellxquote=\"]])
  58. end
  59. -- Running in :terminal
  60. command([[exe printf("terminal %s -u NONE -i NONE --cmd \"]]
  61. ..nvim_set..[[\"]]
  62. ..[[ -c \"echo has('ttyin') has('ttyout')\""]]
  63. ..[[, shellescape(v:progpath))]])
  64. screen:expect([[
  65. ^ |
  66. ~ |
  67. 1 1 |
  68. |
  69. ]])
  70. end)
  71. it('output to pipe: has("ttyin")==1 has("ttyout")==0', function()
  72. if iswin() then
  73. command([[set shellcmdflag=/s\ /c shellxquote=\"]])
  74. end
  75. -- Running in :terminal
  76. command([[exe printf("terminal %s -u NONE -i NONE --cmd \"]]
  77. ..nvim_set..[[\"]]
  78. ..[[ -c \"call writefile([has('ttyin'), has('ttyout')], 'Xtest_startup_ttyout')\"]]
  79. ..[[ -c q | cat -v"]] -- Output to a pipe.
  80. ..[[, shellescape(v:progpath))]])
  81. retry(nil, 3000, function()
  82. sleep(1)
  83. eq('1\n0\n', -- stdin is a TTY, stdout is a pipe
  84. read_file('Xtest_startup_ttyout'))
  85. end)
  86. end)
  87. it('input from pipe: has("ttyin")==0 has("ttyout")==1', function()
  88. if iswin() then
  89. command([[set shellcmdflag=/s\ /c shellxquote=\"]])
  90. end
  91. -- Running in :terminal
  92. command([[exe printf("terminal echo foo | ]] -- Input from a pipe.
  93. ..[[%s -u NONE -i NONE --cmd \"]]
  94. ..nvim_set..[[\"]]
  95. ..[[ -c \"call writefile([has('ttyin'), has('ttyout')], 'Xtest_startup_ttyout')\"]]
  96. ..[[ -c q -- -"]]
  97. ..[[, shellescape(v:progpath))]])
  98. retry(nil, 3000, function()
  99. sleep(1)
  100. eq('0\n1\n', -- stdin is a pipe, stdout is a TTY
  101. read_file('Xtest_startup_ttyout'))
  102. end)
  103. end)
  104. it('input from pipe (implicit) #7679', function()
  105. if helpers.pending_win32(pending) then return end
  106. local screen = Screen.new(25, 4)
  107. screen:attach()
  108. if iswin() then
  109. command([[set shellcmdflag=/s\ /c shellxquote=\"]])
  110. end
  111. -- Running in :terminal
  112. command([[exe printf("terminal echo foo | ]] -- Input from a pipe.
  113. ..[[%s -u NONE -i NONE --cmd \"]]
  114. ..nvim_set..[[\"]]
  115. ..[[ -c \"echo has('ttyin') has('ttyout')\""]]
  116. ..[[, shellescape(v:progpath))]])
  117. screen:expect([[
  118. ^foo |
  119. ~ |
  120. 0 1 |
  121. |
  122. ]])
  123. end)
  124. it('input from pipe + file args #7679', function()
  125. eq('ohyeah\r\n0 0 bufs=3',
  126. funcs.system({nvim_prog, '-n', '-u', 'NONE', '-i', 'NONE', '--headless',
  127. '+.print',
  128. "+echo has('ttyin') has('ttyout') 'bufs='.bufnr('$')",
  129. '+qall!',
  130. '-',
  131. 'test/functional/fixtures/tty-test.c',
  132. 'test/functional/fixtures/shell-test.c',
  133. },
  134. { 'ohyeah', '' }))
  135. end)
  136. it('if stdin is empty: selects buffer 2, deletes buffer 1 #8561', function()
  137. eq('\r\n 2 %a "file1" line 0\r\n 3 "file2" line 0',
  138. funcs.system({nvim_prog, '-n', '-u', 'NONE', '-i', 'NONE', '--headless',
  139. '+ls!',
  140. '+qall!',
  141. '-',
  142. 'file1',
  143. 'file2',
  144. },
  145. { '' }))
  146. end)
  147. it('-e/-E interactive #7679', function()
  148. clear('-e')
  149. local screen = Screen.new(25, 3)
  150. screen:attach()
  151. feed("put ='from -e'<CR>")
  152. screen:expect([[
  153. :put ='from -e' |
  154. from -e |
  155. :^ |
  156. ]])
  157. clear('-E')
  158. screen = Screen.new(25, 3)
  159. screen:attach()
  160. feed("put ='from -E'<CR>")
  161. screen:expect([[
  162. :put ='from -E' |
  163. from -E |
  164. :^ |
  165. ]])
  166. end)
  167. it('stdin with -es/-Es #7679', function()
  168. local input = { 'append', 'line1', 'line2', '.', '%print', '' }
  169. local inputstr = table.concat(input, '\n')
  170. --
  171. -- -Es: read stdin as text
  172. --
  173. eq('partylikeits1999\n',
  174. funcs.system({nvim_prog, '-n', '-u', 'NONE', '-i', 'NONE', '-Es', '+.print', 'test/functional/fixtures/tty-test.c' },
  175. { 'partylikeits1999', '' }))
  176. eq(inputstr,
  177. funcs.system({nvim_prog, '-i', 'NONE', '-Es', '+%print', '-' },
  178. input))
  179. -- with `-u NORC`
  180. eq('thepartycontinues\n',
  181. funcs.system({nvim_prog, '-n', '-u', 'NORC', '-Es', '+.print' },
  182. { 'thepartycontinues', '' }))
  183. -- without `-u`
  184. eq('thepartycontinues\n',
  185. funcs.system({nvim_prog, '-n', '-Es', '+.print' },
  186. { 'thepartycontinues', '' }))
  187. --
  188. -- -es: read stdin as ex-commands
  189. --
  190. eq(' encoding=utf-8\n',
  191. funcs.system({nvim_prog, '-n', '-u', 'NONE', '-i', 'NONE', '-es', 'test/functional/fixtures/tty-test.c' },
  192. { 'set encoding', '' }))
  193. eq('line1\nline2\n',
  194. funcs.system({nvim_prog, '-i', 'NONE', '-es', '-' },
  195. input))
  196. -- with `-u NORC`
  197. eq(' encoding=utf-8\n',
  198. funcs.system({nvim_prog, '-n', '-u', 'NORC', '-es' },
  199. { 'set encoding', '' }))
  200. -- without `-u`
  201. eq(' encoding=utf-8\n',
  202. funcs.system({nvim_prog, '-n', '-es' },
  203. { 'set encoding', '' }))
  204. end)
  205. it('-es/-Es disables swapfile, user config #8540', function()
  206. for _,arg in ipairs({'-es', '-Es'}) do
  207. local out = funcs.system({nvim_prog, arg,
  208. '+set swapfile? updatecount? shada?',
  209. "+put =execute('scriptnames')", '+%print'})
  210. local line1 = string.match(out, '^.-\n')
  211. -- updatecount=0 means swapfile was disabled.
  212. eq(" swapfile updatecount=0 shada=!,'100,<50,s10,h\n", line1)
  213. -- Standard plugins were loaded, but not user config.
  214. eq('health.vim', string.match(out, 'health.vim'))
  215. eq(nil, string.match(out, 'init.vim'))
  216. end
  217. end)
  218. it('-e sets ex mode', function()
  219. local screen = Screen.new(25, 3)
  220. clear('-e')
  221. screen:attach()
  222. -- Verify we set the proper mode both before and after :vi.
  223. feed("put =mode(1)<CR>vi<CR>:put =mode(1)<CR>")
  224. screen:expect([[
  225. cv |
  226. ^n |
  227. :put =mode(1) |
  228. ]])
  229. eq('cv\n',
  230. funcs.system({nvim_prog, '-n', '-es' },
  231. { 'put =mode(1)', 'print', '' }))
  232. end)
  233. it('fails on --embed with -es/-Es', function()
  234. matches('nvim[.exe]*: %-%-embed conflicts with %-es/%-Es',
  235. funcs.system({nvim_prog, '--embed', '-es' }))
  236. matches('nvim[.exe]*: %-%-embed conflicts with %-es/%-Es',
  237. funcs.system({nvim_prog, '--embed', '-Es' }))
  238. end)
  239. it('does not crash if --embed is given twice', function()
  240. clear{args={'--embed'}}
  241. assert_alive()
  242. end)
  243. it('does not crash when expanding cdpath during early_init', function()
  244. clear{env={CDPATH='~doesnotexist'}}
  245. eq(',~doesnotexist', eval('&cdpath'))
  246. end)
  247. it('ENTER dismisses early message #7967', function()
  248. if helpers.pending_win32(pending) then return end
  249. local screen
  250. screen = Screen.new(60, 6)
  251. screen:attach()
  252. command([[let g:id = termopen('"]]..nvim_prog..
  253. [[" -u NONE -i NONE --cmd "set noruler" --cmd "let g:foo = g:bar"')]])
  254. screen:expect([[
  255. ^ |
  256. |
  257. Error detected while processing pre-vimrc command line: |
  258. E121: Undefined variable: g:bar |
  259. Press ENTER or type command to continue |
  260. |
  261. ]])
  262. command([[call chansend(g:id, "\n")]])
  263. screen:expect([[
  264. ^ |
  265. ~ |
  266. ~ |
  267. [No Name] |
  268. |
  269. |
  270. ]])
  271. end)
  272. it("sets 'shortmess' when loading other tabs", function()
  273. clear({args={'-p', 'a', 'b', 'c'}})
  274. local screen = Screen.new(25, 4)
  275. screen:attach()
  276. screen:expect({grid=[[
  277. {1: a }{2: b c }{3: }{2:X}|
  278. ^ |
  279. {4:~ }|
  280. |
  281. ]],
  282. attr_ids={
  283. [1] = {bold = true},
  284. [2] = {background = Screen.colors.LightGrey, underline = true},
  285. [3] = {reverse = true},
  286. [4] = {bold = true, foreground = Screen.colors.Blue1},
  287. }})
  288. end)
  289. it('fixed hang issue with --headless (#11386)', function()
  290. local expected = ''
  291. local period = 100
  292. for i = 1, period - 1 do
  293. expected = expected .. i .. '\r\n'
  294. end
  295. expected = expected .. period
  296. eq(
  297. expected,
  298. -- FIXME(codehex): We should really set a timeout for the system function.
  299. -- If this test fails, there will be a waiting input state.
  300. funcs.system({nvim_prog, '-u', 'NONE', '-c',
  301. 'for i in range(1, 100) | echo i | endfor | quit',
  302. '--headless'
  303. })
  304. )
  305. end)
  306. it("get command line arguments from v:argv", function()
  307. local out = funcs.system({ nvim_prog, '-u', 'NONE', '-i', 'NONE', '--headless',
  308. '--cmd', nvim_set,
  309. '-c', [[echo v:argv[-1:] len(v:argv) > 1]],
  310. '+q' })
  311. eq('[\'+q\'] 1', out)
  312. end)
  313. local function pack_clear(cmd)
  314. -- add packages after config dir in rtp but before config/after
  315. clear{args={'--cmd', 'set packpath=test/functional/fixtures', '--cmd', 'let paths=split(&rtp, ",")', '--cmd', 'let &rtp = paths[0]..",test/functional/fixtures,test/functional/fixtures/middle,"..join(paths[1:],",")', '--cmd', cmd}, env={XDG_CONFIG_HOME='test/functional/fixtures/'}}
  316. end
  317. it("handles &packpath during startup", function()
  318. pack_clear [[
  319. let g:x = bar#test()
  320. let g:y = leftpad#pad("heyya")
  321. ]]
  322. eq(-3, eval 'g:x')
  323. eq(" heyya", eval 'g:y')
  324. pack_clear [[ lua _G.y = require'bar'.doit() _G.z = require'leftpad''howdy' ]]
  325. eq({9003, '\thowdy'}, exec_lua [[ return { _G.y, _G.z } ]])
  326. end)
  327. it("handles require from &packpath in an async handler", function()
  328. -- NO! you cannot just speed things up by calling async functions during startup!
  329. -- It doesn't make anything actually faster! NOOOO!
  330. pack_clear [[ lua require'async_leftpad'('brrrr', 'async_res') ]]
  331. -- haha, async leftpad go brrrrr
  332. eq('\tbrrrr', exec_lua [[ return _G.async_res ]])
  333. end)
  334. it("handles :packadd during startup", function()
  335. -- control group: opt/bonus is not available by default
  336. pack_clear [[
  337. try
  338. let g:x = bonus#secret()
  339. catch
  340. let g:err = v:exception
  341. endtry
  342. ]]
  343. eq('Vim(let):E117: Unknown function: bonus#secret', eval 'g:err')
  344. pack_clear [[ lua _G.test = {pcall(function() require'bonus'.launch() end)} ]]
  345. eq({false, [[[string ":lua"]:1: module 'bonus' not found:]]},
  346. exec_lua [[ _G.test[2] = string.gsub(_G.test[2], '[\r\n].*', '') return _G.test ]])
  347. -- ok, time to launch the nukes:
  348. pack_clear [[ packadd! bonus | let g:x = bonus#secret() ]]
  349. eq('halloj', eval 'g:x')
  350. pack_clear [[ packadd! bonus | lua _G.y = require'bonus'.launch() ]]
  351. eq('CPE 1704 TKS', exec_lua [[ return _G.y ]])
  352. end)
  353. it("handles the correct order with start packages and after/", function()
  354. pack_clear [[ lua _G.test_loadorder = {} vim.cmd "runtime! filen.lua" ]]
  355. eq({'ordinary', 'FANCY', 'mittel', 'FANCY after', 'ordinary after'}, exec_lua [[ return _G.test_loadorder ]])
  356. end)
  357. it("handles the correct order with start packages and after/ after startup", function()
  358. pack_clear [[ lua _G.test_loadorder = {} ]]
  359. command [[ runtime! filen.lua ]]
  360. eq({'ordinary', 'FANCY', 'mittel', 'FANCY after', 'ordinary after'}, exec_lua [[ return _G.test_loadorder ]])
  361. end)
  362. it("handles the correct order with globpath(&rtp, ...)", function()
  363. pack_clear [[ set loadplugins | lua _G.test_loadorder = {} ]]
  364. command [[
  365. for x in globpath(&rtp, "filen.lua",1,1)
  366. call v:lua.dofile(x)
  367. endfor
  368. ]]
  369. eq({'ordinary', 'FANCY', 'mittel', 'FANCY after', 'ordinary after'}, exec_lua [[ return _G.test_loadorder ]])
  370. local rtp = meths.get_option'rtp'
  371. ok(startswith(rtp, 'test/functional/fixtures/nvim,test/functional/fixtures/pack/*/start/*,test/functional/fixtures/start/*,test/functional/fixtures,test/functional/fixtures/middle,'), 'rtp='..rtp)
  372. end)
  373. it("handles the correct order with opt packages and after/", function()
  374. pack_clear [[ lua _G.test_loadorder = {} vim.cmd "packadd! superspecial\nruntime! filen.lua" ]]
  375. eq({'ordinary', 'SuperSpecial', 'FANCY', 'mittel', 'FANCY after', 'SuperSpecial after', 'ordinary after'}, exec_lua [[ return _G.test_loadorder ]])
  376. end)
  377. it("handles the correct order with opt packages and after/ after startup", function()
  378. pack_clear [[ lua _G.test_loadorder = {} ]]
  379. command [[
  380. packadd! superspecial
  381. runtime! filen.lua
  382. ]]
  383. eq({'ordinary', 'SuperSpecial', 'FANCY', 'mittel', 'FANCY after', 'SuperSpecial after', 'ordinary after'}, exec_lua [[ return _G.test_loadorder ]])
  384. end)
  385. it("handles the correct order with opt packages and globpath(&rtp, ...)", function()
  386. pack_clear [[ set loadplugins | lua _G.test_loadorder = {} ]]
  387. command [[
  388. packadd! superspecial
  389. for x in globpath(&rtp, "filen.lua",1,1)
  390. call v:lua.dofile(x)
  391. endfor
  392. ]]
  393. eq({'ordinary', 'SuperSpecial', 'FANCY', 'mittel', 'SuperSpecial after', 'FANCY after', 'ordinary after'}, exec_lua [[ return _G.test_loadorder ]])
  394. end)
  395. it("handles the correct order with a package that changes packpath", function()
  396. pack_clear [[ lua _G.test_loadorder = {} vim.cmd "packadd! funky\nruntime! filen.lua" ]]
  397. eq({'ordinary', 'funky!', 'FANCY', 'mittel', 'FANCY after', 'ordinary after'}, exec_lua [[ return _G.test_loadorder ]])
  398. eq({'ordinary', 'funky!', 'mittel', 'ordinary after'}, exec_lua [[ return _G.nested_order ]])
  399. end)
  400. it("handles the correct order when prepending packpath", function()
  401. clear{args={'--cmd', 'set packpath^=test/functional/fixtures', '--cmd', [[ lua _G.test_loadorder = {} vim.cmd "runtime! filen.lua" ]]}, env={XDG_CONFIG_HOME='test/functional/fixtures/'}}
  402. eq({'ordinary', 'FANCY', 'FANCY after', 'ordinary after'}, exec_lua [[ return _G.test_loadorder ]])
  403. end)
  404. end)
  405. describe('sysinit', function()
  406. local xdgdir = 'Xxdg'
  407. local vimdir = 'Xvim'
  408. local xhome = 'Xhome'
  409. local pathsep = helpers.get_pathsep()
  410. before_each(function()
  411. rmdir(xdgdir)
  412. rmdir(vimdir)
  413. rmdir(xhome)
  414. mkdir(xdgdir)
  415. mkdir(xdgdir .. pathsep .. 'nvim')
  416. write_file(table.concat({xdgdir, 'nvim', 'sysinit.vim'}, pathsep), [[
  417. let g:loaded = get(g:, "loaded", 0) + 1
  418. let g:xdg = 1
  419. ]])
  420. mkdir(vimdir)
  421. write_file(table.concat({vimdir, 'sysinit.vim'}, pathsep), [[
  422. let g:loaded = get(g:, "loaded", 0) + 1
  423. let g:vim = 1
  424. ]])
  425. mkdir(xhome)
  426. end)
  427. after_each(function()
  428. rmdir(xdgdir)
  429. rmdir(vimdir)
  430. rmdir(xhome)
  431. end)
  432. it('prefers XDG_CONFIG_DIRS over VIM', function()
  433. clear{args={'--cmd', 'set nomore undodir=. directory=. belloff='},
  434. args_rm={'-u', '--cmd'},
  435. env={ HOME=xhome,
  436. XDG_CONFIG_DIRS=xdgdir,
  437. VIM=vimdir }}
  438. eq('loaded 1 xdg 1 vim 0',
  439. eval('printf("loaded %d xdg %d vim %d", g:loaded, get(g:, "xdg", 0), get(g:, "vim", 0))'))
  440. end)
  441. it('uses VIM if XDG_CONFIG_DIRS unset', function()
  442. clear{args={'--cmd', 'set nomore undodir=. directory=. belloff='},
  443. args_rm={'-u', '--cmd'},
  444. env={ HOME=xhome,
  445. XDG_CONFIG_DIRS='',
  446. VIM=vimdir }}
  447. eq('loaded 1 xdg 0 vim 1',
  448. eval('printf("loaded %d xdg %d vim %d", g:loaded, get(g:, "xdg", 0), get(g:, "vim", 0))'))
  449. end)
  450. it('fixed hang issue with -D (#12647)', function()
  451. if helpers.pending_win32(pending) then return end
  452. local screen
  453. screen = Screen.new(60, 6)
  454. screen:attach()
  455. command([[let g:id = termopen('"]]..nvim_prog..
  456. [[" -u NONE -i NONE --cmd "set noruler" -D')]])
  457. screen:expect([[
  458. ^ |
  459. Entering Debug mode. Type "cont" to continue. |
  460. cmd: augroup nvim_terminal |
  461. > |
  462. <" -u NONE -i NONE --cmd "set noruler" -D 1,0-1 All|
  463. |
  464. ]])
  465. command([[call chansend(g:id, "cont\n")]])
  466. screen:expect([[
  467. ^ |
  468. ~ |
  469. [No Name] |
  470. |
  471. <" -u NONE -i NONE --cmd "set noruler" -D 1,0-1 All|
  472. |
  473. ]])
  474. end)
  475. end)
  476. describe('clean', function()
  477. clear()
  478. ok(string.find(meths.get_option('runtimepath'), funcs.stdpath('config'), 1, true) ~= nil)
  479. clear('--clean')
  480. ok(string.find(meths.get_option('runtimepath'), funcs.stdpath('config'), 1, true) == nil)
  481. end)
  482. describe('user config init', function()
  483. local xhome = 'Xhome'
  484. local pathsep = helpers.get_pathsep()
  485. local xconfig = xhome .. pathsep .. 'Xconfig'
  486. local xdata = xhome .. pathsep .. 'Xdata'
  487. local init_lua_path = table.concat({xconfig, 'nvim', 'init.lua'}, pathsep)
  488. local xenv = { XDG_CONFIG_HOME=xconfig, XDG_DATA_HOME=xdata }
  489. before_each(function()
  490. rmdir(xhome)
  491. mkdir_p(xconfig .. pathsep .. 'nvim')
  492. mkdir_p(xdata)
  493. write_file(init_lua_path, [[
  494. vim.g.lua_rc = 1
  495. ]])
  496. end)
  497. after_each(function()
  498. rmdir(xhome)
  499. end)
  500. it('loads init.lua from XDG config home by default', function()
  501. clear{ args_rm={'-u' }, env=xenv }
  502. eq(1, eval('g:lua_rc'))
  503. eq(funcs.fnamemodify(init_lua_path, ':p'), eval('$MYVIMRC'))
  504. end)
  505. describe 'with explicitly provided config'(function()
  506. local custom_lua_path = table.concat({xhome, 'custom.lua'}, pathsep)
  507. before_each(function()
  508. write_file(custom_lua_path, [[
  509. vim.g.custom_lua_rc = 1
  510. ]])
  511. end)
  512. it('loads custom lua config and does not set $MYVIMRC', function()
  513. clear{ args={'-u', custom_lua_path }, env=xenv }
  514. eq(1, eval('g:custom_lua_rc'))
  515. eq('', eval('$MYVIMRC'))
  516. end)
  517. end)
  518. describe 'VIMRC also exists'(function()
  519. before_each(function()
  520. write_file(table.concat({xconfig, 'nvim', 'init.vim'}, pathsep), [[
  521. let g:vim_rc = 1
  522. ]])
  523. end)
  524. it('loads default lua config, but shows an error', function()
  525. clear{ args_rm={'-u'}, env=xenv }
  526. feed('<cr>') -- confirm "Conflicting config ..." message
  527. eq(1, eval('g:lua_rc'))
  528. matches('^E5422: Conflicting configs', meths.exec('messages', true))
  529. end)
  530. end)
  531. end)
  532. describe('runtime:', function()
  533. local xhome = 'Xhome'
  534. local pathsep = helpers.get_pathsep()
  535. local xconfig = xhome .. pathsep .. 'Xconfig'
  536. local xdata = xhome .. pathsep .. 'Xdata'
  537. local xenv = { XDG_CONFIG_HOME=xconfig, XDG_DATA_HOME=xdata }
  538. setup(function()
  539. rmdir(xhome)
  540. mkdir_p(xconfig .. pathsep .. 'nvim')
  541. mkdir_p(xdata)
  542. end)
  543. teardown(function()
  544. rmdir(xhome)
  545. end)
  546. it('loads plugin/*.lua from XDG config home', function()
  547. local plugin_folder_path = table.concat({xconfig, 'nvim', 'plugin'}, pathsep)
  548. local plugin_file_path = table.concat({plugin_folder_path, 'plugin.lua'}, pathsep)
  549. mkdir_p(plugin_folder_path)
  550. write_file(plugin_file_path, [[ vim.g.lua_plugin = 1 ]])
  551. clear{ args_rm={'-u'}, env=xenv }
  552. eq(1, eval('g:lua_plugin'))
  553. rmdir(plugin_folder_path)
  554. end)
  555. it('loads plugin/*.lua from start packages', function()
  556. local plugin_path = table.concat({xconfig, 'nvim', 'pack', 'catagory',
  557. 'start', 'test_plugin'}, pathsep)
  558. local plugin_folder_path = table.concat({plugin_path, 'plugin'}, pathsep)
  559. local plugin_file_path = table.concat({plugin_folder_path, 'plugin.lua'},
  560. pathsep)
  561. local profiler_file = 'test_startuptime.log'
  562. mkdir_p(plugin_folder_path)
  563. write_file(plugin_file_path, [[vim.g.lua_plugin = 2]])
  564. clear{ args_rm={'-u'}, args={'--startuptime', profiler_file}, env=xenv }
  565. eq(2, eval('g:lua_plugin'))
  566. -- Check if plugin_file_path is listed in :scriptname
  567. local scripts = meths.exec(':scriptnames', true)
  568. assert.Truthy(scripts:find(plugin_file_path))
  569. -- Check if plugin_file_path is listed in startup profile
  570. local profile_reader = io.open(profiler_file, 'r')
  571. local profile_log = profile_reader:read('*a')
  572. profile_reader:close()
  573. assert.Truthy(profile_log :find(plugin_file_path))
  574. os.remove(profiler_file)
  575. rmdir(plugin_path)
  576. end)
  577. it('loads plugin/*.lua from site packages', function()
  578. local nvimdata = iswin() and "nvim-data" or "nvim"
  579. local plugin_path = table.concat({xdata, nvimdata, 'site', 'pack', 'xa', 'start', 'yb'}, pathsep)
  580. local plugin_folder_path = table.concat({plugin_path, 'plugin'}, pathsep)
  581. local plugin_after_path = table.concat({plugin_path, 'after', 'plugin'}, pathsep)
  582. local plugin_file_path = table.concat({plugin_folder_path, 'plugin.lua'}, pathsep)
  583. local plugin_after_file_path = table.concat({plugin_after_path, 'helloo.lua'}, pathsep)
  584. mkdir_p(plugin_folder_path)
  585. write_file(plugin_file_path, [[table.insert(_G.lista, "unos")]])
  586. mkdir_p(plugin_after_path)
  587. write_file(plugin_after_file_path, [[table.insert(_G.lista, "dos")]])
  588. clear{ args_rm={'-u'}, args={'--cmd', 'lua _G.lista = {}'}, env=xenv }
  589. eq({'unos', 'dos'}, exec_lua "return _G.lista")
  590. rmdir(plugin_path)
  591. end)
  592. it('loads ftdetect/*.lua', function()
  593. local ftdetect_folder = table.concat({xconfig, 'nvim', 'ftdetect'}, pathsep)
  594. local ftdetect_file = table.concat({ftdetect_folder , 'new-ft.lua'}, pathsep)
  595. mkdir_p(ftdetect_folder)
  596. write_file(ftdetect_file , [[vim.g.lua_ftdetect = 1]])
  597. clear{ args_rm={'-u'}, env=xenv }
  598. eq(1, eval('g:lua_ftdetect'))
  599. rmdir(ftdetect_folder)
  600. end)
  601. end)
  602. describe('user session', function()
  603. local xhome = 'Xhome'
  604. local pathsep = helpers.get_pathsep()
  605. local session_file = table.concat({xhome, 'session.lua'}, pathsep)
  606. before_each(function()
  607. rmdir(xhome)
  608. mkdir(xhome)
  609. write_file(session_file, [[
  610. vim.g.lua_session = 1
  611. ]])
  612. end)
  613. after_each(function()
  614. rmdir(xhome)
  615. end)
  616. it('loads session from the provided lua file', function()
  617. clear{ args={'-S', session_file }, env={ HOME=xhome }}
  618. eq(1, eval('g:lua_session'))
  619. end)
  620. end)