From 37a92630154094d8ca376509faf842e487fac467 Mon Sep 17 00:00:00 2001 From: tormozz48 Date: Thu, 27 Aug 2015 12:08:52 +0300 Subject: [PATCH] #2 Increase test coverage up to 99 percentage --- src/acts/config.es6 | 14 +++------- src/acts/run.es6 | 48 +++++++++++++++------------------ src/reporters/base.es6 | 20 +++----------- src/reporters/json.es6 | 4 ++- test/integration.test.js | 11 ++++++++ test/mocha.opts | 2 +- test/mock/my.site.com.js | 6 ++--- test/src/acts/run.test.js | 37 +++++++++++++++++++++++++ test/src/checker.test.js | 26 +++--------------- test/src/reporters/base.test.js | 11 ++++---- 10 files changed, 92 insertions(+), 87 deletions(-) diff --git a/src/acts/config.es6 b/src/acts/config.es6 index bb4f687..5fada0e 100644 --- a/src/acts/config.es6 +++ b/src/acts/config.es6 @@ -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; } diff --git a/src/acts/run.es6 b/src/acts/run.es6 index 688217c..ba91acb 100644 --- a/src/acts/run.es6 +++ b/src/acts/run.es6 @@ -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); } diff --git a/src/reporters/base.es6 b/src/reporters/base.es6 index ccbaa7c..c6fbec3 100644 --- a/src/reporters/base.es6 +++ b/src/reporters/base.es6 @@ -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'; @@ -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); } } diff --git a/src/reporters/json.es6 b/src/reporters/json.es6 index a710ed9..e878536 100644 --- a/src/reporters/json.es6 +++ b/src/reporters/json.es6 @@ -1,3 +1,4 @@ +import moment from 'moment'; import ReporterBase from './base'; /** @@ -9,6 +10,7 @@ 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(), @@ -16,6 +18,6 @@ export default class ReporterJson extends ReporterBase { 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); } } diff --git a/test/integration.test.js b/test/integration.test.js index 86befaa..b1d05f3 100644 --- a/test/integration.test.js +++ b/test/integration.test.js @@ -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) diff --git a/test/mocha.opts b/test/mocha.opts index ffab382..2f7a9ef 100644 --- a/test/mocha.opts +++ b/test/mocha.opts @@ -1,4 +1,4 @@ --reporter spec --ui bdd --growl ---timeout 2000 +--timeout 5000 diff --git a/test/mock/my.site.com.js b/test/mock/my.site.com.js index 2b243e2..460a4fd 100644 --- a/test/mock/my.site.com.js +++ b/test/mock/my.site.com.js @@ -7,11 +7,11 @@ module.exports = { "requestHeaders": { "user-agent": "node-spider" }, - "requestRetriesAmount": 5, - "requestTimeout": 5000, + "requestRetriesAmount": 2, + "requestTimeout": 200, "acceptedSchemes": [ "http:" ], "checkExternalUrls": false, "excludeLinkPatterns": [] -} +}; diff --git a/test/src/acts/run.test.js b/test/src/acts/run.test.js index f22fe5f..0f4fc7b 100644 --- a/test/src/acts/run.test.js +++ b/test/src/acts/run.test.js @@ -1,5 +1,6 @@ var path = require('path'), fs = require('fs'), + nock = require('nock'), should = require('should'), mockFs = require('mock-fs'), run = require('../../../lib/acts/run'); @@ -7,6 +8,11 @@ var path = require('path'), 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' }) } @@ -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); +} diff --git a/test/src/checker.test.js b/test/src/checker.test.js index e8fd6b5..ae07d0e 100644 --- a/test/src/checker.test.js +++ b/test/src/checker.test.js @@ -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'); }); }); diff --git a/test/src/reporters/base.test.js b/test/src/reporters/base.test.js index 4a6f410..6222c84 100644 --- a/test/src/reporters/base.test.js +++ b/test/src/reporters/base.test.js @@ -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 () { @@ -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'); }); }); });