1" Tests for system() and systemlist() 2 3source shared.vim 4source check.vim 5 6func Test_System() 7 if !has('win32') 8 call assert_equal("123\n", system('echo 123')) 9 call assert_equal(['123'], systemlist('echo 123')) 10 call assert_equal('123', system('cat', '123')) 11 call assert_equal(['123'], systemlist('cat', '123')) 12 call assert_equal(["as\<NL>df"], systemlist('cat', ["as\<NL>df"])) 13 else 14 call assert_equal("123\n", system('echo 123')) 15 call assert_equal(["123\r"], systemlist('echo 123')) 16 call assert_equal("123\n", system('more.com', '123')) 17 call assert_equal(["123\r"], systemlist('more.com', '123')) 18 call assert_equal(["as\r", "df\r"], systemlist('more.com', ["as\<NL>df"])) 19 endif 20 21 new Xdummy 22 call setline(1, ['asdf', "pw\<NL>er", 'xxxx']) 23 24 if executable('wc') 25 let out = system('wc -l', bufnr('%')) 26 " On OS/X we get leading spaces 27 let out = substitute(out, '^ *', '', '') 28 call assert_equal("3\n", out) 29 30 let out = systemlist('wc -l', bufnr('%')) 31 " On Windows we may get a trailing CR. 32 if out != ["3\r"] 33 " On OS/X we get leading spaces 34 if type(out) == v:t_list 35 let out[0] = substitute(out[0], '^ *', '', '') 36 endif 37 call assert_equal(['3'], out) 38 endif 39 endif 40 41 if !has('win32') 42 let out = systemlist('cat', bufnr('%')) 43 call assert_equal(['asdf', "pw\<NL>er", 'xxxx'], out) 44 else 45 let out = systemlist('more.com', bufnr('%')) 46 call assert_equal(["asdf\r", "pw\r", "er\r", "xxxx\r"], out) 47 endif 48 bwipe! 49 50 call assert_fails('call system("wc -l", 99999)', 'E86:') 51endfunc 52 53func Test_system_exmode() 54 if has('unix') " echo $? only works on Unix 55 let cmd = ' -es -c "source Xscript" +q; echo "result=$?"' 56 " Need to put this in a script, "catch" isn't found after an unknown 57 " function. 58 call writefile(['try', 'call doesnotexist()', 'catch', 'endtry'], 'Xscript') 59 let a = system(GetVimCommand() . cmd) 60 call assert_match('result=0', a) 61 call assert_equal(0, v:shell_error) 62 endif 63 64 " Error before try does set error flag. 65 call writefile(['call nosuchfunction()', 'try', 'call doesnotexist()', 'catch', 'endtry'], 'Xscript') 66 if has('unix') " echo $? only works on Unix 67 let a = system(GetVimCommand() . cmd) 68 call assert_notequal('0', a[0]) 69 endif 70 71 let cmd = ' -es -c "source Xscript" +q' 72 let a = system(GetVimCommand() . cmd) 73 call assert_notequal(0, v:shell_error) 74 call delete('Xscript') 75 76 if has('unix') " echo $? only works on Unix 77 let cmd = ' -es -c "call doesnotexist()" +q; echo $?' 78 let a = system(GetVimCommand() . cmd) 79 call assert_notequal(0, a[0]) 80 endif 81 82 let cmd = ' -es -c "call doesnotexist()" +q' 83 let a = system(GetVimCommand(). cmd) 84 call assert_notequal(0, v:shell_error) 85 86 if has('unix') " echo $? only works on Unix 87 let cmd = ' -es -c "call doesnotexist()|let a=1" +q; echo $?' 88 let a = system(GetVimCommand() . cmd) 89 call assert_notequal(0, a[0]) 90 endif 91 92 let cmd = ' -es -c "call doesnotexist()|let a=1" +q' 93 let a = system(GetVimCommand() . cmd) 94 call assert_notequal(0, v:shell_error) 95endfunc 96 97func Test_system_with_shell_quote() 98 CheckMSWindows 99 100 call mkdir('Xdir with spaces', 'p') 101 call system('copy "%COMSPEC%" "Xdir with spaces\cmd.exe"') 102 103 let shell_save = &shell 104 let shellxquote_save = &shellxquote 105 try 106 " Set 'shell' always needs noshellslash. 107 let shellslash_save = &shellslash 108 set noshellslash 109 let shell_tests = [ 110 \ expand('$COMSPEC'), 111 \ '"' . fnamemodify('Xdir with spaces\cmd.exe', ':p') . '"', 112 \] 113 let &shellslash = shellslash_save 114 115 let sxq_tests = ['', '(', '"'] 116 117 " Matrix tests: 'shell' * 'shellxquote' 118 for shell in shell_tests 119 let &shell = shell 120 for sxq in sxq_tests 121 let &shellxquote = sxq 122 123 let msg = printf('shell=%s shellxquote=%s', &shell, &shellxquote) 124 125 try 126 let out = 'echo 123'->system() 127 catch 128 call assert_report(printf('%s: %s', msg, v:exception)) 129 continue 130 endtry 131 132 " On Windows we may get a trailing space and CR. 133 if out != "123 \n" 134 call assert_equal("123\n", out, msg) 135 endif 136 137 endfor 138 endfor 139 140 finally 141 let &shell = shell_save 142 let &shellxquote = shellxquote_save 143 call delete('Xdir with spaces', 'rf') 144 endtry 145endfunc 146 147" vim: shiftwidth=2 sts=2 expandtab 148