Skip to content

Commit b49af64

Browse files
committed
Improve handling of multiple input files
The output filename was set to 'report.html' by default, which is a problem when generating report for multiple input files at once. Because the output filename did not change, the HTML reports overwrite eachother. The solution is to match the output to the input filename. So an input of foo.json would result in foo.html output. Thankfully gulp-util's `replaceFileExtension()` method makes this very easy. Along the way I changed the creation of the paths to use Node's own `path` library.
1 parent a35fffe commit b49af64

File tree

2 files changed

+15
-12
lines changed

2 files changed

+15
-12
lines changed

index.js

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,7 @@ function gulpProtractorCucumberHtmlReport(opts) {
1111
var currentDir = __dirname;
1212

1313
opts = opts || {};
14-
if (!opts.dest) {
15-
opts.dest = '.';
16-
}
17-
if (!opts.filename) {
18-
opts.filename = 'report.html';
19-
}
14+
2015
opts.templates = {
2116
featureTemplate: path.join(currentDir, './templates/feature_template.html'),
2217
headerTemplate: path.join(currentDir, './templates/header_template.html'),
@@ -33,17 +28,24 @@ function gulpProtractorCucumberHtmlReport(opts) {
3328
}
3429

3530
if (file.isBuffer()) {
31+
if (!opts.dest) {
32+
opts.dest = __dirname;
33+
}
34+
if (!opts.filename) {
35+
opts.filename = path.basename(gutil.replaceExtension(file.path, '.html'));
36+
}
3637
var testResults = JSON.parse(file.contents);
3738

38-
fs.open(opts.dest + '/' + opts.filename, 'w+', function (err, fd) {
39+
var output = path.join(opts.dest, opts.filename);
40+
fs.open(output, 'w+', function (err, fd) {
3941
if (err) {
4042
fs.mkdirsSync(opts.dest);
41-
fd = fs.openSync(opts.dest + '/' + opts.filename, 'w+');
43+
fd = fs.openSync(output, 'w+');
4244
}
4345
fs.writeSync(fd, formatter.generateReport(testResults, opts.templates));
44-
fs.copySync(currentDir + '/templates/assets', opts.dest + '/assets/');
46+
fs.copySync(path.join(__dirname, 'templates', 'assets'), path.join(opts.dest, 'assets'));
4547

46-
gutil.log(PLUGIN_NAME + ':', 'File \'' + opts.filename + '\' has been created in \'' + opts.dest + '\' directory');
48+
gutil.log(PLUGIN_NAME + ':', 'HTML report has been created in', gutil.colors.cyan(output));
4749

4850
cb(null, file);
4951
});

test/test.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,14 @@ describe('gulp-protractor-cucumber-html-report', function() {
1919

2020
var jsonFileBuffer = fs.readFileSync(path.join(__dirname, './data/cucumber_report.json'));
2121
var jsonFile = new File({
22-
contents: jsonFileBuffer
22+
contents: jsonFileBuffer,
23+
path: path.join(__dirname, './data/cucumber_report.json')
2324
});
2425

2526
var expectedReportBuffer = fs.readFileSync(path.join(__dirname, './data/expected_report.html'));
2627

2728
stream.on('data', function () {
28-
var resultBuffer = fs.readFileSync(outputFolder + '/report.html');
29+
var resultBuffer = fs.readFileSync(outputFolder + '/cucumber_report.html');
2930

3031
assert.equal(resultBuffer.toString(), expectedReportBuffer.toString());
3132
});

0 commit comments

Comments
 (0)