Bram Moolenaar | 12c4492 | 2017-01-08 13:26:03 +0100 | [diff] [blame] | 1 | " Tests for system() and systemlist() |
| 2 | |
| 3 | function! Test_System() |
| 4 | if !executable('echo') || !executable('cat') || !executable('wc') |
| 5 | return |
| 6 | endif |
Bram Moolenaar | 9d9c356 | 2017-01-08 13:55:06 +0100 | [diff] [blame] | 7 | let out = system('echo 123') |
| 8 | " On Windows we may get a trailing space. |
| 9 | if out != "123 \n" |
| 10 | call assert_equal("123\n", out) |
| 11 | endif |
| 12 | |
| 13 | let out = systemlist('echo 123') |
| 14 | " On Windows we may get a trailing space and CR. |
| 15 | if out != ["123 \r"] |
| 16 | call assert_equal(['123'], out) |
| 17 | endif |
| 18 | |
Bram Moolenaar | 12c4492 | 2017-01-08 13:26:03 +0100 | [diff] [blame] | 19 | call assert_equal('123', system('cat', '123')) |
| 20 | call assert_equal(['123'], systemlist('cat', '123')) |
| 21 | call assert_equal(["as\<NL>df"], systemlist('cat', ["as\<NL>df"])) |
Bram Moolenaar | 31f19ce | 2017-01-08 14:14:43 +0100 | [diff] [blame] | 22 | |
Bram Moolenaar | 12c4492 | 2017-01-08 13:26:03 +0100 | [diff] [blame] | 23 | new Xdummy |
| 24 | call setline(1, ['asdf', "pw\<NL>er", 'xxxx']) |
Bram Moolenaar | 31f19ce | 2017-01-08 14:14:43 +0100 | [diff] [blame] | 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) |
Bram Moolenaar | 9d9c356 | 2017-01-08 13:55:06 +0100 | [diff] [blame] | 29 | |
| 30 | let out = systemlist('wc -l', bufnr('%')) |
| 31 | " On Windows we may get a trailing CR. |
| 32 | if out != ["3\r"] |
Bram Moolenaar | 31f19ce | 2017-01-08 14:14:43 +0100 | [diff] [blame] | 33 | " On OS/X we get leading spaces |
| 34 | if type(out) == v:t_list |
| 35 | let out[0] = substitute(out[0], '^ *', '', '') |
| 36 | endif |
Bram Moolenaar | 9d9c356 | 2017-01-08 13:55:06 +0100 | [diff] [blame] | 37 | call assert_equal(['3'], out) |
| 38 | endif |
| 39 | |
| 40 | let out = systemlist('cat', bufnr('%')) |
| 41 | " On Windows we may get a trailing CR. |
| 42 | if out != ["asdf\r", "pw\<NL>er\r", "xxxx\r"] |
| 43 | call assert_equal(['asdf', "pw\<NL>er", 'xxxx'], out) |
| 44 | endif |
Bram Moolenaar | 12c4492 | 2017-01-08 13:26:03 +0100 | [diff] [blame] | 45 | bwipe! |
| 46 | |
| 47 | call assert_fails('call system("wc -l", 99999)', 'E86:') |
| 48 | endfunction |
Bram Moolenaar | 2b7bc56 | 2017-01-14 19:24:52 +0100 | [diff] [blame] | 49 | |
| 50 | function! Test_system_exmode() |
Bram Moolenaar | 97d62d4 | 2017-01-16 22:53:57 +0100 | [diff] [blame] | 51 | if has('unix') " echo $? only works on Unix |
| 52 | let cmd = ' -es -u NONE -c "source Xscript" +q; echo $?' |
| 53 | " Need to put this in a script, "catch" isn't found after an unknown |
| 54 | " function. |
| 55 | call writefile(['try', 'call doesnotexist()', 'catch', 'endtry'], 'Xscript') |
| 56 | let a = system(v:progpath . cmd) |
| 57 | call assert_equal('0', a[0]) |
| 58 | call assert_equal(0, v:shell_error) |
Bram Moolenaar | fad609d | 2017-01-14 19:38:36 +0100 | [diff] [blame] | 59 | endif |
| 60 | |
Bram Moolenaar | 2b7bc56 | 2017-01-14 19:24:52 +0100 | [diff] [blame] | 61 | " Error before try does set error flag. |
| 62 | call writefile(['call nosuchfunction()', 'try', 'call doesnotexist()', 'catch', 'endtry'], 'Xscript') |
Bram Moolenaar | 97d62d4 | 2017-01-16 22:53:57 +0100 | [diff] [blame] | 63 | if has('unix') " echo $? only works on Unix |
| 64 | let a = system(v:progpath . cmd) |
| 65 | call assert_notequal('0', a[0]) |
| 66 | endif |
Bram Moolenaar | 2b7bc56 | 2017-01-14 19:24:52 +0100 | [diff] [blame] | 67 | |
Bram Moolenaar | 97d62d4 | 2017-01-16 22:53:57 +0100 | [diff] [blame] | 68 | let cmd = ' -es -u NONE -c "source Xscript" +q' |
Bram Moolenaar | 2b7bc56 | 2017-01-14 19:24:52 +0100 | [diff] [blame] | 69 | let a = system(v:progpath . cmd) |
| 70 | call assert_notequal(0, v:shell_error) |
Bram Moolenaar | 2b7bc56 | 2017-01-14 19:24:52 +0100 | [diff] [blame] | 71 | call delete('Xscript') |
Bram Moolenaar | 97d62d4 | 2017-01-16 22:53:57 +0100 | [diff] [blame] | 72 | |
| 73 | if has('unix') " echo $? only works on Unix |
| 74 | let cmd = ' -es -u NONE -c "call doesnotexist()" +q; echo $?' |
| 75 | let a = system(v:progpath. cmd) |
| 76 | call assert_notequal(0, a[0]) |
| 77 | endif |
| 78 | |
| 79 | let cmd = ' -es -u NONE -c "call doesnotexist()" +q' |
| 80 | let a = system(v:progpath. cmd) |
| 81 | call assert_notequal(0, v:shell_error) |
| 82 | |
| 83 | if has('unix') " echo $? only works on Unix |
| 84 | let cmd = ' -es -u NONE -c "call doesnotexist()|let a=1" +q; echo $?' |
| 85 | let a = system(v:progpath. cmd) |
| 86 | call assert_notequal(0, a[0]) |
| 87 | endif |
| 88 | |
| 89 | let cmd = ' -es -u NONE -c "call doesnotexist()|let a=1" +q' |
| 90 | let a = system(v:progpath. cmd) |
| 91 | call assert_notequal(0, v:shell_error) |
Bram Moolenaar | 2b7bc56 | 2017-01-14 19:24:52 +0100 | [diff] [blame] | 92 | endfunc |