Blame view

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