Skip to content

Commit

Permalink
#2 Increase test coverage up to 99 percentage
Browse files Browse the repository at this point in the history
  • Loading branch information
tormozz48 committed Aug 27, 2015
1 parent 7d8a416 commit 37a9263
Show file tree
Hide file tree
Showing 10 changed files with 92 additions and 87 deletions.
14 changes: 4 additions & 10 deletions src/acts/config.es6
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,9 @@ export function createConfigFile(fileName) {
fileName = fileName.replace(/\//g, '') + '.js';

createConfigsDir();
try {
fs.writeFileSync(path.join(Util.getConfigurationDirectory(), fileName),

fs.writeFileSync(path.join(Util.getConfigurationDirectory(), fileName),
'module.exports = ' + JSON.stringify(createConfigStub(), null, 4), 'utf-8');
logger.info('Configuration file: => %s has been generated successfully', fileName);
return true;
} catch (error) {
logger
.error('Error occur while saving configuration file: %s', fileName)
.error(error.message);
return false;
}
logger.info('Configuration file: => %s has been generated successfully', fileName);
return true;
}
48 changes: 21 additions & 27 deletions src/acts/run.es6
Original file line number Diff line number Diff line change
Expand Up @@ -32,31 +32,25 @@ export function run (options) {
}
});

return new Promise((resolve, reject) => {
config.onDone = (statistic) => {
logger.info('finish to analyze pages');

logger
.info('-- Internal urls: [%s]', statistic.getInternalCount())
.info('-- External urls: [%s]', statistic.getExternalCount())
.info('-- Broken urls: [%s]', statistic.getBrokenCount())
.info('-- Total urls: [%s]', statistic.getAllCount())
.info('-- Broken urls percentage: [%s] %', (statistic.getBrokenCount() * 100) / statistic.getAllCount());

var reporters = [
new ReporterJson(options),
new ReporterHtml(options)
];

reporters = reporters.map(item => {
return item.createReport(path.basename(configFileName, '.js'), statistic, config);
});

return Promise.all(reporters)
.then(() => { resolve(); })
.catch((error) => { reject(error); });
};

(new Checker(config)).start(options.url || config.url);
});
config.onDone = (statistic) => {
logger.info('finish to analyze pages');

logger
.info('-- Internal urls: [%s]', statistic.getInternalCount())
.info('-- External urls: [%s]', statistic.getExternalCount())
.info('-- Broken urls: [%s]', statistic.getBrokenCount())
.info('-- Total urls: [%s]', statistic.getAllCount())
.info('-- Broken urls percentage: [%s] %', (statistic.getBrokenCount() * 100) / statistic.getAllCount());

var reporters = [
new ReporterJson(options),
new ReporterHtml(options)
];

reporters.map(item => {
return item.createReport(path.basename(configFileName, '.js'), statistic, config);
});
};

(new Checker(config)).start(options.url || config.url);
}
20 changes: 3 additions & 17 deletions src/reporters/base.es6
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import fs from 'fs';
import path from 'path';
import moment from 'moment';
import Logger from 'bem-site-logger';
import Util from '../util';

Expand Down Expand Up @@ -59,25 +58,12 @@ export default class ReporterBase {
* @param {String} date formatted
* @returns {Promise}
*/
saveReportFile(configurationName, type, content, date = moment().format('DD-MM-YYYY:hh:mm:ss')) {
saveReportFile(configurationName, type, content, date) {
this.createReportFolder(configurationName);
var fileName = `${date}.${type}`,
filePath = path.join(Util.getReportsDirectory(), configurationName, fileName);

return new Promise((resolve, reject) => {
fs.writeFile(filePath, content, { encoding: 'utf-8' }, error => {
if (error) {
this._logger.error('Error occur while saving file: %s', filePath);
reject(error);
} else {
this._logger.info('Report saved: %s', filePath);
resolve();
}
});
});
}

createReport() {
// TODO override in child class
fs.writeFileSync(filePath, content, { encoding: 'utf-8' });
this._logger.info('Report saved: %s', filePath);
}
}
4 changes: 3 additions & 1 deletion src/reporters/json.es6
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import moment from 'moment';
import ReporterBase from './base';

/**
Expand All @@ -9,13 +10,14 @@ export default class ReporterJson extends ReporterBase {
createReport(configurationName, statistic) {
this._logger.info('create json report');
var report = {
date: moment().format('DD-MM-YYYY:hh:mm:ss'),
internalCount: statistic.getInternalCount(),
externalCount: statistic.getExternalCount(),
totalCount: statistic.getAllCount(),
brokenCount: statistic.getBrokenCount(),
broken: statistic.getBroken().getAll()
};

return this.saveReportFile(configurationName, 'json', JSON.stringify(report, null, 4));
return this.saveReportFile(configurationName, 'json', JSON.stringify(report, null, 4), report.date);
}
}
11 changes: 11 additions & 0 deletions test/integration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,17 @@ describe('BrokenLinksChecker', function () {
});
});

describe('custom excludeLinkPatterns option', function () {
it('should not check excluded urls', function () {
nock(SERVER_URL)
.get('/')
.reply(200, htmlBuilder.build(['/foo1', '/foo2', '/foo2/foo3']));
runTest({
excludeLinkPatterns: [/\foo2/]
}, 2, 2, 0, 0, function () { return done(); });
});
});

describe('check external urls', function (done) {
it('should not check external link', function () {
nock(SERVER_URL)
Expand Down
2 changes: 1 addition & 1 deletion test/mocha.opts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
--reporter spec
--ui bdd
--growl
--timeout 2000
--timeout 5000
6 changes: 3 additions & 3 deletions test/mock/my.site.com.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ module.exports = {
"requestHeaders": {
"user-agent": "node-spider"
},
"requestRetriesAmount": 5,
"requestTimeout": 5000,
"requestRetriesAmount": 2,
"requestTimeout": 200,
"acceptedSchemes": [
"http:"
],
"checkExternalUrls": false,
"excludeLinkPatterns": []
}
};
37 changes: 37 additions & 0 deletions test/src/acts/run.test.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
var path = require('path'),
fs = require('fs'),
nock = require('nock'),
should = require('should'),
mockFs = require('mock-fs'),
run = require('../../../lib/acts/run');

describe('acts/run', function () {
beforeEach(function () {
mockFs({
src: {
assets: {
'report.html': fs.readFileSync('./src/assets/report.html')
}
},
configs: {
'my.site.com.js': fs.readFileSync('./test/mock/my.site.com.js', { encoding: 'utf-8' })
}
Expand All @@ -21,7 +27,38 @@ describe('acts/run', function () {
(function () { return run.run({ config: './configs/invalid.js' }); }).should.throw('Configuration file not found');
});

describe('execute run action', function () {
beforeEach(function () {
nock('http://localhost:3000')
.get('/')
.reply(200, 'Hello World');
});

it('with default params', function (done) {
runTest({ config: './configs/my.site.com.js' }, done);
});

it('with custom params', function (done) {
runTest({
config: './configs/my.site.com.js',
concurrent: 1,
requestRetriesAmount: 1,
requestTimeout: 500,
checkExternalUrls: true
}, done);
});
});

afterEach(function() {
mockFs.restore();
});
});

function runTest(options, callback) {
run.run(options);
setTimeout(function () {
fs.existsSync('./reports').should.equal(true);
fs.existsSync('./reports/my.site.com').should.equal(true);
callback();
}, 2000);
}
26 changes: 3 additions & 23 deletions test/src/checker.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,28 +89,8 @@ describe('checker', function () {
});
});

describe('_checkExternalLink', function () {
var checker;

beforeEach(function () {
checker = new Checker();
checker.initStatistic(new Statistic());
checker.initModel(new Model());
});

it('should check existed existed external link', function () {
var item = ['http://yandex.ru', { href: 'http://yandex.ru', page: 'http://my.site.com' }];
return checker._checkExternalLink(item).then(function () {
checker.statistic.getExternalCount().should.equal(1);
});
});

it('should check existed non-existed external link', function () {
var item = ['http://invlid-url', { href: 'http://invlid-url', page: 'http://my.site.com' }];
return checker._checkExternalLink(item).then(function () {
checker.statistic.getExternalCount().should.equal(1);
checker.statistic.getBrokenCount().should.equal(1);
});
});
it('should have default onDone handler', function () {
var checker = new Checker();
checker.onDone('Hello World').should.equal('Hello World');
});
});
11 changes: 6 additions & 5 deletions test/src/reporters/base.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var fs = require('fs'),
should = require('should'),
mockFs = require('mock-fs'),
moment = require('moment'),
ReporterBase = require('../../../lib/reporters/base');

describe('Reporters', function () {
Expand All @@ -27,11 +28,11 @@ describe('Reporters', function () {
});

it('should save report file', function () {
var date = +(new Date());
reporterBase.saveReportFile('my.site', 'json', 'hello world').then(function () {
fs.existsSync('./reports/my.site/' + date + '.json').should.equal(true);
fs.readFileSync('./reports/my.site/' + date + '.json', 'utf-8').should.equal('hello world');
});
var date = moment().format('DD-MM-YYYY:hh:mm:ss');
reporterBase.saveReportFile('my.site', 'json', 'hello world', date);

fs.existsSync('./reports/my.site/' + date + '.json').should.equal(true);
fs.readFileSync('./reports/my.site/' + date + '.json', 'utf-8').should.equal('hello world');
});
});
});

0 comments on commit 37a9263

Please sign in to comment.