robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
f907614088
master
1-- 2-- Tests for default options and environment decisions. 3-- 4-- See editor/defaults_spec.lua for default autocmds, mappings, commands, and menus. 5-- 6 7local t =require ( 'test.testutil' ) 8local n =require ( 'test.functional.testnvim' )() 9local Screen =require ( 'test.functional.ui.screen' ) 10 11local describe , it , before_each , after_each , finally = 12t . describe , t . it , t . before_each , t . after_each , t . finally 13local assert_alive =n . assert_alive 14local assert_log =t . assert_log 15local pcall_err =t . pcall_err 16local api =n . api 17local command =n . command 18local clear =n . clear 19local eval =n . eval 20local eq =t . eq 21local ok =t . ok 22local fn =n . fn 23local insert =n . insert 24local neq =t . neq 25local mkdir =t . mkdir 26local rmdir =n . rmdir 27local tbl_contains =vim . tbl_contains 28local expect_exit =n . expect_exit 29local check_close =n . check_close 30local is_os =t . is_os 31 32local testlog ='Xtest-defaults-log' 33 34describe ( 'startup defaults' , function () 35it ( "NVIM_NOTTYFAST=1 unsets 'ttyfast'" , function () 36clear { env ={ NVIM_NOTTYFAST ='1' } } 37eq ( 0 , n . eval ( '&ttyfast' )) 38end ) 39 40describe ( ':filetype' , function () 41local function expect_filetype ( expected ) 42local screen =Screen . new ( 50 , 4 ) 43command ( 'filetype' ) 44screen : expect ( [[ 45^ | 46{1:~ }|*2 47]] .. expected ) 48end 49 50it ( 'all ON after `-u NORC`' , function () 51clear ( '-u' , 'NORC' ) 52expect_filetype ( 'filetype detection:ON plugin:ON indent:ON |' ) 53end ) 54 55it ( 'all ON after `:syntax …` #7765' , function () 56clear ( '-u' , 'NORC' , '--cmd' , 'syntax on' ) 57expect_filetype ( 'filetype detection:ON plugin:ON indent:ON |' ) 58clear ( '-u' , 'NORC' , '--cmd' , 'syntax off' ) 59expect_filetype ( 'filetype detection:ON plugin:ON indent:ON |' ) 60end ) 61 62it ( 'all OFF after `-u NONE`' , function () 63clear ( '-u' , 'NONE' ) 64expect_filetype ( 'filetype detection:OFF plugin:OFF indent:OFF |' ) 65end ) 66 67it ( 'explicit OFF stays OFF' , function () 68clear ( '-u' , 'NORC' , '--cmd' , 'syntax off | filetype off | filetype plugin indent off' ) 69expect_filetype ( 'filetype detection:OFF plugin:OFF indent:OFF |' ) 70clear ( '-u' , 'NORC' , '--cmd' , 'syntax off | filetype plugin indent off' ) 71expect_filetype ( 'filetype detection:ON plugin:OFF indent:OFF |' ) 72clear ( '-u' , 'NORC' , '--cmd' , 'filetype indent off' ) 73expect_filetype ( 'filetype detection:ON plugin:ON indent:OFF |' ) 74clear ( '-u' , 'NORC' , '--cmd' , 'syntax off | filetype off' ) 75expect_filetype ( 'filetype detection:OFF plugin:(on) indent:(on) |' ) 76-- Swap the order. 77clear ( '-u' , 'NORC' , '--cmd' , 'filetype off | syntax off' ) 78expect_filetype ( 'filetype detection:OFF plugin:(on) indent:(on) |' ) 79end ) 80 81it ( 'all ON after early `:filetype … on`' , function () 82-- `:filetype … on` should not change the defaults. #7765 83-- Only an explicit `:filetype … off` sets OFF. 84 85clear ( '-u' , 'NORC' , '--cmd' , 'filetype on' ) 86expect_filetype ( 'filetype detection:ON plugin:ON indent:ON |' ) 87clear ( '-u' , 'NORC' , '--cmd' , 'filetype plugin on' ) 88expect_filetype ( 'filetype detection:ON plugin:ON indent:ON |' ) 89clear ( '-u' , 'NORC' , '--cmd' , 'filetype indent on' ) 90expect_filetype ( 'filetype detection:ON plugin:ON indent:ON |' ) 91end ) 92 93it ( 'late `:filetype … off` stays OFF' , function () 94clear ( '-u' , 'NORC' , '-c' , 'filetype off' ) 95expect_filetype ( 'filetype detection:OFF plugin:(on) indent:(on) |' ) 96clear ( '-u' , 'NORC' , '-c' , 'filetype plugin off' ) 97expect_filetype ( 'filetype detection:ON plugin:OFF indent:ON |' ) 98clear ( '-u' , 'NORC' , '-c' , 'filetype indent off' ) 99expect_filetype ( 'filetype detection:ON plugin:ON indent:OFF |' ) 100end ) 101end ) 102 103describe ( 'syntax' , function () 104it ( 'enabled by `-u NORC`' , function () 105clear ( '-u' , 'NORC' ) 106eq ( 1 , eval ( 'g:syntax_on' )) 107end ) 108 109it ( 'disabled by `-u NONE`' , function () 110clear ( '-u' , 'NONE' ) 111eq ( 0 , eval ( 'exists("g:syntax_on")' )) 112end ) 113 114it ( '`:syntax off` stays off' , function () 115-- early 116clear ( '-u' , 'NORC' , '--cmd' , 'syntax off' ) 117eq ( 0 , eval ( 'exists("g:syntax_on")' )) 118-- late 119clear ( '-u' , 'NORC' , '-c' , 'syntax off' ) 120eq ( 0 , eval ( 'exists("g:syntax_on")' )) 121end ) 122 123it ( '":if 0|syntax on|endif" does not affect default #8728' , function () 124clear ( '-u' , 'NORC' , '--cmd' , ':if 0|syntax on|endif' ) 125eq ( 1 , eval ( 'exists("g:syntax_on")' )) 126clear ( '-u' , 'NORC' , '--cmd' , ':if 0|syntax off|endif' ) 127eq ( 1 , eval ( 'exists("g:syntax_on")' )) 128end ) 129end ) 130 131describe ( "'fillchars'" , function () 132it ( 'vert/fold flags' , function () 133clear () 134local screen =Screen . new ( 50 , 5 ) 135command ( 'set laststatus=0' ) 136insert ( [[ 1371 1382 1393 1404]] ) 141command ( 'normal! ggjzfj' ) 142command ( 'vsp' ) 143screen : expect ( [[ 1441 │1 | 145{13:^+-- 2 lines: 2··········}│{13:+-- 2 lines: 2·········}| 1464 │4 | 147{1:~ }│{1:~ }| 148| 149]] ) 150 151-- ambiwidth=double defaults to single-byte fillchars. 152command ( 'set ambiwidth=double' ) 153screen : expect ( [[ 1541 |1 | 155{13:^+-- 2 lines: 2----------}|{13:+-- 2 lines: 2---------}| 1564 |4 | 157{1:~ }|{1:~ }| 158| 159]] ) 160 161-- change "vert" character to single-cell 162fn . setcellwidths ( { { 0x2502 , 0x2502 , 1 } } ) 163screen : expect ( [[ 1641 │1 | 165{13:^+-- 2 lines: 2----------}│{13:+-- 2 lines: 2---------}| 1664 │4 | 167{1:~ }│{1:~ }| 168| 169]] ) 170 171-- change "vert" character to double-cell 172fn . setcellwidths ( { { 0x2502 , 0x2502 , 2 } } ) 173screen : expect ( [[ 1741 |1 | 175{13:^+-- 2 lines: 2----------}|{13:+-- 2 lines: 2---------}| 1764 |4 | 177{1:~ }|{1:~ }| 178| 179]] ) 180 181-- "vert" character should still default to single-byte fillchars because of setcellwidths(). 182command ( 'set ambiwidth=single' ) 183screen : expect ( [[ 1841 |1 | 185{13:^+-- 2 lines: 2··········}|{13:+-- 2 lines: 2·········}| 1864 |4 | 187{1:~ }|{1:~ }| 188| 189]] ) 190end ) 191end ) 192 193it ( "'shadafile' ('viminfofile')" , function () 194local env ={ 195XDG_DATA_HOME ='Xtest-userdata' , 196XDG_STATE_HOME ='Xtest-userstate' , 197XDG_CONFIG_HOME ='Xtest-userconfig' , 198} 199finally ( function () 200command ( 'set shadafile=NONE' ) -- Avoid writing shada file on exit 201rmdir ( 'Xtest-userstate' ) 202os . remove ( 'Xtest-foo' ) 203end ) 204 205clear { args ={} , args_rm ={ '-i' , '--cmd' } , env =env } 206-- Default 'shadafile' is empty. 207-- This means use the default location. :help shada-file-name 208eq ( '' , api . nvim_get_option_value ( 'shadafile' , {} )) 209eq ( '' , api . nvim_get_option_value ( 'viminfofile' , {} )) 210-- Handles viminfo/viminfofile as alias for shada/shadafile. 211eq ( '\n shadafile=' , eval ( 'execute("set shadafile?")' )) 212eq ( '\n shadafile=' , eval ( 'execute("set viminfofile?")' )) 213eq ( "\n shada=!,'100,<50,s10,h,r/tmp/,r/private/" , eval ( 'execute("set shada?")' )) 214eq ( "\n shada=!,'100,<50,s10,h,r/tmp/,r/private/" , eval ( 'execute("set viminfo?")' )) 215 216-- Remove /tmp/ exclusion so test works when run in temp directories. 217command ( "set shada=!,'100,<50,s10,h" ) 218-- Check that shada data (such as v:oldfiles) is saved/restored. 219command ( 'edit Xtest-foo' ) 220command ( 'write' ) 221local f =eval ( 'fnamemodify(@%,":p")' ) 222assert ( string . len ( f ) > 3 ) 223expect_exit ( command , 'qall' ) 224clear { args ={} , args_rm ={ '-i' , '--cmd' } , env =env } 225eq ( { f } , eval ( 'v:oldfiles' )) 226end ) 227 228it ( "'packpath'" , function () 229clear { 230args_rm ={ 'runtimepath' } , 231} 232-- Defaults to &runtimepath. 233eq ( api . nvim_get_option_value ( 'runtimepath' , {} ), api . nvim_get_option_value ( 'packpath' , {} )) 234 235-- Does not follow modifications to runtimepath. 236command ( 'set runtimepath+=foo' ) 237neq ( api . nvim_get_option_value ( 'runtimepath' , {} ), api . nvim_get_option_value ( 'packpath' , {} )) 238command ( 'set packpath+=foo' ) 239eq ( api . nvim_get_option_value ( 'runtimepath' , {} ), api . nvim_get_option_value ( 'packpath' , {} )) 240end ) 241 242it ( 'v:progpath is set to the absolute path' , function () 243clear () 244eq ( eval ( "fnamemodify(v:progpath, ':p')" ), eval ( 'v:progpath' )) 245end ) 246 247describe ( '$NVIM_LOG_FILE' , function () 248local xdgdir ='Xtest-startup-xdg-logpath' 249local xdgstatedir =is_os ( 'win' ) and xdgdir .. '/nvim-data' or xdgdir .. '/nvim' 250after_each ( function () 251os . remove ( 'Xtest-logpath' ) 252rmdir ( xdgdir ) 253end ) 254 255it ( 'is used if expansion succeeds' , function () 256clear ( { env ={ 257NVIM_LOG_FILE ='Xtest-logpath' , 258} } ) 259eq ( 'Xtest-logpath' , eval ( '$NVIM_LOG_FILE' )) 260end ) 261 262it ( 'defaults to stdpath("log")/nvim.log if empty' , function () 263eq ( true , mkdir ( xdgdir ) and mkdir ( xdgstatedir )) 264clear ( { 265env ={ 266XDG_STATE_HOME =xdgdir , 267NVIM_LOG_FILE ='' , -- Empty is invalid. 268} , 269} ) 270eq (( '%s/logs/nvim.log' ): format ( xdgstatedir ), eval ( '$NVIM_LOG_FILE' )) 271end ) 272 273it ( 'defaults to stdpath("log")/nvim.log if invalid' , function () 274eq ( true , mkdir ( xdgdir ) and mkdir ( xdgstatedir )) 275clear ( { 276env ={ 277XDG_STATE_HOME =xdgdir , 278NVIM_LOG_FILE ='.' , -- Any directory is invalid. 279} , 280} ) 281eq (( '%s/logs/nvim.log' ): format ( xdgstatedir ), eval ( '$NVIM_LOG_FILE' )) 282-- Avoid "failed to open $NVIM_LOG_FILE" noise in test output. 283expect_exit ( command , 'qall!' ) 284end ) 285end ) 286end ) 287 288describe ( 'XDG defaults' , function () 289-- Need separate describe() blocks to not run clear() twice. 290-- Do not put before_each() here for the same reasons. 291 292after_each ( function () 293check_close () 294os . remove ( testlog ) 295end ) 296 297it ( "&runtimepath data-dir matches stdpath('data') #9910" , function () 298clear () 299local rtp =eval ( 'split(&runtimepath, ",")' ) 300local rv ={} 301local data_dir =is_os ( 'win' ) and '/nvim-data' or '/nvim' 302local expected ={ data_dir .. '/site' , data_dir .. '/site/after' } 303 304for _ , v in ipairs ( rtp ) do 305local m =string . match ( v , [=[[/\]nvim[^/\]*[/\]site.*$]=] ) 306if m and not tbl_contains ( rv , m ) then 307table . insert ( rv , m ) 308end 309end 310eq ( expected , rv ) 311end ) 312 313describe ( 'with empty/broken environment' , function () 314it ( 'sets correct defaults' , function () 315clear ( { 316env ={ 317XDG_CONFIG_HOME =nil , 318XDG_DATA_HOME =nil , 319XDG_CACHE_HOME =nil , 320XDG_STATE_HOME =nil , 321XDG_RUNTIME_DIR =nil , 322XDG_CONFIG_DIRS =nil , 323XDG_DATA_DIRS =nil , 324LOCALAPPDATA =nil , 325HOMEPATH =nil , 326HOMEDRIVE =nil , 327HOME =nil , 328TEMP =nil , 329VIMRUNTIME =nil , 330USER =nil , 331} , 332} ) 333 334eq ( '.' , api . nvim_get_option_value ( 'backupdir' , {} )) 335eq ( '.' , api . nvim_get_option_value ( 'viewdir' , {} )) 336eq ( '.' , api . nvim_get_option_value ( 'directory' , {} )) 337eq ( '.' , api . nvim_get_option_value ( 'undodir' , {} )) 338ok (( fn . tempname ()): len () > 4 ) 339end ) 340end ) 341 342local function vimruntime_and_libdir () 343local vimruntime =eval ( '$VIMRUNTIME' ) 344-- libdir is hard to calculate reliably across various ci platforms 345-- local libdir = string.gsub(vimruntime, "share/nvim/runtime$", "lib/nvim") 346local libdir =api . nvim__get_lib_dir () 347return vimruntime , libdir 348end 349 350local env_sep =is_os ( 'win' ) and ';' or ':' 351local data_dir =is_os ( 'win' ) and 'nvim-data' or 'nvim' 352local state_dir =is_os ( 'win' ) and 'nvim-data' or 'nvim' 353local root_path =is_os ( 'win' ) and 'C:' or '' 354 355describe ( 'with too long XDG vars' , function () 356before_each ( function () 357clear ( { 358-- Ensure valid --listen address despite broken XDG vars (else Nvim won't start). 359args ={ '--listen' , is_os ( 'win' ) and '' or t . tmpname ( false ) } , 360args_rm ={ 'runtimepath' } , 361env ={ 362NVIM_LOG_FILE =testlog , 363XDG_CONFIG_HOME =( root_path .. ( '/x' ): rep ( 4096 )), 364XDG_CONFIG_DIRS =( root_path .. ( '/a' ): rep ( 2048 ) .. env_sep .. root_path .. ( '/b' ): rep ( 3652048 366) .. ( env_sep .. root_path .. '/c' ): rep ( 512 )), 367XDG_DATA_HOME =( root_path .. ( '/X' ): rep ( 4096 )), 368XDG_RUNTIME_DIR =( root_path .. ( '/X' ): rep ( 4096 )), 369XDG_STATE_HOME =( root_path .. ( '/X' ): rep ( 4096 )), 370XDG_DATA_DIRS =( root_path .. ( '/A' ): rep ( 2048 ) .. env_sep .. root_path .. ( '/B' ): rep ( 3712048 372) .. ( env_sep .. root_path .. '/C' ): rep ( 512 )), 373} , 374} ) 375end ) 376 377it ( 'are correctly set' , function () 378if not is_os ( 'win' ) then 379-- Broken XDG vars cause serverstart() to fail (except on Windows, where servernames are not 380-- informed by $XDG_STATE_HOME). 381t . matches ( 'Failed to start server: invalid argument' , t . pcall_err ( fn . serverstart )) 382assert_log ( 'Failed to start server: invalid argument: /X/X/X' , testlog , 10 ) 383end 384 385local vimruntime , libdir =vimruntime_and_libdir () 386 387local expected_paths =table . concat ( { 388( '%s%s/nvim' ): format ( root_path , ( '/x' ): rep ( 4096 )), 389( '%s%s/nvim' ): format ( root_path , ( '/a' ): rep ( 2048 )), 390( '%s%s/nvim' ): format ( root_path , ( '/b' ): rep ( 2048 )), 391( '%s/c/nvim' ): format ( root_path ), 392( '%s%s/%s/site' ): format ( root_path , ( '/X' ): rep ( 4096 ), data_dir ), 393( '%s%s/nvim/site' ): format ( root_path , ( '/A' ): rep ( 2048 )), 394( '%s%s/nvim/site' ): format ( root_path , ( '/B' ): rep ( 2048 )), 395( '%s/C/nvim/site' ): format ( root_path ), 396vimruntime , 397libdir , 398( '%s/C/nvim/site/after' ): format ( root_path ), 399( '%s%s/nvim/site/after' ): format ( root_path , ( '/B' ): rep ( 2048 )), 400( '%s%s/nvim/site/after' ): format ( root_path , ( '/A' ): rep ( 2048 )), 401( '%s%s/%s/site/after' ): format ( root_path , ( '/X' ): rep ( 4096 ), data_dir ), 402( '%s/c/nvim/after' ): format ( root_path ), 403( '%s%s/nvim/after' ): format ( root_path , ( '/b' ): rep ( 2048 )), 404( '%s%s/nvim/after' ): format ( root_path , ( '/a' ): rep ( 2048 )), 405( '%s%s/nvim/after' ): format ( root_path , ( '/x' ): rep ( 4096 )), 406} , ',' ) 407eq ( expected_paths , api . nvim_get_option_value ( 'runtimepath' , {} )) 408command ( 'set runtimepath&' ) 409command ( 'set backupdir&' ) 410command ( 'set directory&' ) 411command ( 'set undodir&' ) 412command ( 'set viewdir&' ) 413eq ( expected_paths , api . nvim_get_option_value ( 'runtimepath' , {} )) 414eq ( 415( '.,%s%s/%s/backup//' ): format ( root_path , ( '/X' ): rep ( 4096 ), state_dir ), 416api . nvim_get_option_value ( 'backupdir' , {} ) 417) 418eq ( 419( '%s%s/%s/swap//' ): format ( root_path , ( '/X' ): rep ( 4096 ), state_dir ), 420api . nvim_get_option_value ( 'directory' , {} ) 421) 422eq ( 423( '%s%s/%s/undo//' ): format ( root_path , ( '/X' ): rep ( 4096 ), state_dir ), 424api . nvim_get_option_value ( 'undodir' , {} ) 425) 426eq ( 427( '%s%s/%s/view//' ): format ( root_path , ( '/X' ): rep ( 4096 ), state_dir ), 428api . nvim_get_option_value ( 'viewdir' , {} ) 429) 430end ) 431end ) 432 433describe ( 'with expandable XDG vars' , function () 434before_each ( function () 435clear ( { 436-- Ensure valid --listen address despite broken XDG vars (else Nvim won't start). 437args ={ '--listen' , is_os ( 'win' ) and '' or t . tmpname ( false ) } , 438args_rm ={ 'runtimepath' } , 439env ={ 440NVIM_LOG_FILE =testlog , 441XDG_CONFIG_HOME ='$XDG_DATA_HOME' , 442XDG_CONFIG_DIRS ='$XDG_DATA_DIRS' , 443XDG_DATA_HOME ='$XDG_CONFIG_HOME' , 444XDG_RUNTIME_DIR ='$XDG_RUNTIME_DIR' , 445XDG_STATE_HOME ='$XDG_CONFIG_HOME' , 446XDG_DATA_DIRS ='$XDG_CONFIG_DIRS' , 447} , 448} ) 449end ) 450 451after_each ( function () 452command ( 'set shadafile=NONE' ) -- Avoid writing shada file on exit 453end ) 454 455it ( 'are not expanded' , function () 456if not is_os ( 'win' ) then 457-- Broken XDG vars cause serverstart() to fail (except on Windows, where servernames are not 458-- informed by $XDG_STATE_HOME). 459t . matches ( 'Failed to start server: no such file or directory' , t . pcall_err ( fn . serverstart )) 460assert_log ( 461'Failed to start server: no such file or directory: %$XDG_RUNTIME_DIR%/' , 462testlog , 46310 464) 465end 466 467local vimruntime , libdir =vimruntime_and_libdir () 468local expected_paths =table . concat ( { 469'$XDG_DATA_HOME/nvim' , 470'$XDG_DATA_DIRS/nvim' , 471( '$XDG_CONFIG_HOME/%s/site' ): format ( data_dir ), 472'$XDG_CONFIG_DIRS/nvim/site' , 473vimruntime , 474libdir , 475'$XDG_CONFIG_DIRS/nvim/site/after' , 476( '$XDG_CONFIG_HOME/%s/site/after' ): format ( data_dir ), 477'$XDG_DATA_DIRS/nvim/after' , 478'$XDG_DATA_HOME/nvim/after' , 479} , ',' ) 480eq ( expected_paths , api . nvim_get_option_value ( 'runtimepath' , {} )) 481command ( 'set runtimepath&' ) 482command ( 'set backupdir&' ) 483command ( 'set directory&' ) 484command ( 'set undodir&' ) 485command ( 'set viewdir&' ) 486eq ( expected_paths , api . nvim_get_option_value ( 'runtimepath' , {} )) 487eq ( 488( '.,$XDG_CONFIG_HOME/%s/backup//' ): format ( state_dir ), 489api . nvim_get_option_value ( 'backupdir' , {} ) 490) 491eq ( 492( '$XDG_CONFIG_HOME/%s/swap//' ): format ( state_dir ), 493api . nvim_get_option_value ( 'directory' , {} ) 494) 495eq (( '$XDG_CONFIG_HOME/%s/undo//' ): format ( state_dir ), api . nvim_get_option_value ( 'undodir' , {} )) 496eq (( '$XDG_CONFIG_HOME/%s/view//' ): format ( state_dir ), api . nvim_get_option_value ( 'viewdir' , {} )) 497command ( 'set all&' ) 498eq ( expected_paths , api . nvim_get_option_value ( 'runtimepath' , {} )) 499eq ( 500( '.,$XDG_CONFIG_HOME/%s/backup//' ): format ( state_dir ), 501api . nvim_get_option_value ( 'backupdir' , {} ) 502) 503eq ( 504( '$XDG_CONFIG_HOME/%s/swap//' ): format ( state_dir ), 505api . nvim_get_option_value ( 'directory' , {} ) 506) 507eq (( '$XDG_CONFIG_HOME/%s/undo//' ): format ( state_dir ), api . nvim_get_option_value ( 'undodir' , {} )) 508eq (( '$XDG_CONFIG_HOME/%s/view//' ): format ( state_dir ), api . nvim_get_option_value ( 'viewdir' , {} )) 509eq ( nil , ( fn . tempname ()): match ( 'XDG_RUNTIME_DIR' )) 510end ) 511end ) 512 513describe ( 'with commas' , function () 514before_each ( function () 515clear ( { 516args_rm ={ 'runtimepath' } , 517env ={ 518XDG_CONFIG_HOME =', , ,' , 519XDG_CONFIG_DIRS =',-,-,' .. env_sep .. '-,-,-' , 520XDG_DATA_HOME =',=,=,' , 521XDG_STATE_HOME =',=,=,' , 522XDG_DATA_DIRS =',≡,≡,' .. env_sep .. '≡,≡,≡' , 523} , 524} ) 525end ) 526 527it ( 'are escaped properly' , function () 528local vimruntime , libdir =vimruntime_and_libdir () 529local path_sep ='/' 530eq ( 531( 532'\\, \\, \\,' 533.. path_sep 534.. 'nvim' 535.. ',\\,-\\,-\\,' 536.. path_sep 537.. 'nvim' 538.. ',-\\,-\\,-' 539.. path_sep 540.. 'nvim' 541.. ',\\,=\\,=\\,' 542.. path_sep 543.. data_dir 544.. path_sep 545.. 'site' 546.. ',\\,≡\\,≡\\,' 547.. path_sep 548.. 'nvim' 549.. path_sep 550.. 'site' 551.. ',≡\\,≡\\,≡' 552.. path_sep 553.. 'nvim' 554.. path_sep 555.. 'site' 556.. ',' 557.. vimruntime 558.. ',' 559.. libdir 560.. ',≡\\,≡\\,≡' 561.. path_sep 562.. 'nvim' 563.. path_sep 564.. 'site' 565.. path_sep 566.. 'after' 567.. ',\\,≡\\,≡\\,' 568.. path_sep 569.. 'nvim' 570.. path_sep 571.. 'site' 572.. path_sep 573.. 'after' 574.. ',\\,=\\,=\\,' 575.. path_sep 576.. data_dir 577.. path_sep 578.. 'site' 579.. path_sep 580.. 'after' 581.. ',-\\,-\\,-' 582.. path_sep 583.. 'nvim' 584.. path_sep 585.. 'after' 586.. ',\\,-\\,-\\,' 587.. path_sep 588.. 'nvim' 589.. path_sep 590.. 'after' 591.. ',\\, \\, \\,' 592.. path_sep 593.. 'nvim' 594.. path_sep 595.. 'after' 596), 597api . nvim_get_option_value ( 'runtimepath' , {} ) 598) 599command ( 'set runtimepath&' ) 600command ( 'set backupdir&' ) 601command ( 'set directory&' ) 602command ( 'set undodir&' ) 603command ( 'set viewdir&' ) 604eq ( 605( 606'\\, \\, \\,' 607.. path_sep 608.. 'nvim' 609.. ',\\,-\\,-\\,' 610.. path_sep 611.. 'nvim' 612.. ',-\\,-\\,-' 613.. path_sep 614.. 'nvim' 615.. ',\\,=\\,=\\,' 616.. path_sep 617.. '' 618.. data_dir 619.. '' 620.. path_sep 621.. 'site' 622.. ',\\,≡\\,≡\\,' 623.. path_sep 624.. 'nvim' 625.. path_sep 626.. 'site' 627.. ',≡\\,≡\\,≡' 628.. path_sep 629.. 'nvim' 630.. path_sep 631.. 'site' 632.. ',' 633.. vimruntime 634.. ',' 635.. libdir 636.. ',≡\\,≡\\,≡' 637.. path_sep 638.. 'nvim' 639.. path_sep 640.. 'site' 641.. path_sep 642.. 'after' 643.. ',\\,≡\\,≡\\,' 644.. path_sep 645.. 'nvim' 646.. path_sep 647.. 'site' 648.. path_sep 649.. 'after' 650.. ',\\,=\\,=\\,' 651.. path_sep 652.. '' 653.. data_dir 654.. '' 655.. path_sep 656.. 'site' 657.. path_sep 658.. 'after' 659.. ',-\\,-\\,-' 660.. path_sep 661.. 'nvim' 662.. path_sep 663.. 'after' 664.. ',\\,-\\,-\\,' 665.. path_sep 666.. 'nvim' 667.. path_sep 668.. 'after' 669.. ',\\, \\, \\,' 670.. path_sep 671.. 'nvim' 672.. path_sep 673.. 'after' 674), 675api . nvim_get_option_value ( 'runtimepath' , {} ) 676) 677eq ( 678'.,\\,=\\,=\\,' .. path_sep .. state_dir .. '' .. path_sep .. 'backup' .. ( path_sep ): rep ( 2 ), 679api . nvim_get_option_value ( 'backupdir' , {} ) 680) 681eq ( 682'\\,=\\,=\\,' 683.. path_sep 684.. '' 685.. state_dir 686.. '' 687.. path_sep 688.. 'swap' 689.. ( path_sep ): rep ( 2 ), 690api . nvim_get_option_value ( 'directory' , {} ) 691) 692eq ( 693'\\,=\\,=\\,' 694.. path_sep 695.. '' 696.. state_dir 697.. '' 698.. path_sep 699.. 'undo' 700.. ( path_sep ): rep ( 2 ), 701api . nvim_get_option_value ( 'undodir' , {} ) 702) 703eq ( ',=,=,/' .. state_dir .. '/view//' , api . nvim_get_option_value ( 'viewdir' , {} )) 704end ) 705end ) 706end ) 707 708describe ( 'stdpath()' , function () 709after_each ( function () 710check_close () 711os . remove ( testlog ) 712end ) 713 714--- Windows appends 'nvim-data' instead of just 'nvim' to prevent collisions 715--- due to XDG_CONFIG_HOME, XDG_DATA_HOME and XDG_STATE_HOME being the same. 716--- @param name string 717local function maybe_data ( name ) 718return is_os ( 'win' ) and name .. '-data' or name 719end 720 721local datadir =maybe_data ( 'nvim' ) 722local statedir =maybe_data ( 'nvim' ) 723local env_sep =is_os ( 'win' ) and ';' or ':' 724 725it ( 'works' , function () 726clear () -- Do not explicitly set any env vars. 727 728eq ( 'nvim' , fn . fnamemodify ( fn . stdpath ( 'cache' ), ':t' )) 729eq ( 'nvim' , fn . fnamemodify ( fn . stdpath ( 'config' ), ':t' )) 730eq ( datadir , fn . fnamemodify ( fn . stdpath ( 'data' ), ':t' )) 731eq ( statedir , fn . fnamemodify ( fn . stdpath ( 'state' ), ':t' )) 732eq ( 'table' , type ( fn . stdpath ( 'config_dirs' ))) 733eq ( 'table' , type ( fn . stdpath ( 'data_dirs' ))) 734eq ( 'string' , type ( fn . stdpath ( 'run' ))) 735assert_alive () -- Check for crash. #8393 736end ) 737 738it ( 'failure modes' , function () 739clear () 740eq ( 741'Vim(call):E6100: "capybara" is not a valid stdpath' , 742pcall_err ( command , 'call stdpath("capybara")' ) 743) 744eq ( 'Vim(call):E6100: "" is not a valid stdpath' , pcall_err ( command , 'call stdpath("")' )) 745eq ( 'Vim(call):E6100: "23" is not a valid stdpath' , pcall_err ( command , 'call stdpath(23)' )) 746eq ( 747'Vim(call):E731: Using a Dictionary as a String' , 748pcall_err ( command , 'call stdpath({"eris": 23})' ) 749) 750eq ( 'Vim(call):E730: Using a List as a String' , pcall_err ( command , 'call stdpath([23])' )) 751end ) 752 753it ( '$NVIM_APPNAME' , function () 754local appname ='NVIM_APPNAME_TEST' .. ( '_' ): rep ( 106 ) 755clear ( { env ={ NVIM_APPNAME =appname , NVIM_LOG_FILE =testlog } } ) 756eq ( appname , fn . fnamemodify ( fn . stdpath ( 'config' ), ':t' )) 757eq ( appname , fn . fnamemodify ( fn . stdpath ( 'cache' ), ':t' )) 758eq ( maybe_data ( appname ), fn . fnamemodify ( fn . stdpath ( 'log' ), ':h:t' )) 759eq ( maybe_data ( appname ), fn . fnamemodify ( fn . stdpath ( 'data' ), ':t' )) 760eq ( maybe_data ( appname ), fn . fnamemodify ( fn . stdpath ( 'state' ), ':t' )) 761-- config_dirs and data_dirs are empty on windows, so don't check them on 762-- that platform 763if not is_os ( 'win' ) then 764eq ( appname , fn . fnamemodify ( fn . stdpath ( 'config_dirs' )[ 1 ], ':t' )) 765eq ( appname , fn . fnamemodify ( fn . stdpath ( 'data_dirs' )[ 1 ], ':t' )) 766end 767assert_alive () -- Check for crash. #8393 768 769-- Check that Nvim rejects invalid APPNAMEs 770local function test_appname ( testAppname , expected_exitcode ) 771local p =n . spawn_wait ( { 772args ={ '--listen' , 'x' , '+qall!' } , 773env ={ NVIM_APPNAME =testAppname } , 774} ) 775eq ( expected_exitcode , p . status ) 776end 777-- Invalid appnames: 778test_appname ( 'a/../b' , 1 ) 779test_appname ( '../a' , 1 ) 780test_appname ( 'a/..' , 1 ) 781test_appname ( '..' , 1 ) 782test_appname ( '.' , 1 ) 783test_appname ( '/' , 1 ) 784test_appname ( '\\' , 1 ) 785if is_os ( 'win' ) then 786test_appname ( '..\\a' , 1 ) 787test_appname ( 'a\\..' , 1 ) 788end 789test_appname ( is_os ( 'win' ) and 'C:/a/b' or '/a/b' , 1 ) 790-- Valid appnames: 791test_appname ( 'a/b' , 0 ) 792test_appname ( 'a/b\\c' , 0 ) 793end ) 794 795it ( '$NVIM_APPNAME relative path' , function () 796local tmpdir =t . tmpname ( false ) 797t . mkdir ( tmpdir ) 798-- Short runtime dir so the autogenerated server socket fits in sun_path even 799-- when $TMPDIR is deep (otherwise the server fails to bind, see #38623). 800local xdg_run ='Xtest-appname-run' 801mkdir ( xdg_run ) 802finally ( function () 803rmdir ( xdg_run ) 804end ) 805 806clear ( { 807args_rm ={ '--listen' } , 808env ={ 809NVIM_APPNAME ='relative/appname' , 810NVIM_LOG_FILE =testlog , 811TMPDIR =tmpdir , 812XDG_RUNTIME_DIR =xdg_run , 813} , 814} ) 815 816t . matches ( vim . pesc ( tmpdir ), fn . tempname ()) 817t . assert_nolog ( 'tempdir' , testlog , 100 ) 818t . assert_nolog ( 'TMPDIR' , testlog , 100 ) 819t . matches ( [[/relative%-appname.%d+]] , api . nvim_get_vvar ( 'servername' )) 820end ) 821 822describe ( 'returns a String' , function () 823describe ( 'with "config"' , function () 824it ( 'knows XDG_CONFIG_HOME' , function () 825clear ( { 826env ={ 827XDG_CONFIG_HOME ='/home/docwhat/.config' , 828} , 829} ) 830eq ( '/home/docwhat/.config/nvim' , fn . stdpath ( 'config' )) 831end ) 832 833it ( 'handles changes during runtime' , function () 834clear ( { env ={ 835XDG_CONFIG_HOME ='/home/original' , 836} } ) 837eq ( '/home/original/nvim' , fn . stdpath ( 'config' )) 838command ( "let $XDG_CONFIG_HOME='/home/new'" ) 839eq ( '/home/new/nvim' , fn . stdpath ( 'config' )) 840end ) 841 842it ( "doesn't expand $VARIABLES" , function () 843clear ( { 844env ={ 845XDG_CONFIG_HOME ='$VARIABLES' , 846VARIABLES ='this-should-not-happen' , 847} , 848} ) 849eq ( '$VARIABLES/nvim' , fn . stdpath ( 'config' )) 850end ) 851 852it ( "doesn't expand ~/" , function () 853clear ( { env ={ 854XDG_CONFIG_HOME ='~/frobnitz' , 855} } ) 856eq ( '~/frobnitz/nvim' , fn . stdpath ( 'config' )) 857end ) 858end ) 859 860describe ( 'with "data"' , function () 861it ( 'knows XDG_DATA_HOME' , function () 862clear ( { env ={ 863XDG_DATA_HOME ='/home/docwhat/.local' , 864} } ) 865eq (( '/home/docwhat/.local/%s' ): format ( datadir ), fn . stdpath ( 'data' )) 866end ) 867 868it ( 'handles changes during runtime' , function () 869clear ( { env ={ 870XDG_DATA_HOME ='/home/original' , 871} } ) 872eq (( '/home/original/%s' ): format ( datadir ), fn . stdpath ( 'data' )) 873command ( "let $XDG_DATA_HOME='/home/new'" ) 874eq (( '/home/new/%s' ): format ( datadir ), fn . stdpath ( 'data' )) 875end ) 876 877it ( "doesn't expand $VARIABLES" , function () 878clear ( { 879env ={ 880XDG_DATA_HOME ='$VARIABLES' , 881VARIABLES ='this-should-not-happen' , 882} , 883} ) 884eq (( '$VARIABLES/%s' ): format ( datadir ), fn . stdpath ( 'data' )) 885end ) 886 887it ( "doesn't expand ~/" , function () 888clear ( { env ={ 889XDG_DATA_HOME ='~/frobnitz' , 890} } ) 891eq (( '~/frobnitz/%s' ): format ( datadir ), fn . stdpath ( 'data' )) 892end ) 893end ) 894 895describe ( 'with "state"' , function () 896it ( 'knows XDG_STATE_HOME' , function () 897clear ( { 898env ={ 899XDG_STATE_HOME ='/home/docwhat/.local' , 900} , 901} ) 902eq (( '/home/docwhat/.local/%s' ): format ( statedir ), fn . stdpath ( 'state' )) 903end ) 904 905it ( 'handles changes during runtime' , function () 906clear ( { env ={ 907XDG_STATE_HOME ='/home/original' , 908} } ) 909eq (( '/home/original/%s' ): format ( statedir ), fn . stdpath ( 'state' )) 910command ( "let $XDG_STATE_HOME='" .. '/home/new' .. "'" ) 911eq (( '/home/new/%s' ): format ( statedir ), fn . stdpath ( 'state' )) 912end ) 913 914it ( "doesn't expand $VARIABLES" , function () 915clear ( { 916env ={ 917XDG_STATE_HOME ='$VARIABLES' , 918VARIABLES ='this-should-not-happen' , 919} , 920} ) 921eq (( '$VARIABLES/%s' ): format ( statedir ), fn . stdpath ( 'state' )) 922end ) 923 924it ( "doesn't expand ~/" , function () 925clear ( { env ={ 926XDG_STATE_HOME ='~/frobnitz' , 927} } ) 928eq (( '~/frobnitz/%s' ): format ( statedir ), fn . stdpath ( 'state' )) 929end ) 930end ) 931 932describe ( 'with "cache"' , function () 933it ( 'knows XDG_CACHE_HOME' , function () 934clear ( { 935env ={ 936XDG_CACHE_HOME ='/home/docwhat/.cache' , 937} , 938} ) 939eq ( '/home/docwhat/.cache/nvim' , fn . stdpath ( 'cache' )) 940end ) 941 942it ( 'handles changes during runtime' , function () 943clear ( { env ={ 944XDG_CACHE_HOME ='/home/original' , 945} } ) 946eq ( '/home/original/nvim' , fn . stdpath ( 'cache' )) 947command ( "let $XDG_CACHE_HOME='" .. '/home/new' .. "'" ) 948eq ( '/home/new/nvim' , fn . stdpath ( 'cache' )) 949end ) 950 951it ( "doesn't expand $VARIABLES" , function () 952clear ( { 953env ={ 954XDG_CACHE_HOME ='$VARIABLES' , 955VARIABLES ='this-should-not-happen' , 956} , 957} ) 958eq ( '$VARIABLES/nvim' , fn . stdpath ( 'cache' )) 959end ) 960 961it ( "doesn't expand ~/" , function () 962clear ( { env ={ 963XDG_CACHE_HOME ='~/frobnitz' , 964} } ) 965eq ( '~/frobnitz/nvim' , fn . stdpath ( 'cache' )) 966end ) 967end ) 968end ) 969 970describe ( 'returns a List' , function () 971-- Some OS specific variables the system would have set. 972local function base_env () 973if is_os ( 'win' ) then 974return { 975HOME ='C:\\Users\\docwhat' , -- technically, is not a usual PATH 976HOMEDRIVE ='C:' , 977HOMEPATH ='\\Users\\docwhat' , 978LOCALAPPDATA ='C:\\Users\\docwhat\\AppData\\Local' , 979NVIM_LOG_FILE =testlog , 980TEMP ='C:\\Users\\docwhat\\AppData\\Local\\Temp' , 981TMPDIR ='C:\\Users\\docwhat\\AppData\\Local\\Temp' , 982TMP ='C:\\Users\\docwhat\\AppData\\Local\\Temp' , 983} 984else 985return { 986HOME ='/home/docwhat' , 987HOMEDRIVE ='HOMEDRIVE-should-be-ignored' , 988HOMEPATH ='HOMEPATH-should-be-ignored' , 989LOCALAPPDATA ='LOCALAPPDATA-should-be-ignored' , 990NVIM_LOG_FILE =testlog , 991TEMP ='TEMP-should-be-ignored' , 992TMPDIR ='TMPDIR-should-be-ignored' , 993TMP ='TMP-should-be-ignored' , 994} 995end 996end 997 998local function set_paths_via_system ( var_name , paths ) 999local env =base_env () 1000env [ var_name ] =table . concat ( paths , env_sep ) 1001clear ( { env =env } ) 1002end 1003 1004local function set_paths_at_runtime ( var_name , paths ) 1005clear ( { env =base_env () } ) 1006api . nvim_set_var ( 'env_val' , table . concat ( paths , env_sep )) 1007command (( 'let $%s=g:env_val' ): format ( var_name )) 1008end 1009 1010local function behaves_like_dir_list_env ( msg , stdpath_arg , env_var_name , paths , expected_paths ) 1011describe ( msg , function () 1012it ( 'set via system' , function () 1013set_paths_via_system ( env_var_name , paths ) 1014eq ( expected_paths , fn . stdpath ( stdpath_arg )) 1015if not is_os ( 'win' ) then 1016assert_log ( 1017'$TMPDIR tempdir not a directory[^\n]*TMPDIR%-should%-be%-ignored' , 1018testlog , 1019100 1020) 1021end 1022end ) 1023 1024it ( 'set at runtime' , function () 1025set_paths_at_runtime ( env_var_name , paths ) 1026eq ( expected_paths , fn . stdpath ( stdpath_arg )) 1027if not is_os ( 'win' ) then 1028assert_log ( 1029'$TMPDIR tempdir not a directory[^\n]*TMPDIR%-should%-be%-ignored' , 1030testlog , 1031100 1032) 1033end 1034end ) 1035end ) 1036end 1037 1038describe ( 'with "config_dirs"' , function () 1039behaves_like_dir_list_env ( 1040'handles XDG_CONFIG_DIRS with one path' , 1041'config_dirs' , 1042'XDG_CONFIG_DIRS' , 1043{ 1044'/home/docwhat/.config' , 1045} , 1046{ 1047'/home/docwhat/.config/nvim' , 1048} 1049) 1050 1051behaves_like_dir_list_env ( 1052'handles XDG_CONFIG_DIRS with two paths' , 1053'config_dirs' , 1054'XDG_CONFIG_DIRS' , 1055{ 1056'/home/docwhat/.config' , 1057'/etc/config' , 1058} , 1059{ 1060'/home/docwhat/.config/nvim' , 1061'/etc/config/nvim' , 1062} 1063) 1064 1065behaves_like_dir_list_env ( 1066"doesn't expand $VAR and $IBLES" , 1067'config_dirs' , 1068'XDG_CONFIG_DIRS' , 1069{ '$HOME' , '$TMP' } , 1070{ 1071'$HOME/nvim' , 1072'$TMP/nvim' , 1073} 1074) 1075 1076behaves_like_dir_list_env ( "doesn't expand ~/" , 'config_dirs' , 'XDG_CONFIG_DIRS' , { 1077'~/.oldconfig' , 1078'~/.olderconfig' , 1079} , { 1080'~/.oldconfig/nvim' , 1081'~/.olderconfig/nvim' , 1082} ) 1083end ) 1084 1085describe ( 'with "data_dirs"' , function () 1086behaves_like_dir_list_env ( 'knows XDG_DATA_DIRS with one path' , 'data_dirs' , 'XDG_DATA_DIRS' , { 1087'/home/docwhat/.data' , 1088} , { 1089'/home/docwhat/.data/nvim' , 1090} ) 1091 1092behaves_like_dir_list_env ( 1093'knows XDG_DATA_DIRS with two paths' , 1094'data_dirs' , 1095'XDG_DATA_DIRS' , 1096{ 1097'/home/docwhat/.data' , 1098'/etc/local' , 1099} , 1100{ 1101'/home/docwhat/.data/nvim' , 1102'/etc/local/nvim' , 1103} 1104) 1105 1106behaves_like_dir_list_env ( 1107"doesn't expand $VAR and $IBLES" , 1108'data_dirs' , 1109'XDG_DATA_DIRS' , 1110{ '$HOME' , '$TMP' } , 1111{ 1112'$HOME/nvim' , 1113'$TMP/nvim' , 1114} 1115) 1116 1117behaves_like_dir_list_env ( "doesn't expand ~/" , 'data_dirs' , 'XDG_DATA_DIRS' , { 1118'~/.oldconfig' , 1119'~/.olderconfig' , 1120} , { 1121'~/.oldconfig/nvim' , 1122'~/.olderconfig/nvim' , 1123} ) 1124end ) 1125end ) 1126 1127it ( 'avoids DOS 8.3 filenames for "cache" and "run" #25019' , function () 1128t . skip ( not is_os ( 'win' ), 'N/A: 8.3 filenames are only available on Windows' ) 1129vim . fn . system (( 'fsutil 8dot3name set %s 0' ): format ( n . nvim_dir : sub ( 1 , 2 ))) 1130local dir ='Xtest_temp_path' 1131mkdir ( dir ) 1132finally ( function () 1133rmdir ( dir ) 1134end ) 1135local short_path = 1136vim . fn . system (( 'for %%I in ("%s") do @echo %%~sI' ): format ( dir ), '' ): gsub ( '\n' , '' ) 1137t . matches ( '^XTEST_~[12]$' , vim . fs . basename ( short_path )) 1138clear ( { 1139env ={ 1140TMP =short_path , 1141TEMP =short_path , 1142TMPDIR =short_path , 1143} , 1144} ) 1145t . matches ( dir , fn . stdpath ( 'cache' )) 1146t . matches ( dir , fn . stdpath ( 'run' )) 1147end ) 1148end )