zip_test.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // Licensed to the Software Freedom Conservancy (SFC) under one
  2. // or more contributor license agreements. See the NOTICE file
  3. // distributed with this work for additional information
  4. // regarding copyright ownership. The SFC licenses this file
  5. // to you under the Apache License, Version 2.0 (the
  6. // "License"); you may not use this file except in compliance
  7. // with the License. You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing,
  12. // software distributed under the License is distributed on an
  13. // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. // KIND, either express or implied. See the License for the
  15. // specific language governing permissions and limitations
  16. // under the License.
  17. 'use strict';
  18. const assert = require('assert');
  19. const fs = require('fs');
  20. const path = require('path');
  21. const io = require('../../io');
  22. const devmode = require('../../lib/devmode');
  23. const zip = require('../../io/zip');
  24. const {InvalidArgumentError} = require('../../lib/error');
  25. const SAMPLE_XPI_PATH =
  26. path.join(__dirname, '../../lib/test/data/firefox/sample.xpi');
  27. describe('io/zip', function() {
  28. describe('unzip', function() {
  29. it('creates destination dir if necessary', function() {
  30. return io.tmpDir()
  31. .then(dir => zip.unzip(SAMPLE_XPI_PATH, dir))
  32. .then(dir => {
  33. assertExists(path.join(dir, 'chrome.manifest'));
  34. assertExists(path.join(dir, 'content/overlay.js'));
  35. assertExists(path.join(dir, 'content/overlay.xul'));
  36. assertExists(path.join(dir, 'install.rdf'));
  37. });
  38. });
  39. });
  40. describe('Zip', function() {
  41. let dir;
  42. beforeEach(function() {
  43. return io.tmpDir().then(d => dir = d);
  44. });
  45. it('can convert an archive to a buffer', function() {
  46. let z = new zip.Zip;
  47. return io.mkdirp(path.join(dir, 'a/b/c/d/e')).then(() => {
  48. return Promise.all([
  49. io.write(path.join(dir, 'foo'), 'a file'),
  50. io.write(path.join(dir, 'a/b/c/carrot'), 'an orange carrot'),
  51. io.write(path.join(dir, 'a/b/c/d/e/elephant'), 'e is for elephant')
  52. ]);
  53. })
  54. .then(() => z.addDir(dir))
  55. .then(() => Promise.all([io.tmpDir(), z.toBuffer()]))
  56. .then(([outDir, buf]) => {
  57. let output = path.join(outDir, 'out.zip');
  58. return io.write(output, buf)
  59. .then(() => io.tmpDir())
  60. .then(d => zip.unzip(output, d))
  61. .then(d => {
  62. assertContents(path.join(d, 'foo'), 'a file');
  63. assertContents(path.join(d, 'a/b/c/carrot'), 'an orange carrot');
  64. assertContents(
  65. path.join(d, 'a/b/c/d/e/elephant'),
  66. 'e is for elephant');
  67. });
  68. });
  69. });
  70. describe('getFile', function() {
  71. it('returns archive file contents as a buffer', function() {
  72. let foo = path.join(dir, 'foo');
  73. fs.writeFileSync(foo, 'hello, world!');
  74. let z = new zip.Zip;
  75. return z.addFile(foo).then(() => {
  76. assert.ok(z.has('foo'));
  77. return z.getFile('foo');
  78. }).then(
  79. buffer => assert.equal(buffer.toString('utf8'), 'hello, world!'));
  80. });
  81. it('returns an error if file is not in archive', function() {
  82. let z = new zip.Zip;
  83. assert.ok(!z.has('some-file'));
  84. return z.getFile('some-file')
  85. .then(() => assert.fail('should have failed'),
  86. e => assert.strictEqual(e.constructor, InvalidArgumentError));
  87. });
  88. it(
  89. 'returns a rejected promise if the requested path is a directory',
  90. function() {
  91. let file = path.join(dir, 'aFile');
  92. fs.writeFileSync(file, 'hello, world!');
  93. let z = new zip.Zip;
  94. return z.addDir(dir, 'foo')
  95. .then(() => z.getFile('foo'))
  96. .then(
  97. () => assert.fail('should have failed'),
  98. e => assert.strictEqual(
  99. e.constructor, InvalidArgumentError))
  100. .then(() => z.getFile('foo/aFile'))
  101. .then(b => assert.equal(b.toString('utf8'), 'hello, world!'));
  102. });
  103. });
  104. });
  105. function assertExists(p) {
  106. assert.ok(fs.existsSync(p), `expected ${p} to exist`);
  107. }
  108. function assertContents(p, c) {
  109. assert.strictEqual(
  110. fs.readFileSync(p, 'utf8'), c, `unexpected file contents for ${p}`);
  111. }
  112. });