Skip to content

Commit

Permalink
Merge pull request #1294 from apiaryio/honzajavorek/remove-this-tests
Browse files Browse the repository at this point in the history
Remove redundant 'tests' structure
  • Loading branch information
honzajavorek committed Apr 1, 2019
2 parents 7272095 + 4428ac6 commit e40f7c7
Show file tree
Hide file tree
Showing 20 changed files with 185 additions and 176 deletions.
17 changes: 9 additions & 8 deletions lib/Dredd.js
Expand Up @@ -29,7 +29,6 @@ class Dredd {
init(config) {
this.configuration = applyConfiguration(config);
this.configuration.http = {};
this.tests = [];
this.stats = {
tests: 0,
failures: 0,
Expand All @@ -44,8 +43,6 @@ class Dredd {
this.transactions = [];
this.runner = new Runner(this.configuration);
this.logger = logger;
configureReporters(this.configuration, this.stats, this.tests, this.runner);
this.logProxySettings();
}

logProxySettings() {
Expand All @@ -58,17 +55,21 @@ class Dredd {
}

if (proxySettings.length) {
const message = `
const message = `\
HTTP(S) proxy specified by environment variables: \
${proxySettings.join(', ')}. Please read documentation on how
Dredd works with proxies:
https://dredd.org/en/latest/how-it-works/#using-https-proxy
`;
${proxySettings.join(', ')}. Please read documentation on how \
Dredd works with proxies: \
https://dredd.org/en/latest/how-it-works/#using-https-proxy`;
this.logger.debug(message);
}
}

run(callback) {
this.logProxySettings();

this.logger.debug('Configuring reporters');
configureReporters(this.configuration, this.stats, this.runner);

this.logger.debug('Resolving --require');
if (this.configuration.options.require) {
let mod = this.configuration.options.require;
Expand Down
28 changes: 14 additions & 14 deletions lib/configureReporters.js
Expand Up @@ -23,8 +23,8 @@ function intersection(a, b) {
return Array.from(a).filter(value => Array.from(b).includes(value));
}

function configureReporters(config, stats, tests, runner) {
addReporter('base', config.emitter, stats, tests);
function configureReporters(config, stats, runner) {
addReporter('base', config.emitter, stats);

const reporters = config.options.reporter;
const outputs = config.options.output;
Expand All @@ -36,34 +36,34 @@ function configureReporters(config, stats, tests, runner) {
const usedCliReporters = intersection(reportersArr, cliReporters);
if (usedCliReporters.length === 0) {
return new CLIReporter(
config.emitter, stats, tests, config.options['inline-errors'], config.options.details
config.emitter, stats, config.options['inline-errors'], config.options.details
);
}
return addReporter(usedCliReporters[0], config.emitter, stats, tests);
return addReporter(usedCliReporters[0], config.emitter, stats);
}
return new CLIReporter(
config.emitter, stats, tests, config.options['inline-errors'], config.options.details
config.emitter, stats, config.options['inline-errors'], config.options.details
);
}

function addReporter(reporter, emitter, statistics, testsArg, path) {
function addReporter(reporter, emitter, statistics, path) {
switch (reporter) {
case 'xunit':
return new XUnitReporter(emitter, statistics, testsArg, path, config.options.details);
return new XUnitReporter(emitter, statistics, path, config.options.details);
case 'dot':
return new DotReporter(emitter, statistics, testsArg);
return new DotReporter(emitter, statistics);
case 'nyan':
return new NyanCatReporter(emitter, statistics, testsArg);
return new NyanCatReporter(emitter, statistics);
case 'html':
return new HTMLReporter(emitter, statistics, testsArg, path, config.options.details);
return new HTMLReporter(emitter, statistics, path, config.options.details);
case 'markdown':
return new MarkdownReporter(emitter, statistics, testsArg, path, config.options.details);
return new MarkdownReporter(emitter, statistics, path, config.options.details);
case 'apiary':
return new ApiaryReporter(emitter, statistics, testsArg, config, runner);
return new ApiaryReporter(emitter, statistics, config, runner);
default:
// I don't even know where to begin...
// TODO: DESIGN / REFACTOR WHOLE REPORTER(S) API FROM SCRATCH, THIS IS MADNESS!!1
(new BaseReporter(emitter, stats, tests));
(new BaseReporter(emitter, stats));
}
}

Expand All @@ -88,7 +88,7 @@ provided. Using default paths for additional file-based reporters.

return usedFileReporters.map((usedFileReporter, index) => {
const path = outputs[index] ? outputs[index] : undefined;
return addReporter(usedFileReporter, config.emitter, stats, tests, path);
return addReporter(usedFileReporter, config.emitter, stats, path);
});
}
}
Expand Down
3 changes: 1 addition & 2 deletions lib/reporters/ApiaryReporter.js
Expand Up @@ -19,10 +19,9 @@ const CONNECTION_ERRORS = [
];


function ApiaryReporter(emitter, stats, tests, config, runner) {
function ApiaryReporter(emitter, stats, config, runner) {
this.type = 'apiary';
this.stats = stats;
this.tests = tests;
this.uuid = null;
this.startedAt = null;
this.endedAt = null;
Expand Down
4 changes: 1 addition & 3 deletions lib/reporters/BaseReporter.js
@@ -1,9 +1,8 @@
const logger = require('../logger');

function BaseReporter(emitter, stats, tests) {
function BaseReporter(emitter, stats) {
this.type = 'base';
this.stats = stats;
this.tests = tests;
this.configureEmitter(emitter);
logger.debug(`Using '${this.type}' reporter.`);
}
Expand All @@ -21,7 +20,6 @@ BaseReporter.prototype.configureEmitter = function configureEmitter(emitter) {
});

emitter.on('test start', (test) => {
this.tests.push(test);
this.stats.tests += 1;
test.start = new Date();
});
Expand Down
3 changes: 1 addition & 2 deletions lib/reporters/CLIReporter.js
Expand Up @@ -14,10 +14,9 @@ const CONNECTION_ERRORS = [
];


function CLIReporter(emitter, stats, tests, inlineErrors, details) {
function CLIReporter(emitter, stats, inlineErrors, details) {
this.type = 'cli';
this.stats = stats;
this.tests = tests;
this.inlineErrors = inlineErrors;
this.details = details;
this.errors = [];
Expand Down
3 changes: 1 addition & 2 deletions lib/reporters/DotReporter.js
Expand Up @@ -2,10 +2,9 @@ const logger = require('../logger');
const reporterOutputLogger = require('./reporterOutputLogger');
const prettifyResponse = require('../prettifyResponse');

function DotReporter(emitter, stats, tests) {
function DotReporter(emitter, stats) {
this.type = 'dot';
this.stats = stats;
this.tests = tests;
this.errors = [];

this.configureEmitter(emitter);
Expand Down
3 changes: 1 addition & 2 deletions lib/reporters/HTMLReporter.js
Expand Up @@ -11,12 +11,11 @@ const logger = require('../logger');
const reporterOutputLogger = require('./reporterOutputLogger');
const prettifyResponse = require('../prettifyResponse');

function HTMLReporter(emitter, stats, tests, path, details) {
function HTMLReporter(emitter, stats, path, details) {
EventEmitter.call(this);

this.type = 'html';
this.stats = stats;
this.tests = tests;
this.buf = '';
this.level = 1;
this.details = details;
Expand Down
3 changes: 1 addition & 2 deletions lib/reporters/MarkdownReporter.js
Expand Up @@ -10,12 +10,11 @@ const logger = require('../logger');
const reporterOutputLogger = require('./reporterOutputLogger');
const prettifyResponse = require('../prettifyResponse');

function MarkdownReporter(emitter, stats, tests, path, details) {
function MarkdownReporter(emitter, stats, path, details) {
EventEmitter.call(this);

this.type = 'markdown';
this.stats = stats;
this.tests = tests;
this.buf = '';
this.level = 1;
this.details = details;
Expand Down
3 changes: 1 addition & 2 deletions lib/reporters/NyanReporter.js
Expand Up @@ -4,12 +4,11 @@ const logger = require('../logger');
const prettifyResponse = require('../prettifyResponse');
const reporterOutputLogger = require('./reporterOutputLogger');

function NyanCatReporter(emitter, stats, tests) {
function NyanCatReporter(emitter, stats) {
let windowWidth;

this.type = 'nyan';
this.stats = stats;
this.tests = tests;
this.isatty = tty.isatty(1) && tty.isatty(2);

if (this.isatty) {
Expand Down
3 changes: 1 addition & 2 deletions lib/reporters/XUnitReporter.js
Expand Up @@ -11,12 +11,11 @@ const logger = require('../logger');
const reporterOutputLogger = require('./reporterOutputLogger');
const prettifyResponse = require('../prettifyResponse');

function XUnitReporter(emitter, stats, tests, path, details) {
function XUnitReporter(emitter, stats, path, details) {
EventEmitter.call(this);

this.type = 'xunit';
this.stats = stats;
this.tests = tests;
this.details = details;
this.path = this.sanitizedPath(path);

Expand Down
64 changes: 47 additions & 17 deletions test/unit/Dredd-test.js
Expand Up @@ -622,6 +622,7 @@ GET /url
};

dredd = new Dredd(configuration);
sinon.stub(dredd.runner, 'run').callsArg(1);

const apiary = express();
apiary.use(bodyParser.json({ size: '5mb' }));
Expand All @@ -634,7 +635,7 @@ GET /url

apiary.all('*', (req, res) => res.json({}));

apiaryServer = apiary.listen((PORT + 1), () => done());
apiaryServer = apiary.listen((PORT + 1), () => dredd.run(done));
});

afterEach(done => apiaryServer.close(() => done()));
Expand All @@ -656,7 +657,7 @@ GET /url
dredd = null;
let errorLogger;

beforeEach(() => {
beforeEach((done) => {
errorLogger = sinon.spy(loggerStub, 'error');
configuration = {
server: 'http://127.0.0.1:3000/',
Expand All @@ -674,6 +675,8 @@ GET /url
};

dredd = new Dredd(configuration);
sinon.stub(dredd.runner, 'run').callsArg(1);
dredd.run(done);
});

afterEach(() => loggerStub.error.restore());
Expand All @@ -695,65 +698,92 @@ GET /url
});

describe('#logProxySettings', () => {
const docsLinkSuffix = 'Please read documentation on how Dredd works with '
+ 'proxies: https://dredd.org/en/latest/how-it-works/#using-https-proxy';
let debugLogger;

beforeEach(() => { debugLogger = sinon.spy(loggerStub, 'debug'); });
afterEach(() => loggerStub.debug.restore());

describe('when the proxy is set by lowercase environment variable', () => {
beforeEach(() => {
beforeEach((done) => {
process.env.http_proxy = 'http://proxy.example.com';
dredd = new Dredd({ options: {} });
sinon.stub(dredd.runner, 'run').callsArg(1);
dredd.run(done);
});
afterEach(() => delete process.env.http_proxy);

it('logs about the setting', () => assert.include(debugLogger.lastCall.args[0],
'HTTP(S) proxy specified by environment variables: http_proxy=http://proxy.example.com'));
it('logs about the setting', () => {
assert.isTrue(debugLogger.calledWith(
'HTTP(S) proxy specified by environment variables: '
+ 'http_proxy=http://proxy.example.com.'
+ ` ${docsLinkSuffix}`
));
});
});

describe('when the proxy is set by uppercase environment variable', () => {
beforeEach(() => {
beforeEach((done) => {
process.env.HTTPS_PROXY = 'http://proxy.example.com';
dredd = new Dredd({ options: {} });
sinon.stub(dredd.runner, 'run').callsArg(1);
dredd.run(done);
});
afterEach(() => delete process.env.HTTPS_PROXY);

it('logs about the setting', () => assert.include(debugLogger.lastCall.args[0],
'HTTP(S) proxy specified by environment variables: '
+ 'HTTPS_PROXY=http://proxy.example.com'));
it('logs about the setting', () => {
assert.isTrue(debugLogger.calledWith(
'HTTP(S) proxy specified by environment variables: '
+ 'HTTPS_PROXY=http://proxy.example.com.'
+ ` ${docsLinkSuffix}`
));
});
});

describe('when NO_PROXY environment variable is set', () => {
beforeEach(() => {
beforeEach((done) => {
process.env.HTTPS_PROXY = 'http://proxy.example.com';
process.env.NO_PROXY = 'whitelisted.example.com';
dredd = new Dredd({ options: {} });
sinon.stub(dredd.runner, 'run').callsArg(1);
dredd.run(done);
});
afterEach(() => {
delete process.env.HTTPS_PROXY;
delete process.env.NO_PROXY;
});

it('logs about the setting', () => assert.include(debugLogger.lastCall.args[0],
'HTTP(S) proxy specified by environment variables: '
it('logs about the setting', () => {
assert.isTrue(debugLogger.calledWith(
'HTTP(S) proxy specified by environment variables: '
+ 'HTTPS_PROXY=http://proxy.example.com, '
+ 'NO_PROXY=whitelisted.example.com'));
+ 'NO_PROXY=whitelisted.example.com.'
+ ` ${docsLinkSuffix}`
));
});
});

describe('when DUMMY_PROXY environment variable is set', () => {
beforeEach(() => {
beforeEach((done) => {
process.env.DUMMY_PROXY = 'http://proxy.example.com';
process.env.NO_PROXY = 'whitelisted.example.com';
dredd = new Dredd({ options: {} });
sinon.stub(dredd.runner, 'run').callsArg(1);
dredd.run(done);
});
afterEach(() => {
delete process.env.DUMMY_PROXY;
delete process.env.NO_PROXY;
});

it('is ignored', () => assert.include(debugLogger.lastCall.args[0],
'HTTP(S) proxy specified by environment variables: '
+ 'NO_PROXY=whitelisted.example.com'));
it('is ignored', () => {
assert.isTrue(debugLogger.calledWith(
'HTTP(S) proxy specified by environment variables: '
+ 'NO_PROXY=whitelisted.example.com.'
+ ` ${docsLinkSuffix}`
));
});
});
});
});

0 comments on commit e40f7c7

Please sign in to comment.