robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
7d0adc08f7
master
1" Tests for the Python omni-completion plugin (runtime/autoload/python3complete.vim). 2" 3CheckFeature python3 4 5" Run omni-completion against the given buffer contents and assert that the 6" marker file was not created. Pre-patch behaviour exec()s reconstructed 7" def/class headers, which evaluates the buffer-supplied expression and 8" creates the marker file. Post-patch, the expressions are stripped. 9func s: CompleteAndExpectNoMarker ( buffer_lines , marker_path , msg ) 10call delete ( a: marker_path ) 11defer delete(a:marker_path) 12let g: pythoncomplete_allow_import = 0 13new 14setfiletype python 15call setline ( 1 , a: buffer_lines ) 16call cursor ( line ( '$' ), col ([ line ( '$' ), '$' ])) 17 18" The PoC trigger -- direct invocation of the omnifunc with an empty base. 19" This is the same path Vim takes for CTRL-X CTRL-O. 20silent ! call python3complete#Complete ( 0 , '' ) 21 22call assert_false ( filereadable ( a: marker_path ), 23 \a: msg . ' (marker ' . a: marker_path . ' was created)' ) 24 25bwipe ! 26unlet ! g: pythoncomplete_allow_import 27endfunc 28 29func Test_python3complete_no_exec_via_function_default () 30let marker = tempname () 31call s: CompleteAndExpectNoMarker ([ 32 \'def f(x=open(' . string ( marker ) . ', "w").close()):' , 33 \' pass' , 34 \'f.' , 35 \], marker , 36 \'function default expression was evaluated during omni-completion' ) 37endfunc 38 39func Test_python3complete_no_exec_via_function_annotation () 40let marker = tempname () 41call s: CompleteAndExpectNoMarker ([ 42 \'def f(x: open(' . string ( marker ) . ', "w").close()):' , 43 \' pass' , 44 \'f.' , 45 \], marker , 46 \'function annotation expression was evaluated during omni-completion' ) 47endfunc 48 49func Test_python3complete_no_exec_via_class_base () 50let marker = tempname () 51" "or object" gives the class a valid base after the side-effecting 52" open().close() expression returns None. Without "or object" the 53" exec would raise TypeError, but the file would still be created 54" before the exception -- the assertion would still hold. Using 55" "or object" keeps the buffer parseable as valid Python. 56call s: CompleteAndExpectNoMarker ([ 57 \'class Foo(open(' . string ( marker ) . ', "w").close() or object):' , 58 \' pass' , 59 \'Foo.' , 60 \], marker , 61 \'class base expression was evaluated during omni-completion' ) 62endfunc 63 64func Test_python3complete_no_exec_with_multiple_params () 65" The strip must apply to every parameter, not just the first. 66let marker = tempname () 67call s: CompleteAndExpectNoMarker ([ 68 \'def f(a, b=1, c=open(' . string ( marker ) . ', "w").close(), d=2):' , 69 \' pass' , 70 \'f.' , 71 \], marker , 72 \'non-first parameter default was evaluated during omni-completion' ) 73endfunc 74 75func Test_python3complete_no_exec_via_starargs_default () 76" "*args" and "**kw" must still be preserved after stripping; ensure a 77" default following them is also stripped. 78let marker = tempname () 79call s: CompleteAndExpectNoMarker ([ 80 \'def f(*args, key=open(' . string ( marker ) . ', "w").close(), **kw):' , 81 \' pass' , 82 \'f.' , 83 \], marker , 84 \'keyword-only default after *args was evaluated during omni-completion' ) 85endfunc 86 87func Test_python3complete_normal_completion_still_works () 88" Positive control: completion against a buffer with a legitimate class 89" must still produce completion items. The stripping logic should not 90" break the normal completion path. 91let g: pythoncomplete_allow_import = 0 92 93new 94setfiletype python 95call setline ( 1 , [ 96 \'class MyHelper:' , 97 \' def alpha(self): pass' , 98 \' def beta(self): pass' , 99 \'h = MyHelper()' , 100 \'h.' , 101 \]) 102call cursor ( 5 , 3 ) 103 104" First call returns the column to start completion at; second returns 105" the list of completion items. 106let start = python3complete#Complete ( 1 , '' ) 107call assert_true ( start >= 0 , 108 \'python3complete#Complete(1, "") returned ' . start ) 109 110let items = python3complete#Complete ( 0 , '' ) 111" Items should be a list (possibly empty if the parser can't resolve "h", 112" but should not be a parse error from our stripping changes). 113call assert_equal ( type ([]), type ( items ), 114 \'python3complete#Complete(0, "") did not return a list' ) 115 116bwipe ! 117unlet ! g: pythoncomplete_allow_import 118endfunc 119 120func Test_python3complete_inherited_completion_via_dotted_base () 121" Positive control for the class-base whitelist: a dotted-name base class 122" (the common, safe case) must still be carried into the reconstructed 123" source so that completion on a subclass can resolve inherited members. 124let g: pythoncomplete_allow_import = 0 125 126new 127setfiletype python 128call setline ( 1 , [ 129 \'class Base:' , 130 \' def shared(self): pass' , 131 \'class Derived(Base):' , 132 \' def own(self): pass' , 133 \'d = Derived()' , 134 \'d.' , 135 \]) 136call cursor ( 6 , 3 ) 137 138let items = python3complete#Complete ( 0 , '' ) 139call assert_equal ( type ([]), type ( items ), 140 \'completion against a subclass with a dotted base did not return a list' ) 141 142bwipe ! 143unlet ! g: pythoncomplete_allow_import 144endfunc 145 146" Build a tiny Python module that creates a marker file as a side effect of 147" being imported, add its directory to sys.path, run omni-completion against 148" a buffer containing `import vimtest_marker_mod`, and report whether the 149" marker file was created. Used by the two allow_import tests below. 150func s: RunImportCompletion ( allow_import_value ) 151let g: pythoncomplete_allow_import = a: allow_import_value 152let marker = tempname () 153let module_dir = tempname () 154call mkdir ( module_dir , 'R' ) 155 156call writefile ([ 157 \'open(' . string ( marker ) . ', "w").close()' , 158 \], module_dir . '/vimtest_marker_mod.py' ) 159 160defer delete(marker) 161 162" Pass module_dir to Python via a g: variable so vim.eval() can read it. 163let g: pythoncomplete_test_module_dir = module_dir 164py 3 << EOF 165import sys, vim 166_p = vim.eval ('g: pythoncomplete_test_module_dir') 167if _p not in sys.path: 168sys .path.insert(0, _p) 169#Drop any cached copy so the module body re-runs and the marker side 170# effect fires on import. 171sys .modules.pop('vimtest_marker_mod', None) 172EOF 173 174new 175setfiletype python 176call setline ( 1 , [ 177 \'import vimtest_marker_mod' , 178 \'vimtest_marker_mod.' , 179 \]) 180call cursor ( 2 , 2 ) 181 182silent ! call python3complete#Complete ( 0 , '' ) 183 184let ran = filereadable ( marker ) 185 186bwipe ! 187unlet g: pythoncomplete_allow_import 188 189" Teardown: restore sys.path, drop the cached module so a subsequent 190" test run starts clean, clean up the temp module dir. 191py 3 << EOF 192import sys, vim 193_p = vim.eval ('g: pythoncomplete_test_module_dir') 194if _p in sys.path: 195sys .path.remove(_p) 196sys .modules.pop('vimtest_marker_mod', None) 197EOF 198unlet g: pythoncomplete_test_module_dir 199call delete ( module_dir , 'rf' ) 200call delete ( marker ) 201unlet ! g: pythoncomplete_allow_import 202 203return ran 204endfunc 205 206func Test_python3complete_allow_import_off_blocks_imports () 207" GHSA-52mc-rq6p-rc7c mitigation: with the default flag value (0), an 208" `import` line harvested from the buffer must NOT be exec()'d. The 209" marker module's side effect (creating a file when its body runs) is 210" the observable proof that the exec did or did not happen. 211call assert_false ( s: RunImportCompletion ( 0 ), 212 \'g:pythoncomplete_allow_import=0 did not block the buffer import' ) 213endfunc 214 215func Test_python3complete_allow_import_on_runs_imports () 216" Symmetric positive control: with the flag set to non-zero, the harvested 217" import IS exec()'d and the module loads. Without this control the 218" negative test above could pass for unrelated reasons (e.g. completion 219" failing to parse the buffer at all). 220call assert_true ( s: RunImportCompletion ( 1 ), 221 \'g:pythoncomplete_allow_import=1 did not run the buffer import' ) 222endfunc 223 224func Test_python3complete_no_exec_via_class_docstring () 225" A class-body docstring is emitted verbatim between triple quotes by 226" get_code() and runs at class-definition time during exec(). A single- 227" quoted source docstring lets an embedded """ survive doc()'s leading/ 228" trailing quote strip and break out of the generated literal. 229let marker = tempname () 230call s: CompleteAndExpectNoMarker ([ 231 \'class Foo:' , 232 \' ''x"""+open("' . marker . '", "w").close()+"""y''' , 233 \' pass' , 234 \'Foo.' , 235 \], marker , 236 \'class docstring expression was evaluated during omni-completion' ) 237endfunc 238 239" vim: shiftwidth=2 sts=2 expandtab