2014-03-25 20:05:30 +00:00
|
|
|
define(["Filer", "util"], function(Filer, util) {
|
|
|
|
|
2014-03-26 20:05:28 +00:00
|
|
|
describe('FileSystemShell.zip() and unzip()', function() {
|
2014-03-25 20:05:30 +00:00
|
|
|
beforeEach(util.setup);
|
|
|
|
afterEach(util.cleanup);
|
|
|
|
|
|
|
|
it('should be a function', function() {
|
|
|
|
var shell = util.shell();
|
2014-03-26 20:05:28 +00:00
|
|
|
expect(shell.zip).to.be.a('function');
|
2014-03-25 20:05:30 +00:00
|
|
|
expect(shell.unzip).to.be.a('function');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should fail when path argument is absent', function(done) {
|
|
|
|
var fs = util.fs();
|
|
|
|
var shell = fs.Shell();
|
|
|
|
|
|
|
|
shell.unzip(null, function(err) {
|
|
|
|
expect(err).to.exist;
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-05-21 17:07:31 +00:00
|
|
|
it('should download and unzip the contents of a zip file', function(done) {
|
2014-03-25 20:05:30 +00:00
|
|
|
var fs = util.fs();
|
|
|
|
var shell = fs.Shell();
|
2014-05-21 17:07:31 +00:00
|
|
|
var url = typeof XMLHttpRequest === "undefined" ? "http://localhost:1234/tests/test-file.txt.zip" : "/tests/test-file.txt.zip";
|
|
|
|
var filename = "test-file.txt.zip";
|
2014-03-25 20:05:30 +00:00
|
|
|
var contents = "This is a test file used in some of the tests.\n";
|
|
|
|
|
|
|
|
fs.writeFile('/original', contents, function(err) {
|
|
|
|
if(err) throw err;
|
|
|
|
|
2014-05-21 17:07:31 +00:00
|
|
|
shell.wget(url, {filename: filename}, function(err, path) {
|
2014-03-25 20:05:30 +00:00
|
|
|
if(err) throw err;
|
|
|
|
|
|
|
|
shell.unzip(path, function(err) {
|
|
|
|
if(err) throw err;
|
|
|
|
|
|
|
|
fs.readFile('/original', function(err, originalData) {
|
|
|
|
if(err) throw err;
|
|
|
|
|
|
|
|
|
|
|
|
fs.readFile('/test-file.txt', function(err, data) {
|
|
|
|
if(err) throw err;
|
|
|
|
expect(util.typedArrayEqual(data, originalData)).to.be.true;
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-05-21 17:07:31 +00:00
|
|
|
it('should download and unzip the contents of a zip file with a specified destination', function(done) {
|
2014-03-26 20:05:28 +00:00
|
|
|
var fs = util.fs();
|
|
|
|
var shell = fs.Shell();
|
|
|
|
var Path = Filer.Path;
|
2014-05-21 17:07:31 +00:00
|
|
|
var url = typeof XMLHttpRequest === "undefined" ? "http://localhost:1234/tests/test-file.txt.zip" : "/tests/test-file.txt.zip";
|
|
|
|
var filename = "test-file.txt.zip";
|
2014-03-26 20:05:28 +00:00
|
|
|
var contents = "This is a test file used in some of the tests.\n";
|
|
|
|
|
|
|
|
fs.writeFile('/original', contents, function(err) {
|
|
|
|
if(err) throw err;
|
|
|
|
|
2014-05-21 17:07:31 +00:00
|
|
|
shell.wget(url, {filename: filename}, function(err, path) {
|
2014-03-26 20:05:28 +00:00
|
|
|
if(err) throw err;
|
|
|
|
|
|
|
|
shell.tempDir(function(err, tmp) {
|
|
|
|
if (err) throw err;
|
|
|
|
|
|
|
|
shell.unzip(path, { destination: tmp }, function(err) {
|
|
|
|
if(err) throw err;
|
|
|
|
|
|
|
|
fs.readFile('/original', function(err, originalData) {
|
|
|
|
if(err) throw err;
|
|
|
|
|
|
|
|
fs.readFile(Path.join(tmp, 'test-file.txt'), function(err, data) {
|
|
|
|
if(err) throw err;
|
|
|
|
expect(util.typedArrayEqual(data, originalData)).to.be.true;
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-05-21 17:07:31 +00:00
|
|
|
it('should be able to zip and unzip a file', function(done) {
|
2014-03-25 21:11:31 +00:00
|
|
|
var fs = util.fs();
|
2014-03-26 17:57:16 +00:00
|
|
|
var file = '/test-file.txt';
|
2014-03-25 21:11:31 +00:00
|
|
|
var zipfile = file + '.zip';
|
|
|
|
var shell = fs.Shell();
|
2014-03-26 16:45:10 +00:00
|
|
|
var Path = Filer.Path;
|
2014-03-25 21:11:31 +00:00
|
|
|
var contents = "This is a test file used in some of the tests.\n";
|
|
|
|
|
2014-03-26 17:57:16 +00:00
|
|
|
fs.writeFile(file, contents, function(err) {
|
2014-03-25 21:11:31 +00:00
|
|
|
if(err) throw err;
|
|
|
|
|
2014-03-26 17:57:16 +00:00
|
|
|
shell.zip(zipfile, file, function(err) {
|
2014-03-25 21:11:31 +00:00
|
|
|
if(err) throw err;
|
|
|
|
|
2014-03-26 17:57:16 +00:00
|
|
|
fs.stat(zipfile, function(err, stats) {
|
2014-03-25 21:11:31 +00:00
|
|
|
if(err) throw err;
|
2014-03-26 17:57:16 +00:00
|
|
|
expect(stats.isFile()).to.be.true;
|
2014-03-25 21:11:31 +00:00
|
|
|
|
2014-03-26 17:57:16 +00:00
|
|
|
shell.rm(file, function(err) {
|
2014-03-25 21:11:31 +00:00
|
|
|
if(err) throw err;
|
|
|
|
|
2014-03-26 17:57:16 +00:00
|
|
|
shell.unzip(zipfile, function(err) {
|
2014-03-25 21:11:31 +00:00
|
|
|
if(err) throw err;
|
2014-03-26 17:57:16 +00:00
|
|
|
|
|
|
|
fs.stat(file, function(err, stats) {
|
|
|
|
if(err) throw err;
|
|
|
|
expect(stats.isFile()).to.be.true;
|
|
|
|
|
|
|
|
fs.readFile(Path.join('/', file), 'utf8', function(err, data) {
|
|
|
|
if(err) throw err;
|
|
|
|
expect(data).to.equal(contents);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2014-03-25 21:11:31 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-05-21 17:07:31 +00:00
|
|
|
it('should be able to handle a deep tree structure in a zip', function(done) {
|
2014-03-26 18:28:50 +00:00
|
|
|
// test-dir.zip has the following structure:
|
|
|
|
//
|
|
|
|
// test-dir/
|
|
|
|
// ├── test-dir2
|
|
|
|
// │ └── test-file.txt
|
|
|
|
// ├── test-file.txt
|
|
|
|
// └── test-file2.txt
|
|
|
|
|
|
|
|
var fs = util.fs();
|
|
|
|
var shell = fs.Shell();
|
2014-05-21 17:07:31 +00:00
|
|
|
var url = typeof XMLHttpRequest === "undefined" ? "http://localhost:1234/tests/test-dir.zip" : "/tests/test-dir.zip";
|
|
|
|
var filename = "test-dir.zip";
|
2014-03-26 18:28:50 +00:00
|
|
|
var Path = Filer.Path;
|
|
|
|
var contents = "This is a test file used in some of the tests.\n";
|
|
|
|
|
|
|
|
function confirmFile(filename, callback) {
|
|
|
|
filename = Path.join('/', filename);
|
|
|
|
fs.readFile(filename, 'utf8', function(err, data) {
|
|
|
|
if(err) {
|
|
|
|
console.error('Expected ' + filename + ' to exist.');
|
|
|
|
expect(false).to.be.true;
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
expect(data).to.equal(contents);
|
|
|
|
callback();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function confirmDir(dirname, callback) {
|
|
|
|
dirname = Path.join('/', dirname);
|
|
|
|
fs.stat(dirname, function(err, stats) {
|
|
|
|
if(err) {
|
|
|
|
console.error('Expected ' + dirname + ' to exist.');
|
|
|
|
expect(false).to.be.true;
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
expect(stats.isDirectory()).to.be.true;
|
|
|
|
callback();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-05-21 17:07:31 +00:00
|
|
|
shell.wget(url, {filename: filename}, function(err, path) {
|
2014-03-26 18:28:50 +00:00
|
|
|
if(err) throw err;
|
|
|
|
|
|
|
|
shell.unzip(path, function(err) {
|
|
|
|
if(err) throw err;
|
|
|
|
|
|
|
|
// Forgive the crazy indenting, trying to match tree structure ;)
|
|
|
|
confirmDir('test-dir', function() {
|
|
|
|
confirmDir('test-dir/test-dir2', function() {
|
|
|
|
confirmFile('test-dir/test-dir2/test-file.txt', function() {
|
|
|
|
confirmFile('test-dir/test-file.txt', function() {
|
|
|
|
confirmFile('test-dir/test-file2.txt', function() {
|
|
|
|
done();
|
|
|
|
});});});});});
|
|
|
|
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-05-21 17:07:31 +00:00
|
|
|
it('should be able to re-create (unzip/zip) a deep tree structure in a zip', function(done) {
|
2014-03-26 20:05:28 +00:00
|
|
|
// test-dir.zip has the following structure:
|
|
|
|
//
|
|
|
|
// test-dir/
|
|
|
|
// ├── test-dir2
|
|
|
|
// │ └── test-file.txt
|
|
|
|
// ├── test-file.txt
|
|
|
|
// └── test-file2.txt
|
|
|
|
|
|
|
|
var fs = util.fs();
|
|
|
|
var shell = fs.Shell();
|
2014-05-21 17:07:31 +00:00
|
|
|
var url = typeof XMLHttpRequest === "undefined" ? "http://localhost:1234/tests/test-dir.zip" : "/tests/test-dir.zip";
|
|
|
|
var filename = "test-dir.zip";
|
2014-03-26 20:05:28 +00:00
|
|
|
var Path = Filer.Path;
|
|
|
|
var contents = "This is a test file used in some of the tests.\n";
|
|
|
|
|
|
|
|
function confirmFile(filename, callback) {
|
|
|
|
filename = Path.join('/unzipped', filename);
|
|
|
|
fs.readFile(filename, 'utf8', function(err, data) {
|
|
|
|
if(err) {
|
|
|
|
console.error('Expected ' + filename + ' to exist.');
|
|
|
|
expect(false).to.be.true;
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
expect(data).to.equal(contents);
|
|
|
|
callback();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function confirmDir(dirname, callback) {
|
|
|
|
dirname = Path.join('/unzipped', dirname);
|
|
|
|
fs.stat(dirname, function(err, stats) {
|
|
|
|
if(err) {
|
|
|
|
console.error('Expected ' + dirname + ' to exist.');
|
|
|
|
expect(false).to.be.true;
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
expect(stats.isDirectory()).to.be.true;
|
|
|
|
callback();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-05-21 17:07:31 +00:00
|
|
|
shell.wget(url, {filename: filename}, function(err, path) {
|
2014-03-26 20:05:28 +00:00
|
|
|
if(err) throw err;
|
|
|
|
|
|
|
|
shell.unzip(path, function(err) {
|
|
|
|
if(err) throw err;
|
|
|
|
|
|
|
|
shell.zip('/test-dir2.zip', '/test-dir', { recursive: true }, function(err) {
|
|
|
|
if(err) throw err;
|
|
|
|
|
|
|
|
shell.mkdirp('/unzipped', function(err) {
|
|
|
|
if(err) throw err;
|
|
|
|
|
|
|
|
shell.unzip('/test-dir2.zip', { destination: '/unzipped' }, function(err) {
|
|
|
|
if(err) throw err;
|
|
|
|
|
|
|
|
// Forgive the crazy indenting, trying to match tree structure ;)
|
|
|
|
confirmDir('test-dir', function() {
|
|
|
|
confirmDir('test-dir/test-dir2', function() {
|
|
|
|
confirmFile('test-dir/test-dir2/test-file.txt', function() {
|
|
|
|
confirmFile('test-dir/test-file.txt', function() {
|
|
|
|
confirmFile('test-dir/test-file2.txt', function() {
|
|
|
|
done();
|
|
|
|
});});});});});
|
|
|
|
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-05-21 17:07:31 +00:00
|
|
|
it('should fail if the zipfile already exists', function(done) {
|
2014-03-26 20:05:28 +00:00
|
|
|
var fs = util.fs();
|
|
|
|
var shell = fs.Shell();
|
|
|
|
var file = "/file";
|
|
|
|
var zipfile = "/zipfile.zip";
|
|
|
|
var contents = "This is a test file used in some of the tests.\n";
|
|
|
|
|
|
|
|
fs.writeFile(file, contents, function(err) {
|
|
|
|
if(err) throw err;
|
|
|
|
|
|
|
|
shell.zip(zipfile, file, function(err) {
|
|
|
|
if(err) throw err;
|
|
|
|
shell.zip(zipfile, file, function(err) {
|
|
|
|
expect(err).to.exist;
|
|
|
|
expect(err.code).to.equal('EEXIST');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-03-25 20:05:30 +00:00
|
|
|
});
|
|
|
|
});
|