coverage.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*!
  2. * node-sass: scripts/coverage.js
  3. */
  4. var Mocha = require('mocha'),
  5. fs = require('fs'),
  6. path = require('path'),
  7. mkdirp = require('mkdirp'),
  8. coveralls = require('coveralls'),
  9. istanbul = require('istanbul'),
  10. sourcefiles = ['index.js', 'binding.js', 'extensions.js', 'render.js', 'errors.js'],
  11. summary= istanbul.Report.create('text-summary'),
  12. lcov = istanbul.Report.create('lcovonly', { dir: path.join('coverage') }),
  13. html = istanbul.Report.create('html', { dir: path.join('coverage', 'html') });
  14. function coverage() {
  15. var mocha = new Mocha();
  16. var rep = function(runner) {
  17. runner.on('end', function(){
  18. var cov = global.__coverage__,
  19. collector = new istanbul.Collector();
  20. if (cov) {
  21. mkdirp(path.join('coverage', 'html'), function(err) {
  22. if (err) { throw err; }
  23. collector.add(cov);
  24. summary.writeReport(collector, true);
  25. html.writeReport(collector, true);
  26. lcov.on('done', function() {
  27. fs.readFile(path.join('coverage', 'lcov.info'), function(err, data) {
  28. if (err) { console.error(err); }
  29. coveralls.handleInput(data.toString(),
  30. function (err) { if (err) { console.error(err); } });
  31. });
  32. });
  33. lcov.writeReport(collector, true);
  34. });
  35. } else {
  36. console.warn('No coverage');
  37. }
  38. });
  39. };
  40. var instrumenter = new istanbul.Instrumenter();
  41. var instrumentedfiles = [];
  42. var processfile = function(source) {
  43. fs.readFile(path.join('lib', source), function(err, data) {
  44. if (err) { throw err; }
  45. mkdirp('lib-cov', function(err) {
  46. if (err) { throw err; }
  47. fs.writeFile(path.join('lib-cov', source),
  48. instrumenter.instrumentSync(data.toString(),
  49. path.join('lib', source)),
  50. function(err) {
  51. if (err) { throw err; }
  52. instrumentedfiles.push(source);
  53. if (instrumentedfiles.length === sourcefiles.length) {
  54. fs.readdirSync('test').filter(function(file){
  55. return file.substr(-6) === 'api.js' ||
  56. file.substr(-11) === 'runtime.js' ||
  57. file.substr(-7) === 'spec.js';
  58. }).forEach(function(file){
  59. mocha.addFile(
  60. path.join('test', file)
  61. );
  62. });
  63. process.env.NODESASS_COV = 1;
  64. mocha.reporter(rep).run(function(failures) {
  65. process.on('exit', function () {
  66. process.exit(failures);
  67. });
  68. });
  69. }
  70. });
  71. });
  72. });
  73. };
  74. for (var i in sourcefiles) {
  75. processfile(sourcefiles[i]);
  76. }
  77. }
  78. /**
  79. * Run
  80. */
  81. coverage();