mirror of
https://github.com/scratchfoundation/scratch-parser.git
synced 2025-08-28 22:18:45 -04:00
test($test): Rename zip to unzip for clarity. Add descriptive error message to
Added unit tests for unzip and added a user-friendly error message for unzip being unable to perform its duties of extracting a project.json from the given input (zip).
This commit is contained in:
parent
1501c957c7
commit
663d6ee8e6
6 changed files with 138 additions and 22 deletions
|
@ -1,4 +1,4 @@
|
|||
var zip = require('./zip');
|
||||
var unzip = require('./unzip');
|
||||
|
||||
/**
|
||||
* If input a buffer, transforms buffer into a UTF-8 string.
|
||||
|
@ -45,5 +45,5 @@ module.exports = function (input, callback) {
|
|||
// Return error if legacy encoding detected
|
||||
if (isLegacy) return callback('Parser only supports Scratch 2.X');
|
||||
|
||||
zip(input, callback);
|
||||
unzip(input, callback);
|
||||
};
|
||||
|
|
22
lib/unzip.js
Normal file
22
lib/unzip.js
Normal file
|
@ -0,0 +1,22 @@
|
|||
var JSZip = require('jszip');
|
||||
|
||||
/**
|
||||
* Unpacks a zip
|
||||
*
|
||||
* @param {String} Input
|
||||
*
|
||||
* @return {Object}
|
||||
*/
|
||||
module.exports = function (input, callback) {
|
||||
return JSZip.loadAsync(input)
|
||||
.then(function (zip) {
|
||||
return zip.file('project.json').async('string')
|
||||
.then(function (project) {
|
||||
return callback(null, [project, zip]);
|
||||
});
|
||||
})
|
||||
.catch(function(err) {
|
||||
var msg = 'Failed to unzip and extract project.json, with error: ';
|
||||
return callback(msg + JSON.stringify(err));
|
||||
});
|
||||
};
|
20
lib/zip.js
20
lib/zip.js
|
@ -1,20 +0,0 @@
|
|||
var JSZip = require('jszip');
|
||||
|
||||
/**
|
||||
* Unpacks a zip
|
||||
*
|
||||
* @param {String} Input
|
||||
*
|
||||
* @return {Object}
|
||||
*/
|
||||
module.exports = function (input, callback) {
|
||||
JSZip.loadAsync(input).then(function (zip) {
|
||||
zip.file('project.json').async('string')
|
||||
.then(function (project) {
|
||||
callback(null, [project, zip]);
|
||||
})
|
||||
.catch(function (err) {
|
||||
callback(err);
|
||||
});
|
||||
});
|
||||
};
|
BIN
test/fixtures/data/_zipFakeProjectJson.zip
vendored
Normal file
BIN
test/fixtures/data/_zipFakeProjectJson.zip
vendored
Normal file
Binary file not shown.
BIN
test/fixtures/data/_zipNoProjectJson.zip
vendored
Normal file
BIN
test/fixtures/data/_zipNoProjectJson.zip
vendored
Normal file
Binary file not shown.
114
test/unit/unzip.js
Normal file
114
test/unit/unzip.js
Normal file
|
@ -0,0 +1,114 @@
|
|||
var fs = require('fs');
|
||||
var path = require('path');
|
||||
var test = require('tap').test;
|
||||
var JSZip = require('jszip');
|
||||
var unzip = require('../../lib/unzip');
|
||||
|
||||
var fixtures = {
|
||||
sb: path.resolve(__dirname, '../fixtures/data/_example.sb'),
|
||||
sb2: path.resolve(__dirname, '../fixtures/data/_example.sb2'),
|
||||
zipFakeProjectJSON:
|
||||
path.resolve(__dirname, '../fixtures/data/_zipFakeProjectJson.zip'),
|
||||
zipNoProjectJSON:
|
||||
path.resolve(__dirname, '../fixtures/data/_zipNoProjectJson.zip')
|
||||
};
|
||||
|
||||
for (var i in fixtures) {
|
||||
fixtures[i] = fs.readFileSync(fixtures[i]);
|
||||
}
|
||||
|
||||
test('spec', function (t) {
|
||||
t.type(unzip, 'function');
|
||||
t.end();
|
||||
});
|
||||
|
||||
test('sb', function (t) {
|
||||
var buffer = new Buffer(fixtures.sb);
|
||||
unzip(buffer, 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('sb2', function (t) {
|
||||
var buffer = new Buffer(fixtures.sb2);
|
||||
unzip(buffer, function (err, res) {
|
||||
t.equal(err, null);
|
||||
t.equal(Array.isArray(res), true);
|
||||
t.type(res[0], 'string');
|
||||
t.doesNotThrow(function () {
|
||||
JSON.parse(res[0]);
|
||||
});
|
||||
t.equal(res[1] instanceof JSZip, true);
|
||||
t.end();
|
||||
});
|
||||
});
|
||||
|
||||
test('zip without project json', function (t) {
|
||||
var buffer = new Buffer(fixtures.zipNoProjectJSON);
|
||||
unzip(buffer, 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('zip with fake project json', function (t) {
|
||||
var buffer = new Buffer(fixtures.zipFakeProjectJSON);
|
||||
unzip(buffer, function(err, res) {
|
||||
t.equal(err, null);
|
||||
t.equal(Array.isArray(res), true);
|
||||
t.type(res[0], 'string');
|
||||
t.equal(res[0], 'this is not json\n');
|
||||
t.throws(function() {
|
||||
JSON.parse(res[0]);
|
||||
});
|
||||
t.equal(res[1] instanceof JSZip, true);
|
||||
t.end();
|
||||
});
|
||||
});
|
||||
|
||||
test('random string instead of zip', function (t) {
|
||||
unzip('this is not a zip', 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('undefined', function (t) {
|
||||
unzip(undefined, function (err, res) {
|
||||
t.type(err, 'string');
|
||||
var errorMessage = 'Failed to unzip and extract project.json';
|
||||
t.equal(err.startsWith(errorMessage), true);
|
||||
t.equal(res, undefined);
|
||||
t.end();
|
||||
});
|
||||
});
|
||||
|
||||
test('null', function (t) {
|
||||
unzip(null, function (err, obj) {
|
||||
t.type(err, 'string');
|
||||
var errorMessage = 'Failed to unzip and extract project.json';
|
||||
t.equal(err.startsWith(errorMessage), true);
|
||||
t.equal(obj, undefined);
|
||||
t.end();
|
||||
});
|
||||
});
|
||||
|
||||
test('object', function (t) {
|
||||
unzip({}, function (err, obj) {
|
||||
t.type(err, 'string');
|
||||
var errorMessage = 'Failed to unzip and extract project.json';
|
||||
t.equal(err.startsWith(errorMessage), true);
|
||||
t.equal(obj, undefined);
|
||||
t.end();
|
||||
});
|
||||
});
|
Loading…
Add table
Add a link
Reference in a new issue