test(tests): fix up tests to accommodate new apis

This commit is contained in:
Karishma Chadha 2018-05-02 18:17:32 -04:00
parent b808e7447f
commit b0d1e46cd8
10 changed files with 154 additions and 64 deletions

View file

@ -7,20 +7,22 @@ var validate = require('./lib/validate');
/**
* Unpacks, parses, validates, and analyzes Scratch projects. If successful,
* will return a valid Scratch project object with appended metadata.
* @param {boolean} isSprite Whether this is a sprite (true) or whole project (false)
* @param {Buffer | string} input Buffer or string representing project
* @param {boolean} isSprite Whether this is a sprite (true) or whole project (false)
* @param {Function} callback Returns error or project data
*/
module.exports = function (isSprite, input, callback) {
module.exports = function (input, isSprite, callback) {
// First unpack the input (need this outside of the async waterfall so that
// unpackedProject can be refered to again)
unpack(isSprite, input, function (err, unpackedProject) {
unpack(input, isSprite, function (err, unpackedProject) {
if (err) return callback(err);
async.waterfall([
function (cb) {
parse(unpackedProject[0], cb);
},
// TODO is there a better way to pass this arg
// than partially applying this funciton?
validate.bind(null, isSprite)
], function (error, validatedInput) {
// One more callback wrapper so that we can re-package everything

View file

@ -108,6 +108,10 @@
"currentCostumeIndex"
]
},
"stage_child": {
"type": "object",
"description": "A child of the stage, can be a sprite or a monitor"
},
"stage_object" : {
"type": "object",
"properties": {
@ -175,7 +179,7 @@
},
"children": {
"type": "array",
"items": {"$ref": "#/definitions/sprite_object"}
"items": {"$ref": "#/definitions/stage_child"}
}
},
"required": [

View file

@ -4,13 +4,13 @@ var unzip = require('./unzip');
* If input a buffer, transforms buffer into a UTF-8 string.
* If input is encoded in zip or gzip format, the input will be extracted and decoded.
* If input is a string, passes that string along to the given callback.
* @param {Buffer | string} input Project data
* @param {boolean} isSprite Whether the input should be treated as
* a sprite (true) or a whole project (false)
* @param {Buffer | string} input Project data
* @param {Function} callback Error or stringified project data
* @return {void}
*/
module.exports = function (isSprite, input, callback) {
module.exports = function (input, isSprite, callback) {
if (typeof input === 'string') {
// Pass string to callback
return callback(null, [input, null]);
@ -48,5 +48,5 @@ module.exports = function (isSprite, input, callback) {
// Return error if legacy encoding detected
if (isLegacy) return callback('Parser only supports Scratch 2.X and above');
unzip(isSprite, input, isGZip, callback);
unzip(input, isGZip, isSprite, callback);
};

View file

@ -3,14 +3,14 @@ var GZip = require('gzip-js');
/**
* Unpacks a zip or gzip file.
* @param {boolean} isSprite Whether the input should be treated as
* a sprite (true) or whole project (false)
* @param {string} input Zip file provided as a string
* @param {boolean} isGZip Whether the input is a GZip file, otherwise treat as zip
* @param {boolean} isSprite Whether the input should be treated as
* a sprite (true) or whole project (false)
* @param {array} callback Array including both the project and zip archive
* @return {void}
*/
module.exports = function (isSprite, input, isGZip, callback) {
module.exports = function (input, isGZip, isSprite, callback) {
var msg = 'Failed to unzip and extract project.json, with error: ';
if (isGZip) {
var unpackedProject = null;

View file

@ -4,7 +4,7 @@ var data = require('../fixtures/data');
var parser = require('../../index');
test('sb', function (t) {
parser(data.empty.sb, function (err, res) {
parser(data.empty.sb, false, function (err, res) {
t.type(err, 'string');
t.type(res, 'undefined');
t.end();
@ -12,7 +12,7 @@ test('sb', function (t) {
});
test('sb2', function (t) {
parser(data.empty.sb2, function (err, result) {
parser(data.empty.sb2, false, function (err, result) {
t.equal(err, null);
t.equal(Array.isArray(result), true);
var res = result[0];
@ -25,7 +25,7 @@ test('sb2', function (t) {
});
test('json', function (t) {
parser(data.empty.json, function (err, result) {
parser(data.empty.json, false, function (err, result) {
t.equal(err, null);
t.equal(Array.isArray(result), true);
var res = result[0];
@ -38,7 +38,7 @@ test('json', function (t) {
});
test('json string', function (t) {
parser(data.empty.json.toString('utf-8'), function (err, result) {
parser(data.empty.json.toString('utf-8'), false, function (err, result) {
t.equal(err, null);
t.equal(Array.isArray(result), true);
var res = result[0];
@ -51,7 +51,7 @@ test('json string', function (t) {
});
test('gzipped json', function (t) {
parser(data.empty.gzipJson, function (err, result) {
parser(data.empty.gzipJson, false, function (err, result) {
t.equal(err, null);
t.equal(Array.isArray(result), true);
var res = result[0];

View file

@ -4,7 +4,7 @@ var data = require('../fixtures/data');
var parser = require('../../index');
test('sb', function (t) {
parser(data.example.sb, function (err, res) {
parser(data.example.sb, false, function (err, res) {
t.type(err, 'string');
t.type(res, 'undefined');
t.end();
@ -12,7 +12,7 @@ test('sb', function (t) {
});
test('sb2', function (t) {
parser(data.example.sb2, function (err, result) {
parser(data.example.sb2, false, function (err, result) {
t.equal(err, null);
t.equal(Array.isArray(result), true);
var res = result[0];
@ -25,7 +25,7 @@ test('sb2', function (t) {
});
test('json', function (t) {
parser(data.example.json, function (err, result) {
parser(data.example.json, false, function (err, result) {
t.equal(err, null);
t.equal(Array.isArray(result), true);
var res = result[0];
@ -38,7 +38,7 @@ test('json', function (t) {
});
test('json string', function (t) {
parser(data.example.json.toString('utf-8'), function (err, result) {
parser(data.example.json.toString('utf-8'), false, function (err, result) {
t.equal(err, null);
t.equal(Array.isArray(result), true);
var res = result[0];
@ -51,7 +51,7 @@ test('json string', function (t) {
});
test('gzipped json', function (t) {
parser(data.example.gzipJson, function (err, result) {
parser(data.example.gzipJson, false, function (err, result) {
t.equal(err, null);
t.equal(Array.isArray(result), true);
var res = result[0];

View file

@ -7,7 +7,7 @@ test('sb', function (t) {
var set = data.sb;
t.plan(set.length * 2);
for (var i in data.sb) {
parser(data.sb[i], function (err, res) {
parser(data.sb[i], false, function (err, res) {
t.type(err, 'string');
t.type(res, 'undefined');
});
@ -18,7 +18,7 @@ test('sb2', function (t) {
var set = data.sb2;
t.plan(set.length * 5);
for (var i in data.sb2) {
parser(data.sb2[i], function (err, result) {
parser(data.sb2[i], false, function (err, result) {
t.equal(err, null);
t.equal(Array.isArray(result), true);
var res = result[0];
@ -34,7 +34,7 @@ test('json', function (t) {
var set = data.json;
t.plan(set.length * 5);
for (var i in data.json) {
parser(data.json[i], function (err, result) {
parser(data.json[i], false, function (err, result) {
t.equal(err, null);
t.equal(Array.isArray(result), true);
var res = result[0];
@ -50,7 +50,7 @@ test('json string', function (t) {
var set = data.json;
t.plan(set.length * 5);
for (var i in data.json) {
parser(data.json[i].toString('utf-8'), function (err, result) {
parser(data.json[i].toString('utf-8'), false, function (err, result) {
t.equal(err, null);
t.equal(Array.isArray(result), true);
var res = result[0];

View file

@ -21,7 +21,7 @@ test('spec', function (t) {
test('sb', function (t) {
var buffer = new Buffer(fixtures.sb);
unpack(buffer, function (err, res) {
unpack(buffer, false, function (err, res) {
t.type(err, 'string');
t.type(res, 'undefined');
t.end();
@ -30,7 +30,7 @@ test('sb', function (t) {
test('sb2', function (t) {
var buffer = new Buffer(fixtures.sb2);
unpack(buffer, function (err, res) {
unpack(buffer, false, function (err, res) {
t.equal(err, null);
t.equal(Array.isArray(res), true);
t.type(res[0], 'string');
@ -42,9 +42,18 @@ test('sb2', function (t) {
});
});
test('sb2 does not validate as sprite', function (t) {
var buffer = new Buffer(fixtures.sb2);
unpack(buffer, true, function (err, res) {
t.type(err, 'string');
t.type(res, 'undefined');
t.end();
});
});
test('json', function (t) {
var buffer = new Buffer(fixtures.json);
unpack(buffer, function (err, res) {
unpack(buffer, false, function (err, res) {
t.equal(err, null);
t.equal(Array.isArray(res), true);
t.type(res[0], 'string');
@ -58,7 +67,7 @@ test('json', function (t) {
test('json utf-8 string', function (t) {
var buffer = new Buffer(fixtures.json);
unpack(buffer.toString('utf-8'), function (err, res) {
unpack(buffer.toString('utf-8'), false, function (err, res) {
t.equal(err, null);
t.equal(Array.isArray(res), true);
t.type(res[0], 'string');
@ -71,7 +80,16 @@ test('json utf-8 string', function (t) {
});
test('invalid string', function (t) {
unpack('this is not json', function (err, res) {
unpack('this is not json', false, function (err, res) {
t.equal(err, null);
t.equal(Array.isArray(res), true);
t.type(res[0], 'string');
t.throws(function () {
JSON.parse(res[0]);
});
t.equal(res[1], null);
});
unpack('this is not json', true, function (err, res) {
t.equal(err, null);
t.equal(Array.isArray(res), true);
t.type(res[0], 'string');
@ -85,7 +103,7 @@ test('invalid string', function (t) {
test('undefined', function (t) {
var foo;
unpack(foo, function (err, res) {
unpack(false, foo, function (err, res) {
t.type(err, 'string');
t.type(res, 'undefined');
t.end();
@ -93,7 +111,7 @@ test('undefined', function (t) {
});
test('null', function (t) {
unpack(null, function (err, obj) {
unpack(false, null, function (err, obj) {
t.type(err, 'string');
t.type(obj, 'undefined');
t.end();
@ -101,7 +119,7 @@ test('null', function (t) {
});
test('object', function (t) {
unpack({}, function (err, obj) {
unpack(false, {}, function (err, obj) {
t.type(err, 'string');
t.type(obj, 'undefined');
t.end();

View file

@ -18,6 +18,8 @@ for (var i in fixtures) {
fixtures[i] = fs.readFileSync(fixtures[i]);
}
var errorMessage = 'Failed to unzip and extract project.json';
test('spec', function (t) {
t.type(unzip, 'function');
t.end();
@ -25,9 +27,8 @@ test('spec', function (t) {
test('sb', function (t) {
var buffer = new Buffer(fixtures.sb);
unzip(buffer, false, function (err, res) {
unzip(buffer, false, false, function (err, res) {
t.type(err, 'string');
var errorMessage = 'Failed to unzip and extract project.json';
t.equal(err.startsWith(errorMessage), true);
t.type(res, 'undefined');
t.end();
@ -36,7 +37,7 @@ test('sb', function (t) {
test('sb2', function (t) {
var buffer = new Buffer(fixtures.sb2);
unzip(buffer, false, function (err, res) {
unzip(buffer, false, false, function (err, res) {
t.equal(err, null);
t.equal(Array.isArray(res), true);
t.type(res[0], 'string');
@ -50,7 +51,7 @@ test('sb2', function (t) {
test('gzipped JSON', function (t) {
var buffer = new Buffer(fixtures.gzipJSON);
unzip(buffer, true, function (err, res) {
unzip(buffer, true, false, function (err, res) {
t.equal(err, null);
t.equal(Array.isArray(res), true);
t.type(res[0], 'string');
@ -64,9 +65,8 @@ test('gzipped JSON', function (t) {
test('zip without project json', function (t) {
var buffer = new Buffer(fixtures.zipNoProjectJSON);
unzip(buffer, false, function (err, res) {
unzip(buffer, false, false, function (err, res) {
t.type(err, 'string');
var errorMessage = 'Failed to unzip and extract project.json';
t.equal(err.startsWith(errorMessage), true);
t.type(res, 'undefined');
t.end();
@ -75,7 +75,7 @@ test('zip without project json', function (t) {
test('zip with fake project json', function (t) {
var buffer = new Buffer(fixtures.zipFakeProjectJSON);
unzip(buffer, false, function (err, res) {
unzip(buffer, false, false, function (err, res) {
t.equal(err, null);
t.equal(Array.isArray(res), true);
t.type(res[0], 'string');
@ -88,20 +88,38 @@ test('zip with fake project json', function (t) {
});
});
test('random string instead of zip #1', function (t) {
unzip('this is not a zip', false, function (err, res) {
var randomString = 'this is not a zip';
test('random string instead of zip, whole project', function (t) {
unzip(randomString, false, false, function (err, res) {
t.type(err, 'string');
var errorMessage = 'Failed to unzip and extract project.json';
t.equal(err.startsWith(errorMessage), true);
t.type(res, 'undefined');
t.end();
});
});
test('random string instead of zip #2', function (t) {
unzip('this is not a zip', true, function (err, res) {
test('random string instead of zip, sprite', function (t) {
unzip(randomString, false, true, function (err, res) {
t.type(err, 'string');
t.equal(err.startsWith(errorMessage), true);
t.type(res, 'undefined');
t.end();
});
});
test('random string instead of gzip, whole project ', function (t) {
unzip(randomString, true, false, function (err, res) {
t.type(err, 'string');
t.equal(err.startsWith(errorMessage), true);
t.type(res, 'undefined');
t.end();
});
});
test('random string instead of gzip, sprite', function (t) {
unzip(randomString, true, true, function (err, res) {
t.type(err, 'string');
var errorMessage = 'Failed to unzip and extract project.json';
t.equal(err.startsWith(errorMessage), true);
t.type(res, 'undefined');
t.end();
@ -110,9 +128,8 @@ test('random string instead of zip #2', function (t) {
test('undefined', function (t) {
var foo;
unzip(foo, false, function (err, obj) {
unzip(foo, false, false, function (err, obj) {
t.type(err, 'string');
var errorMessage = 'Failed to unzip and extract project.json';
t.equal(err.startsWith(errorMessage), true);
t.type(obj, 'undefined');
t.end();
@ -121,49 +138,80 @@ test('undefined', function (t) {
test('undefined isGZip', function (t) {
var foo;
unzip(foo, true, function (err, obj) {
unzip(foo, true, false, function (err, obj) {
t.type(err, 'string');
var errorMessage = 'Failed to unzip and extract project.json';
t.equal(err.startsWith(errorMessage), true);
t.type(obj, 'undefined');
t.end();
});
});
test('null', function (t) {
unzip(null, false, function (err, obj) {
test('null instead of zip, whole project', function (t) {
unzip(null, false, false, function (err, obj) {
t.type(err, 'string');
var errorMessage = 'Failed to unzip and extract project.json';
t.equal(err.startsWith(errorMessage), true);
t.type(obj, 'undefined');
t.end();
});
});
test('null isGZip', function (t) {
unzip(null, true, function (err, obj) {
test('null instead of zip, sprite', function (t) {
unzip(null, false, true, function (err, obj) {
t.type(err, 'string');
var errorMessage = 'Failed to unzip and extract project.json';
t.equal(err.startsWith(errorMessage), true);
t.type(obj, 'undefined');
t.end();
});
});
test('object', function (t) {
unzip({}, false, function (err, obj) {
test('null instead of gzip, whole project', function (t) {
unzip(null, true, false, function (err, obj) {
t.type(err, 'string');
var errorMessage = 'Failed to unzip and extract project.json';
t.equal(err.startsWith(errorMessage), true);
t.type(obj, 'undefined');
t.end();
});
});
test('object isGZip', function (t) {
unzip({}, true, function (err, obj) {
test('null instead of gzip, sprite', function (t) {
unzip(null, true, true, function (err, obj) {
t.type(err, 'string');
t.equal(err.startsWith(errorMessage), true);
t.type(obj, 'undefined');
t.end();
});
});
test('object instead of zip, whole project', function (t) {
unzip({}, false, false, function (err, obj) {
t.type(err, 'string');
t.equal(err.startsWith(errorMessage), true);
t.type(obj, 'undefined');
t.end();
});
});
test('object instead of zip, sprite', function (t) {
unzip({}, false, true, function (err, obj) {
t.type(err, 'string');
t.equal(err.startsWith(errorMessage), true);
t.type(obj, 'undefined');
t.end();
});
});
test('object instead of gzip, whole project', function (t) {
unzip({}, true, false, function (err, obj) {
t.type(err, 'string');
t.equal(err.startsWith(errorMessage), true);
t.type(obj, 'undefined');
t.end();
});
});
test('object instead of gzip, sprite', function (t) {
unzip({}, true, false, function (err, obj) {
t.type(err, 'string');
var errorMessage = 'Failed to unzip and extract project.json';
t.equal(err.startsWith(errorMessage), true);
t.type(obj, 'undefined');
t.end();

View file

@ -8,15 +8,33 @@ test('spec', function (t) {
});
test('valid', function (t) {
validate(JSON.parse(data.example.json), function (err, res) {
validate(false, JSON.parse(data.example.json), function (err, res) {
t.equal(err, null);
t.type(res, 'object');
t.end();
});
});
test('invalid', function (t) {
validate({foo: 1}, function (err, res) {
test('invalid, whole project', function (t) {
validate(false, {foo: 1}, function (err, res) {
t.type(err, 'object');
t.type(err.validationError, 'string');
var sb2Errs = err.sb2Errors;
t.equal(Array.isArray(sb2Errs), true);
t.type(res, 'undefined');
t.type(sb2Errs[0], 'object');
t.type(sb2Errs[0].keyword, 'string');
t.type(sb2Errs[0].dataPath, 'string');
t.type(sb2Errs[0].schemaPath, 'string');
t.type(sb2Errs[0].message, 'string');
t.type(sb2Errs[0].params, 'object');
t.type(sb2Errs[0].params.missingProperty, 'string');
t.end();
});
});
test('invalid, sprite', function (t) {
validate(true, {foo: 1}, function (err, res) {
t.type(err, 'object');
t.type(err.validationError, 'string');
var sb2Errs = err.sb2Errors;