-
Notifications
You must be signed in to change notification settings - Fork 155
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(composer): invoke callback only once
In Node.js Streams, if an error is omitted downstream, that error can be propagated back through connected streams. In gulp-uglify, this error was being thrown by the next callback, being caught, and callback called a second time. This CL ensures the callback is called only once, by calling it outside the try..catch block wrapping the minifier. [terinjokes@gmail.com: amended change, rewrote tests and commit message] Signed-off-by: Terin Stock <terinjokes@gmail.com>
- Loading branch information
1 parent
2c7f656
commit 27b26d4
Showing
2 changed files
with
79 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
'use strict'; | ||
var test = require('tape-catch'); | ||
var assert = require('assert'); | ||
var Buffer = require('safe-buffer').Buffer; | ||
var Vinyl = require('vinyl'); | ||
var td = require('testdouble'); | ||
var through = require('through2'); | ||
var composer = require('../composer'); | ||
var GulpUglifyError = require('../lib/gulp-uglify-error'); | ||
|
||
test('composer should forward errors', function(t) { | ||
var badJsFile = new Vinyl({ | ||
cwd: '/', | ||
base: '/test/', | ||
path: '/test/file.js', | ||
contents: Buffer.from('invalid js') | ||
}); | ||
|
||
var uglify = td.object(['minify']); | ||
var logger = td.object(['warn']); | ||
var composed = composer(uglify, logger)({}); | ||
|
||
assert.throws(function() { | ||
composed.write(badJsFile); | ||
}, GulpUglifyError); | ||
|
||
td.reset(); | ||
t.end(); | ||
}); | ||
|
||
test("compose doesn't invoke callback twice", function(t) { | ||
var expectedErr = new Error(); | ||
var jsFile = new Vinyl({ | ||
cwd: '/', | ||
base: '/test/', | ||
path: '/test/file.js', | ||
contents: Buffer.from('var x = 123') | ||
}); | ||
|
||
var thrower = through.obj(function() { | ||
throw expectedErr; | ||
}); | ||
|
||
var uglify = td.object(['minify']); | ||
var logger = td.object(['warn']); | ||
td.when( | ||
uglify.minify( | ||
{'file.js': 'var x = 123'}, | ||
{ | ||
output: {} | ||
} | ||
) | ||
).thenReturn({ | ||
code: '', | ||
map: {} | ||
}); | ||
|
||
var composed = composer(uglify, logger)({}); | ||
composed.pipe(thrower); | ||
|
||
assert.throws( | ||
function() { | ||
composed.write(jsFile); | ||
}, | ||
function(err) { | ||
assert.strictEqual(err, expectedErr); | ||
return true; | ||
} | ||
); | ||
|
||
td.reset(); | ||
t.end(); | ||
}); |