robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
de10d10d3f
master
1" Test for checking the source code style. 2 3func s: ReportError ( fname , lnum , msg ) 4if a: lnum > 0 5call assert_report ( a: fname .. ' line ' .. a: lnum .. ': ' .. a: msg ) 6endif 7endfunc 8 9func s: PerformCheck ( fname , pattern , msg , skip ) 10let prev_lnum = 1 11let lnum = 1 12while ( lnum > 0 ) 13call cursor ( lnum , 1 ) 14let lnum = search ( a: pattern , 'W' , 0 , 0 , a: skip ) 15if ( prev_lnum == lnum ) 16break 17endif 18let prev_lnum = lnum 19if ( lnum > 0 ) 20call s: ReportError ( a: fname , lnum , a: msg ) 21endif 22endwhile 23endfunc 24 25func Test_test_files () 26for fname in glob ( '*.vim' , 0 , 1 ) 27let g: ignoreSwapExists = 'e' 28exe 'edit ' .. fname 29 30" some files intentionally have misplaced white space 31if fname =~ 'test_cindent.vim' || fname =~ 'test_join.vim' 32continue 33endif 34 35" skip files that are known to have a space before a tab 36if fname !~ 'test_comments.vim' 37 \&& fname !~ 'test_listchars.vim' 38 \&& fname !~ 'test_visual.vim' 39call cursor ( 1 , 1 ) 40let skip = 'getline(".") =~ "codestyle: ignore"' 41let lnum = search ( fname =~ "test_regexp_latin" ? '[^á] \t' : ' \t' , 'W' , 0 , 0 , skip ) 42call s: ReportError ( 'testdir/' .. fname , lnum , 'space before Tab' ) 43endif 44 45" skip files that are known to have trailing white space 46if fname !~ 'test_cmdline.vim' 47 \&& fname !~ 'test_let.vim' 48 \&& fname !~ 'test_tagjump.vim' 49 \&& fname !~ 'test_vim9_cmd.vim' 50call cursor ( 1 , 1 ) 51let lnum = search ( 52 \fname =~ 'test_vim9_assign.vim' ? '[^=]\s$' 53 \: fname =~ 'test_vim9_class.vim' ? '[^)]\s$' 54 \: fname =~ 'test_vim9_script.vim' ? '[^,:3]\s$' 55 \: fname =~ 'test_visual.vim' ? '[^/]\s$' 56 \: '[^\\]\s$' ) 57call s: ReportError ( 'testdir/' .. fname , lnum , 'trailing white space' ) 58endif 59endfor 60 61bwipe ! 62endfunc 63 64func Test_help_files () 65set nowrapscan 66 67for fpath in glob ( '../../../runtime/doc/*.txt' , 0 , 1 ) 68let g: ignoreSwapExists = 'e' 69exe 'edit ' .. fpath 70 71let fname = fnamemodify ( fpath , ":t" ) 72 73" todo.txt is for developers, it's not need a strictly check 74" version*.txt is a history and large size, so it's not checked 75if fname == 'todo.txt' || fname =~ 'version.*\.txt' 76continue 77endif 78 79" Check for mixed tabs and spaces 80call cursor ( 1 , 1 ) 81while 1 82let lnum = search ( '[^/] \t' ) 83if fname == 'visual.txt' && getline ( lnum ) =~ "STRING \tjkl" 84 \|| fname == 'usr_27.txt' && getline ( lnum ) =~ "\[^\? \t\]" 85continue 86endif 87call s: ReportError ( fpath , lnum , 'space before tab' ) 88if lnum == 0 89break 90endif 91endwhile 92 93" Check for unnecessary whitespace at the end of a line 94call cursor ( 1 , 1 ) 95while 1 96let lnum = search ( '\%([^/~\\]\|^\)\s\+$' ) 97" skip line that are known to have trailing white space 98if fname == 'map.txt' && getline ( lnum ) =~ "unmap @@ $" 99 \|| fname == 'usr_12.txt' && getline ( lnum ) =~ "^\t/ \t$" 100 \|| fname == 'usr_41.txt' && getline ( lnum ) =~ "map <F4> o#include $" 101 \|| fname == 'change.txt' && getline ( lnum ) =~ "foobar bla $" 102continue 103endif 104call s: ReportError ( 'testdir' .. fpath , lnum , 'trailing white space' ) 105if lnum == 0 106break 107endif 108endwhile 109 110 111endfor 112 113set wrapscan & vim 114bwipe ! 115endfunc 116 117 118func Test_runtime_wrong_shellescape () 119" Check that shellescape() is called with the {special} argument (a second, 120" non-zero argument) when its result is used in a ":!" ex command. 121" This could cause code injection! 122let pattern = '\<shellescape(\%([^,()]\|([^()]*)\)\+)' 123 124let q = "['" .. '"]' 125let bang_exe = '\<\%(exe\%[cute]\|sil\%[ent]\)\>.*' .. q .. '[^"' .. "']*!" 126 127let skip = 'getline(".") !~ ' .. string ( bang_exe ) 128 \.. ' || getline(".") =~ ' .. string ( '\<system\%(list\)\=(' ) 129 \.. ' || getline(".") =~ ' .. string ( '^\s*"' ) 130 131for fpath in glob ( '../../../runtime/**/*.vim' , 0 , 1 ) 132let g: ignoreSwapExists = 'e' 133exe 'edit ' .. fpath 134call s: PerformCheck ( fpath , pattern , 135 \'shellescape() without {special} flag used in ":!" command' , skip ) 136endfor 137 138: %bwipe ! 139endfunc 140 141" vim: shiftwidth=2 sts=2 expandtab