1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
|
local helpers = require('test.functional.helpers')
local clear, nvim, eq, neq, ok, expect, eval, next_message, run, stop, session
= helpers.clear, helpers.nvim, helpers.eq, helpers.neq, helpers.ok,
helpers.expect, helpers.eval, helpers.next_message, helpers.run,
helpers.stop, helpers.session
local nvim_dir, insert = helpers.nvim_dir, helpers.insert
describe('jobs', function()
local channel
before_each(function()
clear()
channel = nvim('get_api_info')[1]
end)
-- Creates the string to make an autocmd to notify us.
local notify_str = function(expr1, expr2)
local str = "au! JobActivity xxx call rpcnotify("..channel..", "..expr1
if expr2 ~= nil then
str = str..", "..expr2
end
return str..")"
end
local notify_job = function()
return "au! JobActivity xxx call rpcnotify("..channel..", 'j', v:job_data)"
end
it('returns 0 when it fails to start', function()
local status, rv = pcall(eval, "jobstart('', '')")
eq(false, status)
ok(rv ~= nil)
end)
it('calls JobActivity when the job writes and exits', function()
nvim('command', notify_str('v:job_data[1]'))
nvim('command', "call jobstart('xxx', 'echo')")
eq({'notification', 'stdout', {}}, next_message())
eq({'notification', 'exit', {}}, next_message())
end)
it('allows interactive commands', function()
nvim('command', notify_str('v:job_data[1]', 'get(v:job_data, 2)'))
nvim('command', "let j = jobstart('xxx', 'cat', ['-'])")
neq(0, eval('j'))
nvim('command', 'call jobsend(j, "abc\\n")')
eq({'notification', 'stdout', {{'abc', ''}}}, next_message())
nvim('command', 'call jobsend(j, "123\\nxyz\\n")')
eq({'notification', 'stdout', {{'123', 'xyz', ''}}}, next_message())
nvim('command', 'call jobsend(j, [123, "xyz", ""])')
eq({'notification', 'stdout', {{'123', 'xyz', ''}}}, next_message())
nvim('command', "call jobstop(j)")
eq({'notification', 'exit', {0}}, next_message())
end)
it('preserves NULs', function()
-- Make a file with NULs in it.
local filename = os.tmpname()
local file = io.open(filename, "w")
file:write("abc\0def\n")
file:close()
-- v:job_data preserves NULs.
nvim('command', notify_str('v:job_data[1]', 'get(v:job_data, 2)'))
nvim('command', "let j = jobstart('xxx', 'cat', ['"..filename.."'])")
eq({'notification', 'stdout', {{'abc\ndef', ''}}}, next_message())
eq({'notification', 'exit', {0}}, next_message())
os.remove(filename)
-- jobsend() preserves NULs.
nvim('command', "let j = jobstart('xxx', 'cat', ['-'])")
nvim('command', [[call jobsend(j, ["123\n456",""])]])
eq({'notification', 'stdout', {{'123\n456', ''}}}, next_message())
nvim('command', "call jobstop(j)")
end)
it('will not buffer data if it doesnt end in newlines', function()
nvim('command', notify_str('v:job_data[1]', 'get(v:job_data, 2)'))
nvim('command', "let j = jobstart('xxx', 'cat', ['-'])")
nvim('command', 'call jobsend(j, "abc\\nxyz")')
eq({'notification', 'stdout', {{'abc', 'xyz'}}}, next_message())
nvim('command', "call jobstop(j)")
eq({'notification', 'exit', {0}}, next_message())
end)
it('can preserve newlines', function()
nvim('command', notify_str('v:job_data[1]', 'get(v:job_data, 2)'))
nvim('command', "let j = jobstart('xxx', 'cat', ['-'])")
nvim('command', 'call jobsend(j, "a\\n\\nc\\n\\n\\n\\nb\\n\\n")')
eq({'notification', 'stdout', {{'a', '', 'c', '', '', '', 'b', '', ''}}},
next_message())
end)
it('can preserve nuls', function()
nvim('command', notify_str('v:job_data[1]', 'get(v:job_data, 2)'))
nvim('command', "let j = jobstart('xxx', 'cat', ['-'])")
nvim('command', 'call jobsend(j, ["\n123\n", "abc\\nxyz\n", ""])')
eq({'notification', 'stdout', {{'\n123\n', 'abc\nxyz\n', ''}}},
next_message())
nvim('command', "call jobstop(j)")
eq({'notification', 'exit', {0}}, next_message())
end)
it('can avoid sending final newline', function()
nvim('command', notify_str('v:job_data[1]', 'get(v:job_data, 2)'))
nvim('command', "let j = jobstart('xxx', 'cat', ['-'])")
nvim('command', 'call jobsend(j, ["some data", "without\nfinal nl"])')
eq({'notification', 'stdout', {{'some data', 'without\nfinal nl'}}},
next_message())
nvim('command', "call jobstop(j)")
eq({'notification', 'exit', {0}}, next_message())
end)
it('will not allow jobsend/stop on a non-existent job', function()
eq(false, pcall(eval, "jobsend(-1, 'lol')"))
eq(false, pcall(eval, "jobstop(-1)"))
end)
it('will not allow jobstop twice on the same job', function()
nvim('command', "let j = jobstart('xxx', 'cat', ['-'])")
neq(0, eval('j'))
eq(true, pcall(eval, "jobstop(j)"))
eq(false, pcall(eval, "jobstop(j)"))
end)
it('will not cause a memory leak if we leave a job running', function()
nvim('command', "call jobstart('xxx', 'cat', ['-'])")
end)
-- FIXME need to wait until jobsend succeeds before calling jobstop
pending('will only emit the "exit" event after "stdout" and "stderr"', function()
nvim('command', notify_job())
nvim('command', "let j = jobstart('xxx', 'cat', ['-'])")
local jobid = nvim('eval', 'j')
nvim('eval', 'jobsend(j, "abcdef")')
nvim('eval', 'jobstop(j)')
eq({'notification', 'j', {{jobid, 'stdout', {'abcdef'}}}}, next_message())
eq({'notification', 'j', {{jobid, 'exit'}}}, next_message())
end)
describe('running tty-test program', function()
local function next_chunk()
local rv = ''
while true do
local msg = next_message()
local data = msg[3][1]
for i = 1, #data do
data[i] = data[i]:gsub('\n', '\000')
end
rv = table.concat(data, '\n')
rv = rv:gsub('\r\n$', '')
if rv ~= '' then
break
end
end
return rv
end
local function send(str)
nvim('command', 'call jobsend(j, "'..str..'")')
end
before_each(function()
-- the full path to tty-test seems to be required when running on travis.
insert(nvim_dir .. '/tty-test')
nvim('command', 'let exec = expand("<cfile>:p")')
nvim('command', notify_str('v:job_data[1]', 'get(v:job_data, 2)'))
nvim('command', "let j = jobstart('xxx', exec, [], {})")
eq('tty ready', next_chunk())
end)
it('echoing input', function()
send('test')
eq('test', next_chunk())
end)
it('resizing window', function()
nvim('command', 'call jobresize(j, 40, 10)')
eq('rows: 10, cols: 40', next_chunk())
nvim('command', 'call jobresize(j, 10, 40)')
eq('rows: 40, cols: 10', next_chunk())
end)
end)
end)
|