From 585cf8c55fb8fb3ddd238e3363a0a4f8dd33b295 Mon Sep 17 00:00:00 2001 From: Mathias Buus Date: Sat, 21 Dec 2013 02:04:02 +0100 Subject: [PATCH] more tests --- test/extract.js | 227 +++++++++++++++++++++++++++++++++++ test/fixtures/index.js | 4 +- test/fixtures/multi-file.tar | Bin 0 -> 3072 bytes test/fixtures/types.tar | Bin 0 -> 2048 bytes test/pack.js | 73 ++++++++++- 5 files changed, 302 insertions(+), 2 deletions(-) create mode 100644 test/fixtures/multi-file.tar create mode 100644 test/fixtures/types.tar diff --git a/test/extract.js b/test/extract.js index f144fb1..32dc6ad 100644 --- a/test/extract.js +++ b/test/extract.js @@ -38,4 +38,231 @@ test('one-file', function(t) { }); extract.end(fs.readFileSync(fixtures.ONE_FILE_TAR)); +}); + +test('chunked-one-file', function(t) { + t.plan(3); + + var extract = tar.extract(); + var noEntries = false; + + extract.on('entry', function(header, stream, callback) { + t.deepEqual(header, { + name: 'test.txt', + mode: 0644, + uid: 501, + gid: 20, + size: 12, + mtime: new Date(1387580181000), + type: 'file', + linkname: null, + uname: 'maf', + gname: 'staff', + devmajor: 0, + devminor: 0 + }); + + stream.pipe(concat(function(data) { + noEntries = true; + t.same(data.toString(), 'hello world\n'); + callback(); + })); + }); + + extract.on('finish', function() { + t.ok(noEntries); + }); + + var b = fs.readFileSync(fixtures.ONE_FILE_TAR); + + for (var i = 0; i < b.length; i += 321) { + extract.write(b.slice(i, i+321)); + } + extract.end(); +}); + + +test('multi-file', function(t) { + t.plan(5); + + var extract = tar.extract(); + var noEntries = false; + + var onfile1 = function(header, stream, callback) { + t.deepEqual(header, { + name: 'file-1.txt', + mode: 0644, + uid: 501, + gid: 20, + size: 12, + mtime: new Date(1387580181000), + type: 'file', + linkname: null, + uname: 'maf', + gname: 'staff', + devmajor: 0, + devminor: 0 + }); + + extract.on('entry', onfile2); + stream.pipe(concat(function(data) { + t.same(data.toString(), 'i am file-1\n'); + callback(); + })); + }; + + var onfile2 = function(header, stream, callback) { + t.deepEqual(header, { + name: 'file-2.txt', + mode: 0644, + uid: 501, + gid: 20, + size: 12, + mtime: new Date(1387580181000), + type: 'file', + linkname: null, + uname: 'maf', + gname: 'staff', + devmajor: 0, + devminor: 0 + }); + + stream.pipe(concat(function(data) { + noEntries = true; + t.same(data.toString(), 'i am file-2\n'); + callback(); + })); + }; + + extract.once('entry', onfile1); + + extract.on('finish', function() { + t.ok(noEntries); + }); + + extract.end(fs.readFileSync(fixtures.MULTI_FILE_TAR)); +}); + +test('chunked-multi-file', function(t) { + t.plan(5); + + var extract = tar.extract(); + var noEntries = false; + + var onfile1 = function(header, stream, callback) { + t.deepEqual(header, { + name: 'file-1.txt', + mode: 0644, + uid: 501, + gid: 20, + size: 12, + mtime: new Date(1387580181000), + type: 'file', + linkname: null, + uname: 'maf', + gname: 'staff', + devmajor: 0, + devminor: 0 + }); + + extract.on('entry', onfile2); + stream.pipe(concat(function(data) { + t.same(data.toString(), 'i am file-1\n'); + callback(); + })); + }; + + var onfile2 = function(header, stream, callback) { + t.deepEqual(header, { + name: 'file-2.txt', + mode: 0644, + uid: 501, + gid: 20, + size: 12, + mtime: new Date(1387580181000), + type: 'file', + linkname: null, + uname: 'maf', + gname: 'staff', + devmajor: 0, + devminor: 0 + }); + + stream.pipe(concat(function(data) { + noEntries = true; + t.same(data.toString(), 'i am file-2\n'); + callback(); + })); + }; + + extract.once('entry', onfile1); + + extract.on('finish', function() { + t.ok(noEntries); + }); + + var b = fs.readFileSync(fixtures.MULTI_FILE_TAR); + for (var i = 0; i < b.length; i += 321) { + extract.write(b.slice(i, i+321)); + } + extract.end(); +}); + +test('types', function(t) { + t.plan(3); + + var extract = tar.extract(); + var noEntries = false; + + var ondir = function(header, stream, callback) { + t.deepEqual(header, { + name: 'directory', + mode: 0755, + uid: 501, + gid: 20, + size: 0, + mtime: new Date(1387580181000), + type: 'directory', + linkname: null, + uname: 'maf', + gname: 'staff', + devmajor: 0, + devminor: 0 + }); + stream.on('data', function() { + t.ok(false); + }); + extract.once('entry', onlink); + callback(); + }; + + var onlink = function(header, stream, callback) { + t.deepEqual(header, { + name: 'directory-link', + mode: 0755, + uid: 501, + gid: 20, + size: 0, + mtime: new Date(1387580181000), + type: 'symlink', + linkname: 'directory', + uname: 'maf', + gname: 'staff', + devmajor: 0, + devminor: 0 + }); + stream.on('data', function() { + t.ok(false); + }); + noEntries = true; + callback(); + }; + + extract.once('entry', ondir); + + extract.on('finish', function() { + t.ok(noEntries); + }); + + extract.end(fs.readFileSync(fixtures.TYPES_TAR)); }); \ No newline at end of file diff --git a/test/fixtures/index.js b/test/fixtures/index.js index 5a59c7e..9658a5a 100644 --- a/test/fixtures/index.js +++ b/test/fixtures/index.js @@ -1,3 +1,5 @@ var path = require('path'); -exports.ONE_FILE_TAR = path.join(__dirname, 'one-file.tar'); \ No newline at end of file +exports.ONE_FILE_TAR = path.join(__dirname, 'one-file.tar'); +exports.MULTI_FILE_TAR = path.join(__dirname, 'multi-file.tar'); +exports.TYPES_TAR = path.join(__dirname, 'types.tar'); diff --git a/test/fixtures/multi-file.tar b/test/fixtures/multi-file.tar new file mode 100644 index 0000000000000000000000000000000000000000..65775c1e0fcd3d8df5f9483906d60758e7f39c88 GIT binary patch literal 3072 zcmeHGK?(yg2+X;!@B=OCYWkiW8d%u9v?=uYm4u}W3wsL$su$xhh>n>&FOMtT%BO^G zfrwEAg5`VEnvq*=kOc`4vnq)SD3W0%wp^=fPsiBFfiIJ&-fuWJP zp_u}MDeatuFuAn2B(ccAfFU