blob: e69a045522651051a5663cd6b085ec212067bf86 [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 Moolenaar9be0e0b2019-09-28 16:25:00 +020016 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"]))
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
45 let out = systemlist('more', 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.
58 call writefile(['try', 'call doesnotexist()', 'catch', 'endtry'], 'Xscript')
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 Moolenaar2b7bc562017-01-14 19:24:52 +010074 call delete('Xscript')
Bram Moolenaar97d62d42017-01-16 22:53:57 +010075
76 if has('unix') " echo $? only works on Unix
Bram Moolenaar93344c22019-08-14 21:12:05 +020077 let cmd = ' -es -c "call doesnotexist()" +q; echo $?'
78 let a = system(GetVimCommand() . cmd)
Bram Moolenaar97d62d42017-01-16 22:53:57 +010079 call assert_notequal(0, a[0])
80 endif
81
Bram Moolenaar93344c22019-08-14 21:12:05 +020082 let cmd = ' -es -c "call doesnotexist()" +q'
83 let a = system(GetVimCommand(). cmd)
Bram Moolenaar97d62d42017-01-16 22:53:57 +010084 call assert_notequal(0, v:shell_error)
85
86 if has('unix') " echo $? only works on Unix
Bram Moolenaar93344c22019-08-14 21:12:05 +020087 let cmd = ' -es -c "call doesnotexist()|let a=1" +q; echo $?'
88 let a = system(GetVimCommand() . cmd)
Bram Moolenaar97d62d42017-01-16 22:53:57 +010089 call assert_notequal(0, a[0])
90 endif
91
Bram Moolenaar93344c22019-08-14 21:12:05 +020092 let cmd = ' -es -c "call doesnotexist()|let a=1" +q'
93 let a = system(GetVimCommand() . cmd)
Bram Moolenaar97d62d42017-01-16 22:53:57 +010094 call assert_notequal(0, v:shell_error)
Bram Moolenaar2b7bc562017-01-14 19:24:52 +010095endfunc
Bram Moolenaar2efc44b2019-10-05 12:09:32 +020096
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