Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
seleniumhq
GitHub Repository: seleniumhq/selenium
Path: blob/trunk/javascript/selenium-webdriver/test/io/io_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
fs = require('node:fs'),
22
path = require('node:path')
23
24
const io = require('selenium-webdriver/io')
25
26
describe('io', function () {
27
describe('copy', function () {
28
let tmpDir
29
30
before(function () {
31
return io.tmpDir().then(function (d) {
32
tmpDir = d
33
34
fs.writeFileSync(path.join(d, 'foo'), 'Hello, world')
35
})
36
})
37
38
it('can copy one file to another', function () {
39
return io.tmpFile().then(function (f) {
40
return io.copy(path.join(tmpDir, 'foo'), f).then(function (p) {
41
assert.strictEqual(p, f)
42
assert.strictEqual('Hello, world', fs.readFileSync(p, 'utf-8'))
43
})
44
})
45
})
46
47
it('can copy symlink to destination', function () {
48
if (process.platform === 'win32') {
49
return // No symlinks on windows.
50
}
51
fs.symlinkSync(path.join(tmpDir, 'foo'), path.join(tmpDir, 'symlinked-foo'))
52
return io.tmpFile().then(function (f) {
53
return io.copy(path.join(tmpDir, 'symlinked-foo'), f).then(function (p) {
54
assert.strictEqual(p, f)
55
assert.strictEqual('Hello, world', fs.readFileSync(p, 'utf-8'))
56
})
57
})
58
})
59
60
it('fails if given a directory as a source', function () {
61
return io
62
.tmpFile()
63
.then(function (f) {
64
return io.copy(tmpDir, f)
65
})
66
.then(
67
function () {
68
throw Error('Should have failed with a type error')
69
},
70
function () {
71
// Do nothing; expected.
72
},
73
)
74
})
75
})
76
77
describe('copyDir', function () {
78
it('copies recursively', function () {
79
return io.tmpDir().then(function (dir) {
80
fs.writeFileSync(path.join(dir, 'file1'), 'hello')
81
fs.mkdirSync(path.join(dir, 'sub'))
82
fs.mkdirSync(path.join(dir, 'sub/folder'))
83
fs.writeFileSync(path.join(dir, 'sub/folder/file2'), 'goodbye')
84
85
return io.tmpDir().then(function (dst) {
86
return io.copyDir(dir, dst).then(function (ret) {
87
assert.strictEqual(dst, ret)
88
89
assert.strictEqual('hello', fs.readFileSync(path.join(dst, 'file1'), 'utf-8'))
90
assert.strictEqual('goodbye', fs.readFileSync(path.join(dst, 'sub/folder/file2'), 'utf-8'))
91
})
92
})
93
})
94
})
95
96
it('creates destination dir if necessary', function () {
97
return io
98
.tmpDir()
99
.then(function (srcDir) {
100
fs.writeFileSync(path.join(srcDir, 'foo'), 'hi')
101
return io.tmpDir().then(function (dstDir) {
102
return io.copyDir(srcDir, path.join(dstDir, 'sub'))
103
})
104
})
105
.then(function (p) {
106
assert.strictEqual('sub', path.basename(p))
107
assert.strictEqual('hi', fs.readFileSync(path.join(p, 'foo'), 'utf-8'))
108
})
109
})
110
111
it('supports regex exclusion filter', function () {
112
return io
113
.tmpDir()
114
.then(function (src) {
115
fs.writeFileSync(path.join(src, 'foo'), 'a')
116
fs.writeFileSync(path.join(src, 'bar'), 'b')
117
fs.writeFileSync(path.join(src, 'baz'), 'c')
118
fs.mkdirSync(path.join(src, 'sub'))
119
fs.writeFileSync(path.join(src, 'sub/quux'), 'd')
120
fs.writeFileSync(path.join(src, 'sub/quot'), 'e')
121
122
return io.tmpDir().then(function (dst) {
123
return io.copyDir(src, dst, /(bar|quux)/)
124
})
125
})
126
.then(function (dir) {
127
assert.strictEqual('a', fs.readFileSync(path.join(dir, 'foo'), 'utf-8'))
128
assert.strictEqual('c', fs.readFileSync(path.join(dir, 'baz'), 'utf-8'))
129
assert.strictEqual('e', fs.readFileSync(path.join(dir, 'sub/quot'), 'utf-8'))
130
131
assert.ok(!fs.existsSync(path.join(dir, 'bar')))
132
assert.ok(!fs.existsSync(path.join(dir, 'sub/quux')))
133
})
134
})
135
136
it('supports exclusion filter function', function () {
137
return io
138
.tmpDir()
139
.then(function (src) {
140
fs.writeFileSync(path.join(src, 'foo'), 'a')
141
fs.writeFileSync(path.join(src, 'bar'), 'b')
142
fs.writeFileSync(path.join(src, 'baz'), 'c')
143
fs.mkdirSync(path.join(src, 'sub'))
144
fs.writeFileSync(path.join(src, 'sub/quux'), 'd')
145
fs.writeFileSync(path.join(src, 'sub/quot'), 'e')
146
147
return io.tmpDir().then(function (dst) {
148
return io.copyDir(src, dst, function (f) {
149
return f !== path.join(src, 'foo') && f !== path.join(src, 'sub/quot')
150
})
151
})
152
})
153
.then(function (dir) {
154
assert.strictEqual('b', fs.readFileSync(path.join(dir, 'bar'), 'utf-8'))
155
assert.strictEqual('c', fs.readFileSync(path.join(dir, 'baz'), 'utf-8'))
156
assert.strictEqual('d', fs.readFileSync(path.join(dir, 'sub/quux'), 'utf-8'))
157
158
assert.ok(!fs.existsSync(path.join(dir, 'foo')))
159
assert.ok(!fs.existsSync(path.join(dir, 'sub/quot')))
160
})
161
})
162
})
163
164
describe('exists', function () {
165
var dir
166
167
before(function () {
168
return io.tmpDir().then(function (d) {
169
dir = d
170
})
171
})
172
173
it('returns a rejected promise if input value is invalid', function () {
174
return io.exists(undefined).then(
175
() => assert.fail('should have failed'),
176
(e) => assert.ok(e instanceof TypeError),
177
)
178
})
179
180
it('works for directories', function () {
181
return io.exists(dir).then(assert.ok)
182
})
183
184
it('works for files', function () {
185
var file = path.join(dir, 'foo')
186
fs.writeFileSync(file, '')
187
return io.exists(file).then(assert.ok)
188
})
189
190
it('does not return a rejected promise if file does not exist', function () {
191
return io.exists(path.join(dir, 'not-there')).then(function (exists) {
192
assert.ok(!exists)
193
})
194
})
195
})
196
197
describe('unlink', function () {
198
let dir
199
200
before(function () {
201
return io.tmpDir().then(function (d) {
202
dir = d
203
})
204
})
205
206
it('silently succeeds if the path does not exist', function () {
207
return io.unlink(path.join(dir, 'not-there'))
208
})
209
210
it('deletes files', function () {
211
const file = path.join(dir, 'foo')
212
fs.writeFileSync(file, '')
213
return io
214
.exists(file)
215
.then(assert.ok)
216
.then(function () {
217
return io.unlink(file)
218
})
219
.then(function () {
220
return io.exists(file)
221
})
222
.then(function (exists) {
223
return assert.ok(!exists)
224
})
225
})
226
})
227
228
describe('rmDir', function () {
229
it('succeeds if the designated directory does not exist', function () {
230
return io.tmpDir().then(function (d) {
231
return io.rmDir(path.join(d, 'i/do/not/exist'))
232
})
233
})
234
235
it('deletes recursively', function () {
236
return io.tmpDir().then(function (dir) {
237
fs.writeFileSync(path.join(dir, 'file1'), 'hello')
238
fs.mkdirSync(path.join(dir, 'sub'))
239
fs.mkdirSync(path.join(dir, 'sub/folder'))
240
fs.writeFileSync(path.join(dir, 'sub/folder/file2'), 'goodbye')
241
242
return io.rmDir(dir).then(function () {
243
assert.ok(!fs.existsSync(dir))
244
assert.ok(!fs.existsSync(path.join(dir, 'sub/folder/file2')))
245
})
246
})
247
})
248
})
249
250
describe('findInPath', function () {
251
const savedPathEnv = process.env['PATH']
252
afterEach(() => (process.env['PATH'] = savedPathEnv))
253
254
const cwd = process.cwd
255
afterEach(() => (process.cwd = cwd))
256
257
let dirs
258
beforeEach(() => {
259
return Promise.all([io.tmpDir(), io.tmpDir(), io.tmpDir()]).then((arr) => {
260
dirs = arr
261
process.env['PATH'] = arr.join(path.delimiter)
262
})
263
})
264
265
it('returns null if file cannot be found', () => {
266
assert.strictEqual(io.findInPath('foo.txt'), null)
267
})
268
269
it('can find file on path', () => {
270
let filePath = path.join(dirs[1], 'foo.txt')
271
fs.writeFileSync(filePath, 'hi')
272
273
assert.strictEqual(io.findInPath('foo.txt'), filePath)
274
})
275
276
it('returns null if file is in a subdir of a directory on the path', () => {
277
let subDir = path.join(dirs[2], 'sub')
278
fs.mkdirSync(subDir)
279
280
let filePath = path.join(subDir, 'foo.txt')
281
fs.writeFileSync(filePath, 'hi')
282
283
assert.strictEqual(io.findInPath('foo.txt'), null)
284
})
285
286
it('does not match on directories', () => {
287
fs.mkdirSync(path.join(dirs[2], 'sub'))
288
assert.strictEqual(io.findInPath('sub'), null)
289
})
290
291
it('will look in cwd first if requested', () => {
292
return io.tmpDir().then((fakeCwd) => {
293
process.cwd = () => fakeCwd
294
295
let theFile = path.join(fakeCwd, 'foo.txt')
296
297
fs.writeFileSync(path.join(dirs[1], 'foo.txt'), 'hi')
298
fs.writeFileSync(theFile, 'bye')
299
300
assert.strictEqual(io.findInPath('foo.txt', true), theFile)
301
})
302
})
303
})
304
305
describe('read', function () {
306
let tmpDir
307
308
before(function () {
309
return io.tmpDir().then(function (d) {
310
tmpDir = d
311
312
fs.writeFileSync(path.join(d, 'foo'), 'Hello, world')
313
})
314
})
315
316
it('can read a file', function () {
317
return io.read(path.join(tmpDir, 'foo')).then((buff) => {
318
assert.ok(buff instanceof Buffer)
319
assert.strictEqual('Hello, world', buff.toString())
320
})
321
})
322
323
it('catches errors from invalid input', function () {
324
return io.read({}).then(
325
() => assert.fail('should have failed'),
326
(e) => assert.ok(e instanceof TypeError),
327
)
328
})
329
330
it('rejects returned promise if file does not exist', function () {
331
return io.read(path.join(tmpDir, 'not-there')).then(
332
() => assert.fail('should have failed'),
333
(e) => assert.strictEqual('ENOENT', e.code),
334
)
335
})
336
})
337
338
describe('mkdirp', function () {
339
it('recursively creates entire directory path', function () {
340
return io.tmpDir().then((root) => {
341
let dst = path.join(root, 'foo/bar/baz')
342
return io.mkdirp(dst).then((d) => {
343
assert.strictEqual(d, dst)
344
return io.stat(d).then((stats) => {
345
assert.ok(stats.isDirectory())
346
})
347
})
348
})
349
})
350
351
it('does nothing if the directory already exists', function () {
352
return io.tmpDir().then((dir) => io.mkdirp(dir).then((d) => assert.strictEqual(d, dir)))
353
})
354
})
355
356
describe('walkDir', function () {
357
it('walk directory', async function () {
358
let dir = await io.tmpDir()
359
360
fs.writeFileSync(path.join(dir, 'file1'), 'hello')
361
fs.mkdirSync(path.join(dir, 'sub'))
362
fs.mkdirSync(path.join(dir, 'sub/folder'))
363
fs.writeFileSync(path.join(dir, 'sub/folder/file2'), 'goodbye')
364
365
let seen = await io.walkDir(dir)
366
seen.sort((a, b) => {
367
if (a.path < b.path) return -1
368
if (a.path > b.path) return 1
369
return 0
370
})
371
assert.deepStrictEqual(seen, [
372
{ path: 'file1', dir: false },
373
{ path: 'sub', dir: true },
374
{ path: path.join('sub', 'folder'), dir: true },
375
{ path: path.join('sub', 'folder', 'file2'), dir: false },
376
])
377
})
378
})
379
})
380
381