1 |
|
2 | (function () {
|
3 | 'use strict';
|
4 |
|
5 | var path = require('path'),
|
6 | XmlDocument = require('xmldoc').XmlDocument,
|
7 | xmlEntities = new (require('html-entities').XmlEntities)();
|
8 |
|
9 | module.exports = function (grunt) {
|
10 | var PREFIX = '<?xml version="1.0"?><unitTest version="1">',
|
11 | SUFFIX = '</unitTest>';
|
12 |
|
13 | |
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 | function convert(inputFile, outputFile) {
|
26 | var resultContent = PREFIX,
|
27 | content = new XmlDocument(grunt.file.read(inputFile)),
|
28 | testsuites;
|
29 |
|
30 | testsuites = content.name === 'testsuite' ? [content] : content.childrenNamed("testsuite");
|
31 |
|
32 | var files = [];
|
33 | for (var i = 0; i < testsuites.length; i++) {
|
34 |
|
35 | testsuites[i].eachChild(function (testcase) {
|
36 | if (testcase.name === 'testcase') {
|
37 | var name = xmlEntities.encode(testcase.attr.name),
|
38 | classname = xmlEntities.encode(testcase.attr.classname),
|
39 | dirname = xmlEntities.encode(testcase.attr.dirname),
|
40 | extension = xmlEntities.encode(testcase.attr.extension),
|
41 | duration = (parseFloat(xmlEntities.encode(testcase.attr.time)) * 1000);
|
42 |
|
43 | var match = files.filter(function (file) {
|
44 | return file.name === classname;
|
45 | });
|
46 |
|
47 | var fileMatch;
|
48 | if (match.length === 0) {
|
49 | fileMatch = {
|
50 | path: 'test' + path.sep + dirname + path.sep + classname + extension,
|
51 | testcases: []
|
52 | };
|
53 | files.push(fileMatch);
|
54 | } else {
|
55 | fileMatch = match[0];
|
56 | }
|
57 |
|
58 | var tc = {
|
59 | name: name,
|
60 | duration: duration > 0 ? duration : 0,
|
61 | failure: []
|
62 | },
|
63 | failures = testcase.childrenNamed('failure'),
|
64 | skipped = testcase.childrenNamed('skipped'),
|
65 | error = testcase.childrenNamed('error');
|
66 |
|
67 | failures.forEach(function (failure) {
|
68 | tc.failure.push(failure.val);
|
69 | });
|
70 |
|
71 | if (skipped.length === 1) {
|
72 | tc.skipped = skipped[0].val;
|
73 | }
|
74 | if (error.length === 1) {
|
75 | tc.error = error[0].val;
|
76 | }
|
77 |
|
78 |
|
79 | fileMatch.testcases.push(tc);
|
80 | }
|
81 | });
|
82 | }
|
83 |
|
84 |
|
85 | files.forEach(function (file) {
|
86 | var fileContent = '<file path="' + file.path + '">';
|
87 | file.testcases.forEach(function (testcase) {
|
88 | fileContent = fileContent.concat('<testCase name="' + testcase.name + '" duration="' + testcase.duration + '"');
|
89 | if (testcase.failure.length > 0) {
|
90 | testcase.failure.forEach(function (failure) {
|
91 | fileContent = fileContent.concat('><failure message="">' + failure + '</failure>');
|
92 | });
|
93 |
|
94 | fileContent = fileContent.concat('</testCase>');
|
95 |
|
96 | } else if (testcase.skipped !== undefined) {
|
97 | fileContent = fileContent.concat('><skipped message=""/>');
|
98 | fileContent = fileContent.concat('</testCase>');
|
99 | } else if (testcase.error !== undefined) {
|
100 | fileContent = fileContent.concat('><error message="">' + testcase.error + '</error>');
|
101 | fileContent = fileContent.concat('</testCase>');
|
102 | } else {
|
103 | fileContent = fileContent.concat('/>');
|
104 | }
|
105 | });
|
106 |
|
107 | fileContent = fileContent.concat('</file>');
|
108 |
|
109 | resultContent = resultContent.concat(fileContent);
|
110 | });
|
111 |
|
112 |
|
113 | grunt.file.write(outputFile, resultContent.concat(SUFFIX), {encoding: 'utf8'});
|
114 | }
|
115 |
|
116 | return {
|
117 | convert: convert
|
118 | };
|
119 | };
|
120 | })();
|