robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
aa1d8a2702
master
1local t =require ( 'test.testutil' ) 2local n =require ( 'test.functional.testnvim' )() 3local Screen =require ( 'test.functional.ui.screen' ) 4local describe , it , before_each , after_each , setup , teardown = 5t . describe , t . it , t . before_each , t . after_each , t . setup , t . teardown 6local skip_integ =os . getenv ( 'NVIM_TEST_INTEG' ) ~= '1' 7 8local api =n . api 9local fn =n . fn 10 11local eq =t . eq 12local matches =t . matches 13local pcall_err =t . pcall_err 14local exec_lua =n . exec_lua 15 16-- Helpers ==================================================================== 17-- Installed plugins ---------------------------------------------------------- 18 19local function pack_get_dir () 20return vim . fs . joinpath ( fn . stdpath ( 'data' ), 'site' , 'pack' , 'core' , 'opt' ) 21end 22 23local function pack_get_plug_path ( plug_name ) 24return vim . fs . joinpath ( pack_get_dir (), plug_name ) 25end 26 27local function pack_exists ( plug_name ) 28local path =vim . fs . joinpath ( pack_get_dir (), plug_name ) 29return vim . uv . fs_stat ( path ) ~= nil 30end 31 32--- Assert content from the main Lua file inside installed plugin. 33--- Used as a proxy for checking plugin state on disk. 34local function pack_assert_content ( plug_name , content ) 35local full_path =vim . fs . joinpath ( pack_get_plug_path ( plug_name ), 'lua' , plug_name .. '.lua' ) 36eq ( content , fn . readblob ( full_path )) 37end 38 39-- Test repos (to be installed) ----------------------------------------------- 40 41local repos_dir =vim . fs . abspath ( 'test/functional/lua/pack-test-repos' ) 42 43--- Map from repo name to its proper `src` used in plugin spec 44--- @type table<string,string> 45local repos_src ={} 46 47local function repo_get_path ( repo_name ) 48vim . validate ( 'repo_name' , repo_name , 'string' ) 49return vim . fs . joinpath ( repos_dir , repo_name ) 50end 51 52local function repo_write_file ( repo_name , rel_path , text , no_dedent , append ) 53local path =vim . fs . joinpath ( repo_get_path ( repo_name ), rel_path ) 54fn . mkdir ( vim . fs . dirname ( path ), 'p' ) 55t . write_file ( path , text , no_dedent , append ) 56end 57 58local function git_cmd ( cmd , repo_name ) 59local git_cmd_prefix ={ 60'git' , 61'-c' , 62'gc.auto=0' , 63'-c' , 64'user.name=Marvim' , 65'-c' , 66'user.email=marvim@neovim.io' , 67'-c' , 68'init.defaultBranch=main' , 69} 70 71cmd =vim . list_extend ( git_cmd_prefix , cmd ) 72local cwd =repo_get_path ( repo_name ) 73local sys_opts ={ cwd =cwd , text =true , clear_env =true } 74local out =n . system_sync ( cmd , sys_opts ) 75if out . code ~= 0 then 76error ( out . stderr ) 77end 78return ( out . stdout : gsub ( '\n+$' , '' )) 79end 80 81local function init_test_repo ( repo_name ) 82local path =repo_get_path ( repo_name ) 83fn . mkdir ( path , 'p' ) 84repos_src [ repo_name ] ='file://' .. path 85 86git_cmd ( { 'init' } , repo_name ) 87if t . is_arch ( 's390x' ) then 88-- Ensure default branch is 'main' even if git is too old for `init.defaultBranch`. 89git_cmd ( { 'symbolic-ref' , 'HEAD' , 'refs/heads/main' } , repo_name ) 90end 91end 92 93local function git_add_commit ( msg , repo_name ) 94git_cmd ( { 'add' , '*' } , repo_name ) 95git_cmd ( { 'commit' , '-m' , msg } , repo_name ) 96end 97 98local function git_get_hash ( rev , repo_name ) 99return git_cmd ( { 'rev-list' , '-1' , rev } , repo_name ) 100end 101 102local function git_get_short_hash ( rev , repo_name ) 103-- Pin `--abbrev`. Git's default abbrev differs across environments, which would shift the 104-- hard-coded column alignment and LSP link positions in the confirmation-buffer tests. 105return git_cmd ( { 'rev-list' , '-1' , '--abbrev=7' , '--abbrev-commit' , rev } , repo_name ) 106end 107 108-- Common test repos ---------------------------------------------------------- 109--- @type table<string,function> 110local repos_setup ={} 111 112function repos_setup . basic () 113init_test_repo ( 'basic' ) 114 115repo_write_file ( 'basic' , 'lua/basic.lua' , 'return "basic init"' ) 116git_add_commit ( 'Initial commit for "basic"' , 'basic' ) 117repo_write_file ( 'basic' , 'lua/basic.lua' , 'return "basic main"' ) 118git_add_commit ( 'Commit in `main` but not in `feat-branch`' , 'basic' ) 119 120git_cmd ( { 'checkout' , 'main~' } , 'basic' ) 121git_cmd ( { 'checkout' , '-b' , 'feat-branch' } , 'basic' ) 122 123repo_write_file ( 'basic' , 'lua/basic.lua' , 'return "basic some-tag"' ) 124git_add_commit ( 'Add commit for some tag' , 'basic' ) 125git_cmd ( { 'tag' , 'some-tag' } , 'basic' ) 126 127repo_write_file ( 'basic' , 'lua/basic.lua' , 'return "basic feat-branch"' ) 128git_add_commit ( 'Add important feature' , 'basic' ) 129 130-- Make sure that `main` is the default remote branch 131git_cmd ( { 'checkout' , 'main' } , 'basic' ) 132end 133 134function repos_setup . plugindirs () 135init_test_repo ( 'plugindirs' ) 136 137-- Add semver tag 138repo_write_file ( 'plugindirs' , 'lua/plugindirs.lua' , 'return "plugindirs v0.0.1"' ) 139git_add_commit ( 'Add version v0.0.1' , 'plugindirs' ) 140git_cmd ( { 'tag' , 'v0.0.1' } , 'plugindirs' ) 141 142-- Add various 'plugin/' files 143repo_write_file ( 'plugindirs' , 'lua/plugindirs.lua' , 'return "plugindirs main"' ) 144repo_write_file ( 'plugindirs' , 'plugin/dirs.lua' , 'vim.g._plugin = true' ) 145repo_write_file ( 'plugindirs' , 'plugin/dirs_log.lua' , '_G.DL = _G.DL or {}; DL[#DL+1] = "p"' ) 146repo_write_file ( 'plugindirs' , 'plugin/dirs.vim' , 'let g:_plugin_vim=v:true' ) 147repo_write_file ( 'plugindirs' , 'plugin/sub/dirs.lua' , 'vim.g._plugin_sub = true' ) 148repo_write_file ( 'plugindirs' , 'plugin/bad % name.lua' , 'vim.g._plugin_bad = true' ) 149repo_write_file ( 'plugindirs' , 'after/plugin/dirs.lua' , 'vim.g._after_plugin = true' ) 150repo_write_file ( 'plugindirs' , 'after/plugin/dirs_log.lua' , '_G.DL = _G.DL or {}; DL[#DL+1] = "a"' ) 151repo_write_file ( 'plugindirs' , 'after/plugin/dirs.vim' , 'let g:_after_plugin_vim=v:true' ) 152repo_write_file ( 'plugindirs' , 'after/plugin/sub/dirs.lua' , 'vim.g._after_plugin_sub = true' ) 153repo_write_file ( 'plugindirs' , 'after/plugin/bad % name.lua' , 'vim.g._after_plugin_bad = true' ) 154git_add_commit ( 'Initial commit for "plugindirs"' , 'plugindirs' ) 155end 156 157function repos_setup . helptags () 158init_test_repo ( 'helptags' ) 159repo_write_file ( 'helptags' , 'lua/helptags.lua' , 'return "helptags main"' ) 160repo_write_file ( 'helptags' , 'doc/my-test-help.txt' , '*my-test-help*' ) 161repo_write_file ( 'helptags' , 'doc/bad % name.txt' , '*my-test-help-bad*' ) 162repo_write_file ( 'helptags' , 'doc/bad % dir/file.txt' , '*my-test-help-sub-bad*' ) 163git_add_commit ( 'Initial commit for "helptags"' , 'helptags' ) 164end 165 166function repos_setup . pluginerr () 167init_test_repo ( 'pluginerr' ) 168 169repo_write_file ( 'pluginerr' , 'lua/pluginerr.lua' , 'return "pluginerr main"' ) 170repo_write_file ( 'pluginerr' , 'plugin/err.lua' , 'error("Wow, an error")' ) 171git_add_commit ( 'Initial commit for "pluginerr"' , 'pluginerr' ) 172end 173 174function repos_setup . defbranch () 175init_test_repo ( 'defbranch' ) 176 177repo_write_file ( 'defbranch' , 'lua/defbranch.lua' , 'return "defbranch main"' ) 178git_add_commit ( 'Initial commit for "defbranch"' , 'defbranch' ) 179 180-- Make `dev` the default remote branch 181git_cmd ( { 'checkout' , '-b' , 'dev' } , 'defbranch' ) 182 183repo_write_file ( 'defbranch' , 'lua/defbranch.lua' , 'return "defbranch dev"' ) 184git_add_commit ( 'Add to new default branch' , 'defbranch' ) 185end 186 187function repos_setup . gitsuffix () 188init_test_repo ( 'gitsuffix.git' ) 189 190repo_write_file ( 'gitsuffix.git' , 'lua/gitsuffix.lua' , 'return "gitsuffix main"' ) 191git_add_commit ( 'Initial commit for "gitsuffix"' , 'gitsuffix.git' ) 192end 193 194function repos_setup . semver () 195init_test_repo ( 'semver' ) 196 197local function add_tag ( name ) 198repo_write_file ( 'semver' , 'lua/semver.lua' , 'return "semver ' .. name .. '"' ) 199git_add_commit ( 'Add version ' .. name , 'semver' ) 200git_cmd ( { 'tag' , name } , 'semver' ) 201end 202 203add_tag ( 'v0.0.1' ) 204add_tag ( 'v0.0.2' ) 205add_tag ( 'v0.1.0' ) 206add_tag ( 'v0.1.1' ) 207add_tag ( 'v0.2.0-dev' ) 208add_tag ( 'v0.2.0' ) 209add_tag ( 'v0.3.0' ) 210repo_write_file ( 'semver' , 'lua/semver.lua' , 'return "semver middle-commit' ) 211git_add_commit ( 'Add middle commit' , 'semver' ) 212add_tag ( '0.3.1' ) -- Semver even without `v` prefix 213add_tag ( 'v0.4' ) -- Not semver since it requires all three version numbers 214add_tag ( 'non-semver' ) 215add_tag ( 'v0.2.1' ) -- Intentionally add version not in order 216add_tag ( 'v1.0.0' ) 217end 218 219function repos_setup . with_subs () 220-- To-be-submodule repo 221init_test_repo ( 'sub' ) 222 223repo_write_file ( 'sub' , 'sub.lua' , 'return "sub init"' ) 224git_add_commit ( 'Initial commit for "sub"' , 'sub' ) 225 226-- With-submodules repo with submodule recorded at its initial commit 227init_test_repo ( 'with_subs' ) 228 229repo_write_file ( 'with_subs' , 'lua/with_subs.lua' , 'return "with_subs init"' ) 230local sub_src ='file://' .. repo_get_path ( 'sub' ) 231git_cmd ( { '-c' , 'protocol.file.allow=always' , 'submodule' , 'add' , sub_src , 'sub' } , 'with_subs' ) 232git_add_commit ( 'Initial commit for "with_subs"' , 'with_subs' ) 233git_cmd ( { 'tag' , 'init-commit' } , 'with_subs' ) 234 235-- Advance both submodule and with-submodules repos by one commit 236repo_write_file ( 'sub' , 'sub.lua' , 'return "sub main"' ) 237git_add_commit ( 'Second commit for "sub"' , 'sub' ) 238 239repo_write_file ( 'with_subs' , 'lua/with_subs.lua' , 'return "with_subs main"' ) 240git_cmd ( 241{ '-c' , 'protocol.file.allow=always' , 'submodule' , 'update' , '--remote' , 'sub' } , 242'with_subs' 243) 244git_add_commit ( 'Second commit for "with_subs"' , 'with_subs' ) 245end 246 247function repos_setup . with_manifest () 248init_test_repo ( 'manifest' ) 249 250repo_write_file ( 'manifest' , 'lua/manifest.lua' , 'return "manifest init"' ) 251 252local manifest_tbl ={ 253name ='plug' , 254description ='Nvim plugin' , 255engine ={ nvim ='>=0.12.0' , vim ='>=9.1.0' } , 256-- Should handle both `*.lua` and `*.vim` files 257scripts ={ 258install ='scripts/install.lua' , 259preupdate ='scripts/preupdate.vim' , 260update ='scripts/update.lua' , 261preuninstall ='scirpts/preuninstall.vim' , 262} , 263} 264repo_write_file ( 'manifest' , 'pkg.json' , vim . json . encode ( manifest_tbl )) 265 266-- Create scripts that use Lua module from the same plugin 267local hook_module_text =[[ 268return { 269hook = function(script_name) 270-- NOTE: share the log with the one used for watching events to test 271-- relative order of events and script 272_G.event_log = _G.event_log or {} 273local rtp = vim.api.nvim_list_runtime_paths() 274local data = { kind = script_name, cwd = vim.fs.normalize(vim.uv.cwd()), rtp = rtp } 275table.insert(_G.event_log, { event = 'script', data = data }) 276 277-- Should handle errors during script exectuion 278error('Error in ' .. script_name .. ' script') 279end 280} 281]] 282repo_write_file ( 'manifest' , 'lua/manifest-hook.lua' , hook_module_text ) 283 284for name , script_path in pairs ( manifest_tbl . scripts ) do 285local text =( "require('manifest-hook').hook('%s')" ): format ( name ) 286if vim . endswith ( script_path , '.vim' ) then 287text ='lua << EOF\n' .. text .. '\nEOF' 288end 289repo_write_file ( 'manifest' , script_path , text ) 290end 291 292git_add_commit ( 'Initial commit' , 'manifest' ) 293 294-- Add extra branch to test script execution in `vim.pack.update()` 295git_cmd ( { 'checkout' , '-b' , 'other-branch' } , 'manifest' ) 296repo_write_file ( 'manifest' , 'lua/manifest.lua' , 'return "manifest other-branch"' ) 297local other_script ="_G.other_script = true\nrequire('manifest-hook').hook('update')" 298repo_write_file ( 'manifest' , 'scripts/update.lua' , other_script ) 299git_add_commit ( 'Other commit' , 'manifest' ) 300 301git_cmd ( { 'checkout' , 'main' } , 'manifest' ) 302end 303 304-- Utility -------------------------------------------------------------------- 305 306--- Execute `vim.pack.add()` inside `testnvim` instance 307local function vim_pack_add ( specs , opts ) 308exec_lua ( function () 309vim . pack . add ( specs , opts ) 310end ) 311end 312 313local function watch_events ( event ) 314exec_lua ( function () 315_G . event_log =_G . event_log or {} --- @type table[] 316vim . api . nvim_create_autocmd ( event , { 317callback =function ( ev ) 318table . insert ( _G . event_log , { event =ev . event , match =ev . match , data =ev . data } ) 319end , 320} ) 321end ) 322end 323 324--- Input is array of { suffix, kind, name, version, active, user_data } 325--- @param log_compact ([string,string,string,string,boolean,any])[] 326local function assert_packchanged ( log_compact ) 327local log =exec_lua ( 'return _G.event_log' ) 328local expected ={} --- @type table[] 329for i , l in ipairs ( log_compact ) do 330local path =pack_get_plug_path ( l [ 3 ]) 331local spec ={ name =l [ 3 ], src =repos_src [ l [ 3 ]], version =l [ 4 ], data =l [ 6 ] } 332local data ={ active =l [ 5 ], kind =l [ 2 ], path =path , spec =spec } 333expected [ i ] ={ event ='PackChanged' .. l [ 1 ], match =vim . fs . abspath ( path ), data =data } 334end 335eq ( expected , log ) 336end 337 338local function assert_manifest_scripts ( plug_path , ref_log_trimmed ) 339-- Should be sourced in particular order relative to `PackChanged` events 340local log_trimmed ={} --- @type table[] 341for i , tbl in ipairs ( exec_lua ( 'return _G.event_log' )) do 342local rtp_has_plug =tbl . data . rtp and vim . tbl_contains ( tbl . data . rtp , plug_path ) or nil 343local kind , cwd =tbl . data . kind , tbl . data . cwd 344log_trimmed [ i ] ={ event =tbl . event , kind =kind , cwd =cwd , rtp_has_plug =rtp_has_plug } 345end 346eq ( ref_log_trimmed , log_trimmed ) 347end 348 349local function track_nvim_echo () 350exec_lua ( function () 351_G . echo_log ={} 352local nvim_echo_orig =vim . api . nvim_echo 353---@diagnostic disable-next-line: duplicate-set-field 354vim . api . nvim_echo =function ( ... ) 355table . insert ( _G . echo_log , vim . deepcopy ( { ... } )) 356return nvim_echo_orig ( ... ) 357end 358end ) 359end 360 361local function is_in_echo_log ( chunk_substring , chunk_hl ) 362---@diagnostic disable-next-line: no-unknown 363for _ , msg in ipairs ( exec_lua ( 'return _G.echo_log' )) do 364local chunk =msg [ 1 ][ 1 ] --- @type [string, string] 365if chunk [ 1 ]: find ( chunk_substring , 0 , true ) and chunk [ 2 ] == chunk_hl then 366return true 367end 368end 369return false 370end 371 372--- @param echo_log table[]? 373local function assert_progress_report ( echo_log , action , step_names ) 374echo_log =echo_log or exec_lua ( 'return _G.echo_log' ) 375local n_steps =# step_names 376eq ( n_steps + 2 , # echo_log ) 377 378local progress = 379{ kind ='progress' , source ='vim.pack' , title ='vim.pack' , status ='running' , percent =0 } 380local init_step ={ { { ( '%s (0/%d)' ): format ( action , n_steps ) } } , true , progress } 381eq ( init_step , echo_log [ 1 ]) 382 383local steps_seen ={} --- @type table<string,boolean> 384for i =1 , n_steps do 385local echo_args =echo_log [ i + 1 ] 386 387-- NOTE: There is no guaranteed order (as it is async), so check that some 388-- expected step name is used in the message 389local msg =( '%s (%d/%d)' ): format ( action , i , n_steps ) 390local pattern ='^' .. vim . pesc ( msg ) .. ' %- (%S+)$' 391local step =echo_args [ 1 ][ 1 ][ 1 ]: match ( pattern ) ---@type string 392eq ( true , vim . tbl_contains ( step_names , step )) 393steps_seen [ step ] =true 394 395-- Should not add intermediate progress report to history 396eq ( false , echo_args [ 2 ]) 397 398-- Should update a single message by its id (computed after first call) 399progress . id =progress . id or echo_args [ 3 ]. id ---@type integer 400progress . percent =math . floor ( 100 * i / n_steps ) 401eq ( progress , echo_args [ 3 ]) 402end 403 404-- Should report all steps 405eq ( n_steps , vim . tbl_count ( steps_seen )) 406 407progress . percent , progress . status =100 , 'success' 408local final_step ={ { { ( '%s (%d/%d)' ): format ( action , n_steps , n_steps ) } } , true , progress } 409eq ( final_step , echo_log [ n_steps + 2 ]) 410end 411 412local function mock_confirm ( output_value ) 413exec_lua ( function () 414_G . confirm_log =_G . confirm_log or {} 415 416---@diagnostic disable-next-line: duplicate-set-field 417vim . fn . confirm =function ( ... ) 418table . insert ( _G . confirm_log , { ... } ) 419return output_value 420end 421end ) 422end 423 424local function mock_git_file_transport () 425-- HACK: mock `vim.system()` to have `git` commands be executed 426-- with temporarily set 'protocol.file.allow=always' option. 427-- Otherwise performing `git` operations with submodules from `vim.pack` 428-- itself will fail with `fatal: transport 'file' not allowed`. 429-- Directly adding `-c protocol.file.allow=always` to `git_cmd` in `vim.pack` 430-- itself is too much and might be bad for security. 431exec_lua ( function () 432local vim_system_orig =vim . system 433vim . system =function ( cmd , opts , on_exit ) 434if cmd [ 1 ] == 'git' then 435table . insert ( cmd , 2 , '-c' ) 436table . insert ( cmd , 3 , 'protocol.file.allow=always' ) 437end 438return vim_system_orig ( cmd , opts , on_exit ) 439end 440end ) 441end 442 443local function is_jit () 444return exec_lua ( 'return package.loaded.jit ~= nil' ) 445end 446 447local function get_lock_path () 448return vim . fs . joinpath ( fn . stdpath ( 'config' ), 'nvim-pack-lock.json' ) 449end 450 451local function get_other_lock_path () 452return vim . fs . joinpath ( fn . stdpath ( 'state' ), 'test-pack-lock' ) 453end 454 455--- @return {plugins:table<string, {rev:string, src:string, version?:string}>} 456local function get_lock_tbl () 457return vim . json . decode ( fn . readblob ( get_lock_path ())) 458end 459 460-- Tests ====================================================================== 461 462describe ( 'vim.pack' , function () 463setup ( function () 464n . clear () 465for _ , r_setup in pairs ( repos_setup ) do 466r_setup () 467end 468end ) 469 470before_each ( function () 471n . clear () 472end ) 473 474after_each ( function () 475local pack_dir =pack_get_dir () 476local lock_path =get_lock_path () 477local other_lock_path =get_other_lock_path () 478local log_path =vim . fs . joinpath ( fn . stdpath ( 'log' ), 'nvim-pack.log' ) 479 480-- Wait for neovim to close before removing directories so it can release 481-- the file handles it has open. We don't want to conflict with open files 482-- when we remove dirs below. 483n . check_close () 484 485n . rmdir ( pack_dir ) 486pcall ( vim . fs . rm , lock_path , { force =true } ) 487pcall ( vim . fs . rm , other_lock_path , { force =true } ) 488pcall ( vim . fs . rm , log_path , { force =true } ) 489end ) 490 491teardown ( function () 492n . rmdir ( repos_dir ) 493end ) 494 495describe ( 'add()' , function () 496it ( 'installs only once' , function () 497vim_pack_add ( { repos_src . basic } ) 498n . clear () 499 500watch_events ( { 'PackChanged' } ) 501vim_pack_add ( { repos_src . basic } ) 502eq ( 0 , exec_lua ( 'return #_G.event_log' )) 503 504-- Should not create redundant stash entry 505local basic_path =pack_get_plug_path ( 'basic' ) 506local stash_list =n . system_sync ( { 'git' , 'stash' , 'list' } , { cwd =basic_path } ). stdout 507or '' 508eq ( '' , stash_list ) 509end ) 510 511it ( 'passes `data` field through to `opts.load`' , function () 512local out =exec_lua ( function () 513local map ={} ---@type table<string,boolean> 514local function load ( p ) 515local name =p . spec . name ---@type string 516map [ name ] =name == 'basic' and ( p . spec . data . test == 'value' ) or ( p . spec . data == 'value' ) 517end 518vim . pack . add ( { 519{ src =repos_src . basic , data ={ test ='value' } } , 520{ src =repos_src . defbranch , data ='value' } , 521} , { load =load } ) 522return map 523end ) 524eq ( { basic =true , defbranch =true } , out ) 525end ) 526 527it ( 'asks for installation confirmation' , function () 528-- Do not confirm installation to see what happens (should not error) 529mock_confirm ( 2 ) 530 531vim_pack_add ( { repos_src . basic , { src =repos_src . defbranch , name ='other-name' } } ) 532eq ( false , pack_exists ( 'basic' )) 533eq ( false , pack_exists ( 'defbranch' )) 534eq ( { plugins ={} } , get_lock_tbl ()) 535 536local confirm_msg_lines =( [[ 537These plugins will be installed: 538 539basic from %s 540other-name from %s]] ): format ( repos_src . basic , repos_src . defbranch ) 541local confirm_msg =vim . trim ( vim . text . indent ( 0 , confirm_msg_lines )) 542local ref_log ={ { confirm_msg .. '\n' , 'Proceed? &Yes\n&No\n&Always' , 1 , 'Question' } } 543eq ( ref_log , exec_lua ( 'return _G.confirm_log' )) 544 545-- Should remove lock data if not confirmed during lockfile sync 546n . clear () 547vim_pack_add ( { repos_src . basic } ) 548eq ( true , pack_exists ( 'basic' )) 549eq ( 'table' , type ( get_lock_tbl (). plugins . basic )) 550 551n . rmdir ( pack_get_dir ()) 552n . clear () 553mock_confirm ( 2 ) 554 555vim_pack_add ( { repos_src . basic } ) 556eq ( false , pack_exists ( 'basic' )) 557eq ( { plugins ={} } , get_lock_tbl ()) 558 559-- Should ask for confirm twice: during lockfile sync and inside 560-- `vim.pack.add()` (i.e. not confirming during lockfile sync has 561-- an immediate effect on whether a plugin is installed or not) 562eq ( 2 , exec_lua ( 'return #_G.confirm_log' )) 563end ) 564 565it ( 'respects `opts.confirm`' , function () 566mock_confirm ( 1 ) 567vim_pack_add ( { repos_src . basic } , { confirm =false } ) 568 569eq ( 0 , exec_lua ( 'return #_G.confirm_log' )) 570eq ( true , pack_exists ( 'basic' )) 571 572-- Should also respect `confirm` when installing during lockfile sync 573n . rmdir ( pack_get_dir ()) 574n . clear () 575mock_confirm ( 1 ) 576eq ( 'table' , type ( get_lock_tbl (). plugins . basic )) 577 578vim_pack_add ( {} , { confirm =false } ) 579eq ( 0 , exec_lua ( 'return #_G.confirm_log' )) 580eq ( true , pack_exists ( 'basic' )) 581end ) 582 583it ( 'can always confirm in current session' , function () 584mock_confirm ( 3 ) 585 586vim_pack_add ( { repos_src . basic } ) 587eq ( 1 , exec_lua ( 'return #_G.confirm_log' )) 588eq ( 'basic main' , exec_lua ( 'return require("basic")' )) 589 590vim_pack_add ( { repos_src . defbranch } ) 591eq ( 1 , exec_lua ( 'return #_G.confirm_log' )) 592eq ( 'defbranch dev' , exec_lua ( 'return require("defbranch")' )) 593 594-- Should still ask in next session 595n . clear () 596mock_confirm ( 3 ) 597vim_pack_add ( { repos_src . plugindirs } ) 598eq ( 1 , exec_lua ( 'return #_G.confirm_log' )) 599eq ( 'plugindirs main' , exec_lua ( 'return require("plugindirs")' )) 600end ) 601 602it ( 'creates lockfile' , function () 603local helptags_rev =git_get_hash ( 'HEAD' , 'helptags' ) 604exec_lua ( function () 605vim . pack . add ( { 606{ src =repos_src . basic , version ='some-tag' } , 607{ src =repos_src . defbranch , version ='main' } , 608{ src =repos_src . helptags , version =helptags_rev } , 609{ src =repos_src . plugindirs } , 610{ src =repos_src . semver , version =vim . version . range ( '*' ) } , 611} ) 612end ) 613 614local basic_rev =git_get_hash ( 'some-tag' , 'basic' ) 615local defbranch_rev =git_get_hash ( 'main' , 'defbranch' ) 616local plugindirs_rev =git_get_hash ( 'HEAD' , 'plugindirs' ) 617local semver_rev =git_get_hash ( 'v1.0.0' , 'semver' ) 618 619-- Should properly format as indented JSON. Notes: 620-- - Branch, tag, and commit should be serialized like `'value'` to be 621-- distinguishable from version ranges. 622-- - Absent `version` should be missing and not autoresolved. 623local ref_lockfile_lines =( [[ 624{ 625"plugins": { 626"basic": { 627"rev": "%s", 628"src": "%s", 629"version": "'some-tag'" 630}, 631"defbranch": { 632"rev": "%s", 633"src": "%s", 634"version": "'main'" 635}, 636"helptags": { 637"rev": "%s", 638"src": "%s", 639"version": "'%s'" 640}, 641"plugindirs": { 642"rev": "%s", 643"src": "%s" 644}, 645"semver": { 646"rev": "%s", 647"src": "%s", 648"version": ">=0.0.0" 649} 650} 651} 652]] ): format ( 653basic_rev , 654repos_src . basic , 655defbranch_rev , 656repos_src . defbranch , 657helptags_rev , 658repos_src . helptags , 659helptags_rev , 660plugindirs_rev , 661repos_src . plugindirs , 662semver_rev , 663repos_src . semver 664) 665eq ( vim . text . indent ( 0 , ref_lockfile_lines ), fn . readblob ( get_lock_path ())) 666end ) 667 668it ( 'updates lockfile' , function () 669vim_pack_add ( { repos_src . basic } ) 670local ref_lockfile ={ 671plugins ={ 672basic ={ rev =git_get_hash ( 'main' , 'basic' ), src =repos_src . basic } , 673} , 674} 675eq ( ref_lockfile , get_lock_tbl ()) 676 677n . clear () 678vim_pack_add ( { { src =repos_src . basic , version ='main' } } ) 679 680ref_lockfile . plugins . basic . version ="'main'" 681eq ( ref_lockfile , get_lock_tbl ()) 682end ) 683 684it ( 'uses lockfile during install' , function () 685vim_pack_add ( { { src =repos_src . basic , version ='feat-branch' } , repos_src . defbranch } ) 686 687-- Mock clean initial install, but with lockfile present 688n . rmdir ( pack_get_dir ()) 689n . clear () 690watch_events ( { 'PackChangedPre' , 'PackChanged' } ) 691 692local basic_rev =git_get_hash ( 'feat-branch' , 'basic' ) 693local defbranch_rev =git_get_hash ( 'HEAD' , 'defbranch' ) 694local ref_lockfile ={ 695plugins ={ 696basic ={ rev =basic_rev , src =repos_src . basic , version ="'feat-branch'" } , 697defbranch ={ rev =defbranch_rev , src =repos_src . defbranch } , 698} , 699} 700eq ( ref_lockfile , get_lock_tbl ()) 701 702mock_confirm ( 1 ) 703-- Should use revision from lockfile (pointing at latest 'feat-branch' 704-- commit) and not use latest `main` commit. Although should report 705-- `version = 'main'` inside event data and write it to the lockfile as 706-- to preserve user input as much as possible. 707-- Should also preserve `data` field in event data. 708vim_pack_add ( { { src =repos_src . basic , version ='main' , data ={ 'd' } } } ) 709pack_assert_content ( 'basic' , 'return "basic feat-branch"' ) 710ref_lockfile . plugins . basic . version ="'main'" 711eq ( ref_lockfile , get_lock_tbl ()) 712 713local confirm_log =exec_lua ( 'return _G.confirm_log' ) 714eq ( 1 , # confirm_log ) 715matches ( 'basic.*defbranch' , confirm_log [ 1 ][ 1 ]) 716 717-- Should install `defbranch` (as it is in lockfile), but not load it 718eq ( true , pack_exists ( 'defbranch' )) 719eq ( false , exec_lua ( 'return pcall(require, "defbranch")' )) 720 721-- Should trigger `kind=install` events in a pre-defined order 722assert_packchanged ( { 723{ 'Pre' , 'install' , 'basic' , 'main' , false , { 'd' } } , 724{ 'Pre' , 'install' , 'defbranch' , nil , false } , 725{ '' , 'install' , 'basic' , 'main' , false , { 'd' } } , 726{ '' , 'install' , 'defbranch' , nil , false } , 727} ) 728 729-- Running `update()` should still update to use `main` 730exec_lua ( function () 731vim . pack . update ( { 'basic' } , { force =true } ) 732end ) 733pack_assert_content ( 'basic' , 'return "basic main"' ) 734 735ref_lockfile . plugins . basic . rev =git_get_hash ( 'main' , 'basic' ) 736ref_lockfile . plugins . basic . version ="'main'" 737eq ( ref_lockfile , get_lock_tbl ()) 738 739-- Improper or string spec input should not interfere with initial install 740n . rmdir ( pack_get_dir ()) 741n . clear () 742 743mock_confirm ( 1 ) 744pcall_err ( vim_pack_add , { repos_src . basic , 1 } ) 745eq ( true , pack_exists ( 'basic' )) 746eq ( true , pack_exists ( 'defbranch' )) 747 748-- Direct install from the lockfile should not write to it 749n . rmdir ( pack_get_dir ()) 750n . clear () 751local lockfile_fs_stat =vim . uv . fs_stat ( get_lock_path ()) 752vim_pack_add ( {} ) 753eq ( lockfile_fs_stat , vim . uv . fs_stat ( get_lock_path ())) 754end ) 755 756it ( 'handles lockfile during install errors' , function () 757local repo_not_exist ='file://' .. repo_get_path ( 'does-not-exist' ) 758pcall_err ( exec_lua , function () 759vim . pack . add ( { 760repo_not_exist , 761{ src =repos_src . basic , version ='not-exist' } , 762{ src =repos_src . pluginerr , version ='main' } , 763} ) 764end ) 765 766local pluginerr_hash =git_get_hash ( 'main' , 'pluginerr' ) 767local ref_lockfile ={ 768-- Should be no entry for `repo_not_exist` and `basic` as they did not 769-- fully install 770plugins ={ 771-- Error during sourcing 'plugin/' should not affect lockfile 772pluginerr ={ rev =pluginerr_hash , src =repos_src . pluginerr , version ="'main'" } , 773} , 774} 775eq ( ref_lockfile , get_lock_tbl ()) 776end ) 777 778it ( 'regenerates manually deleted lockfile' , function () 779vim_pack_add ( { 780{ src =repos_src . basic , name ='other' , version ='feat-branch' } , 781repos_src . defbranch , 782} ) 783local lock_path =get_lock_path () 784eq ( true , vim . uv . fs_stat ( lock_path ) ~= nil ) 785 786local basic_rev =git_get_hash ( 'feat-branch' , 'basic' ) 787local plugindirs_rev =git_get_hash ( 'dev' , 'defbranch' ) 788 789-- Should try its best to regenerate lockfile based on installed plugins 790fn . delete ( get_lock_path ()) 791n . clear () 792vim_pack_add ( {} ) 793local ref_lockfile ={ 794plugins ={ 795-- No `version = 'feat-branch'` as there is no way to get that info 796-- (lockfile was the only source of that on disk) 797other ={ rev =basic_rev , src =repos_src . basic } , 798defbranch ={ rev =plugindirs_rev , src =repos_src . defbranch } , 799} , 800} 801eq ( ref_lockfile , get_lock_tbl ()) 802 803local ref_messages ='vim.pack: Repaired corrupted lock data for plugins: defbranch, other' 804eq ( ref_messages , n . exec_capture ( 'messages' )) 805 806-- Calling `add()` with `version` should still add it to lockfile 807vim_pack_add ( { { src =repos_src . basic , name ='other' , version ='feat-branch' } } ) 808eq ( "'feat-branch'" , get_lock_tbl (). plugins . other . version ) 809end ) 810 811it ( 'repairs corrupted lock data for installed plugins' , function () 812vim_pack_add ( { 813-- Should preserve present `version` 814{ src =repos_src . basic , version ='feat-branch' } , 815repos_src . defbranch , 816repos_src . semver , 817repos_src . helptags , 818} ) 819 820local lock_tbl =get_lock_tbl () 821local ref_lock_tbl =vim . deepcopy ( lock_tbl ) 822local assert =function () 823vim_pack_add ( {} ) 824eq ( ref_lock_tbl , get_lock_tbl ()) 825eq ( true , pack_exists ( 'basic' )) 826eq ( true , pack_exists ( 'defbranch' )) 827eq ( true , pack_exists ( 'semver' )) 828eq ( true , pack_exists ( 'helptags' )) 829end 830 831-- Missing lock data required field 832lock_tbl . plugins . basic . rev =nil 833-- Wrong lock data field type 834lock_tbl . plugins . defbranch . src =1 ---@diagnostic disable-line: assign-type-mismatch 835-- Wrong lock data type 836lock_tbl . plugins . semver =1 ---@diagnostic disable-line: assign-type-mismatch 837 838local lockfile_text =vim . json . encode ( lock_tbl , { indent =' ' , sort_keys =true } ) 839fn . writefile ( vim . split ( lockfile_text , '\n' ), get_lock_path ()) 840 841n . clear () 842assert () 843 844local ref_messages = 845'vim.pack: Repaired corrupted lock data for plugins: basic, defbranch, semver' 846eq ( ref_messages , n . exec_capture ( 'messages' )) 847 848-- Should work even for badly corrupted lockfile 849lockfile_text =vim . json . encode ( { plugins =1 } , { indent =' ' , sort_keys =true } ) 850fn . writefile ( vim . split ( lockfile_text , '\n' ), get_lock_path ()) 851 852n . clear () 853-- Can not preserve `version` if it was deleted from the lockfile 854ref_lock_tbl . plugins . basic . version =nil 855assert () 856end ) 857 858it ( "respects 'packlockfile'" , function () 859local other_lock_path =get_other_lock_path () 860api . nvim_set_option_value ( 'packlockfile' , other_lock_path , { scope ='global' } ) 861exec_lua ( function () 862vim . pack . add ( { repos_src . basic } ) 863end ) 864local ref_lockfile ={ 865plugins ={ 866basic ={ rev =git_get_hash ( 'main' , 'basic' ), src =repos_src . basic } , 867} , 868} 869eq ( ref_lockfile , vim . json . decode ( fn . readblob ( other_lock_path ))) 870eq ( false , vim . uv . fs_stat ( get_lock_path ()) ~= nil ) 871 872n . clear () 873api . nvim_set_option_value ( 'packlockfile' , other_lock_path , { scope ='global' } ) 874exec_lua ( function () 875vim . pack . add ( { { src =repos_src . basic , version ='feat-branch' } } ) 876end ) 877ref_lockfile . plugins . basic . version ="'feat-branch'" 878eq ( ref_lockfile , vim . json . decode ( fn . readblob ( other_lock_path ))) 879end ) 880 881it ( 'removes unrepairable corrupted data and plugins' , function () 882vim_pack_add ( { repos_src . basic , repos_src . defbranch , repos_src . semver , repos_src . helptags } ) 883 884local lock_tbl =get_lock_tbl () 885local ref_lock_tbl =vim . deepcopy ( lock_tbl ) 886 887-- Corrupted data for missing plugin 888vim . fs . rm ( pack_get_plug_path ( 'basic' ), { recursive =true , force =true } ) 889lock_tbl . plugins . basic . rev =nil 890 891-- Good data for corrupted plugin 892local defbranch_path =pack_get_plug_path ( 'defbranch' ) 893vim . fs . rm ( defbranch_path , { recursive =true , force =true } ) 894fn . writefile ( { 'File and not directory' } , defbranch_path ) 895 896-- Corrupted data for corrupted plugin 897local semver_path =pack_get_plug_path ( 'semver' ) 898vim . fs . rm ( semver_path , { recursive =true , force =true } ) 899fn . writefile ( { 'File and not directory' } , semver_path ) 900lock_tbl . plugins . semver . rev =1 ---@diagnostic disable-line: assign-type-mismatch 901 902local lockfile_text =vim . json . encode ( lock_tbl , { indent =' ' , sort_keys =true } ) 903fn . writefile ( vim . split ( lockfile_text , '\n' ), get_lock_path ()) 904 905n . clear () 906vim_pack_add ( {} ) 907ref_lock_tbl . plugins . basic =nil 908ref_lock_tbl . plugins . defbranch =nil 909ref_lock_tbl . plugins . semver =nil 910eq ( ref_lock_tbl , get_lock_tbl ()) 911 912eq ( false , pack_exists ( 'basic' )) 913eq ( false , pack_exists ( 'defbranch' )) 914eq ( false , pack_exists ( 'semver' )) 915eq ( true , pack_exists ( 'helptags' )) 916end ) 917 918it ( 'installs at proper version' , function () 919local out =exec_lua ( function () 920vim . pack . add ( { { src =repos_src . basic , version ='feat-branch' } } ) 921-- Should have plugin available immediately (not even on the next loop) 922return require ( 'basic' ) 923end ) 924 925eq ( 'basic feat-branch' , out ) 926 927local rtp =api . nvim_list_runtime_paths () 928local plug_path =pack_get_plug_path ( 'basic' ) 929local after_dir =vim . fs . joinpath ( plug_path , 'after' ) 930eq ( true , vim . tbl_contains ( rtp , plug_path )) 931-- No 'after/' directory in runtimepath because it is not present in plugin 932eq ( false , vim . tbl_contains ( rtp , after_dir )) 933end ) 934 935it ( 'installs with submodules' , function () 936mock_git_file_transport () 937vim_pack_add ( { repos_src . with_subs } ) 938 939local sub_lua_file =vim . fs . joinpath ( pack_get_plug_path ( 'with_subs' ), 'sub' , 'sub.lua' ) 940eq ( 'return "sub main"' , fn . readblob ( sub_lua_file )) 941end ) 942 943it ( 'does not install on bad `version`' , function () 944local err =pcall_err ( exec_lua , function () 945vim . pack . add ( { { src =repos_src . basic , version ='not-exist' } } ) 946end ) 947matches ( '`not%-exist` is not a branch/tag/commit' , err ) 948eq ( false , pack_exists ( 'basic' )) 949end ) 950 951it ( 'can install from the Internet' , function () 952t . skip ( skip_integ , 'NVIM_TEST_INTEG not set (network integration test)' ) 953vim_pack_add ( { 'https://github.com/neovim/nvim-lspconfig' } ) 954eq ( true , exec_lua ( 'return pcall(require, "lspconfig")' )) 955end ) 956 957describe ( 'startup' , function () 958local config_dir , pack_add_cmd ='' , '' 959 960before_each ( function () 961config_dir =fn . stdpath ( 'config' ) 962fn . mkdir ( vim . fs . joinpath ( config_dir , 'plugin' ), 'p' ) 963 964pack_add_cmd =( 'vim.pack.add({ %s })' ): format ( vim . inspect ( repos_src . plugindirs )) 965end ) 966 967after_each ( function () 968n . rmdir ( config_dir ) 969end ) 970 971local function assert_loaded () 972eq ( 'plugindirs main' , exec_lua ( 'return require("plugindirs")' )) 973 974-- Should source 'plugin/' and 'after/plugin/' exactly once 975eq ( { true , true } , n . exec_lua ( 'return { vim.g._plugin, vim.g._after_plugin }' )) 976eq ( { 'p' , 'a' } , n . exec_lua ( 'return _G.DL' )) 977end 978 979local function assert_works () 980-- Should auto-install but wait before executing code after it 981n . clear ( { args_rm ={ '-u' } } ) 982t . retry ( nil , t . is_os ( 'win' ) and 30000 or 5000 , function () 983eq ( true , exec_lua ( 'return _G.done' )) 984end ) 985assert_loaded () 986 987-- Should only `:packadd!`/`:packadd` already installed plugin 988n . clear ( { args_rm ={ '-u' } } ) 989assert_loaded () 990end 991 992it ( 'works in init.lua' , function () 993local init_lua =vim . fs . joinpath ( config_dir , 'init.lua' ) 994fn . writefile ( { pack_add_cmd , '_G.done = true' } , init_lua ) 995assert_works () 996 997-- Should not load plugins if `--noplugin`, only adjust 'runtimepath' 998n . clear ( { args ={ '--noplugin' } , args_rm ={ '-u' } } ) 999eq ( 'plugindirs main' , exec_lua ( 'return require("plugindirs")' )) 1000eq ( {} , n . exec_lua ( 'return { vim.g._plugin, vim.g._after_plugin }' )) 1001eq ( vim . NIL , n . exec_lua ( 'return _G.DL' )) 1002end ) 1003 1004it ( 'works in plugin/' , function () 1005local plugin_file =vim . fs . joinpath ( config_dir , 'plugin' , 'mine.lua' ) 1006fn . writefile ( { pack_add_cmd , '_G.done = true' } , plugin_file ) 1007-- Should source plugin's 'plugin/' files without explicit `load=true` 1008assert_works () 1009end ) 1010end ) 1011 1012it ( 'shows progress report during installation' , function () 1013track_nvim_echo () 1014vim_pack_add ( { repos_src . basic , repos_src . defbranch } ) 1015assert_progress_report ( nil , 'Installing plugins' , { 'basic' , 'defbranch' } ) 1016end ) 1017 1018it ( 'triggers relevant events' , function () 1019watch_events ( { 'PackChangedPre' , 'PackChanged' } ) 1020 1021-- Should provide event-data respecting manual `version` without inferring default 1022-- Should order events as they were supplied in `vim.pack.add` 1023vim_pack_add ( { repos_src . defbranch , { src =repos_src . basic , version ='feat-branch' } } ) 1024assert_packchanged ( { 1025{ 'Pre' , 'install' , 'defbranch' , nil , false } , 1026{ 'Pre' , 'install' , 'basic' , 'feat-branch' , false } , 1027{ '' , 'install' , 'defbranch' , nil , false } , 1028{ '' , 'install' , 'basic' , 'feat-branch' , false } , 1029} ) 1030end ) 1031 1032it ( 'sources relevant manifest scripts' , function () 1033watch_events ( { 'PackChangedPre' , 'PackChanged' } ) 1034track_nvim_echo () 1035local cwd =fn . getcwd () 1036exec_lua ( function () 1037vim . pack . add ( { repos_src . manifest } , { load =function () end } ) 1038end ) 1039 1040-- Should not have side effects after executing script in special context 1041eq ( cwd , fn . getcwd ()) 1042local plug_path =pack_get_plug_path ( 'manifest' ) 1043eq ( false , vim . tbl_contains ( api . nvim_list_runtime_paths (), plug_path )) 1044 1045-- Should execute script in special context after triggering events 1046assert_manifest_scripts ( plug_path , { 1047{ event ='PackChangedPre' , kind ='install' } , 1048{ event ='PackChanged' , kind ='install' } , 1049{ event ='script' , kind ='install' , cwd =plug_path , rtp_has_plug =true } , 1050} ) 1051 1052-- Should warn on errors during script execution 1053eq ( true , is_in_echo_log ( 'Error in install script' , 'WarningMsg' )) 1054end ) 1055 1056it ( 'recognizes several `version` types' , function () 1057local prev_commit =git_get_hash ( 'HEAD~' , 'defbranch' ) 1058exec_lua ( function () 1059vim . pack . add ( { 1060{ src =repos_src . basic , version ='some-tag' } , -- Tag 1061{ src =repos_src . defbranch , version =prev_commit } , -- Commit hash 1062{ src =repos_src . semver , version =vim . version . range ( '<1' ) } , -- Semver constraint 1063} ) 1064end ) 1065 1066eq ( 'basic some-tag' , exec_lua ( 'return require("basic")' )) 1067eq ( 'defbranch main' , exec_lua ( 'return require("defbranch")' )) 1068eq ( 'semver 0.3.1' , exec_lua ( 'return require("semver")' )) 1069end ) 1070 1071it ( 'respects plugin/ and after/plugin/ scripts' , function () 1072local function assert ( load , ref ) 1073vim_pack_add ( { { src =repos_src . plugindirs , name ='plugin % dirs' } } , { load =load } ) 1074-- Should handle bad plugin directory name 1075local out =exec_lua ( function () 1076return { 1077vim . g . _plugin , 1078vim . g . _plugin_vim , 1079vim . g . _plugin_sub , 1080vim . g . _plugin_bad , 1081vim . g . _after_plugin , 1082vim . g . _after_plugin_vim , 1083vim . g . _after_plugin_sub , 1084vim . g . _after_plugin_bad , 1085} 1086end ) 1087eq ( ref , out ) 1088 1089-- Should add necessary directories to runtimepath regardless of `opts.load` 1090local rtp =api . nvim_list_runtime_paths () 1091local plug_path =pack_get_plug_path ( 'plugin % dirs' ) 1092local after_dir =vim . fs . joinpath ( plug_path , 'after' ) 1093eq ( true , vim . tbl_contains ( rtp , plug_path )) 1094eq ( true , vim . tbl_contains ( rtp , after_dir )) 1095end 1096 1097assert ( nil , { true , true , true , true , true , true , true , true } ) 1098 1099n . clear () 1100assert ( false , {} ) 1101end ) 1102 1103it ( 'can use function `opts.load`' , function () 1104local function assert () 1105n . exec_lua ( function () 1106_G . load_log ={} 1107local function load ( ... ) 1108table . insert ( _G . load_log , { ... } ) 1109end 1110vim . pack . add ( { repos_src . plugindirs , repos_src . basic } , { load =load } ) 1111end ) 1112 1113-- Order of execution should be the same as supplied in `add()` 1114local plugindirs_data ={ 1115spec ={ src =repos_src . plugindirs , name ='plugindirs' } , 1116path =pack_get_plug_path ( 'plugindirs' ), 1117} 1118local basic_data ={ 1119spec ={ src =repos_src . basic , name ='basic' } , 1120path =pack_get_plug_path ( 'basic' ), 1121} 1122-- - Only single table argument should be supplied to `load` 1123local ref_log ={ { plugindirs_data } , { basic_data } } 1124eq ( ref_log , n . exec_lua ( 'return _G.load_log' )) 1125 1126-- Should not add plugin to the session in any way 1127eq ( false , exec_lua ( 'return pcall(require, "plugindirs")' )) 1128eq ( false , exec_lua ( 'return pcall(require, "basic")' )) 1129 1130-- Should not source 'plugin/' 1131eq ( {} , n . exec_lua ( 'return { vim.g._plugin, vim.g._after_plugin }' )) 1132 1133-- Plugins should still be marked as "active", since they were added 1134eq ( true , exec_lua ( 'return vim.pack.get({ "plugindirs" })[1].active' )) 1135eq ( true , exec_lua ( 'return vim.pack.get({ "basic" })[1].active' )) 1136end 1137 1138-- Works on initial install 1139assert () 1140 1141-- Works when loading already installed plugin 1142n . clear () 1143assert () 1144end ) 1145 1146it ( 'generates help tags' , function () 1147vim_pack_add ( { { src =repos_src . helptags , name ='help tags' } } ) 1148local target_tags =fn . getcompletion ( 'my-test' , 'help' ) 1149table . sort ( target_tags ) 1150eq ( { 'my-test-help' , 'my-test-help-bad' , 'my-test-help-sub-bad' } , target_tags ) 1151end ) 1152 1153it ( 'reports install/load errors after loading all input' , function () 1154t . skip ( not is_jit (), "Non LuaJIT reports errors differently due to 'coxpcall'" ) 1155local function assert ( err_pat ) 1156local err =pcall_err ( exec_lua , function () 1157vim . pack . add ( { 1158{ src =repos_src . basic , version ='wrong-version' } , -- Error during initial checkout 1159{ src =repos_src . semver , version =vim . version . range ( '>=2.0.0' ) } , -- Missing version 1160{ src =repos_src . plugindirs , version ='main' } , 1161{ src =repos_src . pluginerr , version ='main' } , -- Error during 'plugin/' source 1162} ) 1163end ) 1164 1165matches ( err_pat , err ) 1166 1167-- Should have processed non-errored 'plugin/' and add to 'rtp' 1168eq ( 'plugindirs main' , exec_lua ( 'return require("plugindirs")' )) 1169eq ( true , exec_lua ( 'return vim.g._plugin' )) 1170 1171-- Should add plugin to 'rtp' even if 'plugin/' has error 1172eq ( 'pluginerr main' , exec_lua ( 'return require("pluginerr")' )) 1173end 1174 1175-- During initial install 1176local err_pat_parts ={ 1177'vim%.pack' , 1178'`basic`:\n' , 1179-- Should report available branches and tags if revision is absent 1180'`wrong%-version`' , 1181-- Should list default branch first 1182'Available:\nTags: some%-tag\nBranches: main, feat%-branch' , 1183-- Should report available branches and versions if no constraint match 1184'`semver`' , 1185'Available:\nVersions: v1%.0%.0, 0%.3%.1, v0%.3%.0.*\nBranches: main\n' , 1186'`pluginerr`:\n' , 1187'Wow, an error' , 1188} 1189assert ( table . concat ( err_pat_parts , '.*' )) 1190 1191-- During loading already installed plugin. 1192n . clear () 1193-- NOTE: There is no error for wrong `version`, because there is no check 1194-- for already installed plugins. Might change in the future. 1195assert ( 'vim%.pack.*`pluginerr`:\n.*Wow, an error' ) 1196end ) 1197 1198it ( 'normalizes each spec' , function () 1199vim_pack_add ( { 1200repos_src . basic , -- String should be inferred as `{ src = ... }` 1201{ src =repos_src . defbranch } , -- Default `version` is remote's default branch 1202{ src =repos_src [ 'gitsuffix.git' ] } , -- Default `name` comes from `src` repo name 1203{ src =repos_src . plugindirs , name ='plugin/dirs' } , -- Ensure proper directory name 1204} ) 1205 1206eq ( 'basic main' , exec_lua ( 'return require("basic")' )) 1207eq ( 'defbranch dev' , exec_lua ( 'return require("defbranch")' )) 1208eq ( 'gitsuffix main' , exec_lua ( 'return require("gitsuffix")' )) 1209eq ( true , exec_lua ( 'return vim.g._plugin' )) 1210 1211eq ( true , pack_exists ( 'gitsuffix' )) 1212eq ( true , pack_exists ( 'dirs' )) 1213end ) 1214 1215it ( 'handles problematic names' , function () 1216vim_pack_add ( { { src =repos_src . basic , name ='bad % name' } } ) 1217eq ( 'basic main' , exec_lua ( 'return require("basic")' )) 1218end ) 1219 1220it ( 'is not affected by special environment variables' , function () 1221fn . setenv ( 'GIT_WORK_TREE' , t . paths . test_source_path ) 1222fn . setenv ( 'GIT_DIR' , vim . fs . joinpath ( t . paths . test_source_path , '.git' )) 1223local index_path =vim . fs . joinpath ( repo_get_path ( 'defbranch' ), '.git' , 'index' ) 1224local index =fn . readblob ( index_path ) 1225fn . setenv ( 'GIT_INDEX_FILE' , index_path ) 1226local ref_environ =fn . environ () 1227 1228vim_pack_add ( { repos_src . basic } ) 1229eq ( 'basic main' , exec_lua ( 'return require("basic")' )) 1230 1231eq ( ref_environ , fn . environ ()) 1232eq ( index , fn . readblob ( index_path )) 1233end ) 1234 1235it ( 'validates input' , function () 1236local function assert ( err_pat , input ) 1237local function add_input () 1238vim . pack . add ( input ) 1239end 1240matches ( err_pat , pcall_err ( exec_lua , add_input )) 1241end 1242 1243-- Separate spec entries 1244assert ( 'list' , repos_src . basic ) 1245assert ( 'spec:.*table' , { 1 } ) 1246assert ( 'spec%.src:.*string' , { { src =1 } } ) 1247assert ( 'spec%.src:.*non%-empty string' , { { src ='' } } ) 1248assert ( 'spec%.name:.*string' , { { src =repos_src . basic , name =1 } } ) 1249assert ( 'spec%.name:.*non%-empty string' , { { src =repos_src . basic , name ='' } } ) 1250assert ( 1251'spec%.version:.*string or vim%.VersionRange' , 1252{ { src =repos_src . basic , version =1 } } 1253) 1254 1255-- Conflicts in input array 1256local version_conflict ={ 1257{ src =repos_src . basic , version ='feat-branch' } , 1258{ src =repos_src . basic , version ='main' } , 1259} 1260assert ( 'Conflicting `version` for `basic`.*feat%-branch.*main' , version_conflict ) 1261 1262local src_conflict ={ 1263{ src =repos_src . basic , name ='my-plugin' } , 1264{ src =repos_src . semver , name ='my-plugin' } , 1265} 1266assert ( 'Conflicting `src` for `my%-plugin`.*basic.*semver' , src_conflict ) 1267end ) 1268end ) 1269 1270describe ( 'update()' , function () 1271-- Tables with hashes used to test confirmation buffer and log content 1272local hashes --- @type table<string,string> 1273local short_hashes --- @type table<string,string> 1274 1275before_each ( function () 1276-- Create a dedicated clean repo for which "push changes" will be mocked 1277init_test_repo ( 'fetch' ) 1278 1279repo_write_file ( 'fetch' , 'lua/fetch.lua' , 'return "fetch init"' ) 1280git_add_commit ( 'Initial commit for "fetch"' , 'fetch' ) 1281 1282repo_write_file ( 'fetch' , 'lua/fetch.lua' , 'return "fetch main"' ) 1283git_add_commit ( 'Commit from `main` to be removed' , 'fetch' ) 1284 1285hashes ={ fetch_head =git_get_hash ( 'HEAD' , 'fetch' ) } 1286short_hashes ={ fetch_head =git_get_short_hash ( 'HEAD' , 'fetch' ) } 1287 1288-- Install initial versions of tested plugins 1289vim_pack_add ( { 1290{ src =repos_src . fetch , version ='main' } , 1291{ src =repos_src . semver , version ='v0.3.0' } , 1292repos_src . defbranch , 1293} ) 1294n . clear () 1295 1296-- Pin git's short-hash length so the confirmation buffer's `%h` columns are deterministic. 1297exec_lua ( [[ 1298vim.env.GIT_CONFIG_COUNT = '1' 1299vim.env.GIT_CONFIG_KEY_0 = 'core.abbrev' 1300vim.env.GIT_CONFIG_VALUE_0 = '7' 1301]] ) 1302 1303-- Mock remote repo update 1304-- - Force push 1305repo_write_file ( 'fetch' , 'lua/fetch.lua' , 'return "fetch new"' ) 1306git_cmd ( { 'add' , '*' } , 'fetch' ) 1307git_cmd ( { 'commit' , '--amend' , '-m' , 'Commit to be added 1' } , 'fetch' ) 1308 1309-- - Presence of a tag (should be shown in changelog) 1310git_cmd ( { 'tag' , 'dev-tag' } , 'fetch' ) 1311 1312repo_write_file ( 'fetch' , 'lua/fetch.lua' , 'return "fetch new 2"' ) 1313git_add_commit ( 'Commit to be added 2' , 'fetch' ) 1314 1315-- Make `dev` default remote branch to check that `version` is respected 1316git_cmd ( { 'checkout' , '-b' , 'dev' } , 'fetch' ) 1317repo_write_file ( 'fetch' , 'lua/fetch.lua' , 'return "fetch dev"' ) 1318git_add_commit ( 'Commit from default `dev` branch' , 'fetch' ) 1319end ) 1320 1321after_each ( function () 1322n . rmdir ( repo_get_path ( 'fetch' )) 1323local log_path =vim . fs . joinpath ( fn . stdpath ( 'log' ), 'nvim-pack.log' ) 1324pcall ( vim . fs . rm , log_path , { force =true } ) 1325end ) 1326 1327describe ( 'confirmation buffer' , function () 1328it ( 'works' , function () 1329vim_pack_add ( { 1330repos_src . fetch , 1331{ src =repos_src . semver , version ='v0.3.0' } , 1332{ src =repos_src . defbranch , version ='does-not-exist' } , 1333} ) 1334pack_assert_content ( 'fetch' , 'return "fetch main"' ) 1335 1336exec_lua ( function () 1337-- Enable highlighting of special filetype 1338vim . cmd ( 'filetype plugin on' ) 1339vim . pack . update () 1340end ) 1341 1342-- Buffer should be special and shown in a separate tabpage 1343eq ( 2 , # api . nvim_list_tabpages ()) 1344eq ( 2 , fn . tabpagenr ()) 1345eq ( 'nvim-pack' , api . nvim_get_option_value ( 'filetype' , {} )) 1346eq ( false , api . nvim_get_option_value ( 'modifiable' , {} )) 1347eq ( 'acwrite' , api . nvim_get_option_value ( 'buftype' , {} )) 1348local confirm_bufnr =api . nvim_get_current_buf () 1349local confirm_winnr =api . nvim_get_current_win () 1350local confirm_tabpage =api . nvim_get_current_tabpage () 1351eq ( 'nvim-pack://confirm#' .. confirm_bufnr , api . nvim_buf_get_name ( 0 )) 1352 1353-- Adjust lines for a more robust screenshot testing 1354local fetch_src =repos_src . fetch 1355local fetch_path =pack_get_plug_path ( 'fetch' ) 1356local semver_src =repos_src . semver 1357local semver_path =pack_get_plug_path ( 'semver' ) 1358local pack_runtime ='/lua/vim/pack.lua' 1359 1360exec_lua ( function () 1361-- Replace matches in line to preserve extmark highlighting 1362local function replace_in_line ( i , pattern , repl ) 1363local line =vim . api . nvim_buf_get_lines ( 0 , i - 1 , i , false )[ 1 ] 1364local from , to =line : find ( pattern ) 1365while from and to do 1366vim . api . nvim_buf_set_text ( 0 , i - 1 , from - 1 , i - 1 , to , { repl } ) 1367line =vim . api . nvim_buf_get_lines ( 0 , i - 1 , i , false )[ 1 ] 1368from , to =line : find ( pattern ) 1369end 1370end 1371 1372vim . bo . modifiable =true 1373local lines =vim . api . nvim_buf_get_lines ( 0 , 0 , - 1 , false ) 1374-- NOTE: replace path to `vim.pack` in error traceback accounting for 1375-- pcall source truncation and possibly different slashes on Windows 1376local pack_runtime_pesc =vim . pesc ( pack_runtime ): gsub ( '/' , '[\\/]' ) 1377local pack_runtime_pattern =( '%%S.+%s:%%d+' ): format ( pack_runtime_pesc ) 1378for i =1 , # lines do 1379replace_in_line ( i , pack_runtime_pattern , 'VIM_PACK_RUNTIME' ) 1380replace_in_line ( i , vim . pesc ( fetch_path ), 'FETCH_PATH' ) 1381replace_in_line ( i , vim . pesc ( fetch_src ), 'FETCH_SRC' ) 1382replace_in_line ( i , vim . pesc ( semver_path ), 'SEMVER_PATH' ) 1383replace_in_line ( i , vim . pesc ( semver_src ), 'SEMVER_SRC' ) 1384end 1385vim . bo . modified =false 1386vim . bo . modifiable =false 1387end ) 1388 1389-- Use screenshot to test highlighting, otherwise prefer text matching. 1390-- This requires computing target hashes on each test run because they 1391-- change due to source repos being cleanly created on each file test. 1392local screen 1393screen =Screen . new ( 85 , 34 ) 1394 1395hashes . fetch_new =git_get_hash ( 'main' , 'fetch' ) 1396short_hashes . fetch_new =git_get_short_hash ( 'main' , 'fetch' ) 1397short_hashes . fetch_new_prev =git_get_short_hash ( 'main~' , 'fetch' ) 1398hashes . semver_head =git_get_hash ( 'v0.3.0' , 'semver' ) 1399 1400local tab_name ='n' .. ( t . is_os ( 'win' ) and ':' or '' ) .. '//confirm#2' 1401 1402local ref_screen_lines =( [[ 1403{24: [No Name] }{5: %s }{2:%s }{24:X}| 1404{19:^# Error ────────────────────────────────────────────────────────────────────────} | 1405| 1406{19:## defbranch} | 1407| 1408VIM_PACK_RUNTIME: `does-not-exist` is not a branch/tag/commit. Available: | 1409Tags: | 1410Branches: dev, main | 1411| 1412{101:# Update ───────────────────────────────────────────────────────────────────────} | 1413| 1414{101:## fetch} | 1415Path: {103:FETCH_PATH} | 1416Source: {103:FETCH_SRC} | 1417Revision before: {103:%s} | 1418Revision after: {103:%s} {102:(main)} | 1419| 1420Pending updates: | 1421{19:< %s │ Commit from `main` to be removed} | 1422{104:> %s │ Commit to be added 2} | 1423{104:> %s │ Commit to be added 1 (tag: dev-tag)} | 1424| 1425{102:# Same ─────────────────────────────────────────────────────────────────────────} | 1426| 1427{102:## semver} | 1428Path: {103:SEMVER_PATH} | 1429Source: {103:SEMVER_SRC} | 1430Revision: {103:%s} {102:(v0.3.0)} | 1431| 1432Available newer versions: | 1433• {102:v1.0.0} | 1434• {102:0.3.1} | 1435{1:~ }| 1436| 1437]] ): format ( 1438tab_name , 1439t . is_os ( 'win' ) and '' or ' ' , 1440hashes . fetch_head , 1441hashes . fetch_new , 1442short_hashes . fetch_head , 1443short_hashes . fetch_new , 1444short_hashes . fetch_new_prev , 1445hashes . semver_head 1446) 1447 1448screen : add_extra_attr_ids ( { 1449[ 101 ] ={ foreground =Screen . colors . Orange } , 1450[ 102 ] ={ foreground =Screen . colors . LightGray } , 1451[ 103 ] ={ foreground =Screen . colors . LightBlue } , 1452[ 104 ] ={ foreground =Screen . colors . SeaGreen } , 1453} ) 1454-- NOTE: Non LuaJIT reports errors differently due to 'coxpcall' 1455if is_jit () then 1456local ref_screen =vim . text . indent ( 0 , ref_screen_lines ) 1457screen : expect ( ref_screen ) 1458end 1459 1460-- `:write` should confirm 1461n . exec ( 'write' ) 1462 1463-- - Apply changes immediately 1464pack_assert_content ( 'fetch' , 'return "fetch new 2"' ) 1465 1466-- - Clean up buffer+window+tabpage 1467eq ( false , api . nvim_buf_is_valid ( confirm_bufnr )) 1468eq ( false , api . nvim_win_is_valid ( confirm_winnr )) 1469eq ( false , api . nvim_tabpage_is_valid ( confirm_tabpage )) 1470 1471-- - Write to log file 1472local log_path =vim . fs . joinpath ( fn . stdpath ( 'log' ), 'nvim-pack.log' ) 1473local log_text =fn . readblob ( log_path ) 1474local log_1 , log_rest =log_text : match ( '^(.-)\n(.*)$' ) --- @type string, string 1475matches ( '========== Update %d%d%d%d%-%d%d%-%d%d %d%d:%d%d:%d%d ==========' , log_1 ) 1476local ref_log_lines =( [[ 1477# Update ─────────────────────────────────────────────────────────────────────── 1478 1479## fetch 1480Path: %s 1481Source: %s 1482Revision before: %s 1483Revision after: %s (main) 1484 1485Pending updates: 1486< %s │ Commit from `main` to be removed 1487> %s │ Commit to be added 2 1488> %s │ Commit to be added 1 (tag: dev-tag)]] ): format ( 1489fetch_path , 1490fetch_src , 1491hashes . fetch_head , 1492hashes . fetch_new , 1493short_hashes . fetch_head , 1494short_hashes . fetch_new , 1495short_hashes . fetch_new_prev 1496) 1497eq ( vim . text . indent ( 0 , ref_log_lines ), vim . trim ( log_rest )) 1498end ) 1499 1500it ( 'can be dismissed with `:quit`' , function () 1501vim_pack_add ( { repos_src . fetch } ) 1502exec_lua ( 'vim.pack.update({ "fetch" })' ) 1503eq ( 'nvim-pack' , api . nvim_get_option_value ( 'filetype' , {} )) 1504 1505-- Should not apply updates 1506n . exec ( 'quit' ) 1507pack_assert_content ( 'fetch' , 'return "fetch main"' ) 1508end ) 1509 1510it ( 'closes full tabpage' , function () 1511vim_pack_add ( { repos_src . fetch } ) 1512exec_lua ( 'vim.pack.update()' ) 1513 1514-- Confirm with `:write` 1515local confirm_tabpage =api . nvim_get_current_tabpage () 1516n . exec ( '-tab split other-tab' ) 1517local other_tabpage =api . nvim_get_current_tabpage () 1518n . exec ( 'tabnext' ) 1519n . exec ( 'write' ) 1520eq ( true , api . nvim_get_current_tabpage () == other_tabpage ) 1521eq ( false , api . nvim_tabpage_is_valid ( confirm_tabpage )) 1522 1523-- Not confirm with `:quit` 1524n . exec ( 'tab split other-tab-2' ) 1525local other_tabpage_2 =api . nvim_get_current_tabpage () 1526exec_lua ( 'vim.pack.update()' ) 1527confirm_tabpage =api . nvim_get_current_tabpage () 1528 1529-- - Temporary split window in tabpage should prevent from closing 1530n . exec ( 'vsplit other-buf' ) 1531n . exec ( 'wincmd w' ) 1532 1533n . exec ( 'tabclose ' .. api . nvim_tabpage_get_number ( other_tabpage_2 )) 1534eq ( confirm_tabpage , api . nvim_get_current_tabpage ()) 1535n . exec ( 'quit' ) 1536eq ( confirm_tabpage , api . nvim_get_current_tabpage ()) 1537n . exec ( 'quit' ) 1538eq ( false , api . nvim_tabpage_is_valid ( confirm_tabpage )) 1539 1540-- Should work even if it is the last tabpage 1541exec_lua ( 'vim.pack.update()' ) 1542n . exec ( 'tabonly' ) 1543n . exec ( 'write' ) 1544eq ( '' , n . eval ( 'v:errmsg' )) 1545 1546-- Should cleanly close tabpage even if there are only scratch buffers 1547n . exec ( '%bwipeout' ) 1548local init_buf =api . nvim_get_current_buf () 1549api . nvim_set_current_buf ( api . nvim_create_buf ( false , true )) 1550api . nvim_buf_delete ( init_buf , { force =true } ) 1551exec_lua ( 'vim.pack.update()' ) 1552n . exec ( 'write' ) 1553eq ( 1 , # api . nvim_list_tabpages ()) 1554eq ( 1 , # api . nvim_list_bufs ()) 1555end ) 1556 1557it ( 'has in-process LSP features' , function () 1558t . skip ( not is_jit (), "Non LuaJIT reports errors differently due to 'coxpcall'" ) 1559track_nvim_echo () 1560vim_pack_add ( { 1561repos_src . fetch , 1562-- No `semver` to test with non-active plugins 1563{ src =repos_src . defbranch , version ='does-not-exist' } , 1564} ) 1565exec_lua ( 'vim.pack.update()' ) 1566 1567eq ( 1 , exec_lua ( 'return #vim.lsp.get_clients({ bufnr = 0 })' )) 1568 1569-- textDocument/documentLink 1570--- @param ref ([number, number, number, number, string])[] 1571local assert_links =function ( ref ) 1572--- @type table[] 1573local out_links =exec_lua ( function () 1574local params ={ textDocument =vim . lsp . util . make_text_document_params ( 0 ) } 1575local response =vim . lsp . buf_request_sync ( 0 , 'textDocument/documentLink' , params ) 1576return response [ 1 ]. result 1577end ) 1578 1579--- @type table[] 1580local ref_links ={} 1581for i , r in ipairs ( ref ) do 1582local start ={ line =r [ 1 ], character =r [ 2 ] } 1583local end_ ={ line =r [ 3 ], character =r [ 4 ] } 1584ref_links [ i ] ={ range ={ start =start , [ 'end' ] =end_ } , target =r [ 5 ] } 1585end 1586 1587eq ( ref_links , out_links ) 1588end 1589 1590local fetch_src =repos_src . fetch 1591local fetch_path =pack_get_plug_path ( 'fetch' ) 1592local fetch_path_uri =vim . uri_from_fname ( fetch_path ) 1593local semver_src =repos_src . semver 1594local semver_path =pack_get_plug_path ( 'semver' ) 1595local semver_path_uri =vim . uri_from_fname ( semver_path ) 1596 1597-- With `file://` sources it can only return "Path:" and "Source:" links 1598local ref_file_links ={ 1599{ 11 , 17 , 11 , 17 + fetch_path : len () - 1 , fetch_path_uri } , -- Path: ... 1600{ 12 , 17 , 12 , 17 + fetch_src : len () - 1 , fetch_src } , -- Source: ... 1601{ 24 , 10 , 24 , 10 + semver_path : len () - 1 , semver_path_uri } , -- Path: ... 1602{ 25 , 10 , 25 , 10 + semver_src : len () - 1 , semver_src } , -- Source: ... 1603} 1604assert_links ( ref_file_links ) 1605 1606-- Mock using common sources which should provide all links 1607local fetch_github ='https://github.com/user/fetch' 1608local semver_codeberg ='https://codeberg.org/user/semver' 1609local lines =api . nvim_buf_get_lines ( 0 , 0 , - 1 , false ) 1610lines [ 13 ] =lines [ 13 ]: gsub ( '^(Source: +)(.+)$' , '%1' .. fetch_github ) 1611lines [ 26 ] =lines [ 26 ]: gsub ( '^(Source: +)(.+)$' , '%1' .. semver_codeberg ) 1612api . nvim_set_option_value ( 'modifiable' , true , { buf =0 } ) 1613api . nvim_buf_set_lines ( 0 , 0 , - 1 , false , lines ) 1614api . nvim_set_option_value ( 'modifiable' , false , { buf =0 } ) 1615 1616local ref_github_links ={ 1617{ 11 , 17 , 11 , 17 + fetch_path : len () - 1 , fetch_path_uri } , -- Path: ... 1618{ 12 , 17 , 12 , 45 , fetch_github } , -- Source: ... 1619{ 13 , 17 , 13 , 56 , fetch_github .. '/commit/' .. lines [ 14 ]: match ( '%S+$' ) } , -- Revision before: ... 1620{ 14 , 17 , 14 , 56 , fetch_github .. '/commit/' .. lines [ 15 ]: match ( '(%S+) %b()$' ) } , -- Revision before: ... 1621{ 17 , 2 , 17 , 8 , fetch_github .. '/commit/' .. lines [ 18 ]: match ( '^< (%S+)' ) } , 1622{ 18 , 2 , 18 , 8 , fetch_github .. '/commit/' .. lines [ 19 ]: match ( '^> (%S+)' ) } , 1623{ 19 , 2 , 19 , 8 , fetch_github .. '/commit/' .. lines [ 20 ]: match ( '^> (%S+)' ) } , 1624{ 24 , 10 , 24 , 10 + semver_path : len () - 1 , semver_path_uri } , -- Path: ... 1625{ 25 , 10 , 25 , 41 , semver_codeberg } , -- Source: ... 1626{ 26 , 10 , 26 , 49 , semver_codeberg .. '/commit/' .. lines [ 27 ]: match ( '(%S+) %b()$' ) } , -- Revision: ... 1627-- Should use UTF index 1628{ 29 , 2 , 29 , 7 , semver_codeberg .. '/src/tag/v1.0.0' } , 1629{ 30 , 2 , 30 , 6 , semver_codeberg .. '/src/tag/0.3.1' } , 1630} 1631assert_links ( ref_github_links ) 1632 1633n . exec ( 'quit' ) 1634 1635-- textDocument/documentSymbol 1636exec_lua ( 'vim.pack.update()' ) 1637exec_lua ( 'vim.lsp.buf.document_symbol()' ) 1638local loclist =vim . tbl_map ( function ( x ) --- @param x table 1639return { 1640lnum =x . lnum , --- @type integer 1641col =x . col , --- @type integer 1642end_lnum =x . end_lnum , --- @type integer 1643end_col =x . end_col , --- @type integer 1644text =x . text , --- @type string 1645} 1646end , fn . getloclist ( 0 )) 1647local ref_loclist ={ 1648{ lnum =1 , col =1 , end_lnum =9 , end_col =1 , text ='[Namespace] Error' } , 1649{ lnum =3 , col =1 , end_lnum =9 , end_col =1 , text ='[Module] defbranch' } , 1650{ lnum =9 , col =1 , end_lnum =22 , end_col =1 , text ='[Namespace] Update' } , 1651{ lnum =11 , col =1 , end_lnum =22 , end_col =1 , text ='[Module] fetch' } , 1652{ lnum =22 , col =1 , end_lnum =31 , end_col =1 , text ='[Namespace] Same' } , 1653{ lnum =24 , col =1 , end_lnum =31 , end_col =1 , text ='[Module] semver (not active)' } , 1654} 1655eq ( ref_loclist , loclist ) 1656 1657n . exec ( 'lclose' ) 1658 1659-- textDocument/hover 1660local confirm_winnr =api . nvim_get_current_win () 1661local function assert_hover ( pos , commit_msg ) 1662-- Should not be affected by special environment variables 1663fn . setenv ( 'GIT_WORK_TREE' , t . paths . test_source_path ) 1664fn . setenv ( 'GIT_DIR' , vim . fs . joinpath ( t . paths . test_source_path , '.git' )) 1665fn . setenv ( 'GIT_INDEX_FILE' , vim . fs . joinpath ( repo_get_path ( 'defbranch' ), '.git' , 'index' )) 1666api . nvim_win_set_cursor ( 0 , pos ) 1667exec_lua ( function () 1668vim . lsp . buf . hover () 1669-- Default hover is async shown in floating window 1670vim . wait ( 1000 , function () 1671return # vim . api . nvim_tabpage_list_wins ( 0 ) > 1 1672end ) 1673end ) 1674 1675local all_wins =api . nvim_tabpage_list_wins ( 0 ) 1676eq ( 2 , # all_wins ) 1677local float_winnr =all_wins [ 1 ] == confirm_winnr and all_wins [ 2 ] or all_wins [ 1 ] 1678eq ( true , api . nvim_win_get_config ( float_winnr ). relative ~= '' ) 1679 1680local float_buf =api . nvim_win_get_buf ( float_winnr ) 1681local text =table . concat ( api . nvim_buf_get_lines ( float_buf , 0 , - 1 , false ), '\n' ) 1682 1683local ref_pattern ='Marvim <marvim@neovim%.io>\nDate:.*' .. vim . pesc ( commit_msg ) 1684matches ( ref_pattern , text ) 1685 1686exec_lua ( 'vim.uv.os_unsetenv("GIT_WORK_TREE")' ) 1687exec_lua ( 'vim.uv.os_unsetenv("GIT_DIR")' ) 1688exec_lua ( 'vim.uv.os_unsetenv("GIT_INDEX_FILE")' ) 1689end 1690 1691assert_hover ( { 14 , 0 } , 'Commit from `main` to be removed' ) 1692assert_hover ( { 15 , 0 } , 'Commit to be added 2' ) 1693assert_hover ( { 18 , 0 } , 'Commit from `main` to be removed' ) 1694assert_hover ( { 19 , 0 } , 'Commit to be added 2' ) 1695assert_hover ( { 20 , 0 } , 'Commit to be added 1' ) 1696assert_hover ( { 27 , 0 } , 'Add version v0.3.0' ) 1697assert_hover ( { 30 , 0 } , 'Add version v1.0.0' ) 1698assert_hover ( { 31 , 0 } , 'Add version 0.3.1' ) 1699 1700-- textDocument/codeAction 1701n . exec_lua ( function () 1702-- Mock `vim.ui.select()` which is a default code action selection 1703_G . select_idx =0 1704 1705---@diagnostic disable-next-line: duplicate-set-field 1706vim . ui . select =function ( items , _ , on_choice ) 1707_G . select_items =items 1708local idx =_G . select_idx 1709if idx > 0 then 1710on_choice ( items [ idx ], idx ) 1711-- Minor delay before continue because LSP cmd execution is async 1712vim . wait ( 10 ) 1713end 1714end 1715end ) 1716 1717local ref_lockfile =get_lock_tbl () --- @type vim.pack.Lock 1718 1719local function assert_action ( pos , action_titles , select_idx , should_preserve_lines ) 1720api . nvim_win_set_cursor ( 0 , pos ) 1721 1722local buf_lines =api . nvim_buf_get_lines ( 0 , 0 , - 1 , false ) 1723n . exec_lua ( function () 1724_G . select_items =nil 1725_G . select_idx =select_idx 1726vim . lsp . buf . code_action () 1727end ) 1728local titles =vim . tbl_map ( function ( x ) --- @param x table 1729return x . action . title 1730end , n . exec_lua ( 'return _G.select_items or {}' )) 1731eq ( titles , action_titles ) 1732 1733-- If no action is asked (like via cancel), should not delete lines 1734if select_idx <= 0 or should_preserve_lines then 1735eq ( buf_lines , api . nvim_buf_get_lines ( 0 , 0 , - 1 , false )) 1736end 1737end 1738 1739-- - Should not include "namespace" header as "plugin at cursor" 1740assert_action ( { 1 , 1 } , {} , 0 ) 1741assert_action ( { 2 , 0 } , {} , 0 ) 1742-- - "Delete" action should be present for active plugins 1743local defbranch_actions ={ 'Delete `defbranch`' } 1744assert_action ( { 3 , 1 } , defbranch_actions , 0 ) 1745assert_action ( { 7 , 0 } , defbranch_actions , 0 ) 1746-- - Should not include separator blank line as "plugin at cursor" 1747assert_action ( { 8 , 0 } , {} , 0 ) 1748assert_action ( { 9 , 0 } , {} , 0 ) 1749assert_action ( { 10 , 0 } , {} , 0 ) 1750-- - Should suggest updating related actions if updates available 1751local fetch_actions ={ 'Update `fetch`' , 'Skip updating `fetch`' , 'Delete `fetch`' } 1752assert_action ( { 11 , 0 } , fetch_actions , 0 ) 1753assert_action ( { 14 , 0 } , fetch_actions , 0 ) 1754assert_action ( { 20 , 0 } , fetch_actions , 0 ) 1755assert_action ( { 21 , 0 } , {} , 0 ) 1756assert_action ( { 22 , 0 } , {} , 0 ) 1757assert_action ( { 23 , 0 } , {} , 0 ) 1758-- - "Delete" action should be present for not active plugin 1759local semver_actions ={ 'Delete `semver`' } 1760assert_action ( { 24 , 0 } , semver_actions , 0 ) 1761assert_action ( { 28 , 0 } , semver_actions , 0 ) 1762assert_action ( { 31 , 0 } , semver_actions , 0 ) 1763 1764-- - Should correctly perform action and remove plugin's lines 1765local function line_match ( lnum , pattern ) 1766matches ( pattern , api . nvim_buf_get_lines ( 0 , lnum - 1 , lnum , false )[ 1 ]) 1767end 1768 1769-- - Delete not active plugin. Should remove from disk and update lockfile. 1770assert_action ( { 24 , 0 } , semver_actions , 1 ) 1771eq ( false , pack_exists ( 'semver' )) 1772line_match ( 22 , '^# Same' ) 1773eq ( 22 , api . nvim_buf_line_count ( 0 )) 1774 1775ref_lockfile . plugins . semver =nil 1776eq ( ref_lockfile , get_lock_tbl ()) 1777 1778-- - Skip udating 1779assert_action ( { 11 , 0 } , fetch_actions , 2 ) 1780pack_assert_content ( 'fetch' , 'return "fetch main"' ) 1781line_match ( 9 , '^# Update' ) 1782line_match ( 10 , '^$' ) 1783line_match ( 11 , '^# Same' ) 1784 1785-- - Delete active plugin. Should ask for confirmation before force deleting 1786-- with possibility to not confirm. 1787mock_confirm ( 2 ) -- No 1788assert_action ( { 3 , 0 } , defbranch_actions , 1 , true ) 1789eq ( true , pack_exists ( 'defbranch' )) 1790eq ( ref_lockfile , get_lock_tbl ()) 1791local confirm_msg ='Plugin `defbranch` is active.' 1792.. ' Make sure its `vim.pack.add` call is removed from config.' 1793local ref_confirm_log ={ { confirm_msg , 'Delete? &Yes\n&No' , 1 , 'Question' } } 1794eq ( ref_confirm_log , exec_lua ( 'return _G.confirm_log' )) 1795 1796mock_confirm ( 1 ) -- Yes 1797assert_action ( { 3 , 0 } , defbranch_actions , 1 ) 1798eq ( false , pack_exists ( 'defbranch' )) 1799ref_lockfile . plugins . defbranch =nil 1800eq ( ref_lockfile , get_lock_tbl ()) 1801eq ( 5 , api . nvim_buf_line_count ( 0 )) 1802line_match ( 1 , '^# Error' ) 1803line_match ( 3 , '^# Update' ) 1804line_match ( 5 , '^# Same' ) 1805 1806-- - Update plugin. Should not re-fetch new data and update lockfile. 1807n . exec ( 'quit' ) 1808n . exec_lua ( function () 1809vim . pack . update ( { 'fetch' } ) 1810end ) 1811exec_lua ( '_G.echo_log = {}' ) 1812 1813ref_lockfile . plugins . fetch . rev =git_get_hash ( 'main' , 'fetch' ) 1814git_cmd ( { 'checkout' , 'main' } , 'fetch' ) 1815repo_write_file ( 'fetch' , 'lua/fetch.lua' , 'return "fetch new 3"' ) 1816git_add_commit ( 'Commit to be added 3' , 'fetch' ) 1817 1818assert_action ( { 3 , 0 } , fetch_actions , 1 ) 1819 1820pack_assert_content ( 'fetch' , 'return "fetch new 2"' ) 1821 1822local echo_log =exec_lua ( 'return _G.echo_log' ) 1823assert_progress_report ( vim . list_slice ( echo_log , 1 , 3 ), 'Computing updates' , { 'fetch' } ) 1824assert_progress_report ( vim . list_slice ( echo_log , 4 , 6 ), 'Applying updates' , { 'fetch' } ) 1825eq ( 6 , # echo_log ) 1826 1827line_match ( 1 , '^# Update' ) 1828eq ( 1 , api . nvim_buf_line_count ( 0 )) 1829 1830eq ( ref_lockfile , get_lock_tbl ()) 1831 1832-- - Can still respect `:write` after action 1833n . exec ( 'write' ) 1834eq ( 'vim.pack: Nothing to update' , n . exec_capture ( '1messages' )) 1835eq ( '' , api . nvim_get_option_value ( 'filetype' , {} )) 1836end ) 1837 1838it ( 'has buffer-local mappings' , function () 1839t . skip ( not is_jit (), "Non LuaJIT reports update errors differently due to 'coxpcall'" ) 1840vim_pack_add ( { 1841repos_src . fetch , 1842{ src =repos_src . semver , version ='v0.3.0' } , 1843{ src =repos_src . defbranch , version ='does-not-exist' } , 1844} ) 1845-- Enable sourcing filetype script (that creates mappings) 1846n . exec ( 'filetype plugin on' ) 1847exec_lua ( 'vim.pack.update()' ) 1848 1849-- Plugin sections navigation 1850local function assert ( keys , ref_cursor ) 1851n . feed ( keys ) 1852eq ( ref_cursor , api . nvim_win_get_cursor ( 0 )) 1853end 1854 1855api . nvim_win_set_cursor ( 0 , { 1 , 1 } ) 1856assert ( ']]' , { 3 , 0 } ) 1857assert ( ']]' , { 11 , 0 } ) 1858assert ( ']]' , { 24 , 0 } ) 1859-- - Should not wrap around the edge 1860assert ( ']]' , { 24 , 0 } ) 1861 1862api . nvim_win_set_cursor ( 0 , { 31 , 1 } ) 1863assert ( '[[' , { 24 , 0 } ) 1864assert ( '[[' , { 11 , 0 } ) 1865assert ( '[[' , { 3 , 0 } ) 1866-- - Should not wrap around the edge 1867assert ( '[[' , { 3 , 0 } ) 1868end ) 1869 1870it ( 'suggests newer versions when on non-tagged commit' , function () 1871local commit =git_get_hash ( '0.3.1~' , 'semver' ) 1872 1873-- Make fresh install for cleaner test 1874exec_lua ( 'vim.pack.del({ "semver" })' ) 1875vim_pack_add ( { { src =repos_src . semver , version =commit } } ) 1876exec_lua ( 'vim.pack.update({ "semver" })' ) 1877 1878-- Should correctly infer that 0.3.0 is the latest version and suggest 1879-- versions greater than that 1880local confirm_text =table . concat ( api . nvim_buf_get_lines ( 0 , 0 , - 1 , false ), '\n' ) 1881matches ( 'Available newer versions:\n• v1%.0%.0\n• 0%.3%.1$' , confirm_text ) 1882end ) 1883 1884it ( 'updates lockfile' , function () 1885vim_pack_add ( { repos_src . fetch } ) 1886local ref_fetch_lock ={ rev =hashes . fetch_head , src =repos_src . fetch } 1887eq ( ref_fetch_lock , get_lock_tbl (). plugins . fetch ) 1888 1889exec_lua ( 'vim.pack.update()' ) 1890n . exec ( 'write' ) 1891 1892ref_fetch_lock . rev =git_get_hash ( 'main' , 'fetch' ) 1893eq ( ref_fetch_lock , get_lock_tbl (). plugins . fetch ) 1894end ) 1895 1896it ( 'hints about not active plugins' , function () 1897exec_lua ( function () 1898vim . pack . update () 1899end ) 1900 1901for _ , l in ipairs ( api . nvim_buf_get_lines ( 0 , 0 , - 1 , false )) do 1902if l : match ( '^## ' ) then 1903matches ( ' %(not active%)$' , l ) 1904end 1905end 1906 1907-- Should also hint in `textDocument/documentSymbol` of in-process LSP, 1908-- yet still work for navigation 1909exec_lua ( 'vim.lsp.buf.document_symbol()' ) 1910local loclist =fn . getloclist ( 0 ) 1911matches ( ' %(not active%)$' , loclist [ 2 ]. text ) 1912matches ( ' %(not active%)$' , loclist [ 4 ]. text ) 1913matches ( ' %(not active%)$' , loclist [ 5 ]. text ) 1914 1915n . exec ( 'llast' ) 1916eq ( 21 , api . nvim_win_get_cursor ( 0 )[ 1 ]) 1917end ) 1918end ) 1919 1920it ( 'works with not active plugins' , function () 1921-- No plugins are added, but they are installed in `before_each()` 1922exec_lua ( function () 1923-- By default should also include not active plugins 1924vim . pack . update () 1925end ) 1926pack_assert_content ( 'fetch' , 'return "fetch main"' ) 1927n . exec ( 'write' ) 1928pack_assert_content ( 'fetch' , 'return "fetch new 2"' ) 1929end ) 1930 1931it ( 'works with submodules' , function () 1932mock_git_file_transport () 1933vim_pack_add ( { { src =repos_src . with_subs , version ='init-commit' } } ) 1934 1935local sub_lua_file =vim . fs . joinpath ( pack_get_plug_path ( 'with_subs' ), 'sub' , 'sub.lua' ) 1936eq ( 'return "sub init"' , fn . readblob ( sub_lua_file )) 1937 1938n . clear () 1939mock_git_file_transport () 1940vim_pack_add ( { repos_src . with_subs } ) 1941exec_lua ( 'vim.pack.update({ "with_subs" })' ) 1942n . exec ( 'write' ) 1943eq ( 'return "sub main"' , fn . readblob ( sub_lua_file )) 1944end ) 1945 1946it ( 'can force update' , function () 1947vim_pack_add ( { repos_src . fetch } ) 1948exec_lua ( 'vim.pack.update({ "fetch" }, { force = true })' ) 1949 1950-- Apply changes immediately 1951local fetch_src =repos_src . fetch 1952local fetch_path =pack_get_plug_path ( 'fetch' ) 1953pack_assert_content ( 'fetch' , 'return "fetch new 2"' ) 1954 1955-- No special buffer/window/tabpage 1956eq ( 1 , # api . nvim_list_tabpages ()) 1957eq ( 1 , # api . nvim_list_wins ()) 1958eq ( '' , api . nvim_get_option_value ( 'filetype' , {} )) 1959 1960-- Write to log file 1961hashes . fetch_new =git_get_hash ( 'main' , 'fetch' ) 1962short_hashes . fetch_new =git_get_short_hash ( 'main' , 'fetch' ) 1963short_hashes . fetch_new_prev =git_get_short_hash ( 'main~' , 'fetch' ) 1964 1965local log_path =vim . fs . joinpath ( fn . stdpath ( 'log' ), 'nvim-pack.log' ) 1966local log_text =fn . readblob ( log_path ) 1967local log_1 , log_rest =log_text : match ( '^(.-)\n(.*)$' ) --- @type string, string 1968matches ( '========== Update %d%d%d%d%-%d%d%-%d%d %d%d:%d%d:%d%d ==========' , log_1 ) 1969local ref_log_lines =( [[ 1970# Update ─────────────────────────────────────────────────────────────────────── 1971 1972## fetch 1973Path: %s 1974Source: %s 1975Revision before: %s 1976Revision after: %s (main) 1977 1978Pending updates: 1979< %s │ Commit from `main` to be removed 1980> %s │ Commit to be added 2 1981> %s │ Commit to be added 1 (tag: dev-tag)]] ): format ( 1982fetch_path , 1983fetch_src , 1984hashes . fetch_head , 1985hashes . fetch_new , 1986short_hashes . fetch_head , 1987short_hashes . fetch_new , 1988short_hashes . fetch_new_prev 1989) 1990eq ( vim . text . indent ( 0 , ref_log_lines ), vim . trim ( log_rest )) 1991 1992-- Should update lockfile 1993eq ( hashes . fetch_new , get_lock_tbl (). plugins . fetch . rev ) 1994end ) 1995 1996it ( 'can use lockfile revision as a target' , function () 1997vim_pack_add ( { repos_src . fetch } ) 1998pack_assert_content ( 'fetch' , 'return "fetch main"' ) 1999 2000-- Mock "update -> revert lockfile -> revert plugin" 2001local lock_path =get_lock_path () 2002local lockfile_before =fn . readblob ( lock_path ) 2003hashes . fetch_new =git_get_hash ( 'main' , 'fetch' ) 2004 2005-- - Update 2006exec_lua ( 'vim.pack.update({ "fetch" }, { force = true })' ) 2007pack_assert_content ( 'fetch' , 'return "fetch new 2"' ) 2008 2009-- - Revert lockfile 2010fn . writefile ( vim . split ( lockfile_before , '\n' ), lock_path ) 2011n . clear () 2012 2013-- - Revert plugin 2014pack_assert_content ( 'fetch' , 'return "fetch new 2"' ) 2015exec_lua ( 'vim.pack.update({ "fetch" }, { target = "lockfile" })' ) 2016local confirm_lines =api . nvim_buf_get_lines ( 0 , 0 , - 1 , false ) 2017n . exec ( 'write' ) 2018pack_assert_content ( 'fetch' , 'return "fetch main"' ) 2019eq ( hashes . fetch_head , get_lock_tbl (). plugins . fetch . rev ) 2020 2021-- - Should mention that new revision comes from *lockfile* 2022eq ( confirm_lines [ 6 ], ( 'Revision before: %s' ): format ( hashes . fetch_new )) 2023eq ( confirm_lines [ 7 ], ( 'Revision after: %s (*lockfile*)' ): format ( hashes . fetch_head )) 2024end ) 2025 2026it ( 'can change `src` of installed plugin' , function () 2027local basic_src =repos_src . basic 2028local defbranch_src =repos_src . defbranch 2029vim_pack_add ( { basic_src } ) 2030 2031local function assert_origin ( ref ) 2032-- Should be in sync both on disk and in lockfile 2033local opts ={ cwd =pack_get_plug_path ( 'basic' ) } 2034local real_origin =n . system_sync ( { 'git' , 'remote' , 'get-url' , 'origin' } , opts ) 2035eq ( ref , vim . trim ( real_origin . stdout )) 2036 2037eq ( ref , get_lock_tbl (). plugins . basic . src ) 2038end 2039 2040n . clear () 2041watch_events ( { 'PackChangedPre' , 'PackChanged' } ) 2042 2043assert_origin ( basic_src ) 2044vim_pack_add ( { { src =defbranch_src , name ='basic' } } ) 2045-- Should not yet (after `add()`) affect plugin source 2046assert_origin ( basic_src ) 2047 2048-- Should update source immediately (to work if updates are discarded) 2049exec_lua ( function () 2050vim . pack . update ( { 'basic' } ) 2051end ) 2052assert_origin ( defbranch_src ) 2053 2054-- Should not revert source change even if update is discarded 2055n . exec ( 'quit' ) 2056assert_origin ( defbranch_src ) 2057eq ( {} , exec_lua ( 'return _G.event_log' )) 2058 2059-- Should work with forced update 2060n . clear () 2061vim_pack_add ( { basic_src } ) 2062exec_lua ( 'vim.pack.update({ "basic" }, { force = true })' ) 2063assert_origin ( basic_src ) 2064end ) 2065 2066it ( 'can do offline update' , function () 2067vim_pack_add ( { { src =repos_src . defbranch , version ='main' } } ) 2068track_nvim_echo () 2069 2070pack_assert_content ( 'defbranch' , 'return "defbranch dev"' ) 2071n . exec_lua ( function () 2072vim . pack . update ( { 'defbranch' } , { offline =true } ) 2073end ) 2074 2075-- There should be no progress report about downloading updates 2076assert_progress_report ( nil , 'Computing updates' , { 'defbranch' } ) 2077 2078n . exec ( 'write' ) 2079pack_assert_content ( 'defbranch' , 'return "defbranch main"' ) 2080end ) 2081 2082it ( 'shows progress report' , function () 2083track_nvim_echo () 2084vim_pack_add ( { repos_src . fetch , repos_src . defbranch } ) 2085-- Should also include updates from not active plugins 2086exec_lua ( 'vim.pack.update()' ) 2087 2088-- During initial download 2089assert_progress_report ( nil , 'Downloading updates' , { 'fetch' , 'defbranch' , 'semver' } ) 2090exec_lua ( '_G.echo_log = {}' ) 2091 2092-- During application (only for plugins that have updates) 2093n . exec ( 'write' ) 2094assert_progress_report ( nil , 'Applying updates' , { 'fetch' } ) 2095 2096-- During force update 2097n . clear () 2098track_nvim_echo () 2099git_cmd ( { 'checkout' , 'main' } , 'fetch' ) 2100repo_write_file ( 'fetch' , 'lua/fetch.lua' , 'return "fetch new 3"' ) 2101git_add_commit ( 'Commit to be added 3' , 'fetch' ) 2102 2103vim_pack_add ( { repos_src . fetch , repos_src . defbranch } ) 2104exec_lua ( 'vim.pack.update(nil, { force = true })' ) 2105 2106local echo_log =exec_lua ( 'return _G.echo_log' ) 2107local all_plugs ={ 'fetch' , 'defbranch' , 'semver' } 2108assert_progress_report ( vim . list_slice ( echo_log , 1 , 5 ), 'Downloading updates' , all_plugs ) 2109assert_progress_report ( vim . list_slice ( echo_log , 6 , 8 ), 'Applying updates' , { 'fetch' } ) 2110eq ( 8 , # echo_log ) 2111end ) 2112 2113it ( 'triggers relevant events' , function () 2114watch_events ( { 'PackChangedPre' , 'PackChanged' } ) 2115vim_pack_add ( { repos_src . fetch , repos_src . defbranch } ) 2116exec_lua ( '_G.event_log = {}' ) 2117exec_lua ( 'vim.pack.update()' ) 2118eq ( {} , exec_lua ( 'return _G.event_log' )) 2119 2120-- Should trigger relevant events only for actually updated plugins 2121n . exec ( 'write' ) 2122assert_packchanged ( { 2123{ 'Pre' , 'update' , 'fetch' , nil , true } , 2124{ '' , 'update' , 'fetch' , nil , true } , 2125} ) 2126end ) 2127 2128it ( 'sources relevant manifest scripts' , function () 2129vim_pack_add ( { repos_src . manifest } ) 2130n . clear () 2131 2132exec_lua ( function () 2133local specs ={ { src =repos_src . manifest , version ='other-branch' } } 2134vim . pack . add ( specs , { load =function () end } ) 2135end ) 2136 2137watch_events ( { 'PackChangedPre' , 'PackChanged' } ) 2138track_nvim_echo () 2139local cwd =fn . getcwd () 2140exec_lua ( "vim.pack.update({ 'manifest' }, { force = true })" ) 2141 2142-- Should not have side effects after executing script in special context 2143eq ( cwd , fn . getcwd ()) 2144local plug_path =pack_get_plug_path ( 'manifest' ) 2145eq ( false , vim . tbl_contains ( api . nvim_list_runtime_paths (), plug_path )) 2146 2147-- Should execute script in special context after triggering events 2148assert_manifest_scripts ( plug_path , { 2149{ event ='PackChangedPre' , kind ='update' } , 2150{ event ='script' , kind ='preupdate' , cwd =plug_path , rtp_has_plug =true } , 2151{ event ='PackChanged' , kind ='update' } , 2152{ event ='script' , kind ='update' , cwd =plug_path , rtp_has_plug =true } , 2153} ) 2154-- - Should execute update script as it is *after the update* 2155eq ( true , exec_lua ( 'return _G.other_script' )) 2156 2157-- Should warn on errors during script execution 2158eq ( true , is_in_echo_log ( 'Error in preupdate script' , 'WarningMsg' )) 2159eq ( true , is_in_echo_log ( 'Error in update script' , 'WarningMsg' )) 2160end ) 2161 2162it ( 'stashes before applying changes' , function () 2163local fetch_lua_file =vim . fs . joinpath ( pack_get_plug_path ( 'fetch' ), 'lua' , 'fetch.lua' ) 2164fn . writefile ( { 'A text that will be stashed' } , fetch_lua_file ) 2165 2166vim_pack_add ( { repos_src . fetch } ) 2167exec_lua ( 'vim.pack.update()' ) 2168n . exec ( 'write' ) 2169 2170local fetch_path =pack_get_plug_path ( 'fetch' ) 2171local stash_list =n . system_sync ( { 'git' , 'stash' , 'list' } , { cwd =fetch_path } ). stdout 2172or '' 2173matches ( 'vim%.pack: %d%d%d%d%-%d%d%-%d%d %d%d:%d%d:%d%d Stash before checkout' , stash_list ) 2174 2175-- Update should still be applied 2176pack_assert_content ( 'fetch' , 'return "fetch new 2"' ) 2177end ) 2178 2179it ( 'is not affected by special environment variables' , function () 2180fn . setenv ( 'GIT_WORK_TREE' , t . paths . test_source_path ) 2181fn . setenv ( 'GIT_DIR' , vim . fs . joinpath ( t . paths . test_source_path , '.git' )) 2182local index_path =vim . fs . joinpath ( repo_get_path ( 'defbranch' ), '.git' , 'index' ) 2183local index =fn . readblob ( index_path ) 2184fn . setenv ( 'GIT_INDEX_FILE' , index_path ) 2185local ref_environ =fn . environ () 2186 2187vim_pack_add ( { repos_src . fetch } ) 2188exec_lua ( 'vim.pack.update({ "fetch" }, { force = true })' ) 2189pack_assert_content ( 'fetch' , 'return "fetch new 2"' ) 2190 2191eq ( ref_environ , fn . environ ()) 2192eq ( index , fn . readblob ( index_path )) 2193end ) 2194 2195it ( 'works with out of sync lockfile' , function () 2196-- Should first autoinstall missing plugin (with confirmation) 2197n . rmdir ( pack_get_plug_path ( 'fetch' )) 2198n . clear () 2199mock_confirm ( 1 ) 2200exec_lua ( function () 2201vim . pack . update ( nil , { force =true } ) 2202end ) 2203eq ( 1 , exec_lua ( 'return #_G.confirm_log' )) 2204-- - Should checkout `version='main'` as it says in the lockfile 2205pack_assert_content ( 'fetch' , 'return "fetch new 2"' ) 2206 2207-- Should regenerate absent lockfile (from present plugins) 2208vim . fs . rm ( get_lock_path ()) 2209n . clear () 2210exec_lua ( function () 2211vim . pack . update ( nil , { force =true } ) 2212end ) 2213local lock_plugins =get_lock_tbl (). plugins 2214eq ( 3 , vim . tbl_count ( lock_plugins )) 2215-- - Should checkout default branch since `version='main'` info is lost 2216-- after lockfile is deleted. 2217eq ( nil , lock_plugins . fetch . version ) 2218pack_assert_content ( 'fetch' , 'return "fetch dev"' ) 2219end ) 2220 2221it ( "respects 'packlockfile'" , function () 2222n . rmdir ( pack_get_dir ()) 2223pcall ( vim . fs . rm , get_lock_path (), { force =true } ) 2224 2225n . clear () 2226local other_lock_path =get_other_lock_path () 2227api . nvim_set_option_value ( 'packlockfile' , other_lock_path , { scope ='global' } ) 2228 2229exec_lua ( function () 2230vim . pack . add ( { repos_src . basic } ) 2231end ) 2232 2233eq ( true , vim . uv . fs_stat ( other_lock_path ) ~= nil ) 2234eq ( false , vim . uv . fs_stat ( get_lock_path ()) ~= nil ) 2235 2236n . clear () 2237api . nvim_set_option_value ( 'packlockfile' , other_lock_path , { scope ='global' } ) 2238 2239exec_lua ( function () 2240vim . pack . add ( { { src =repos_src . basic , version ='feat-branch' } } ) 2241vim . pack . update ( { 'basic' } , { force =true } ) 2242end ) 2243pack_assert_content ( 'basic' , 'return "basic feat-branch"' ) 2244local basic_lock ={ 2245rev =git_get_hash ( 'feat-branch' , 'basic' ), 2246src =repos_src . basic , 2247version ="'feat-branch'" , 2248} 2249eq ( { plugins ={ basic =basic_lock } } , vim . json . decode ( fn . readblob ( other_lock_path ))) 2250end ) 2251 2252it ( 'validates input' , function () 2253local function assert ( err_pat , input ) 2254local function update_input () 2255vim . pack . update ( input ) 2256end 2257matches ( err_pat , pcall_err ( exec_lua , update_input )) 2258end 2259 2260assert ( 'list' , 1 ) 2261 2262-- Should first check if every plugin name represents installed plugin 2263-- If not - stop early before any update 2264vim_pack_add ( { repos_src . basic } ) 2265 2266assert ( 'Plugin `ccc` is not installed' , { 'ccc' , 'basic' , 'aaa' } ) 2267 2268-- Empty list is allowed 2269n . exec ( 'messages clear' ) 2270exec_lua ( function () 2271vim . pack . update ( {} ) 2272end ) 2273eq ( '' , n . exec_capture ( 'messages' )) 2274end ) 2275end ) 2276 2277describe ( 'get()' , function () 2278local function make_basic_data ( active , info ) 2279local spec ={ name ='basic' , src =repos_src . basic , version ='feat-branch' } 2280local path =pack_get_plug_path ( 'basic' ) 2281local res ={ active =active , path =path , spec =spec } 2282if info then 2283res . branches ={ 'main' , 'feat-branch' } 2284res . rev =git_get_hash ( 'feat-branch' , 'basic' ) 2285res . rev_to =res . rev 2286res . tags ={ 'some-tag' } 2287else 2288res . rev =get_lock_tbl (). plugins . basic . rev 2289end 2290return res 2291end 2292 2293local function make_defbranch_data ( active , info ) 2294local spec ={ name ='defbranch' , src =repos_src . defbranch } 2295local path =pack_get_plug_path ( 'defbranch' ) 2296local res ={ active =active , path =path , spec =spec } 2297if info then 2298res . branches ={ 'dev' , 'main' } 2299res . rev =git_get_hash ( 'dev' , 'defbranch' ) 2300res . rev_to =res . rev 2301res . tags ={} 2302else 2303res . rev =get_lock_tbl (). plugins . defbranch . rev 2304end 2305return res 2306end 2307 2308local function make_plugindirs_data ( active , info ) 2309local spec = 2310{ name ='plugindirs' , src =repos_src . plugindirs , version =vim . version . range ( '*' ) } 2311local path =pack_get_plug_path ( 'plugindirs' ) 2312local res ={ active =active , path =path , spec =spec } 2313if info then 2314res . branches ={ 'main' } 2315res . rev =git_get_hash ( 'v0.0.1' , 'plugindirs' ) 2316res . rev_to =res . rev 2317res . tags ={ 'v0.0.1' } 2318else 2319res . rev =get_lock_tbl (). plugins . plugindirs . rev 2320end 2321return res 2322end 2323 2324it ( 'returns list with necessary data' , function () 2325local basic_data , defbranch_data , plugindirs_data 2326 2327-- Should work just after installation 2328exec_lua ( function () 2329vim . pack . add ( { 2330repos_src . defbranch , 2331{ src =repos_src . basic , version ='feat-branch' } , 2332{ src =repos_src . plugindirs , version =vim . version . range ( '*' ) } , 2333} ) 2334end ) 2335defbranch_data =make_defbranch_data ( true , true ) 2336basic_data =make_basic_data ( true , true ) 2337plugindirs_data =make_plugindirs_data ( true , true ) 2338-- Should preserve order in which plugins were `vim.pack.add()`ed 2339eq ( { defbranch_data , basic_data , plugindirs_data } , exec_lua ( 'return vim.pack.get()' )) 2340 2341-- Should also list non-active plugins and use proper source for `rev` 2342local lock_tbl =get_lock_tbl () 2343lock_tbl . plugins . defbranch . rev ='aaa' 2344lock_tbl . plugins . basic . rev ='bbb' 2345lock_tbl . plugins . plugindirs . rev ='ccc' 2346local lockfile_text =vim . json . encode ( lock_tbl , { indent =' ' , sort_keys =true } ) 2347fn . writefile ( vim . split ( lockfile_text , '\n' ), get_lock_path ()) 2348n . clear () 2349 2350vim_pack_add ( { repos_src . defbranch } ) 2351defbranch_data =make_defbranch_data ( true , true ) 2352basic_data =make_basic_data ( false , true ) 2353plugindirs_data =make_plugindirs_data ( false , true ) 2354 2355-- Should first list active, then non-active (including their latest 2356-- set `version` which is inferred from lockfile) 2357eq ( { defbranch_data , basic_data , plugindirs_data } , exec_lua ( 'return vim.pack.get()' )) 2358 2359-- Should respect `names` for both active and not active plugins 2360eq ( { basic_data } , exec_lua ( 'return vim.pack.get({ "basic" })' )) 2361eq ( { defbranch_data } , exec_lua ( 'return vim.pack.get({ "defbranch" })' )) 2362eq ( { basic_data , defbranch_data } , exec_lua ( 'return vim.pack.get({ "basic", "defbranch" })' )) 2363 2364local bad_get_cmd ='return vim.pack.get({ "ccc", "basic", "aaa" })' 2365matches ( 'Plugin `ccc` is not installed' , pcall_err ( exec_lua , bad_get_cmd )) 2366 2367-- Should respect `opts.info` 2368defbranch_data =make_defbranch_data ( true , false ) 2369basic_data =make_basic_data ( false , false ) 2370plugindirs_data =make_plugindirs_data ( false , false ) 2371eq ( 2372{ defbranch_data , basic_data , plugindirs_data } , 2373exec_lua ( 'return vim.pack.get(nil, { info = false })' ) 2374) 2375eq ( { basic_data } , exec_lua ( 'return vim.pack.get({ "basic" }, { info = false })' )) 2376eq ( { defbranch_data } , exec_lua ( 'return vim.pack.get({ "defbranch" }, { info = false })' )) 2377 2378-- Reports manifest 2379vim_pack_add ( { repos_src . manifest } ) 2380local manifest_tbl ={ 2381name ='plug' , 2382description ='Nvim plugin' , 2383engine ={ nvim ='>=0.12.0' , vim ='>=9.1.0' } , 2384scripts ={ 2385install ='scripts/install.lua' , 2386preupdate ='scripts/preupdate.vim' , 2387update ='scripts/update.lua' , 2388preuninstall ='scirpts/preuninstall.vim' , 2389} , 2390} 2391eq ( manifest_tbl , exec_lua ( 'return vim.pack.get({ "manifest" })[1].manifest' )) 2392end ) 2393 2394it ( 'reports potential revision after update' , function () 2395-- Install and set up different version without running `vim.pack.update()` 2396vim_pack_add ( { repos_src . defbranch , { src =repos_src . basic , version ='feat-branch' } } ) 2397pack_assert_content ( 'defbranch' , 'return "defbranch dev"' ) 2398pack_assert_content ( 'basic' , 'return "basic feat-branch"' ) 2399 2400n . clear () 2401vim_pack_add ( { { src =repos_src . defbranch , version ='main' } , repos_src . basic } ) 2402n . clear () 2403 2404-- Should report correct `rev_to` with active and not active plugins 2405vim_pack_add ( { { src =repos_src . defbranch , version ='main' } } ) 2406local defbranch_data =make_defbranch_data ( true , true ) 2407defbranch_data . spec . version ='main' 2408defbranch_data . rev_to =git_get_hash ( 'main' , 'defbranch' ) 2409local basic_data =make_basic_data ( false , true ) 2410basic_data . spec . version =nil 2411basic_data . rev_to =git_get_hash ( 'main' , 'basic' ) 2412 2413eq ( { defbranch_data , basic_data } , exec_lua ( 'return vim.pack.get()' )) 2414end ) 2415 2416describe ( 'opts.offline' , function () 2417after_each ( function () 2418n . rmdir ( repo_get_path ( 'fetch' )) 2419end ) 2420 2421it ( 'can fetch new updates' , function () 2422-- Create a dedicated clean repo for which "push changes" will be mocked 2423init_test_repo ( 'fetch' ) 2424 2425repo_write_file ( 'fetch' , 'lua/fetch.lua' , 'return "fetch init"' ) 2426git_add_commit ( 'Initial commit' , 'fetch' ) 2427 2428local fetch_head =git_get_hash ( 'HEAD' , 'fetch' ) 2429 2430-- Install initial versions of tested plugins 2431vim_pack_add ( { repos_src . fetch } ) 2432 2433-- Mock remote repo update 2434-- - Force push 2435repo_write_file ( 'fetch' , 'lua/fetch.lua' , 'return "fetch new"' ) 2436git_cmd ( { 'add' , '*' } , 'fetch' ) 2437git_cmd ( { 'commit' , '--amend' , '-m' , 'Second commit' } , 'fetch' ) 2438local fetch_new =git_get_hash ( 'HEAD' , 'fetch' ) 2439t . neq ( fetch_head , fetch_new ) 2440 2441-- Should not fetch new data with `offline=true` (default) 2442-- or if `info=false` 2443local spec ={ name ='fetch' , src =repos_src . fetch } 2444local path =pack_get_plug_path ( 'fetch' ) 2445local fetch_data ={ active =true , path =path , spec =spec } 2446fetch_data . branches ={ 'main' } 2447fetch_data . tags ={} 2448fetch_data . rev =fetch_head 2449fetch_data . rev_to =fetch_head 2450 2451exec_lua ( 'vim.pack.get(nil, { info = false, offline = false })' ) 2452eq ( { fetch_data } , exec_lua ( 'return vim.pack.get()' )) 2453 2454-- Should fetch new data with `offline=false` 2455fetch_data . rev_to =fetch_new 2456eq ( { fetch_data } , exec_lua ( 'return vim.pack.get(nil, { offline = false })' )) 2457 2458-- Should keep using already fetched data with `offline=true` 2459eq ( { fetch_data } , exec_lua ( 'return vim.pack.get(nil, {})' )) 2460end ) 2461end ) 2462 2463it ( 'respects `data` field' , function () 2464vim_pack_add ( { 2465{ src =repos_src . basic , version ='feat-branch' , data ={ test ='value' } } , 2466{ src =repos_src . defbranch , data ='value' } , 2467} ) 2468local out =exec_lua ( function () 2469local plugs =vim . pack . get () 2470---@type table<string,string> 2471return { basic =plugs [ 1 ]. spec . data . test , defbranch =plugs [ 2 ]. spec . data } 2472end ) 2473eq ( { basic ='value' , defbranch ='value' } , out ) 2474end ) 2475 2476it ( 'works with `del()`' , function () 2477vim_pack_add ( { repos_src . defbranch , { src =repos_src . basic , version ='feat-branch' } } ) 2478 2479exec_lua ( function () 2480_G . get_log ={} 2481vim . api . nvim_create_autocmd ( { 'PackChangedPre' , 'PackChanged' } , { 2482callback =function () 2483table . insert ( _G . get_log , vim . pack . get ()) 2484end , 2485} ) 2486end ) 2487 2488-- Should not include removed plugins immediately after they are removed, 2489-- while still returning list without holes 2490local defbranch_data =make_defbranch_data ( true , true ) 2491local basic_data =make_basic_data ( true , true ) 2492exec_lua ( 'vim.pack.del({ "defbranch" }, { force = true })' ) 2493eq ( { { defbranch_data , basic_data } , { basic_data } } , exec_lua ( 'return _G.get_log' )) 2494end ) 2495 2496it ( 'works with out of sync lockfile' , function () 2497vim_pack_add ( { repos_src . basic , repos_src . defbranch } ) 2498eq ( 2 , vim . tbl_count ( get_lock_tbl (). plugins )) 2499 2500-- Should first autoinstall missing plugin (with confirmation) 2501n . rmdir ( pack_get_plug_path ( 'basic' )) 2502n . clear () 2503mock_confirm ( 1 ) 2504eq ( 2 , exec_lua ( 'return #vim.pack.get()' )) 2505 2506eq ( 1 , exec_lua ( 'return #_G.confirm_log' )) 2507pack_assert_content ( 'basic' , 'return "basic main"' ) 2508 2509-- Should regenerate absent lockfile (from present plugins) 2510vim . fs . rm ( get_lock_path ()) 2511n . clear () 2512eq ( 2 , exec_lua ( 'return #vim.pack.get()' )) 2513eq ( 2 , vim . tbl_count ( get_lock_tbl (). plugins )) 2514end ) 2515 2516it ( "respects 'packlockfile'" , function () 2517n . rmdir ( pack_get_dir ()) 2518pcall ( vim . fs . rm , get_lock_path (), { force =true } ) 2519 2520n . clear () 2521local other_lock_path =get_other_lock_path () 2522api . nvim_set_option_value ( 'packlockfile' , other_lock_path , { scope ='global' } ) 2523 2524exec_lua ( function () 2525vim . pack . add ( { { src =repos_src . basic , version ='feat-branch' } } ) 2526end ) 2527 2528local basic_data =make_basic_data ( true , true ) 2529eq ( { basic_data } , exec_lua ( 'return vim.pack.get()' )) 2530eq ( false , vim . uv . fs_stat ( get_lock_path ()) ~= nil ) 2531end ) 2532end ) 2533 2534describe ( 'del()' , function () 2535it ( 'works' , function () 2536local basic_spec ={ src =repos_src . basic , version ='feat-branch' } 2537vim_pack_add ( { repos_src . plugindirs , repos_src . defbranch , basic_spec } ) 2538 2539local assert_on_disk =function ( installed_map ) 2540local installed ={} 2541for p_name , is_installed in pairs ( installed_map ) do 2542eq ( is_installed , pack_exists ( p_name )) 2543if is_installed then 2544installed [ # installed + 1 ] =p_name 2545end 2546end 2547 2548table . sort ( installed ) 2549local locked =vim . tbl_keys ( get_lock_tbl (). plugins ) 2550table . sort ( locked ) 2551eq ( installed , locked ) 2552end 2553 2554assert_on_disk ( { basic =true , defbranch =true , plugindirs =true } ) 2555 2556-- By default should delete only non-active plugins, even if 2557-- there is active one among input plugin names 2558n . clear () 2559vim_pack_add ( { repos_src . defbranch } ) 2560watch_events ( { 'PackChangedPre' , 'PackChanged' } ) 2561 2562local err =pcall_err ( exec_lua , function () 2563vim . pack . del ( { 'basic' , 'defbranch' , 'plugindirs' } ) 2564end ) 2565matches ( 'Some plugins are active and were not deleted: defbranch' , err ) 2566 2567assert_on_disk ( { basic =false , defbranch =true , plugindirs =false } ) 2568 2569local msg ='vim.pack: Removed plugins: basic, plugindirs' 2570eq ( msg , n . exec_capture ( 'messages' )) 2571 2572-- Should trigger relevant events in order as specified in `vim.pack.add()` 2573assert_packchanged ( { 2574{ 'Pre' , 'delete' , 'basic' , 'feat-branch' , false } , 2575{ 'Pre' , 'delete' , 'defbranch' , nil , true } , 2576{ 'Pre' , 'delete' , 'plugindirs' , nil , false } , 2577{ '' , 'delete' , 'basic' , 'feat-branch' , false } , 2578{ '' , 'delete' , 'plugindirs' , nil , false } , 2579} ) 2580exec_lua ( '_G.event_log = {}' ) 2581 2582-- `:packdel` should output E5810 instead of the normal error 2583eq ( 2584'Vim(packdel):E5810: Some plugins are active and were not deleted: defbranch' , 2585pcall_err ( n . command , 'packdel defbranch' ) 2586) 2587assert_on_disk ( { defbranch =true } ) 2588assert_packchanged ( { { 'Pre' , 'delete' , 'defbranch' , nil , true } } ) 2589 2590-- Should be possible to force delete active plugins 2591n . exec ( 'messages clear' ) 2592exec_lua ( '_G.event_log = {}' ) 2593exec_lua ( function () 2594vim . pack . del ( { 'defbranch' } , { force =true } ) 2595end ) 2596 2597eq ( 'vim.pack: Removed plugin: defbranch' , n . exec_capture ( 'messages' )) 2598 2599assert_on_disk ( { basic =false , defbranch =false , plugindirs =false } ) 2600assert_packchanged ( { 2601{ 'Pre' , 'delete' , 'defbranch' , nil , true } , 2602{ '' , 'delete' , 'defbranch' , nil , false } , 2603} ) 2604end ) 2605 2606it ( 'works without prior `add()`' , function () 2607vim_pack_add ( { repos_src . basic } ) 2608n . clear () 2609 2610eq ( true , pack_exists ( 'basic' )) 2611exec_lua ( function () 2612vim . pack . del ( { 'basic' } ) 2613end ) 2614eq ( false , pack_exists ( 'basic' )) 2615eq ( { plugins ={} } , get_lock_tbl ()) 2616end ) 2617 2618it ( 'works with out of sync lockfile' , function () 2619vim_pack_add ( { repos_src . basic , repos_src . defbranch , repos_src . plugindirs } ) 2620eq ( 3 , vim . tbl_count ( get_lock_tbl (). plugins )) 2621 2622-- Should first autoinstall missing plugin (with confirmation) 2623n . rmdir ( pack_get_plug_path ( 'basic' )) 2624n . clear () 2625mock_confirm ( 1 ) 2626exec_lua ( 'vim.pack.del({ "defbranch" })' ) 2627 2628eq ( 1 , exec_lua ( 'return #_G.confirm_log' )) 2629eq ( true , pack_exists ( 'basic' )) 2630eq ( false , pack_exists ( 'defbranch' )) 2631eq ( true , pack_exists ( 'plugindirs' )) 2632 2633-- Should regenerate absent lockfile (from present plugins) 2634vim . fs . rm ( get_lock_path ()) 2635n . clear () 2636exec_lua ( 'vim.pack.del({ "basic" })' ) 2637eq ( 1 , exec_lua ( 'return #vim.pack.get()' )) 2638eq ( { 'plugindirs' } , vim . tbl_keys ( get_lock_tbl (). plugins )) 2639end ) 2640 2641it ( "respects 'packlockfile'" , function () 2642n . rmdir ( pack_get_dir ()) 2643pcall ( vim . fs . rm , get_lock_path (), { force =true } ) 2644 2645n . clear () 2646local other_lock_path =vim . fs . joinpath ( fn . stdpath ( 'data' ), 'test-pack-lock' ) 2647api . nvim_set_option_value ( 'packlockfile' , other_lock_path , { scope ='global' } ) 2648 2649exec_lua ( function () 2650vim . pack . add ( { repos_src . basic } ) 2651end ) 2652 2653exec_lua ( 'vim.pack.del({ "basic" }, { force = true })' ) 2654eq ( { plugins ={} } , vim . json . decode ( fn . readblob ( other_lock_path ))) 2655eq ( false , pack_exists ( 'basic' )) 2656eq ( false , vim . uv . fs_stat ( get_lock_path ()) ~= nil ) 2657end ) 2658 2659it ( 'sources relevant manifest scripts' , function () 2660vim_pack_add ( { repos_src . manifest } ) 2661n . clear () 2662 2663watch_events ( { 'PackChangedPre' , 'PackChanged' } ) 2664track_nvim_echo () 2665local cwd =fn . getcwd () 2666exec_lua ( "vim.pack.del({ 'manifest' })" ) 2667 2668-- Should not have side effects after executing script in special context 2669eq ( cwd , fn . getcwd ()) 2670local plug_path =pack_get_plug_path ( 'manifest' ) 2671eq ( false , vim . tbl_contains ( api . nvim_list_runtime_paths (), plug_path )) 2672 2673-- Should execute script in special context after triggering events 2674assert_manifest_scripts ( plug_path , { 2675{ event ='PackChangedPre' , kind ='delete' } , 2676{ event ='script' , kind ='preuninstall' , cwd =plug_path , rtp_has_plug =true } , 2677{ event ='PackChanged' , kind ='delete' } , 2678} ) 2679 2680-- Should warn on errors during script execution 2681eq ( true , is_in_echo_log ( 'Error in preuninstall script' , 'WarningMsg' )) 2682end ) 2683 2684it ( 'validates input' , function () 2685local function assert ( err_pat , input ) 2686local function del_input () 2687vim . pack . del ( input ) 2688end 2689matches ( err_pat , pcall_err ( exec_lua , del_input )) 2690end 2691 2692assert ( 'list' , nil ) 2693 2694-- Should first check if every plugin name represents installed plugin 2695-- If not - stop early before any delete 2696vim_pack_add ( { repos_src . basic } ) 2697 2698assert ( 'Plugin `ccc` is not installed' , { 'ccc' , 'basic' , 'aaa' } ) 2699eq ( true , pack_exists ( 'basic' )) 2700 2701-- Empty list is allowed 2702n . exec ( 'messages clear' ) 2703exec_lua ( function () 2704vim . pack . del ( {} ) 2705end ) 2706eq ( '' , n . exec_capture ( 'messages' )) 2707end ) 2708end ) 2709end )