Skip to content

Commit

Permalink
Breaking: Reduce argument juggling (closes #15)
Browse files Browse the repository at this point in the history
  • Loading branch information
phated committed Jun 17, 2017
1 parent 86aa9c7 commit 61473f8
Show file tree
Hide file tree
Showing 3 changed files with 195 additions and 88 deletions.
31 changes: 22 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ var helpers = require('./lib/helpers');

var PLUGIN_NAME = 'vinyl-sourcemap';

function isObject(value) {
return value && typeof value === 'object' && !Array.isArray(value);
}

/**
* Add a sourcemap to a vinyl file (async, with callback function)
* @param file
Expand All @@ -14,21 +18,23 @@ var PLUGIN_NAME = 'vinyl-sourcemap';
*/
function add(file, options, callback) {

// check if options are passed or a callback as second argument
// if there are 3 arguments, the options param should be an object
// Check if options or a callback are passed as second argument
if (typeof options === 'function') {
callback = options;
options = {};
} else if (!options || typeof options !== 'object') {
return callback(new Error(PLUGIN_NAME + '-add: Invalid argument: options'));
}

// Throw an error if the file argument is not a vinyl file
// Default options if not an object
if (!isObject(options)) {
options = {};
}

// Bail early an error if the file argument is not a Vinyl file
if (!File.isVinyl(file)) {
return callback(new Error(PLUGIN_NAME + '-add: Not a vinyl file'));
}

// Return the file if already has sourcemaps
// Bail early successfully if file already has sourcemap
if (file.sourceMap) {
return callback(null, file);
}
Expand Down Expand Up @@ -56,23 +62,30 @@ function add(file, options, callback) {
*/
function write(file, options, callback) {

// Check arguments for optional destPath, options, or callback function
// Check if options or a callback are passed as second argument
if (typeof options === 'function') {
callback = options;
options = {};
}

options = options || {};
// Default options if not an object
if (!isObject(options)) {
options = {};
}

// Throw an error if the file argument is not a vinyl file
// Bail early with an error if the file argument is not a Vinyl file
if (!File.isVinyl(file)) {
return callback(new Error(PLUGIN_NAME + '-write: Not a vinyl file'));
}

// Bail early with an error if file has streaming contents
// TODO: needs test
if (file.isStream()) {
return callback(new Error(PLUGIN_NAME + '-write: Streaming not supported'));
}

// Bail early successfully if file is null or doesn't have sourcemap
// TODO: needs test (at least for null contents?)
if (file.isNull() || !file.sourceMap) {
return callback(null, file);
}
Expand Down
121 changes: 84 additions & 37 deletions test/add.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,56 +39,103 @@ function makeFileWithInlineSourceMap() {

describe('add', function() {

it('should not accept null as argument', function(done) {
sourcemaps.add(null, function(err) {
expect(err instanceof Error && err.message === 'vinyl-sourcemap-add: Not a vinyl file').toExist();
done();
describe('ensures file argument', function() {

it('is not undefined', function(done) {
sourcemaps.add(undefined, function(err) {
expect(err instanceof Error && err.message === 'vinyl-sourcemap-add: Not a vinyl file').toExist();
done();
});
});
});

it('should not accept an empty object as argument', function(done) {
sourcemaps.add({}, function(err) {
expect(err instanceof Error && err.message === 'vinyl-sourcemap-add: Not a vinyl file').toExist();
done();
it('is not null', function(done) {
sourcemaps.add(null, function(err) {
expect(err instanceof Error && err.message === 'vinyl-sourcemap-add: Not a vinyl file').toExist();
done();
});
});
});

it('should not accept a stream as argument', function(done) {
sourcemaps.add(new stream.Readable(), function(err) {
expect(err instanceof Error && err.message === 'vinyl-sourcemap-add: Not a vinyl file').toExist();
done();
it('is not a plain object', function(done) {
sourcemaps.add({}, function(err) {
expect(err instanceof Error && err.message === 'vinyl-sourcemap-add: Not a vinyl file').toExist();
done();
});
});
});

it('should not accept undefined as options argument', function(done) {
var file = makeFile();
sourcemaps.add(file, undefined, function(err) {
expect(err instanceof Error && err.message === 'vinyl-sourcemap-add: Invalid argument: options').toExist();
done();
// TODO: seems like a bad test
it('is not a stream', function(done) {
sourcemaps.add(new stream.Readable(), function(err) {
expect(err instanceof Error && err.message === 'vinyl-sourcemap-add: Not a vinyl file').toExist();
done();
});
});
});

it('should not accept null as options argument', function(done) {
var file = makeFile();
sourcemaps.add(file, null, function(err) {
expect(err instanceof Error && err.message === 'vinyl-sourcemap-add: Invalid argument: options').toExist();
done();
it('is a vinyl object', function(done) {
var file = makeFile();
sourcemaps.add(file, function(err) {
expect(err).toNotExist();
done();
});
});
});

it('should not accept empty string as options argument', function(done) {
var file = makeFile();
sourcemaps.add(file, '', function(err) {
expect(err instanceof Error && err.message === 'vinyl-sourcemap-add: Invalid argument: options').toExist();
done();
describe('ensures options argument', function() {


it('is defaulted if undefined', function(done) {
var file = makeFile();
sourcemaps.add(file, undefined, function(err) {
expect(err).toNotExist();
done();
});
});
});

it('should not accept boolean as options argument', function(done) {
var file = makeFile();
sourcemaps.add(file, true, function(err) {
expect(err instanceof Error && err.message === 'vinyl-sourcemap-add: Invalid argument: options').toExist();
done();
it('is defaulted if null', function(done) {
var file = makeFile();
sourcemaps.add(file, null, function(err) {
expect(err).toNotExist();
done();
});
});

it('is defaulted if empty string', function(done) {
var file = makeFile();
sourcemaps.add(file, '', function(err) {
expect(err).toNotExist();
done();
});
});

it('is defaulted if non-empty string', function(done) {
var file = makeFile();
sourcemaps.add(file, 'invalid', function(err) {
expect(err).toNotExist();
done();
});
});

it('is defaulted if boolean false', function(done) {
var file = makeFile();
sourcemaps.add(file, false, function(err) {
expect(err).toNotExist();
done();
});
});

it('is defaulted if boolean true', function(done) {
var file = makeFile();
sourcemaps.add(file, true, function(err) {
expect(err).toNotExist();
done();
});
});

it('is defaulted if array', function(done) {
var file = makeFile();
sourcemaps.add(file, [], function(err) {
expect(err).toNotExist();
done();
});
});
});

Expand Down
131 changes: 89 additions & 42 deletions test/write.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,66 +50,113 @@ function base64JSON(object) {

describe('write', function() {

it('should return an error when on valid vinyl file is provided', function(done) {
sourcemaps.write(undefined, function(err) {
expect(err instanceof Error && err.message === 'vinyl-sourcemap-write: Not a vinyl file').toExist();
done();
describe('ensures file argument', function() {

it('is not undefined', function(done) {
sourcemaps.write(undefined, function(err) {
expect(err instanceof Error && err.message === 'vinyl-sourcemap-write: Not a vinyl file').toExist();
done();
});
});
});

it('should return an error when on valid vinyl file is provided', function(done) {
sourcemaps.write(null, function(err) {
expect(err instanceof Error && err.message === 'vinyl-sourcemap-write: Not a vinyl file').toExist();
done();
it('is not null', function(done) {
sourcemaps.write(null, function(err) {
expect(err instanceof Error && err.message === 'vinyl-sourcemap-write: Not a vinyl file').toExist();
done();
});
});
});

it('should return an error when on valid vinyl file is provided', function(done) {
sourcemaps.write({}, function(err) {
expect(err instanceof Error && err.message === 'vinyl-sourcemap-write: Not a vinyl file').toExist();
done();
it('is not a plain object', function(done) {
sourcemaps.write({}, function(err) {
expect(err instanceof Error && err.message === 'vinyl-sourcemap-write: Not a vinyl file').toExist();
done();
});
});
});

it('should return an error when on valid vinyl file is provided', function(done) {
sourcemaps.write(new stream.Readable(), function(err) {
expect(err instanceof Error && err.message === 'vinyl-sourcemap-write: Not a vinyl file').toExist();
done();
// TODO: seems like a bad test
it('is not a stream', function(done) {
sourcemaps.write(new stream.Readable(), function(err) {
expect(err instanceof Error && err.message === 'vinyl-sourcemap-write: Not a vinyl file').toExist();
done();
});
});
});

it('calls back with the untouched file if sourceMap property does not exist', function(done) {
var file = makeFile();
delete file.sourceMap;
sourcemaps.write(file, function(err, outFile) {
expect(err).toNotExist();
expect(file).toExist();
expect(outFile).toEqual(file);
done(err);
it('is a vinyl object', function(done) {
var file = makeFile();
sourcemaps.write(file, function(err) {
expect(err).toNotExist();
done();
});
});
});

it('should return an error when invalid arguments are provided', function(done) {
var file = makeFile();
sourcemaps.write(file, undefined, function(err) {
expect(err).toNotExist();
done();
describe('ensures options argument', function() {

it('is defaulted if undefined', function(done) {
var file = makeFile();
sourcemaps.write(file, undefined, function(err) {
expect(err).toNotExist();
done();
});
});
});

it('should return an error when invalid arguments are provided', function(done) {
var file = makeFile();
sourcemaps.write(file, null, function(err) {
expect(err).toNotExist();
done();
it('is defaulted if null', function(done) {
var file = makeFile();
sourcemaps.write(file, null, function(err) {
expect(err).toNotExist();
done();
});
});

it('is defaulted if empty string', function(done) {
var file = makeFile();
sourcemaps.write(file, '', function(err) {
expect(err).toNotExist();
done();
});
});

it('is defaulted if non-empty string', function(done) {
var file = makeFile();
sourcemaps.write(file, 'invalid', function(err) {
expect(err).toNotExist();
done();
});
});

it('is defaulted if boolean false', function(done) {
var file = makeFile();
sourcemaps.write(file, false, function(err) {
expect(err).toNotExist();
done();
});
});

it('is defaulted if boolean true', function(done) {
var file = makeFile();
sourcemaps.write(file, true, function(err) {
expect(err).toNotExist();
done();
});
});

it('is defaulted if array', function(done) {
var file = makeFile();
sourcemaps.write(file, [], function(err) {
expect(err).toNotExist();
done();
});
});
});

it('should return an error when invalid arguments are provided', function(done) {
it('calls back with the untouched file if sourceMap property does not exist', function(done) {
var file = makeFile();
sourcemaps.write(file, true, function(err) {
delete file.sourceMap;
sourcemaps.write(file, function(err, outFile) {
expect(err).toNotExist();
done();
expect(file).toExist();
expect(outFile).toEqual(file);
done(err);
});
});

Expand Down

0 comments on commit 61473f8

Please sign in to comment.