Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mock stdline and errline #462

Merged
merged 6 commits into from
Dec 12, 2018
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions node/mock-toolrunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,13 @@ export class ToolRunner extends events.EventEmitter {
if (!ops.silent) {
ops.outStream.write(res.stdout + os.EOL);
}
const stdLineArray = res.stdout.split(os.EOL);
for (const line of stdLineArray.slice(0, -1)) {
this.emit('stdline', line);
}
if(stdLineArray.length > 0 && stdLineArray[stdLineArray.length - 1].length > 0) {
this.emit('stdline', stdLineArray[stdLineArray.length - 1]);
}
}

if (res.stderr) {
Expand All @@ -238,6 +245,13 @@ export class ToolRunner extends events.EventEmitter {
var s = ops.failOnStdErr ? ops.errStream : ops.outStream;
s.write(res.stderr + os.EOL);
}
const stdErrArray = res.stderr.split(os.EOL);
for (const line of stdErrArray.slice(0, -1)) {
this.emit('errline', line);
}
if (stdErrArray.length > 0 && stdErrArray[stdErrArray.length - 1].length > 0) {
this.emit('errline', stdErrArray[stdErrArray.length - 1]);
}
}


Expand Down
98 changes: 97 additions & 1 deletion node/test/mocktests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import * as mtr from '../_build/mock-toolrunner';
import * as ma from '../_build/mock-answer';
import * as tl from '../_build/task';

import os = require('os');
import testutil = require('./testutil');

describe('Mock Tests', function () {
Expand Down Expand Up @@ -183,5 +184,100 @@ describe('Mock Tests', function () {

assert(tool, "tool should not be null");
assert(rc == 0, "rc is 0");
})
})

it('Mock toolRunner returns correct output', async () => {
const expectedStdout = "atool output here" + os.EOL + "abc";
damccorm marked this conversation as resolved.
Show resolved Hide resolved
const expectedStderr = "atool with this stderr output" + os.EOL + "def";
var a: ma.TaskLibAnswers = <ma.TaskLibAnswers>{
"exec": {
"/usr/local/bin/atool --arg foo": {
"code": 0,
"stdout": expectedStdout,
"stderr": expectedStderr
}
}
};

mt.setAnswers(a);

let tool: mtr.ToolRunner = mt.tool('/usr/local/bin/atool');
tool.arg('--arg');
tool.arg('foo');

let firstStdline = true;
let firstErrline = true;
let numStdLineCalls = 0;
let numStdErrCalls = 0;
tool.on('stdout', (out) => {
assert.equal(expectedStdout, out);
});
tool.on('stderr', (out) => {
assert.equal(expectedStderr, out);
});
tool.on('stdline', (out) => {
numStdLineCalls += 1;
if (firstStdline) {
assert.equal("atool output here", out);
firstStdline = false;
}
else {
assert.equal("abc", out);
}
});
damccorm marked this conversation as resolved.
Show resolved Hide resolved
tool.on('errline', (out) => {
numStdErrCalls += 1;
if (firstErrline) {
assert.equal("atool with this stderr output", out);
firstErrline = false;
}
else {
assert.equal("def", out);
}
});
await tool.exec(<mtr.IExecOptions>{});

assert.equal(numStdLineCalls, 2);
assert.equal(numStdErrCalls, 2);
})

it('Mock toolRunner returns correct output when ending on EOL', async () => {
const expectedStdout = os.EOL;
const expectedStderr = os.EOL;
var a: ma.TaskLibAnswers = <ma.TaskLibAnswers>{
"exec": {
"/usr/local/bin/atool --arg foo": {
"code": 0,
"stdout": expectedStdout,
"stderr": expectedStderr
}
}
};

mt.setAnswers(a);

let tool: mtr.ToolRunner = mt.tool('/usr/local/bin/atool');
tool.arg('--arg');
tool.arg('foo');
let numStdLineCalls = 0;
let numStdErrCalls = 0;
tool.on('stdout', (out) => {
assert.equal(expectedStdout, out);
});
tool.on('stderr', (out) => {
assert.equal(expectedStderr, out);
});
tool.on('stdline', (out) => {
numStdLineCalls += 1;
assert.equal("", out);
});
tool.on('errline', (out) => {
numStdErrCalls += 1;
assert.equal("", out);
});
await tool.exec(<mtr.IExecOptions>{});

assert.equal(numStdLineCalls, 1);
assert.equal(numStdErrCalls, 1);
})
});