test_system.vim (5107B)
1 " Tests for system() and systemlist() 2 3 source shared.vim 4 source check.vim 5 6 func Test_System() 7 if !executable('echo') || !executable('cat') || !executable('wc') 8 return 9 endif 10 let out = 'echo 123'->system() 11 call assert_equal("123\n", out) 12 13 let out = 'echo 123'->systemlist() 14 if &shell =~# 'cmd.exe$' 15 call assert_equal(["123\r"], out) 16 else 17 call assert_equal(['123'], out) 18 endif 19 20 call assert_equal('123', system('cat', '123')) 21 call assert_equal(['123'], systemlist('cat', '123')) 22 call assert_equal(["as\<NL>df"], systemlist('cat', ["as\<NL>df"])) 23 24 new Xdummy 25 call setline(1, ['asdf', "pw\<NL>er", 'xxxx']) 26 let out = system('wc -l', bufnr('%')) 27 " On OS/X we get leading spaces 28 let out = substitute(out, '^ *', '', '') 29 call assert_equal("3\n", out) 30 31 let out = systemlist('wc -l', bufnr('%')) 32 " On Windows we may get a trailing CR. 33 if out != ["3\r"] 34 " On OS/X we get leading spaces 35 if type(out) == v:t_list 36 let out[0] = substitute(out[0], '^ *', '', '') 37 endif 38 call assert_equal(['3'], out) 39 endif 40 41 let out = systemlist('cat', bufnr('%')) 42 " On Windows we may get a trailing CR. 43 if out != ["asdf\r", "pw\<NL>er\r", "xxxx\r"] 44 call assert_equal(['asdf', "pw\<NL>er", 'xxxx'], out) 45 endif 46 bwipe! 47 48 call assert_fails('call system("wc -l", 99999)', 'E86:') 49 endfunc 50 51 func Test_system_exmode() 52 if has('unix') " echo $? only works on Unix 53 let cmd = ' -es -c "source Xscript" +q; echo "result=$?"' 54 " Need to put this in a script, "catch" isn't found after an unknown 55 " function. 56 call writefile(['try', 'call doesnotexist()', 'catch', 'endtry'], 'Xscript', 'D') 57 let a = system(GetVimCommand() . cmd) 58 call assert_match('result=0', a) 59 call assert_equal(0, v:shell_error) 60 endif 61 62 " Error before try does set error flag. 63 call writefile(['call nosuchfunction()', 'try', 'call doesnotexist()', 'catch', 'endtry'], 'Xscript') 64 if has('unix') " echo $? only works on Unix 65 let a = system(GetVimCommand() . cmd) 66 call assert_notequal('0', a[0]) 67 endif 68 69 let cmd = ' -es -c "source Xscript" +q' 70 let a = system(GetVimCommand() . cmd) 71 call assert_notequal(0, v:shell_error) 72 73 if has('unix') " echo $? only works on Unix 74 let cmd = ' -es -c "call doesnotexist()" +q; echo $?' 75 let a = system(GetVimCommand() . cmd) 76 call assert_notequal(0, a[0]) 77 endif 78 79 let cmd = ' -es -c "call doesnotexist()" +q' 80 let a = system(GetVimCommand(). cmd) 81 call assert_notequal(0, v:shell_error) 82 83 if has('unix') " echo $? only works on Unix 84 let cmd = ' -es -c "call doesnotexist()|let a=1" +q; echo $?' 85 let a = system(GetVimCommand() . cmd) 86 call assert_notequal(0, a[0]) 87 endif 88 89 let cmd = ' -es -c "call doesnotexist()|let a=1" +q' 90 let a = system(GetVimCommand() . cmd) 91 call assert_notequal(0, v:shell_error) 92 endfunc 93 94 func Test_system_with_shell_quote() 95 CheckMSWindows 96 97 call mkdir('Xdir with spaces', 'p') 98 call system('copy "%COMSPEC%" "Xdir with spaces\cmd.exe"') 99 100 let shell_save = &shell 101 let shellxquote_save = &shellxquote 102 try 103 " Set 'shell' always needs noshellslash. 104 let shellslash_save = &shellslash 105 set noshellslash 106 let shell_tests = [ 107 \ expand('$COMSPEC'), 108 \ '"' . fnamemodify('Xdir with spaces\cmd.exe', ':p') . '"', 109 \] 110 let &shellslash = shellslash_save 111 112 let sxq_tests = ['', '(', '"'] 113 114 " Matrix tests: 'shell' * 'shellxquote' 115 for shell in shell_tests 116 let &shell = shell 117 for sxq in sxq_tests 118 let &shellxquote = sxq 119 120 let msg = printf('shell=%s shellxquote=%s', &shell, &shellxquote) 121 122 try 123 let out = 'echo 123'->system() 124 catch 125 call assert_report(printf('%s: %s', msg, v:exception)) 126 continue 127 endtry 128 129 " On Windows we may get a trailing space and CR. 130 if out != "123 \n" 131 call assert_equal("123\n", out, msg) 132 endif 133 134 endfor 135 endfor 136 137 finally 138 let &shell = shell_save 139 let &shellxquote = shellxquote_save 140 call delete('Xdir with spaces', 'rf') 141 endtry 142 endfunc 143 144 " Check that Vim does not execute anything from current directory 145 func Test_windows_external_cmd_in_cwd() 146 CheckMSWindows 147 148 " just in case 149 call system('rd /S /Q Xfolder') 150 call mkdir('Xfolder', 'R') 151 cd Xfolder 152 153 let contents = ['@echo off', 'echo filename1.txt:1:AAAA'] 154 call writefile(contents, 'findstr.cmd') 155 156 let file1 = ['AAAA', 'THIS FILE SHOULD NOT BE FOUND'] 157 let file2 = ['BBBB', 'THIS FILE SHOULD BE FOUND'] 158 159 call writefile(file1, 'filename1.txt') 160 call writefile(file2, 'filename2.txt') 161 162 if has('quickfix') 163 " use silent to avoid hit-enter-prompt 164 sil grep BBBB filename*.txt 165 call assert_equal('filename2.txt', @%) 166 endif 167 168 let output = system('findstr BBBB filename*') 169 " Match trailing newline byte 170 call assert_match('filename2.txt:BBBB.', output) 171 172 if has('gui') 173 set guioptions+=! 174 let output = system('findstr BBBB filename*') 175 call assert_match('filename2.txt:BBBB.', output) 176 endif 177 178 cd - 179 set guioptions& 180 endfunc 181 182 " vim: shiftwidth=2 sts=2 expandtab