UNPKG

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