-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
240 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
var path = require('path'), | ||
environ = require('bem-environ'), | ||
instrumenter = new (require('istanbul').Instrumenter)({ preserveComments : true }), | ||
PRJ_ROOT = environ.PRJ_ROOT, | ||
EXCLUDE_RE = new RegExp([ | ||
'(?:^' + environ.LIB_ROOT + ')', | ||
'(?:\/node_modules\/)', | ||
'(?:\.spec\.js$)' | ||
].join('|')); | ||
|
||
module.exports = function(borschik) { | ||
var base = borschik.getTech('js'), | ||
File = base.File.inherit({ | ||
processInclude : function(basePath, content) { | ||
var filePath = this.path; | ||
return EXCLUDE_RE.test(filePath)? | ||
this.__base.apply(this, arguments) : | ||
instrumenter.instrumentSync(this.__base(basePath), path.relative(PRJ_ROOT, filePath)); | ||
} | ||
}), | ||
Tech = base.Tech.inherit({ File : File }); | ||
|
||
return { | ||
File : File, | ||
Tech : Tech | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/** | ||
* An "after run" hook for [mocha-phantomjs](https://github.com/metaskills/mocha-phantomjs) | ||
* to store instrumented code coverage results to temporary buffer, | ||
* so they could be consumed into Istanbul#Collector for future manipulations. | ||
* @example | ||
* $ mocha-phantomjs \ | ||
* --hooks <path/to/hook/file.js> \ | ||
* --setting coverage-file=<path/to/coverage/buffer.json> | ||
*/ | ||
var fs = require('fs'), | ||
sys = require('system'), | ||
args = sys.args; | ||
|
||
module.exports = { | ||
afterEnd : function(reporter) { | ||
var covData = reporter.page.evaluate(function() { | ||
return window.__coverage__; | ||
}); | ||
|
||
if(!covData || args.length < 3) return; | ||
|
||
args = JSON.parse(args[3]); | ||
|
||
var covBufferPath = args.settings['coverage-file']; | ||
if(!covBufferPath) return; | ||
|
||
fs.write(covBufferPath, JSON.stringify(covData), 'w'); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
var FS = require('fs'), | ||
PATH = require('path'), | ||
URL = require('url'), | ||
UTIL = require('util'), | ||
BEM = require('bem'), | ||
exec = require('child_process').exec, | ||
Q = BEM.require('q'), | ||
LOGGER = BEM.logger, | ||
MOCHA_PHANTOM_BIN = require.resolve('../../libs/bem-pr/node_modules/mocha-phantomjs/bin/mocha-phantomjs'), | ||
MOCHA_PHANTOM_HOOK = PATH.resolve(__dirname, '../hooks/mocha-phantomjs.js'), | ||
MOCHA_PHANTOM_REPORTER = process.env.MOCHA_PHANTOM_REPORTER || 'spec', | ||
MOCHA_PHANTOM_MAX_COUNT = parseInt(process.env.MOCHA_PHANTOM_MAX_COUNT, 10) || 5, | ||
phantomCount = 0, | ||
phantomQueue = [], | ||
NEED_COVERAGE = process.env.ISTANBUL_COVERAGE, | ||
covCollector, | ||
covIdx; | ||
|
||
if(NEED_COVERAGE) { | ||
covCollector = new (require('istanbul').Collector)(); | ||
covIdx = 0; | ||
} | ||
|
||
exports.API_VER = 2; | ||
|
||
exports.techMixin = { | ||
|
||
getDependencies : function() { | ||
return ['html', 'spec.js']; | ||
}, | ||
|
||
getCoverageReportDir : function() { | ||
return this.context.opts.dir || process.cwd(); | ||
}, | ||
|
||
createByDecl : function(item, level) { | ||
var root = this.context.opts.dir || '', | ||
prefix = level.getByObj(item), | ||
relPrefix = PATH.relative(root, prefix), | ||
path = this.getPath(prefix, 'html'), | ||
args = '--reporter ' + MOCHA_PHANTOM_REPORTER, | ||
url = URL.format({ | ||
protocol : 'file', | ||
host : '/', | ||
pathname : path | ||
}), | ||
defer = Q.defer(), | ||
covBuffer; | ||
|
||
if(NEED_COVERAGE) { | ||
covBuffer = PATH.join(root, UTIL.format( | ||
'__coverage-%s-%s-%s.json', | ||
relPrefix.replace(/[^A-Za-z0-9_. ]/g, '_'), | ||
Date.now()-0, | ||
covIdx++)); | ||
|
||
LOGGER.fdebug('Store coverage buffer for "%s" into "%s"', relPrefix, covBuffer); | ||
|
||
args += ' --hooks ' + MOCHA_PHANTOM_HOOK; | ||
args += ' --setting coverage-file=' + covBuffer; | ||
} | ||
|
||
phantomCount < MOCHA_PHANTOM_MAX_COUNT? | ||
runMochaPhantom() : | ||
phantomQueue.push(runMochaPhantom); | ||
|
||
var _this = this; | ||
|
||
function getCovData() { | ||
var data = {}, | ||
exists = FS.existsSync(covBuffer); | ||
|
||
LOGGER.fdebug('Coverage buffer for "%s" %s', relPrefix, exists? 'found' : 'doesn\'t exist'); | ||
|
||
if(exists) { | ||
data = JSON.parse(FS.readFileSync(covBuffer, 'utf8')); | ||
FS.unlinkSync(covBuffer); | ||
} | ||
|
||
return data; | ||
} | ||
|
||
function runMochaPhantom() { | ||
phantomCount++; | ||
|
||
exec([MOCHA_PHANTOM_BIN, args, url].join(' '), function(err, stdout, stderr) { | ||
var passed = err === null; | ||
|
||
LOGGER.finfo('Tests results for "%s":\n%s', | ||
PATH.relative(root, path), stdout, stderr? '\nStderr: ' + stderr : ''); | ||
|
||
if(passed) { | ||
defer.resolve(); | ||
if(NEED_COVERAGE) { | ||
LOGGER.fdebug('Going to read coverage buffer for "%s"', relPrefix); | ||
covCollector.add(getCovData()); | ||
} | ||
} else { | ||
LOGGER.error('Tests failed:', err); | ||
defer.reject(err); | ||
} | ||
|
||
--phantomCount; | ||
|
||
phantomQueue.length && phantomQueue.shift()(); | ||
phantomCount || | ||
passed && NEED_COVERAGE && _this.storeFinalCoverage(); | ||
}); | ||
} | ||
|
||
LOGGER.finfo('[i] Page was sent to Phantom (%s)', url); | ||
|
||
return defer.promise; | ||
}, | ||
|
||
storeFinalCoverage : function() { | ||
var covFile = PATH.resolve(this.getCoverageReportDir(), 'coverage.json'); | ||
LOGGER.finfo('[i] Store final coverage "%s"', covFile); | ||
FS.writeFileSync(covFile, JSON.stringify(covCollector.getFinalCoverage()), 'utf8'); | ||
} | ||
|
||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,3 +16,5 @@ node_modules | |
/*.specs | ||
/*.tests | ||
|
||
/coverage | ||
/coverage*.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters