blob: e28d162b84d8dfd73b18b4bc4e071f72df4887d3 [file] [log] [blame]
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001" Functions shared by several tests.
2
3" Get the name of the Python executable.
4" Also keeps it in s:python.
5func PythonProg()
6 " This test requires the Python command to run the test server.
7 " This most likely only works on Unix and Windows.
8 if has('unix')
9 " We also need the job feature or the pkill command to make sure the server
10 " can be stopped.
11 if !(executable('python') && (has('job') || executable('pkill')))
12 return ''
13 endif
14 let s:python = 'python'
15 elseif has('win32')
16 " Use Python Launcher for Windows (py.exe) if available.
17 if executable('py.exe')
18 let s:python = 'py.exe'
19 elseif executable('python.exe')
20 let s:python = 'python.exe'
21 else
22 return ''
23 endif
24 else
25 return ''
26 endif
27 return s:python
28endfunc
29
30" Run "cmd". Returns the job if using a job.
31func RunCommand(cmd)
32 let job = 0
33 if has('job')
34 let job = job_start(a:cmd, {"stoponexit": "hup"})
35 call job_setoptions(job, {"stoponexit": "kill"})
36 elseif has('win32')
37 exe 'silent !start cmd /c start "test_channel" ' . a:cmd
38 else
39 exe 'silent !' . a:cmd . '&'
40 endif
41 return job
42endfunc
43
44" Read the port number from the Xportnr file.
45func GetPort()
46 let l = []
47 for i in range(200)
48 try
49 let l = readfile("Xportnr")
50 catch
51 endtry
52 if len(l) >= 1
53 break
54 endif
55 sleep 10m
56 endfor
57 call delete("Xportnr")
58
59 if len(l) == 0
60 " Can't make the connection, give up.
61 return 0
62 endif
63 return l[0]
64endfunc
65
66" Run a Python server for "cmd" and call "testfunc".
67" Always kills the server before returning.
68func RunServer(cmd, testfunc, args)
69 " The Python program writes the port number in Xportnr.
70 call delete("Xportnr")
71
72 if len(a:args) == 1
73 let arg = ' ' . a:args[0]
74 else
75 let arg = ''
76 endif
77 let pycmd = s:python . " " . a:cmd . arg
78
79 try
80 let g:currentJob = RunCommand(pycmd)
81
82 " Wait for up to 2 seconds for the port number to be there.
83 let port = GetPort()
84 if port == 0
85 call assert_false(1, "Can't start " . a:cmd)
86 return
87 endif
88
89 call call(function(a:testfunc), [port])
90 catch
Bram Moolenaar4b785f62016-11-29 21:54:44 +010091 call assert_false(1, 'Caught exception: "' . v:exception . '" in ' . v:throwpoint)
Bram Moolenaar321efdd2016-07-15 17:09:11 +020092 finally
93 call s:kill_server(a:cmd)
94 endtry
95endfunc
96
97func s:kill_server(cmd)
98 if has('job')
99 if exists('g:currentJob')
100 call job_stop(g:currentJob)
101 unlet g:currentJob
102 endif
103 elseif has('win32')
104 let cmd = substitute(a:cmd, ".py", '', '')
105 call system('taskkill /IM ' . s:python . ' /T /F /FI "WINDOWTITLE eq ' . cmd . '"')
106 else
107 call system("pkill -f " . a:cmd)
108 endif
109endfunc
110
111" Wait for up to a second for "expr" to become true.
Bram Moolenaarf267f8b2016-08-22 21:40:29 +0200112" Return time slept in milliseconds. With the +reltime feature this can be
113" more than the actual waiting time. Without +reltime it can also be less.
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200114func WaitFor(expr)
Bram Moolenaarf267f8b2016-08-22 21:40:29 +0200115 " using reltime() is more accurate, but not always available
116 if has('reltime')
117 let start = reltime()
118 else
119 let slept = 0
120 endif
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200121 for i in range(100)
122 try
123 if eval(a:expr)
Bram Moolenaarf267f8b2016-08-22 21:40:29 +0200124 if has('reltime')
125 return float2nr(reltimefloat(reltime(start)) * 1000)
126 endif
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200127 return slept
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200128 endif
129 catch
130 endtry
Bram Moolenaarf267f8b2016-08-22 21:40:29 +0200131 if !has('reltime')
132 let slept += 10
133 endif
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200134 sleep 10m
135 endfor
Bram Moolenaarf267f8b2016-08-22 21:40:29 +0200136 return 1000
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200137endfunc
Bram Moolenaar66459b72016-08-06 19:01:55 +0200138
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200139" Wait for up to a given milliseconds.
140" With the +timers feature this waits for key-input by getchar(), Resume()
141" feeds key-input and resumes process. Return time waited in milliseconds.
142" Without +timers it uses simply :sleep.
143func Standby(msec)
144 if has('timers')
145 let start = reltime()
146 let g:_standby_timer = timer_start(a:msec, function('s:feedkeys'))
147 call getchar()
148 return float2nr(reltimefloat(reltime(start)) * 1000)
149 else
150 execute 'sleep ' a:msec . 'm'
151 return a:msec
152 endif
153endfunc
154
155func Resume()
156 if exists('g:_standby_timer')
157 call timer_stop(g:_standby_timer)
158 call s:feedkeys(0)
159 unlet g:_standby_timer
160 endif
161endfunc
162
163func s:feedkeys(timer)
164 call feedkeys('x', 'nt')
165endfunc
166
Bram Moolenaar15bf76d2017-03-18 16:18:37 +0100167" Get the command to run Vim, with -u NONE and --not-a-term arguments.
168" Returns an empty string on error.
169func GetVimCommand()
170 if !filereadable('vimcmd')
171 return ''
172 endif
173 let cmd = readfile('vimcmd')[0]
174 let cmd = substitute(cmd, '-u \f\+', '-u NONE', '')
175 if cmd !~ '-u NONE'
176 let cmd = cmd . ' -u NONE'
177 endif
178 let cmd .= ' --not-a-term'
179 let cmd = substitute(cmd, 'VIMRUNTIME=.*VIMRUNTIME;', '', '')
180 return cmd
181endfunc
182
Bram Moolenaar66459b72016-08-06 19:01:55 +0200183" Run Vim, using the "vimcmd" file and "-u NORC".
Bram Moolenaar3a938382016-08-07 16:36:40 +0200184" "before" is a list of Vim commands to be executed before loading plugins.
185" "after" is a list of Vim commands to be executed after loading plugins.
Bram Moolenaar66459b72016-08-06 19:01:55 +0200186" Plugins are not loaded, unless 'loadplugins' is set in "before".
187" Return 1 if Vim could be executed.
Bram Moolenaar472a0a82016-08-06 22:31:42 +0200188func RunVim(before, after, arguments)
Bram Moolenaar7a9a5f42016-08-08 22:34:14 +0200189 return RunVimPiped(a:before, a:after, a:arguments, '')
Bram Moolenaar3a938382016-08-07 16:36:40 +0200190endfunc
191
192func RunVimPiped(before, after, arguments, pipecmd)
Bram Moolenaar15bf76d2017-03-18 16:18:37 +0100193 let cmd = GetVimCommand()
194 if cmd == ''
Bram Moolenaar66459b72016-08-06 19:01:55 +0200195 return 0
196 endif
Bram Moolenaarba98bef2016-08-07 15:51:39 +0200197 let args = ''
Bram Moolenaar472a0a82016-08-06 22:31:42 +0200198 if len(a:before) > 0
199 call writefile(a:before, 'Xbefore.vim')
200 let args .= ' --cmd "so Xbefore.vim"'
201 endif
202 if len(a:after) > 0
203 call writefile(a:after, 'Xafter.vim')
204 let args .= ' -S Xafter.vim'
205 endif
Bram Moolenaar66459b72016-08-06 19:01:55 +0200206
Bram Moolenaar3a938382016-08-07 16:36:40 +0200207 exe "silent !" . a:pipecmd . cmd . args . ' ' . a:arguments
Bram Moolenaar66459b72016-08-06 19:01:55 +0200208
Bram Moolenaar472a0a82016-08-06 22:31:42 +0200209 if len(a:before) > 0
210 call delete('Xbefore.vim')
211 endif
212 if len(a:after) > 0
213 call delete('Xafter.vim')
214 endif
Bram Moolenaar66459b72016-08-06 19:01:55 +0200215 return 1
216endfunc