UNPKG

4.51 kBJavaScriptView Raw
1/*jshint -W083 */
2(function () {
3 'use strict';
4
5 module.exports = function (grunt) {
6 var glob = require("glob"),
7 path = require('path'),
8 _ = require('lodash'),
9 fs = require('fs'),
10 JASMINE_TESTCASE_REGEX = /[^\w]x?it\s?\([\'\"](.*)[\'\"]/g,
11 XmlDocument = require('xmldoc').XmlDocument,
12 xmlEntities = new (require('html-entities').XmlEntities)();
13
14 /**
15 * Merge all junit results.
16 * Because the junit-reporter does not use the name of the spec file for the classname, the
17 * results need to be updated with the correct classname.
18 *
19 * #1 iterate over each file and check if there are jasmine tests by checking for it().
20 * #2 add all the jasmine tests to the array.
21 * #3 replace the classname with the spec filename.
22 * #4 append the tests to the result.
23 */
24 function mergeLocation(source, cwd, testDir, testType) {
25 var resultContent = '',
26 specs = [];
27
28 // #1
29 glob.sync('**/*.js', {cwd: testDir, root: '/'}).forEach(function (file) {
30 var tests = [],
31 content = grunt.file.read(testDir + path.sep + file),
32 matches = content.match(JASMINE_TESTCASE_REGEX);
33
34 if (matches !== null) {
35 // #2
36 for (var i = 0, len = matches.length; i < len; i++) {
37 tests.push(xmlEntities.encode(matches[i].replace(JASMINE_TESTCASE_REGEX, '$1')).replace(/\\/g, '')); // remove escaped characters
38 }
39 specs.push({
40 'name': path.basename(file, path.extname(file)),
41 'tests': tests
42 });
43 }
44 });
45
46 glob.sync(source, {cwd: cwd, root: '/'}).forEach(function (file) {
47 var reportFile = cwd + file,
48 reportFileExists = grunt.file.exists(reportFile);
49
50 if (reportFileExists) {
51 // #3
52 var content = new XmlDocument(grunt.file.read(reportFile)),
53 testsuites;
54 if (content.name === 'testsuite') {
55 testsuites = [content];
56 } else {
57 testsuites = content.childrenNamed("testsuite");
58 }
59 for (var i = 0; i < testsuites.length; i++) {
60 testsuites[i].eachChild(function (testcase) {
61 if (testcase.name === 'testcase') {
62 var name = xmlEntities.encode(testcase.attr.name);
63
64 var matchingSpecs = _.map(_.filter(specs, function (spec) {
65 return _.find(spec.tests, function (test) {
66 return test === name || xmlEntities.encode(test) === name;
67 });
68 }), 'name');
69 if (matchingSpecs.length === 0) {
70 grunt.log.warn('No spec filename found for test [' + name + ']');
71 } else {
72 var matchingSpec = matchingSpecs[0];
73 if (matchingSpecs.length > 1) {
74 var m = _.find(specs, function (spec) {
75 return spec.name === matchingSpec;
76 });
77 m.tests = _.without(m.tests, name);
78 }
79 testcase.attr.name = name;
80 testcase.attr.classname = matchingSpec.replace(/\./g, '_');
81 }
82 }
83 });
84 }
85 // #4
86 resultContent = resultContent.concat(testsuites);
87 } else {
88 if (!reportFileExists) {
89 grunt.log.warn('The specified' + testType + ' jUnit report [' + reportFile + '] does not exist');
90 }
91 }
92 });
93 return resultContent;
94 }
95
96 return {
97 mergeLocation: mergeLocation
98 };
99 };
100})();