test($test): Add tests for new gzip support.

This commit is contained in:
Karishma Chadha 2018-04-05 13:34:56 -04:00
parent 9cf4d13ba8
commit 36b90ce8c6
6 changed files with 102 additions and 15 deletions

17
test/fixtures/data.js vendored
View file

@ -6,25 +6,30 @@ var path = require('path');
var sb = glob.sync(path.resolve(__dirname, './data/*.sb'));
var sb2 = glob.sync(path.resolve(__dirname, './data/*.sb2'));
var json = glob.sync(path.resolve(__dirname, './data/*.json'));
var gzipJson = glob.sync(path.resolve(__dirname, './data/*.json.gz'));
// Read files and convert to buffers
for (var x in sb) sb[x] = fs.readFileSync(sb[x]);
for (var y in sb2) sb2[y] = fs.readFileSync(sb2[y]);
for (var z in json) json[z] = fs.readFileSync(json[z]);
for (var w in sb) sb[w] = fs.readFileSync(sb[w]);
for (var x in sb2) sb2[x] = fs.readFileSync(sb2[x]);
for (var y in json) json[y] = fs.readFileSync(json[y]);
for (var z in gzipJson) gzipJson[z] = fs.readFileSync(gzipJson[z]);
// Return listings
module.exports = {
empty: {
sb: fs.readFileSync(path.resolve(__dirname, './data/_empty.sb')),
sb2: fs.readFileSync(path.resolve(__dirname, './data/_empty.sb2')),
json: fs.readFileSync(path.resolve(__dirname, './data/_empty.json'))
json: fs.readFileSync(path.resolve(__dirname, './data/_empty.json')),
gzipJson: fs.readFileSync(path.resolve(__dirname, './data/_empty.json.gz'))
},
example: {
sb: fs.readFileSync(path.resolve(__dirname, './data/_example.sb')),
sb2: fs.readFileSync(path.resolve(__dirname, './data/_example.sb2')),
json: fs.readFileSync(path.resolve(__dirname, './data/_example.json'))
json: fs.readFileSync(path.resolve(__dirname, './data/_example.json')),
gzipJson: fs.readFileSync(path.resolve(__dirname, './data/_example.json.gz'))
},
sb: sb,
sb2: sb2,
json: json
json: json,
gzipJson: gzipJson
};

BIN
test/fixtures/data/_empty.json.gz vendored Executable file

Binary file not shown.

BIN
test/fixtures/data/_example.json.gz vendored Executable file

Binary file not shown.

View file

@ -49,3 +49,16 @@ test('json string', function (t) {
t.end();
});
});
test('gzipped json', function (t) {
parser(data.empty.gzipJson, function (err, result) {
t.equal(err, null);
t.equal(Array.isArray(result), true);
var res = result[0];
var possibleZip = result[1];
t.type(res, 'object');
t.type(res.info, 'object');
t.equal(possibleZip, null);
t.end();
});
});

View file

@ -49,3 +49,16 @@ test('json string', function (t) {
t.end();
});
});
test('gzipped json', function (t) {
parser(data.example.gzipJson, function (err, result) {
t.equal(err, null);
t.equal(Array.isArray(result), true);
var res = result[0];
var possibleZip = result[1];
t.type(res, 'object');
t.type(res.info, 'object');
t.equal(possibleZip, null);
t.end();
});
});

View file

@ -7,6 +7,7 @@ var unzip = require('../../lib/unzip');
var fixtures = {
sb: path.resolve(__dirname, '../fixtures/data/_example.sb'),
sb2: path.resolve(__dirname, '../fixtures/data/_example.sb2'),
gzipJSON: path.resolve(__dirname, '../fixtures/data/_example.json.gz'),
zipFakeProjectJSON:
path.resolve(__dirname, '../fixtures/data/_zipFakeProjectJson.zip'),
zipNoProjectJSON:
@ -24,7 +25,7 @@ test('spec', function (t) {
test('sb', function (t) {
var buffer = new Buffer(fixtures.sb);
unzip(buffer, function (err, res) {
unzip(buffer, false, function (err, res) {
t.type(err, 'string');
var errorMessage = 'Failed to unzip and extract project.json';
t.equal(err.startsWith(errorMessage), true);
@ -35,7 +36,7 @@ test('sb', function (t) {
test('sb2', function (t) {
var buffer = new Buffer(fixtures.sb2);
unzip(buffer, function (err, res) {
unzip(buffer, false, function (err, res) {
t.equal(err, null);
t.equal(Array.isArray(res), true);
t.type(res[0], 'string');
@ -47,9 +48,23 @@ test('sb2', function (t) {
});
});
test('gzipped JSON', function (t) {
var buffer = new Buffer(fixtures.gzipJSON);
unzip(buffer, true, 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], null);
t.end();
});
});
test('zip without project json', function (t) {
var buffer = new Buffer(fixtures.zipNoProjectJSON);
unzip(buffer, function (err, res) {
unzip(buffer, false, function (err, res) {
t.type(err, 'string');
var errorMessage = 'Failed to unzip and extract project.json';
t.equal(err.startsWith(errorMessage), true);
@ -60,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, function (err, res) {
unzip(buffer, false, function (err, res) {
t.equal(err, null);
t.equal(Array.isArray(res), true);
t.type(res[0], 'string');
@ -73,8 +88,18 @@ test('zip with fake project json', function (t) {
});
});
test('random string instead of zip', function (t) {
unzip('this is not a zip', function (err, res) {
test('random string instead of zip #1', function (t) {
unzip('this is not a zip', 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) {
t.type(err, 'string');
var errorMessage = 'Failed to unzip and extract project.json';
t.equal(err.startsWith(errorMessage), true);
@ -85,7 +110,18 @@ test('random string instead of zip', function (t) {
test('undefined', function (t) {
var foo;
unzip(foo, function (err, obj) {
unzip(foo, 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('undefined isGZip', function (t) {
var foo;
unzip(foo, true, function (err, obj) {
t.type(err, 'string');
var errorMessage = 'Failed to unzip and extract project.json';
t.equal(err.startsWith(errorMessage), true);
@ -95,7 +131,17 @@ test('undefined', function (t) {
});
test('null', function (t) {
unzip(null, function (err, obj) {
unzip(null, 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) {
t.type(err, 'string');
var errorMessage = 'Failed to unzip and extract project.json';
t.equal(err.startsWith(errorMessage), true);
@ -105,7 +151,17 @@ test('null', function (t) {
});
test('object', function (t) {
unzip({}, function (err, obj) {
unzip({}, 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) {
t.type(err, 'string');
var errorMessage = 'Failed to unzip and extract project.json';
t.equal(err.startsWith(errorMessage), true);