blob: 879eaed3be9eaf0d89d1c9e96c16f0c6d3f97819 [file] [log] [blame]
Bram Moolenaar12c44922017-01-08 13:26:03 +01001" Tests for system() and systemlist()
2
Bram Moolenaar93344c22019-08-14 21:12:05 +02003source shared.vim
Bram Moolenaar2efc44b2019-10-05 12:09:32 +02004source check.vim
Bram Moolenaar93344c22019-08-14 21:12:05 +02005
Bram Moolenaar1e115362019-01-09 23:01:02 +01006func Test_System()
Bram Moolenaar1a613392019-09-28 15:51:37 +02007 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'))
Bram Moolenaar4cc45a32020-07-23 14:51:02 +020016 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"]))
Bram Moolenaar1a613392019-09-28 15:51:37 +020019 endif
20
Bram Moolenaar12c44922017-01-08 13:26:03 +010021 new Xdummy
22 call setline(1, ['asdf', "pw\<NL>er", 'xxxx'])
Bram Moolenaar9d9c3562017-01-08 13:55:06 +010023
Bram Moolenaar077ff432019-10-28 00:42:21 +010024 if executable('wc')
25 let out = system('wc -l', bufnr('%'))
Bram Moolenaar31f19ce2017-01-08 14:14:43 +010026 " On OS/X we get leading spaces
Bram Moolenaar077ff432019-10-28 00:42:21 +010027 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)
Bram Moolenaar31f19ce2017-01-08 14:14:43 +010038 endif
Bram Moolenaar9d9c3562017-01-08 13:55:06 +010039 endif
40
Bram Moolenaar1a613392019-09-28 15:51:37 +020041 if !has('win32')
42 let out = systemlist('cat', bufnr('%'))
43 call assert_equal(['asdf', "pw\<NL>er", 'xxxx'], out)
44 else
Bram Moolenaar4cc45a32020-07-23 14:51:02 +020045 let out = systemlist('more.com', bufnr('%'))
Bram Moolenaar9be0e0b2019-09-28 16:25:00 +020046 call assert_equal(["asdf\r", "pw\r", "er\r", "xxxx\r"], out)
Bram Moolenaar9d9c3562017-01-08 13:55:06 +010047 endif
Bram Moolenaar12c44922017-01-08 13:26:03 +010048 bwipe!
49
50 call assert_fails('call system("wc -l", 99999)', 'E86:')
Bram Moolenaar1e115362019-01-09 23:01:02 +010051endfunc
Bram Moolenaar2b7bc562017-01-14 19:24:52 +010052
Bram Moolenaar1e115362019-01-09 23:01:02 +010053func Test_system_exmode()
Bram Moolenaar97d62d42017-01-16 22:53:57 +010054 if has('unix') " echo $? only works on Unix
Bram Moolenaar93344c22019-08-14 21:12:05 +020055 let cmd = ' -es -c "source Xscript" +q; echo "result=$?"'
Bram Moolenaar97d62d42017-01-16 22:53:57 +010056 " Need to put this in a script, "catch" isn't found after an unknown
57 " function.
Bram Moolenaar56564962022-10-10 22:39:42 +010058 call writefile(['try', 'call doesnotexist()', 'catch', 'endtry'], 'Xscript', 'D')
Bram Moolenaar93344c22019-08-14 21:12:05 +020059 let a = system(GetVimCommand() . cmd)
Bram Moolenaar11e79bb2017-07-08 17:03:21 +020060 call assert_match('result=0', a)
Bram Moolenaar97d62d42017-01-16 22:53:57 +010061 call assert_equal(0, v:shell_error)
Bram Moolenaarfad609d2017-01-14 19:38:36 +010062 endif
63
Bram Moolenaar2b7bc562017-01-14 19:24:52 +010064 " Error before try does set error flag.
65 call writefile(['call nosuchfunction()', 'try', 'call doesnotexist()', 'catch', 'endtry'], 'Xscript')
Bram Moolenaar97d62d42017-01-16 22:53:57 +010066 if has('unix') " echo $? only works on Unix
Bram Moolenaar93344c22019-08-14 21:12:05 +020067 let a = system(GetVimCommand() . cmd)
Bram Moolenaar97d62d42017-01-16 22:53:57 +010068 call assert_notequal('0', a[0])
69 endif
Bram Moolenaar2b7bc562017-01-14 19:24:52 +010070
Bram Moolenaar93344c22019-08-14 21:12:05 +020071 let cmd = ' -es -c "source Xscript" +q'
72 let a = system(GetVimCommand() . cmd)
Bram Moolenaar2b7bc562017-01-14 19:24:52 +010073 call assert_notequal(0, v:shell_error)
Bram Moolenaar97d62d42017-01-16 22:53:57 +010074
75 if has('unix') " echo $? only works on Unix
Bram Moolenaar93344c22019-08-14 21:12:05 +020076 let cmd = ' -es -c "call doesnotexist()" +q; echo $?'
77 let a = system(GetVimCommand() . cmd)
Bram Moolenaar97d62d42017-01-16 22:53:57 +010078 call assert_notequal(0, a[0])
79 endif
80
Bram Moolenaar93344c22019-08-14 21:12:05 +020081 let cmd = ' -es -c "call doesnotexist()" +q'
82 let a = system(GetVimCommand(). cmd)
Bram Moolenaar97d62d42017-01-16 22:53:57 +010083 call assert_notequal(0, v:shell_error)
84
85 if has('unix') " echo $? only works on Unix
Bram Moolenaar93344c22019-08-14 21:12:05 +020086 let cmd = ' -es -c "call doesnotexist()|let a=1" +q; echo $?'
87 let a = system(GetVimCommand() . cmd)
Bram Moolenaar97d62d42017-01-16 22:53:57 +010088 call assert_notequal(0, a[0])
89 endif
90
Bram Moolenaar93344c22019-08-14 21:12:05 +020091 let cmd = ' -es -c "call doesnotexist()|let a=1" +q'
92 let a = system(GetVimCommand() . cmd)
Bram Moolenaar97d62d42017-01-16 22:53:57 +010093 call assert_notequal(0, v:shell_error)
Bram Moolenaar2b7bc562017-01-14 19:24:52 +010094endfunc
Bram Moolenaar2efc44b2019-10-05 12:09:32 +020095
96func Test_system_with_shell_quote()
97 CheckMSWindows
98
99 call mkdir('Xdir with spaces', 'p')
100 call system('copy "%COMSPEC%" "Xdir with spaces\cmd.exe"')
101
102 let shell_save = &shell
103 let shellxquote_save = &shellxquote
104 try
105 " Set 'shell' always needs noshellslash.
106 let shellslash_save = &shellslash
107 set noshellslash
108 let shell_tests = [
109 \ expand('$COMSPEC'),
110 \ '"' . fnamemodify('Xdir with spaces\cmd.exe', ':p') . '"',
111 \]
112 let &shellslash = shellslash_save
113
114 let sxq_tests = ['', '(', '"']
115
116 " Matrix tests: 'shell' * 'shellxquote'
117 for shell in shell_tests
118 let &shell = shell
119 for sxq in sxq_tests
120 let &shellxquote = sxq
121
122 let msg = printf('shell=%s shellxquote=%s', &shell, &shellxquote)
123
124 try
125 let out = 'echo 123'->system()
126 catch
127 call assert_report(printf('%s: %s', msg, v:exception))
128 continue
129 endtry
130
131 " On Windows we may get a trailing space and CR.
132 if out != "123 \n"
133 call assert_equal("123\n", out, msg)
134 endif
135
136 endfor
137 endfor
138
139 finally
140 let &shell = shell_save
141 let &shellxquote = shellxquote_save
142 call delete('Xdir with spaces', 'rf')
143 endtry
144endfunc
Bram Moolenaar0ff5ded2020-05-07 18:43:44 +0200145
Bram Moolenaar0ff5ded2020-05-07 18:43:44 +0200146" vim: shiftwidth=2 sts=2 expandtab