grunt-reporter.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // grunt-reporter.js
  2. //
  3. // A communication bridge between blanket.js and the grunt-blanket-qunit plugin
  4. // Distributed as part of the grunt-blanket-qunit library
  5. //
  6. // Copyright (C) 2013 Model N, Inc.
  7. // Distributed under the MIT License
  8. //
  9. // Documentation and full license available at:
  10. // https://github.com/ModelN/grunt-blanket-qunit
  11. //
  12. (function (){
  13. "use strict";
  14. // this is an ugly hack, but it's the official way of communicating between
  15. // the parent phantomjs and the inner grunt-contrib-qunit library...
  16. var sendMessage = function sendMessage() {
  17. var args = [].slice.call(arguments);
  18. alert(JSON.stringify(args));
  19. };
  20. // helper function for computing coverage info for a particular file
  21. var reportFile = function( data ) {
  22. var ret = {
  23. coverage: 0,
  24. hits: 0,
  25. misses: 0,
  26. sloc: 0
  27. };
  28. for (var i = 0; i < data.source.length; i++) {
  29. var line = data.source[i];
  30. var num = i + 1;
  31. if (data[num] === 0) {
  32. ret.misses++;
  33. ret.sloc++;
  34. } else if (data[num] !== undefined) {
  35. ret.hits++;
  36. ret.sloc++;
  37. }
  38. }
  39. ret.coverage = ret.hits / ret.sloc * 100;
  40. return [ret.hits,ret.sloc];
  41. };
  42. // this function is invoked by blanket.js when the coverage data is ready. it will
  43. // compute per-file coverage info, and send a message to the parent phantomjs process
  44. // for each file, which the grunt task will use to report passes & failures.
  45. var reporter = function(cov){
  46. cov = window._$blanket;
  47. var sortedFileNames = [];
  48. var totals =[];
  49. for (var filename in cov) {
  50. if (cov.hasOwnProperty(filename)) {
  51. sortedFileNames.push(filename);
  52. }
  53. }
  54. sortedFileNames.sort();
  55. for (var i = 0; i < sortedFileNames.length; i++) {
  56. var thisFile = sortedFileNames[i];
  57. var data = cov[thisFile];
  58. var thisTotal= reportFile( data );
  59. sendMessage("blanket:fileDone", thisTotal, thisFile);
  60. }
  61. sendMessage("blanket:done");
  62. };
  63. blanket.customReporter = reporter;
  64. })();