Path: blob/trunk/javascript/selenium-webdriver/test/io/zip_test.js
2885 views
// Licensed to the Software Freedom Conservancy (SFC) under one1// or more contributor license agreements. See the NOTICE file2// distributed with this work for additional information3// regarding copyright ownership. The SFC licenses this file4// to you under the Apache License, Version 2.0 (the5// "License"); you may not use this file except in compliance6// with the License. You may obtain a copy of the License at7//8// http://www.apache.org/licenses/LICENSE-2.09//10// Unless required by applicable law or agreed to in writing,11// software distributed under the License is distributed on an12// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY13// KIND, either express or implied. See the License for the14// specific language governing permissions and limitations15// under the License.1617'use strict'1819const assert = require('node:assert')20const fs = require('node:fs')21const path = require('node:path')2223const io = require('selenium-webdriver/io')24const zip = require('selenium-webdriver/io/zip')25const { InvalidArgumentError } = require('selenium-webdriver/lib/error')26const { locate } = require('../../lib/test/resources')2728const XPI_PATH = locate('common/extensions/webextensions-selenium-example.xpi')2930describe('io/zip', function () {31describe('unzip', function () {32it('creates destination dir if necessary', function () {33return io34.tmpDir()35.then((dir) => zip.unzip(XPI_PATH, dir))36.then((dir) => {37assertExists(path.join(dir, 'inject.js'))38assertExists(path.join(dir, 'manifest.json'))39assertExists(path.join(dir, 'META-INF/manifest.mf'))40})41})42})4344describe('Zip', function () {45let dir4647beforeEach(function () {48return io.tmpDir().then((d) => (dir = d))49})5051it('can convert an archive to a buffer', function () {52let z = new zip.Zip()53return io54.mkdirp(path.join(dir, 'a/b/c/d/e'))55.then(() => {56return Promise.all([57io.write(path.join(dir, 'foo'), 'a file'),58io.write(path.join(dir, 'a/b/c/carrot'), 'an orange carrot'),59io.write(path.join(dir, 'a/b/c/d/e/elephant'), 'e is for elephant'),60])61})62.then(() => z.addDir(dir))63.then(() => Promise.all([io.tmpDir(), z.toBuffer()]))64.then(([outDir, buf]) => {65let output = path.join(outDir, 'out.zip')66return io67.write(output, buf)68.then(() => io.tmpDir())69.then((d) => zip.unzip(output, d))70.then((d) => {71assertContents(path.join(d, 'foo'), 'a file')72assertContents(path.join(d, 'a/b/c/carrot'), 'an orange carrot')73assertContents(path.join(d, 'a/b/c/d/e/elephant'), 'e is for elephant')74})75})76})7778describe('getFile', function () {79it('returns archive file contents as a buffer', function () {80let foo = path.join(dir, 'foo')81fs.writeFileSync(foo, 'hello, world!')8283let z = new zip.Zip()84return z85.addFile(foo)86.then(() => {87assert.ok(z.has('foo'))88return z.getFile('foo')89})90.then((buffer) => assert.strictEqual(buffer.toString('utf8'), 'hello, world!'))91})9293it('returns an error if file is not in archive', function () {94let z = new zip.Zip()95assert.ok(!z.has('some-file'))96return z.getFile('some-file').then(97() => assert.fail('should have failed'),98(e) => assert.strictEqual(e.constructor, InvalidArgumentError),99)100})101102it('returns a rejected promise if the requested path is a directory', function () {103let file = path.join(dir, 'aFile')104fs.writeFileSync(file, 'hello, world!')105106let z = new zip.Zip()107return z108.addDir(dir, 'foo')109.then(() => z.getFile('foo'))110.then(111() => assert.fail('should have failed'),112(e) => assert.strictEqual(e.constructor, InvalidArgumentError),113)114.then(() => z.getFile('foo/aFile'))115.then((b) => assert.strictEqual(b.toString('utf8'), 'hello, world!'))116})117})118})119120function assertExists(p) {121assert.ok(fs.existsSync(p), `expected ${p} to exist`)122}123124function assertContents(p, c) {125assert.strictEqual(fs.readFileSync(p, 'utf8'), c, `unexpected file contents for ${p}`)126}127})128129130