Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
seleniumhq
GitHub Repository: seleniumhq/selenium
Path: blob/trunk/javascript/selenium-webdriver/test/io/zip_test.js
2885 views
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
18
'use strict'
19
20
const assert = require('node:assert')
21
const fs = require('node:fs')
22
const path = require('node:path')
23
24
const io = require('selenium-webdriver/io')
25
const zip = require('selenium-webdriver/io/zip')
26
const { InvalidArgumentError } = require('selenium-webdriver/lib/error')
27
const { locate } = require('../../lib/test/resources')
28
29
const XPI_PATH = locate('common/extensions/webextensions-selenium-example.xpi')
30
31
describe('io/zip', function () {
32
describe('unzip', function () {
33
it('creates destination dir if necessary', function () {
34
return io
35
.tmpDir()
36
.then((dir) => zip.unzip(XPI_PATH, dir))
37
.then((dir) => {
38
assertExists(path.join(dir, 'inject.js'))
39
assertExists(path.join(dir, 'manifest.json'))
40
assertExists(path.join(dir, 'META-INF/manifest.mf'))
41
})
42
})
43
})
44
45
describe('Zip', function () {
46
let dir
47
48
beforeEach(function () {
49
return io.tmpDir().then((d) => (dir = d))
50
})
51
52
it('can convert an archive to a buffer', function () {
53
let z = new zip.Zip()
54
return io
55
.mkdirp(path.join(dir, 'a/b/c/d/e'))
56
.then(() => {
57
return Promise.all([
58
io.write(path.join(dir, 'foo'), 'a file'),
59
io.write(path.join(dir, 'a/b/c/carrot'), 'an orange carrot'),
60
io.write(path.join(dir, 'a/b/c/d/e/elephant'), 'e is for elephant'),
61
])
62
})
63
.then(() => z.addDir(dir))
64
.then(() => Promise.all([io.tmpDir(), z.toBuffer()]))
65
.then(([outDir, buf]) => {
66
let output = path.join(outDir, 'out.zip')
67
return io
68
.write(output, buf)
69
.then(() => io.tmpDir())
70
.then((d) => zip.unzip(output, d))
71
.then((d) => {
72
assertContents(path.join(d, 'foo'), 'a file')
73
assertContents(path.join(d, 'a/b/c/carrot'), 'an orange carrot')
74
assertContents(path.join(d, 'a/b/c/d/e/elephant'), 'e is for elephant')
75
})
76
})
77
})
78
79
describe('getFile', function () {
80
it('returns archive file contents as a buffer', function () {
81
let foo = path.join(dir, 'foo')
82
fs.writeFileSync(foo, 'hello, world!')
83
84
let z = new zip.Zip()
85
return z
86
.addFile(foo)
87
.then(() => {
88
assert.ok(z.has('foo'))
89
return z.getFile('foo')
90
})
91
.then((buffer) => assert.strictEqual(buffer.toString('utf8'), 'hello, world!'))
92
})
93
94
it('returns an error if file is not in archive', function () {
95
let z = new zip.Zip()
96
assert.ok(!z.has('some-file'))
97
return z.getFile('some-file').then(
98
() => assert.fail('should have failed'),
99
(e) => assert.strictEqual(e.constructor, InvalidArgumentError),
100
)
101
})
102
103
it('returns a rejected promise if the requested path is a directory', function () {
104
let file = path.join(dir, 'aFile')
105
fs.writeFileSync(file, 'hello, world!')
106
107
let z = new zip.Zip()
108
return z
109
.addDir(dir, 'foo')
110
.then(() => z.getFile('foo'))
111
.then(
112
() => assert.fail('should have failed'),
113
(e) => assert.strictEqual(e.constructor, InvalidArgumentError),
114
)
115
.then(() => z.getFile('foo/aFile'))
116
.then((b) => assert.strictEqual(b.toString('utf8'), 'hello, world!'))
117
})
118
})
119
})
120
121
function assertExists(p) {
122
assert.ok(fs.existsSync(p), `expected ${p} to exist`)
123
}
124
125
function assertContents(p, c) {
126
assert.strictEqual(fs.readFileSync(p, 'utf8'), c, `unexpected file contents for ${p}`)
127
}
128
})
129
130