Skip to content

Commit

Permalink
Mock stdline and errline (microsoft#462)
Browse files Browse the repository at this point in the history
* Mock stdline and errline

* Responsd to feedback

* Fix issue with displaying extra line

* Don't ignore last line

* Bump patch version
  • Loading branch information
Danny McCormick authored Dec 12, 2018
1 parent ea92aac commit 4c183d3
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 3 deletions.
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
2 changes: 1 addition & 1 deletion node/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion node/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "azure-pipelines-task-lib",
"version": "2.7.7",
"version": "2.7.8",
"description": "VSTS Task SDK",
"main": "./task.js",
"typings": "./task.d.ts",
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";
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);
}
});
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);
})
});

0 comments on commit 4c183d3

Please sign in to comment.