1 |
|
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 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 | function mergeLocation(source, cwd, testDir, testType) {
|
25 | var resultContent = '',
|
26 | specs = [];
|
27 |
|
28 |
|
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 |
|
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, ''));
|
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 |
|
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 |
|
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 | })();
|