Skip to content

Commit

Permalink
Getting exit status from job with job-control
Browse files Browse the repository at this point in the history
Fixes #1795 by making v:job_data[2] be the exit status when the
"activity" is "exit".

Added test for non-zero job statuses and fixed another test

Test error codes up to 255
  • Loading branch information
Paul Harper authored and benekastah committed Jan 26, 2015
1 parent 65adcc2 commit 4ef2fb2
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 6 deletions.
5 changes: 3 additions & 2 deletions runtime/doc/job_control.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ event. The best way to understand is with a complete example:
elseif v:job_data[1] == 'stderr'
let str = 'shell '.v:job_data[0].' stderr: '.join(v:job_data[2])
else
let str = 'shell '.v:job_data[0].' exited'
let str = 'shell '.v:job_data[0].' exited with status '.v:job_data[2]
endif
call append(line('$'), str)
Expand Down Expand Up @@ -81,7 +81,8 @@ Here's what is happening:
0: The job id
1: The kind of activity: one of "stdout", "stderr" or "exit"
2: When "activity" is "stdout" or "stderr", this will contain a list of
lines read from stdout or stderr
lines read from stdout or stderr. When "activity" is "exit", this will
contain the exit status of the job.

To send data to the job's stdin, one can use the |jobsend()| function, like
this:
Expand Down
11 changes: 8 additions & 3 deletions src/nvim/eval.c
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ static dictitem_T vimvars_var; /* variable used for v: */

// Memory pool for reusing JobEvent structures
typedef struct {
int id;
int id, status;
char *name, *type, *received;
size_t received_len;
} JobEvent;
Expand Down Expand Up @@ -19765,6 +19765,7 @@ char_u *do_string_sub(char_u *str, char_u *pat, char_u *sub, char_u *flags)
rstream_read(r, event_data->received, read_count); \
} \
event_data->id = job_id(j); \
event_data->status = job_status(j); \
event_data->name = job_data(j); \
event_data->type = t; \
event_push((Event) { \
Expand Down Expand Up @@ -19796,12 +19797,14 @@ static void on_job_event(Event event)
{
JobEvent *data = event.data;
apply_job_autocmds(data->id, data->name, data->type,
data->received, data->received_len);
data->received, data->received_len,
data->status);
kmp_free(JobEventPool, job_event_pool, data);
}

static void apply_job_autocmds(int id, char *name, char *type,
char *received, size_t received_len)
char *received, size_t received_len,
int status)
{
// Create the list which will be set to v:job_data
list_T *list = list_alloc();
Expand All @@ -19818,6 +19821,8 @@ static void apply_job_autocmds(int id, char *name, char *type,
list_append(list, str_slot);

free(received);
} else {
list_append_number(list, status);
}

// Update v:job_data for the autocommands
Expand Down
9 changes: 9 additions & 0 deletions src/nvim/os/job.c
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,15 @@ int job_id(Job *job)
return job->id;
}

/// Get the job status
///
/// @param job A pointer to the job
/// @return The job status
int job_status(Job *job)
{
return job->status;
}

/// Get data associated with a job
///
/// @param job A pointer to the job
Expand Down
13 changes: 12 additions & 1 deletion test/functional/job/job_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,17 @@ describe('jobs', function()
nvim('eval', 'jobsend(j, "abcdef")')
nvim('eval', 'jobstop(j)')
eq({'notification', 'j', {{jobid, 'stdout', {'abcdef'}}}}, next_message())
eq({'notification', 'j', {{jobid, 'exit'}}}, next_message())
eq({'notification', 'j', {{jobid, 'exit', 0}}}, next_message())
end)

it('will return the proper exit status for the job', function()
nvim('command', notify_job())
nvim('command', "let j = jobstart('xxx', 'cat', ['/dev/null/fake'])")
local jobid = nvim('eval', 'j')
next_message()
eq({'notification', 'j', {{jobid, 'exit', 1}}}, next_message())
nvim('command', "let j = jobstart('xxx', 'bash', ['-c', 'exit 255'])")
local jobid = nvim('eval', 'j')
eq({'notification', 'j', {{jobid, 'exit', 255}}}, next_message())
end)
end)

0 comments on commit 4ef2fb2

Please sign in to comment.