Skip to content

Commit

Permalink
Breaking: Error in .add if file is stream, callback when null
Browse files Browse the repository at this point in the history
  • Loading branch information
phated committed Jun 17, 2017
1 parent da3ba78 commit 720590d
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 23 deletions.
9 changes: 7 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,13 @@ function add(file, callback) {
return callback(new Error(PLUGIN_NAME + '-add: Not a vinyl file'));
}

// Bail early successfully if file already has sourcemap
if (file.sourceMap) {
// Bail early with an error if file has streaming contents
if (file.isStream()) {
return callback(new Error(PLUGIN_NAME + '-add: Streaming not supported'));
}

// Bail early successfully if file is null or already has a sourcemap
if (file.isNull() || file.sourceMap) {
return callback(null, file);
}

Expand Down
65 changes: 44 additions & 21 deletions test/add.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ var File = require('vinyl');
var path = require('path');
var expect = require('expect');
var convert = require('convert-source-map');
var miss = require('mississippi');

var sourcemaps = require('..');

var from = miss.from;

var sourceContent = fs.readFileSync(path.join(__dirname, 'assets/helloworld.js'));

function makeFile() {
Expand Down Expand Up @@ -64,14 +67,54 @@ describe('add', function() {
});
});

it('does not error if file argument is a vinyl object', function(done) {
it('does not error if file argument is a Vinyl object with Buffer contents', function(done) {
var file = makeFile();
sourcemaps.add(file, function(err) {
expect(err).toNotExist();
done();
});
});

it('errors if file argument is a Vinyl object with Stream contents', function(done) {
var file = makeFile();
file.contents = from([]);
sourcemaps.add(file, function(err) {
expect(err instanceof Error && err.message === 'vinyl-sourcemap-add: Streaming not supported').toExist();
done();
});
});

it('calls back with the untouched file if file already has a sourcemap', function(done) {
var sourceMap = {
version: 3,
names: [],
mappings: '',
sources: ['test.js'],
sourcesContent: ['testContent'],
};

var file = makeFile();
file.sourceMap = sourceMap;
sourcemaps.add(file, function(err, data) {
expect(data).toExist();
expect(File.isVinyl(data)).toEqual(true);
expect(data.sourceMap).toBe(sourceMap);
expect(data).toBe(file);
done(err);
});
});

it('calls back with the untouched file if file contents are null', function(done) {
var file = makeFile();
file.contents = null;
sourcemaps.add(file, function(err, outFile) {
expect(err).toNotExist();
expect(file).toExist();
expect(outFile).toEqual(file);
done(err);
});
});

it('adds an empty sourceMap if none are found', function(done) {
sourcemaps.add(makeFile(), function(err, data) {
expect(data.sourceMap).toExist();
Expand Down Expand Up @@ -301,24 +344,4 @@ describe('add', function() {
done(err);
});
});

it('passes file through when it already has a sourcemap', function(done) {
var sourceMap = {
version: 3,
names: [],
mappings: '',
sources: ['test.js'],
sourcesContent: ['testContent'],
};

var file = makeFile();
file.sourceMap = sourceMap;
sourcemaps.add(file, function(err, data) {
expect(data).toExist();
expect(File.isVinyl(data)).toEqual(true);
expect(data.sourceMap).toBe(sourceMap);
expect(data).toBe(file);
done(err);
});
});
});

0 comments on commit 720590d

Please sign in to comment.