robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
f907614088
master
1-- Tests for autocommands 2-- - FileWritePre writing a compressed file 3-- - FileReadPost reading a compressed file 4-- - BufNewFile reading a file template 5-- - BufReadPre decompressing the file to be read 6-- - FilterReadPre substituting characters in the temp file 7-- - FilterReadPost substituting characters after filtering 8-- - FileReadPre set options for decompression 9-- - FileReadPost decompress the file 10-- Note: This test is skipped if "gzip" is not available. 11-- $GZIP is made empty, "-v" would cause trouble. 12-- Use a FileChangedShell autocommand to avoid a prompt for "Xtestfile.gz" 13-- being modified outside of Vim (noticed on Solaris). 14 15local t =require ( 'test.testutil' ) 16local n =require ( 'test.functional.testnvim' )() 17 18local describe , it , before_each , setup , teardown , pending = 19t . describe , t . it , t . before_each , t . setup , t . teardown , t . pending 20local clear , feed_command , expect , eq , neq , dedent , write_file , feed = 21n . clear , n . feed_command , n . expect , t . eq , t . neq , t . dedent , t . write_file , n . feed 22local command =n . command 23local read_file =t . read_file 24local is_os =t . is_os 25 26local function has_gzip () 27local null =is_os ( 'win' ) and 'nul' or '/dev/null' 28return os . execute ( 'gzip --help >' .. null .. ' 2>&1' ) == 0 29end 30 31local function prepare_gz_file ( name , text ) 32write_file ( name , text .. '\n' ) 33-- Compress the file with gzip. 34command ( [[call system(['gzip', '--force', ']] .. name .. [['])]] ) 35-- This should create the .gz file and delete the original. 36neq ( nil , vim . uv . fs_stat ( name .. '.gz' )) 37eq ( nil , vim . uv . fs_stat ( name )) 38end 39 40local function prepare_file ( name , text ) 41os . remove ( name ) 42write_file ( name , text ) 43end 44 45describe ( 'file reading, writing and bufnew and filter autocommands' , function () 46local text1 =dedent ( [[ 47start of testfile 48line 2 Abcdefghijklmnopqrstuvwxyz 49line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 50line 4 Abcdefghijklmnopqrstuvwxyz 51line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 52line 6 Abcdefghijklmnopqrstuvwxyz 53line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 54line 8 Abcdefghijklmnopqrstuvwxyz 55line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 56line 10 Abcdefghijklmnopqrstuvwxyz 57end of testfile]] ) 58setup ( function () 59write_file ( 60'Xtest.c' , 61[[ 62/* 63* Here is a new .c file 64*/ 65]] 66) 67end ) 68before_each ( function () 69clear ( { env ={ GZIP =nil } } ) 70end ) 71teardown ( function () 72os . remove ( 'Xtestfile.gz' ) 73os . remove ( 'Xtestfile' ) 74os . remove ( 'XtestfileByFileReadPost' ) 75os . remove ( 'Xtest.c' ) 76os . remove ( 'test.out' ) 77end ) 78 79it ( 'FileReadPost' , function () 80feed_command ( 'set bin' ) 81prepare_file ( 'Xtestfile' , text1 ) 82os . remove ( 'XtestfileByFileReadPost' ) 83--execute('au FileChangedShell * echo "caught FileChangedShell"') 84feed_command ( "au FileReadPost Xtestfile '[,']w XtestfileByFileReadPost" ) 85-- Read the testfile. 86feed_command ( '$r Xtestfile' ) 87expect ( '\n' .. text1 ) 88eq ( text1 , read_file ( 'XtestfileByFileReadPost' )) 89end ) 90 91if not has_gzip () then 92pending ( 'skipped (missing `gzip` utility)' , function () end ) 93else 94it ( 'BufReadPre, BufReadPost (using gzip)' , function () 95prepare_gz_file ( 'Xtestfile' , text1 ) 96local gzip_data =read_file ( 'Xtestfile.gz' ) 97-- Setup autocommands to decompress before reading and re-compress afterwards. 98feed_command ( "au BufReadPre *.gz exe '!gzip -d ' . shellescape(expand('<afile>'))" ) 99feed_command ( "au BufReadPre *.gz call rename(expand('<afile>:r'), expand('<afile>'))" ) 100feed_command ( "au BufReadPost *.gz call rename(expand('<afile>'), expand('<afile>:r'))" ) 101feed_command ( "au BufReadPost *.gz exe '!gzip ' . shellescape(expand('<afile>:r'))" ) 102-- Edit compressed file. 103feed_command ( 'e! Xtestfile.gz' ) 104-- Discard all prompts and messages. 105feed ( '<C-L>' ) 106-- Expect the decompressed file in the buffer. 107expect ( text1 ) 108-- Expect the original file to be unchanged. 109eq ( gzip_data , read_file ( 'Xtestfile.gz' )) 110end ) 111 112-- luacheck: ignore 621 (Indentation) 113-- luacheck: ignore 611 (Line contains only whitespaces) 114it ( 'FileReadPre, FileReadPost' , function () 115prepare_gz_file ( 'Xtestfile' , text1 ) 116feed_command ( 117'au! FileReadPre *.gz exe "silent !gzip -d " . shellescape(expand("<afile>"))' 118) 119feed_command ( 'au FileReadPre *.gz call rename(expand("<afile>:r"), expand("<afile>"))' ) 120feed_command ( "au! FileReadPost *.gz '[,']s/l/L/" ) 121-- Read compressed file. 122feed_command ( '$r Xtestfile.gz' ) 123-- Discard all prompts and messages. 124feed ( '<C-L>' ) 125expect ( [[ 126127 start of testfiLe 128Line 2 Abcdefghijklmnopqrstuvwxyz 129Line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 130Line 4 Abcdefghijklmnopqrstuvwxyz 131Line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 132Line 6 Abcdefghijklmnopqrstuvwxyz 133Line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 134Line 8 Abcdefghijklmnopqrstuvwxyz 135Line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 136Line 10 Abcdefghijklmnopqrstuvwxyz 137end of testfiLe]] ) 138end ) 139end 140 141it ( 'FileAppendPre, FileAppendPost' , function () 142feed_command ( 'au BufNewFile *.c read Xtest.c' ) 143-- Will load Xtest.c. 144feed_command ( 'e! foo.c' ) 145feed_command ( "au FileAppendPre *.out '[,']s/new/NEW/" ) 146feed_command ( 'au FileAppendPost *.out !cat Xtest.c >test.out' ) 147-- Append it to the output file. 148feed_command ( 'w>>test.out' ) 149-- Discard all prompts and messages. 150feed ( '<C-L>' ) 151expect ( [[ 152153 /* 154* Here is a NEW .c file 155*/]] ) 156end ) 157 158it ( 'FilterReadPre, FilterReadPost' , function () 159-- Write a special input file for this test block. 160write_file ( 'test.out' , dedent ( [[ 161startstart 162]] ) .. text1 .. dedent ( [[ 163164 165 start of test.c 166/* 167* Here is a new .c file 168*/ 169end of test.c 170]] ) .. text1 .. dedent ( [[ 171172 173 /* 174* Here is a NEW .c file 175*/ 176/* 177* Here is a new .c file 178*/ 179]] ) .. text1 .. dedent ( [[ 180181 /* 182* Here is a new .c file 183*/]] )) 184-- Need temp files here. 185feed_command ( 'set shelltemp' ) 186feed_command ( 187'au FilterReadPre *.out call rename(expand("<afile>"), expand("<afile>") . ".t")' 188) 189feed_command ( 190'au FilterReadPre *.out exe "silent !sed s/e/E/ " . shellescape(expand("<afile>")) . ".t >" . shellescape(expand("<afile>"))' 191) 192feed_command ( 193'au FilterReadPre *.out exe "silent !rm " . shellescape(expand("<afile>")) . ".t"' 194) 195feed_command ( "au FilterReadPost *.out '[,']s/x/X/g" ) 196-- Edit the output file. 197feed_command ( 'e! test.out' ) 198feed_command ( '23,$!cat' ) 199-- Discard all prompts and messages. 200feed ( '<C-L>' ) 201-- Remove CR for when sed adds them. 202feed_command ( [[23,$s/\r$//]] ) 203expect ( [[ 204startstart 205start of testfile 206line 2 Abcdefghijklmnopqrstuvwxyz 207line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 208line 4 Abcdefghijklmnopqrstuvwxyz 209line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 210line 6 Abcdefghijklmnopqrstuvwxyz 211line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 212line 8 Abcdefghijklmnopqrstuvwxyz 213line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 214line 10 Abcdefghijklmnopqrstuvwxyz 215end of testfile 216217 start of test.c 218/* 219* Here is a new .c file 220*/ 221end of test.c 222start of testfile 223line 2 Abcdefghijklmnopqrstuvwxyz 224line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 225line 4 Abcdefghijklmnopqrstuvwxyz 226linE 5 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX 227linE 6 AbcdefghijklmnopqrstuvwXyz 228linE 7 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX 229linE 8 AbcdefghijklmnopqrstuvwXyz 230linE 9 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX 231linE 10 AbcdefghijklmnopqrstuvwXyz 232End of testfile 233234 /* 235* HEre is a NEW .c file 236*/ 237/* 238* HEre is a new .c file 239*/ 240start of tEstfile 241linE 2 AbcdefghijklmnopqrstuvwXyz 242linE 3 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX 243linE 4 AbcdefghijklmnopqrstuvwXyz 244linE 5 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX 245linE 6 AbcdefghijklmnopqrstuvwXyz 246linE 7 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX 247linE 8 AbcdefghijklmnopqrstuvwXyz 248linE 9 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX 249linE 10 AbcdefghijklmnopqrstuvwXyz 250End of testfile 251/* 252* HEre is a new .c file 253*/]] ) 254end ) 255end )