Skip to content

Commit bb9f2cf

Browse files
author
Marc Rooding
committed
Replaced the usage of Grunt in the HTML formatter
1 parent d46db9b commit bb9f2cf

File tree

4 files changed

+99
-104
lines changed

4 files changed

+99
-104
lines changed

.editorconfig

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
root = true
2+
3+
[*.js]
4+
indent_style = space
5+
indent_size = 2

index.js

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -8,42 +8,42 @@ PluginError = gutil.PluginError;
88
var PLUGIN_NAME = 'gulp-protractor-cucumber-html-reporter';
99

1010
function gulpProtractorCucumberHtmlReport(opts) {
11-
var currentDir = __dirname;
12-
13-
opts = opts || {};
14-
if (!opts.dest) {
15-
opts.dest = '.';
16-
}
17-
if (!opts.filename) {
18-
opts.filename = 'report.html';
11+
var currentDir = __dirname;
12+
13+
opts = opts || {};
14+
if (!opts.dest) {
15+
opts.dest = '.';
16+
}
17+
if (!opts.filename) {
18+
opts.filename = 'report.html';
19+
}
20+
opts.templates = {
21+
featureTemplate: path.join(currentDir, './templates/feature_template.html'),
22+
headerTemplate: path.join(currentDir, './templates/header_template.html'),
23+
reportTemplate: path.join(currentDir, './templates/report_template.html'),
24+
scenarioTemplate: path.join(currentDir, './templates/scenario_template.html'),
25+
stepTemplate: path.join(currentDir, './templates/step_template.html')
26+
};
27+
28+
return through.obj(function (file, enc, cb) {
29+
if (file.isNull()) {
30+
return cb(null, file);
1931
}
20-
opts.templates = {
21-
featureTemplate: path.join(currentDir, './templates/feature_template.html'),
22-
headerTemplate: path.join(currentDir, './templates/header_template.html'),
23-
reportTemplate: path.join(currentDir, './templates/report_template.html'),
24-
scenarioTemplate: path.join(currentDir, './templates/scenario_template.html'),
25-
stepTemplate: path.join(currentDir, './templates/step_template.html')
26-
};
2732

28-
return through.obj(function (file, enc, cb) {
29-
if (file.isNull()) {
30-
return cb(null, file);
31-
}
33+
if (file.isBuffer()) {
34+
var testResults = JSON.parse(file.contents);
3235

33-
if (file.isBuffer()) {
34-
var testResults = JSON.parse(file.contents);
36+
fs.writeFileSync(opts.dest + '/' + opts.filename, formatter.generateReport(testResults, opts.templates));
3537

36-
fs.writeFileSync(opts.dest + '/' + opts.filename, formatter.generateReport(testResults, opts.templates));
38+
fs.copySync(currentDir + '/templates/assets', opts.dest + '/assets/');
3739

38-
fs.copySync(currentDir + '/templates/assets', opts.dest + '/assets/');
39-
40-
gutil.log('File ' + opts.filename + ' has been created in \'' + opts.dest + '\' directory');
41-
} else {
42-
throw new PluginError(PLUGIN_NAME, '[Error] Currently only buffers are supported');
43-
}
40+
gutil.log('File ' + opts.filename + ' has been created in \'' + opts.dest + '\' directory');
41+
} else {
42+
throw new PluginError(PLUGIN_NAME, '[Error] Currently only buffers are supported');
43+
}
4444

45-
return cb(null, file);
46-
});
45+
return cb(null, file);
46+
});
4747
}
4848

4949
// Exporting the plugin main function

lib/html_formatter.js

Lines changed: 63 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,29 @@
33
*/
44

55

6-
module.exports = (function() {
7-
8-
var grunt = require('grunt'),
9-
path = require('path'),
10-
fs = require('fs'),
11-
currentDir = path.dirname(fs.realpathSync(__filename)),
12-
statuses = {
13-
FAILED: 'failed',
14-
PASSED: 'passed',
15-
UNDEFINED: 'undefined',
16-
PENDING: 'pending',
17-
SKIPPED: 'skipped'
18-
},
19-
html = '',
20-
templates;
6+
module.exports = (function () {
7+
8+
var lodashTemplate = require('lodash.template'),
9+
path = require('path'),
10+
fs = require('fs'),
11+
statuses = {
12+
FAILED: 'failed',
13+
PASSED: 'passed',
14+
UNDEFINED: 'undefined',
15+
PENDING: 'pending',
16+
SKIPPED: 'skipped'
17+
},
18+
html = '',
19+
templates;
2120

2221
/**
2322
* Convert html tags to html entites
2423
* @param str
2524
* @returns {XML|string|*|void}
2625
*/
27-
function toHtmlEntities (str) {
26+
function toHtmlEntities(str) {
2827
str = str || '';
29-
return str.replace(/./gm, function(s) {
28+
return str.replace(/./gm, function (s) {
3029
return "&#" + s.charCodeAt(0) + ";";
3130
});
3231
}
@@ -38,17 +37,14 @@ module.exports = (function() {
3837
* @returns string
3938
*/
4039
function getStep(step) {
41-
var template = grunt.file.read(templates.stepTemplate),
42-
stepTemplate;
43-
44-
stepTemplate = grunt.template.process(template, {
45-
data:{
46-
status: step.result ? step.result.status: '',
47-
errorDetails: step.result ? toHtmlEntities(step.result.error_message): '',
48-
name: step.keyword + step.name
49-
}
40+
var template = fs.readFileSync(templates.stepTemplate),
41+
compiled = lodashTemplate(template.toString());
42+
43+
return compiled({
44+
status: step.result ? step.result.status : '',
45+
errorDetails: step.result ? toHtmlEntities(step.result.error_message) : '',
46+
name: step.keyword + step.name
5047
});
51-
return stepTemplate;
5248
}
5349

5450
/**
@@ -58,16 +54,13 @@ module.exports = (function() {
5854
* @returns string
5955
*/
6056
function getScenario(scenario, isPassed) {
61-
var template = grunt.file.read(templates.scenarioTemplate),
62-
scenarioTemplate;
57+
var template = fs.readFileSync(templates.scenarioTemplate),
58+
compiled = lodashTemplate(template.toString());
6359

64-
scenarioTemplate = grunt.template.process(template, {
65-
data:{
66-
status: isPassed ? statuses.PASSED : statuses.FAILED,
67-
name: scenario.keyword + ':' + scenario.name
68-
}
60+
return compiled({
61+
status: isPassed ? statuses.PASSED : statuses.FAILED,
62+
name: scenario.keyword + ':' + scenario.name
6963
});
70-
return scenarioTemplate;
7164
}
7265

7366
/**
@@ -77,19 +70,16 @@ module.exports = (function() {
7770
*/
7871
function getFeature(feature, scenariosNumberInFeature, passedScenariosNumberInFeature, stepsNumberInFeature, passedStepsInFeature) {
7972

80-
var template = grunt.file.read(templates.featureTemplate),
81-
featureTemplate;
73+
var template = fs.readFileSync(templates.featureTemplate),
74+
compiled = lodashTemplate(template.toString());
8275

83-
featureTemplate = grunt.template.process(template, {
84-
data:{
85-
name: feature.name,
86-
scenariosNumberInFeature: scenariosNumberInFeature,
87-
passedScenariosNumberInFeature: passedScenariosNumberInFeature,
88-
stepsNumberInFeature: stepsNumberInFeature,
89-
passedStepsInFeature: passedStepsInFeature
90-
}
76+
return compiled({
77+
name: feature.name,
78+
scenariosNumberInFeature: scenariosNumberInFeature,
79+
passedScenariosNumberInFeature: passedScenariosNumberInFeature,
80+
stepsNumberInFeature: stepsNumberInFeature,
81+
passedStepsInFeature: passedStepsInFeature
9182
});
92-
return featureTemplate;
9383
}
9484

9585
/**
@@ -111,15 +101,14 @@ module.exports = (function() {
111101
* @returns {*}
112102
*/
113103
function getHeader(scenariosNumber, passedScenarios, stepsNumber, passedSteps) {
114-
var template = grunt.file.read(templates.headerTemplate),
115-
header = grunt.template.process(template, {
116-
data:{
117-
status: areTestsPassed(scenariosNumber, passedScenarios) ? 'passed' : 'failed',
118-
scenariosSummary: scenariosNumber + ' scenarios ' + '( ' + passedScenarios + ' passed)',
119-
stepsSummary: stepsNumber + ' steps ' + '( ' + passedSteps + ' passed)'
120-
}
121-
});
122-
return header;
104+
var template = fs.readFileSync(templates.headerTemplate),
105+
compiled = lodashTemplate(template.toString());
106+
107+
return compiled({
108+
status: areTestsPassed(scenariosNumber, passedScenarios) ? 'passed' : 'failed',
109+
scenariosSummary: scenariosNumber + ' scenarios ' + '( ' + passedScenarios + ' passed)',
110+
stepsSummary: stepsNumber + ' steps ' + '( ' + passedSteps + ' passed)'
111+
});
123112
}
124113

125114
/**
@@ -129,19 +118,19 @@ module.exports = (function() {
129118
*/
130119
function generateHTML(testResults) {
131120
var stepsHtml = '',
132-
header = '',
133-
isPassed = false,
134-
passedScenarios = 0,
135-
passedSteps = 0,
136-
stepsNumber = 0,
137-
scenariosNumber = 0,
138-
scenariosNumberInFeature = 0,
139-
passedScenariosNumberInFeature = 0,
140-
stepsNumberInFeature = 0,
141-
passedStepsInFeature = 0,
142-
scenariosHtml = '',
143-
element,
144-
step;
121+
header = '',
122+
isPassed = false,
123+
passedScenarios = 0,
124+
passedSteps = 0,
125+
stepsNumber = 0,
126+
scenariosNumber = 0,
127+
scenariosNumberInFeature = 0,
128+
passedScenariosNumberInFeature = 0,
129+
stepsNumberInFeature = 0,
130+
passedStepsInFeature = 0,
131+
scenariosHtml = '',
132+
element,
133+
step;
145134

146135
for (var i = 0; i < testResults.length; i++) {
147136

@@ -181,7 +170,7 @@ module.exports = (function() {
181170
}
182171
}
183172
}
184-
html += getFeature(testResults[i],scenariosNumberInFeature, passedScenariosNumberInFeature, stepsNumberInFeature, passedStepsInFeature);
173+
html += getFeature(testResults[i], scenariosNumberInFeature, passedScenariosNumberInFeature, stepsNumberInFeature, passedStepsInFeature);
185174
html += scenariosHtml;
186175
}
187176
header = getHeader(scenariosNumber, passedScenarios, stepsNumber, passedSteps);
@@ -194,11 +183,11 @@ module.exports = (function() {
194183
* @returns string which contains html code of report
195184
*/
196185
function generateReport(html) {
197-
var template = grunt.file.read(templates.reportTemplate);
198-
return grunt.template.process(template, {
199-
data:{
200-
scenarios: html
201-
}
186+
var template = fs.readFileSync(templates.reportTemplate),
187+
compiled = lodashTemplate(template.toString());
188+
189+
return compiled({
190+
scenarios: html
202191
});
203192
}
204193

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
],
3232
"dependencies": {
3333
"gulp-util": "^3.0.0",
34+
"lodash.template": "^3.6.2",
3435
"through2": "^0.6.1"
3536
},
3637
"devDependencies": {

0 commit comments

Comments
 (0)