Skip to content

Commit

Permalink
test: update tests to reflect the logging split
Browse files Browse the repository at this point in the history
  • Loading branch information
honzajavorek committed Jan 23, 2019
1 parent 4a90863 commit 79baef6
Show file tree
Hide file tree
Showing 13 changed files with 209 additions and 208 deletions.
24 changes: 10 additions & 14 deletions test/integration/cli/configuration-cli-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,25 +54,21 @@ function execCommand(custom = {}, cb) {
describe('CLI class Integration', () => {
before(() => {
['warn', 'error'].forEach((method) => {
sinon.stub(loggerStub, method).callsFake((chunk) => { stderr += `\n${method}: ${chunk}`; });
sinon
.stub(loggerStub, method)
.callsFake((chunk) => { stderr += `\n${method}: ${chunk}`; });
});
[
'log', 'info', 'silly', 'verbose', 'test',
'hook', 'complete', 'pass', 'skip', 'debug',
'fail', 'request', 'expected', 'actual',
].forEach((method) => {
sinon.stub(loggerStub, method).callsFake((chunk) => { stdout += `\n${method}: ${chunk}`; });
['log', 'info', 'silly', 'verbose', 'debug'].forEach((method) => {
sinon
.stub(loggerStub, method)
.callsFake((chunk) => { stdout += `\n${method}: ${chunk}`; });
});
});

after(() => {
['warn', 'error'].forEach((method) => { loggerStub[method].restore(); });

[
'log', 'info', 'silly', 'verbose', 'test',
'hook', 'complete', 'pass', 'skip', 'debug',
'fail', 'request', 'expected', 'actual',
].forEach((method) => { loggerStub[method].restore(); });
['warn', 'error', 'log', 'info', 'silly', 'verbose', 'debug'].forEach((method) => {
loggerStub[method].restore();
});
});

describe('When using configuration file', () => {
Expand Down
8 changes: 4 additions & 4 deletions test/integration/cli/hookfiles-cli-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,7 @@ describe('CLI', () => {
});
});

describe('when setting the log output level with -l', () => {
describe('when setting the log output level with --level', () => {
let runtimeInfo;

before((done) => {
Expand All @@ -543,7 +543,8 @@ describe('CLI', () => {
const args = [
'./test/fixtures/single-get.apib',
`http://127.0.0.1:${DEFAULT_SERVER_PORT}`,
'-l=error',
'--level=error',
'--color=false',
];
runCLIWithServer(args, app, (err, info) => {
runtimeInfo = info;
Expand All @@ -552,8 +553,7 @@ describe('CLI', () => {
});

it('should not display anything', () => {
// At the "error" level, complete should not be shown
assert.isOk(runtimeInfo.dredd.stdout.indexOf('complete') === -1);
assert.notInclude(runtimeInfo.dredd.output, 'info:');
});
});

Expand Down
105 changes: 42 additions & 63 deletions test/integration/dredd-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,20 @@ const bodyParser = require('body-parser');
const clone = require('clone');
const express = require('express');
const fs = require('fs');
const proxyquire = require('proxyquire').noCallThru();
const sinon = require('sinon');
const { assert } = require('chai');

const loggerStub = require('../../lib/logger');
const logger = require('../../lib/logger');
const reporterOutputLogger = require('../../lib/reporters/reporterOutputLogger');
const Dredd = require('../../lib/Dredd');

const PORT = 9876;

let exitStatus;

let stderr = '';
let stdout = '';

const addHooksStub = proxyquire('../../lib/addHooks', {
'./logger': loggerStub,
});

const transactionRunner = proxyquire('../../lib/TransactionRunner', {
'./addHooks': addHooksStub,
'./logger': loggerStub,
});

const Dredd = proxyquire('../../lib/Dredd', {
'./TransactionRunner': transactionRunner,
'./logger': loggerStub,
});
let output = '';

function execCommand(options = {}, cb) {
stdout = '';
stderr = '';
output = '';
exitStatus = null;
let finished = false;
if (!options.server) { options.server = `http://127.0.0.1:${PORT}`; }
Expand All @@ -40,41 +24,36 @@ function execCommand(options = {}, cb) {
if (!finished) {
finished = true;
if (error ? error.message : undefined) {
stderr += error.message;
output += error.message;
}
exitStatus = (error || (((1 * stats.failures) + (1 * stats.errors)) > 0)) ? 1 : 0;
cb(null, stdout, stderr, exitStatus);
cb();
}
});
}


function record(transport, level, message) {
output += `\n${level}: ${message}`;
}


describe('Dredd class Integration', () => {
before(() => {
['warn', 'error'].forEach((method) => {
sinon.stub(loggerStub, method).callsFake((chunk) => { stderr += `\n${method}: ${chunk}`; });
});
[
'log', 'info', 'silly', 'verbose', 'test',
'hook', 'complete', 'pass', 'skip', 'debug',
'fail', 'request', 'expected', 'actual',
].forEach((method) => {
sinon.stub(loggerStub, method).callsFake((chunk) => { stdout += `\n${method}: ${chunk}`; });
});
logger.transports.console.silent = true;
logger.on('logging', record);

reporterOutputLogger.transports.console.silent = true;
reporterOutputLogger.on('logging', record);
});

after(() => {
['warn', 'error'].forEach((method) => {
loggerStub[method].restore();
});
[
'log', 'info', 'silly', 'verbose', 'test',
'hook', 'complete', 'pass', 'skip', 'debug',
'fail', 'request', 'expected', 'actual',
].forEach((method) => {
loggerStub[method].restore();
});
});
logger.transports.console.silent = false;
logger.removeListener('logging', record);

reporterOutputLogger.transports.console.silent = false;
reporterOutputLogger.removeListener('logging', record);
});

describe('when creating Dredd instance with existing API description document and responding server', () => {
describe('when the server is responding as specified in the API description', () => {
Expand Down Expand Up @@ -181,7 +160,7 @@ describe('Dredd class Integration', () => {
});
});

it('should not print warning about missing Apiary API settings', () => assert.notInclude(stderr, 'Apiary API Key or API Project Subdomain were not provided.'));
it('should not print warning about missing Apiary API settings', () => assert.notInclude(output, 'Apiary API Key or API Project Subdomain were not provided.'));

it('should contain Authentication header thanks to apiaryApiKey and apiaryApiName configuration', () => {
assert.propertyVal(receivedHeaders, 'authentication', 'Token the-key');
Expand All @@ -193,7 +172,7 @@ describe('Dredd class Integration', () => {
assert.propertyVal(receivedRequestTestRuns, 'public', false);
});

it('should print using the new reporter', () => assert.include(stdout, 'http://url.me/test/run/1234_id'));
it('should print using the new reporter', () => assert.include(output, 'http://url.me/test/run/1234_id'));

it('should send results from Gavel', () => {
assert.isObject(receivedRequest);
Expand Down Expand Up @@ -276,7 +255,7 @@ describe('Dredd class Integration', () => {
server2.on('close', done);
});

it('should print using the reporter', () => assert.include(stdout, 'http://url.me/test/run/1234_id'));
it('should print using the reporter', () => assert.include(output, 'http://url.me/test/run/1234_id'));

it('should send results from gavel', () => {
assert.isObject(receivedRequest);
Expand Down Expand Up @@ -338,11 +317,11 @@ describe('Dredd class Integration', () => {
server.on('close', done);
});

it('should print warning about missing Apiary API settings', () => assert.include(stderr, 'Apiary API Key or API Project Subdomain were not provided.'));
it('should print warning about missing Apiary API settings', () => assert.include(output, 'Apiary API Key or API Project Subdomain were not provided.'));

it('should print link to documentation', () => assert.include(stderr, 'https://dredd.org/en/latest/how-to-guides/#using-apiary-reporter-and-apiary-tests'));
it('should print link to documentation', () => assert.include(output, 'https://dredd.org/en/latest/how-to-guides/#using-apiary-reporter-and-apiary-tests'));

it('should print using the new reporter', () => assert.include(stdout, 'http://url.me/test/run/1234_id'));
it('should print using the new reporter', () => assert.include(output, 'http://url.me/test/run/1234_id'));

it('should send results from Gavel', () => {
assert.isObject(receivedRequest);
Expand Down Expand Up @@ -426,10 +405,10 @@ describe('Dredd class Integration', () => {

it('should exit with status 1', () => assert.equal(exitStatus, 1));

it('should print error message to stderr', () => {
assert.include(stderr, 'Error when loading file from URL');
assert.include(stderr, 'Is the provided URL correct?');
assert.include(stderr, 'connection-error.apib');
it('should print error message to the output', () => {
assert.include(output, 'Error when loading file from URL');
assert.include(output, 'Is the provided URL correct?');
assert.include(output, 'connection-error.apib');
});
});

Expand All @@ -444,10 +423,10 @@ describe('Dredd class Integration', () => {

it('should exit with status 1', () => assert.equal(exitStatus, 1));

it('should print error message to stderr', () => {
assert.include(stderr, 'Unable to load file from URL');
assert.include(stderr, 'responded with status code 404');
assert.include(stderr, 'not-found.apib');
it('should print error message to the output', () => {
assert.include(output, 'Unable to load file from URL');
assert.include(output, 'responded with status code 404');
assert.include(output, 'not-found.apib');
});
});

Expand All @@ -463,7 +442,7 @@ describe('Dredd class Integration', () => {
});

describe('when OpenAPI 2 document has multiple responses', () => {
const reTransaction = /(\w+): (\w+) \((\d+)\) \/honey/g;
const reTransaction = /(skip|fail): (\w+) \((\d+)\) \/honey/g;
let actual;

before(done => execCommand({
Expand All @@ -475,7 +454,7 @@ describe('Dredd class Integration', () => {
let groups;
const matches = [];
// eslint-disable-next-line
while (groups = reTransaction.exec(stdout)) { matches.push(groups); }
while (groups = reTransaction.exec(output)) { matches.push(groups); }
actual = matches.map((match) => {
const keyMap = {
0: 'name', 1: 'action', 2: 'method', 3: 'statusCode',
Expand All @@ -499,7 +478,7 @@ describe('Dredd class Integration', () => {
});

describe('when OpenAPI 2 document has multiple responses and hooks unskip some of them', () => {
const reTransaction = /(\w+): (\w+) \((\d+)\) \/honey/g;
const reTransaction = /(skip|fail): (\w+) \((\d+)\) \/honey/g;
let actual;

before(done => execCommand({
Expand All @@ -512,7 +491,7 @@ describe('Dredd class Integration', () => {
let groups;
const matches = [];
// eslint-disable-next-line
while (groups = reTransaction.exec(stdout)) { matches.push(groups); }
while (groups = reTransaction.exec(output)) { matches.push(groups); }
actual = matches.map((match) => {
const keyMap = {
0: 'name', 1: 'action', 2: 'method', 3: 'statusCode',
Expand Down Expand Up @@ -552,7 +531,7 @@ describe('Dredd class Integration', () => {
let groups;
matches = [];
// eslint-disable-next-line
while (groups = reTransactionName.exec(stdout)) { matches.push(groups[1]); }
while (groups = reTransactionName.exec(output)) { matches.push(groups[1]); }
done(err);
}));

Expand Down
21 changes: 17 additions & 4 deletions test/integration/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const ps = require('ps-node');
const spawn = require('cross-spawn');

const logger = require('../../lib/logger');
const reporterOutputLogger = require('../../lib/reporters/reporterOutputLogger');

const DEFAULT_SERVER_PORT = 9876;
const DREDD_BIN = require.resolve('../../bin/dredd');
Expand All @@ -22,16 +23,28 @@ const DREDD_BIN = require.resolve('../../bin/dredd');
// from the 'fn' function
// - logging (string) - the recorded logging output
function recordLogging(fn, callback) {
const silent = !!logger.transports.console.silent;
logger.transports.console.silent = true; // Supress Dredd's console output (remove if debugging)
const loggerSilent = !!logger.transports.console.silent;
const reporterOutputLoggerSilent = !!reporterOutputLogger.transports.console.silent;

// Supress Dredd's console output (remove if debugging)
logger.transports.console.silent = true;
reporterOutputLogger.transports.console.silent = true;

let logging = '';
const record = (transport, level, message) => { logging += `${level}: ${message}\n`; };
const record = (transport, level, message) => {
logging += `${level}: ${message}\n`;
};

logger.on('logging', record);
reporterOutputLogger.on('logging', record);

fn((...args) => {
logger.removeListener('logging', record);
logger.transports.console.silent = silent;
logger.transports.console.silent = loggerSilent;

reporterOutputLogger.removeListener('logging', record);
reporterOutputLogger.transports.console.silent = reporterOutputLoggerSilent;

callback(null, args, logging);
});
}
Expand Down
25 changes: 8 additions & 17 deletions test/unit/CLI-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,28 +64,19 @@ function execCommand(custom = {}, cb) {
describe('CLI class', () => {
before(() => {
['warn', 'error'].forEach((method) => {
sinon.stub(loggerStub, method).callsFake((chunk) => { stderr += `\n${method}: ${chunk}`; });
sinon
.stub(loggerStub, method)
.callsFake((chunk) => { stderr += `\n${method}: ${chunk}`; });
});

[
'log', 'info', 'silly', 'verbose', 'test',
'hook', 'complete', 'pass', 'skip', 'debug',
'fail', 'request', 'expected', 'actual',
].forEach((method) => {
sinon.stub(loggerStub, method).callsFake((chunk) => { stdout += `\n${method}: ${chunk}`; });
['log', 'info', 'silly', 'verbose', 'debug'].forEach((method) => {
sinon
.stub(loggerStub, method)
.callsFake((chunk) => { stdout += `\n${method}: ${chunk}`; });
});
});

after(() => {
['warn', 'error'].forEach((method) => {
loggerStub[method].restore();
});

[
'log', 'info', 'silly', 'verbose', 'test',
'hook', 'complete', 'pass', 'skip', 'debug',
'fail', 'request', 'expected', 'actual',
].forEach((method) => {
['warn', 'error', 'log', 'info', 'silly', 'verbose', 'debug'].forEach((method) => {
loggerStub[method].restore();
});
});
Expand Down
Loading

0 comments on commit 79baef6

Please sign in to comment.