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