1 | 'use strict';
|
2 |
|
3 | var fs = require('fs');
|
4 |
|
5 | var unitTestReport = [];
|
6 | var result = function result(inp) {
|
7 | var testPathPattern = process.argv[process.argv.length - 1];
|
8 |
|
9 | if (testPathPattern.indexOf('--') != -1) {
|
10 | testPathPattern = '';
|
11 | } else {
|
12 | testPathPattern = fs.realpathSync(process.cwd());
|
13 | }
|
14 | var testPathRegex = new RegExp(testPathPattern);
|
15 | var testResults = inp.testResults;
|
16 | var testFilesArr = [];
|
17 | var testCaseFiles = [];
|
18 |
|
19 | if (fs.existsSync('./coverageTest/result.json')) {
|
20 | var jsonData = JSON.parse(fs.readFileSync('./coverageTest/result.json', 'utf8'));
|
21 | }
|
22 | testResults.forEach(function (testResult, i) {
|
23 | var filePath = testResult.testFilePath;
|
24 |
|
25 | if (!testPathRegex.test(filePath)) {
|
26 | return;
|
27 | }
|
28 | filePath = filePath.replace('.spec', '');
|
29 | filePath = filePath.replace('/__tests__', '');
|
30 | filePath = filePath.replace('/__test__', '');
|
31 | var relatPath = filePath.replace(process.cwd(), '').substring(1);
|
32 |
|
33 | if (jsonData.FILES.includes(relatPath) || jsonData.FILES.includes(testResult.testFilePath.replace(process.cwd(), ''))) {
|
34 | var fileJson = {};
|
35 | fileJson.testPath = testResult.testFilePath;
|
36 | fileJson.sourcePath = filePath;
|
37 | fileJson.data = testResult;
|
38 | testFilesArr.push(fileJson);
|
39 | jsonData.FILES.includes(relatPath) ? testCaseFiles.push(relatPath) : jsonData.FILES.includes(testResult.testFilePath.replace(process.cwd(), '')) ? testCaseFiles.push(testResult.testFilePath.replace(process.cwd(), '')) : '';
|
40 | }
|
41 | testResult.testResults && testResult.testResults.filter(function (t) {
|
42 | return t.status == 'failed';
|
43 | }).forEach(function (t) {
|
44 | unitTestReport.push({
|
45 | title: t.title,
|
46 | fullName: t.fullName,
|
47 | filePath: testResult.testFilePath
|
48 | });
|
49 | });
|
50 | });
|
51 | var coverageSummary = {};
|
52 | if (fs.existsSync('./commitCoverage/coverage-summary.json')) {
|
53 | coverageSummary = fs.readFileSync('./commitCoverage/coverage-summary.json').toString();
|
54 | if (coverageSummary.indexOf('\\') != -1) {
|
55 | coverageSummary = coverageSummary.replace(/\\/g, '\\\\');
|
56 | }
|
57 | }
|
58 |
|
59 | var coverageJson = JSON.parse(coverageSummary);
|
60 | var linesPercent = 0;
|
61 | var functionPercent = 0;
|
62 | var statementPerment = 0;
|
63 | var branchesPercent = 0;
|
64 |
|
65 | var fileList = '<h4>Changed files in last code check-in</h4><ul>';
|
66 | for (var i = 0; i < testFilesArr.length; i++) {
|
67 | var curSourceFile = testFilesArr[i].sourcePath;
|
68 | curSourceFile.replace(process.cwd(), '').substring(1);
|
69 | fileList = fileList + '<li>' + curSourceFile + '</li>';
|
70 | var coverageData = coverageJson[curSourceFile];
|
71 | if (coverageData == undefined) {
|
72 | console.log("Can't able to find source for " + testFilesArr[i].testPath + '\n Please check the file name and the path is correct for test file');
|
73 | continue;
|
74 | }
|
75 | linesPercent += coverageData.lines.pct;
|
76 | functionPercent += coverageData.functions.pct;
|
77 | statementPerment += coverageData.statements.pct;
|
78 | branchesPercent += coverageData.branches.pct;
|
79 | }
|
80 | fileList = fileList + '</ul>';
|
81 | if (testFilesArr.length == 0) {
|
82 | fileList = '<div></div>';
|
83 | }
|
84 |
|
85 | var uncoveredList = '<h4>Uncovered files :- </h4><ul>';
|
86 | jsonData.FILES.filter(function (fileName) {
|
87 | if (!testCaseFiles.includes(fileName)) {
|
88 | uncoveredList = uncoveredList + '<li>' + fileName + '</li>';
|
89 | }
|
90 | });
|
91 | uncoveredList = uncoveredList + '</ul>';
|
92 |
|
93 | var totalLinesPercent = linesPercent / (i * 100) * 100;
|
94 | var totalFunctionPercent = functionPercent / (i * 100) * 100;
|
95 | var totalStatementPercent = statementPerment / (i * 100) * 100;
|
96 | var totalBranchesPercent = branchesPercent / (i * 100) * 100;
|
97 | var totalPercentage = totalLinesPercent + totalFunctionPercent + totalStatementPercent + totalBranchesPercent;
|
98 | var coverage = (totalPercentage / 4).toFixed(2);
|
99 | coverage = Number(coverage);
|
100 |
|
101 | if (Number.isNaN(coverage)) {
|
102 | console.log("This build does't have any JS changes!");
|
103 | coverage = 0;
|
104 | } else {
|
105 | console.log('COVERAGE ' + coverage + '%');
|
106 | }
|
107 |
|
108 | var html = '<html><head><style>.red{font-weight:bold;color:red;}.green{font-weight:bold;color:green;}</style></head><body><br/>COVERAGE <span class="' + (coverage < 60 ? 'red' : 'green') + '">' + coverage + '%</span> <br/> less than 60% consider failure' + fileList + uncoveredList + '</body></html>';
|
109 |
|
110 | if (!fs.existsSync('./coverageTest')) {
|
111 | fs.mkdirSync('./coverageTest');
|
112 | fs.writeFileSync('./coverageTest/index.html', html, 'utf8');
|
113 | } else {
|
114 | fs.writeFileSync('./coverageTest/index.html', html, 'utf8');
|
115 | }
|
116 | return inp;
|
117 | };
|
118 |
|
119 | module.exports = result; |
\ | No newline at end of file |