Repository navigation
Expand file tree
/
Copy pathtest.js
More file actions
291 lines (248 loc) · 9.29 KB
/
Copy pathtest.js
File metadata and controls
291 lines (248 loc) · 9.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
import {Buffer} from 'node:buffer';
import path from 'node:path';
import fs from 'node:fs';
import stream from 'node:stream';
import tempDir from 'temp-dir';
import {pathExists} from 'path-exists';
import touch from 'touch';
import test from 'ava';
import {
temporaryFile,
temporaryFileTask,
temporaryDirectory,
temporaryDirectoryTask,
temporaryWrite,
temporaryWriteTask,
temporaryWriteSync,
rootTemporaryDirectory,
} from './index.js';
test('.file()', t => {
t.true(temporaryFile().includes(tempDir));
t.false(temporaryFile().endsWith('.'));
t.false(temporaryFile({extension: undefined}).endsWith('.'));
t.false(temporaryFile({extension: null}).endsWith('.'));
t.true(temporaryFile({extension: 'png'}).endsWith('.png'));
t.true(temporaryFile({extension: '.png'}).endsWith('.png'));
t.false(temporaryFile({extension: '.png'}).endsWith('..png'));
t.true(temporaryFile({name: 'custom-name.md'}).endsWith('custom-name.md'));
t.throws(() => {
temporaryFile({name: 'custom-name.md', extension: '.ext'});
});
t.throws(() => {
temporaryFile({name: 'custom-name.md', extension: ''});
});
t.notThrows(() => {
temporaryFile({name: 'custom-name.md', extension: undefined});
});
t.notThrows(() => {
temporaryFile({name: 'custom-name.md', extension: null});
});
});
test('.file.task()', async t => {
let temporaryFilePath;
t.is(await temporaryFileTask(async temporaryFile => {
await touch(temporaryFile);
temporaryFilePath = temporaryFile;
return temporaryFile;
}), temporaryFilePath);
t.false(await pathExists(temporaryFilePath));
});
test('.task() - cleans up even if callback throws', async t => {
let temporaryDirectoryPath;
await t.throwsAsync(temporaryDirectoryTask(async temporaryDirectory => {
temporaryDirectoryPath = temporaryDirectory;
throw new Error('Catch me if you can!');
}), {
instanceOf: Error,
message: 'Catch me if you can!',
});
t.false(await pathExists(temporaryDirectoryPath));
});
test('.directory()', t => {
const prefix = 'name_';
t.true(temporaryDirectory().includes(tempDir));
t.true(path.basename(temporaryDirectory({prefix})).startsWith(prefix));
});
test('.directory.task()', async t => {
let temporaryDirectoryPath;
t.is(await temporaryDirectoryTask(async temporaryDirectory => {
temporaryDirectoryPath = temporaryDirectory;
return temporaryDirectory;
}), temporaryDirectoryPath);
t.false(await pathExists(temporaryDirectoryPath));
});
test('.write(string)', async t => {
const filePath = await temporaryWrite('unicorn', {name: 'test.png'});
t.is(fs.readFileSync(filePath, 'utf8'), 'unicorn');
t.is(path.basename(filePath), 'test.png');
});
test('.write.task(string)', async t => {
let temporaryFilePath;
t.is(await temporaryWriteTask('', async temporaryFile => {
temporaryFilePath = temporaryFile;
return temporaryFile;
}), temporaryFilePath);
t.false(await pathExists(temporaryFilePath));
});
test('.write(buffer)', async t => {
const filePath = await temporaryWrite(Buffer.from('unicorn'));
t.is(fs.readFileSync(filePath, 'utf8'), 'unicorn');
});
test('.write(stream)', async t => {
const readable = new stream.Readable({
read() {},
});
readable.push('unicorn');
readable.push(null); // eslint-disable-line unicorn/no-array-push-push
const filePath = await temporaryWrite(readable);
t.is(fs.readFileSync(filePath, 'utf8'), 'unicorn');
});
test('.write(stream) failing stream', async t => {
const readable = new stream.Readable({
read() {},
});
readable.push('unicorn');
setImmediate(() => {
readable.emit('error', new Error('Catch me if you can!'));
readable.push(null);
});
await t.throwsAsync(temporaryWrite(readable), {
instanceOf: Error,
message: 'Catch me if you can!',
});
});
test('.writeSync()', t => {
t.is(fs.readFileSync(temporaryWriteSync('unicorn'), 'utf8'), 'unicorn');
});
test('.root', t => {
t.true(rootTemporaryDirectory.length > 0);
t.true(path.isAbsolute(rootTemporaryDirectory));
});
test('.file() with parentDirectory', t => {
const filePath = temporaryFile({parentDirectory: 'my-app'});
t.true(filePath.includes(path.join(tempDir, 'my-app')));
});
test('.file() with parentDirectory and extension', t => {
const filePath = temporaryFile({parentDirectory: 'my-app', extension: 'png'});
t.true(filePath.includes(path.join(tempDir, 'my-app')));
t.true(filePath.endsWith('.png'));
});
test('.file() with parentDirectory and name', t => {
const filePath = temporaryFile({parentDirectory: 'my-app', name: 'unicorn.png'});
t.true(filePath.includes(path.join(tempDir, 'my-app')));
t.is(path.basename(filePath), 'unicorn.png');
});
test('.directory() with parentDirectory', t => {
const directory = temporaryDirectory({parentDirectory: 'my-app'});
t.true(directory.includes(path.join(tempDir, 'my-app')));
t.true(fs.statSync(directory).isDirectory());
});
test('.directory() with parentDirectory and prefix', t => {
const directory = temporaryDirectory({parentDirectory: 'my-app', prefix: 'name_'});
t.true(directory.includes(path.join(tempDir, 'my-app')));
t.true(path.basename(directory).startsWith('name_'));
});
test('.file() with parentDirectory - auto-creates the directory', t => {
const parentDirectory = `auto-create-name-${Date.now()}`;
const filePath = temporaryFile({parentDirectory, name: 'test.txt'});
t.true(fs.existsSync(path.dirname(filePath)));
});
test('.file() with parentDirectory and extension - auto-creates the directory', t => {
const parentDirectory = `auto-create-ext-${Date.now()}`;
const filePath = temporaryFile({parentDirectory, extension: 'png'});
t.true(fs.existsSync(path.dirname(filePath)));
t.true(filePath.endsWith('.png'));
});
test('.write() with parentDirectory', async t => {
const parentDirectory = `auto-create-write-${Date.now()}`;
const filePath = await temporaryWrite('unicorn', {parentDirectory});
t.is(fs.readFileSync(filePath, 'utf8'), 'unicorn');
t.true(filePath.includes(path.join(tempDir, parentDirectory)));
});
test('.file() with parentDirectory - rejects unsafe values', t => {
for (const fixture of ['..', '../foo', 'foo/bar', 'foo\\bar', 'foo\0bar']) {
t.throws(() => temporaryFile({parentDirectory: fixture}), {message: /Unsafe path component/});
}
});
test('.file() with rootDirectory', t => {
const rootDirectory = path.join(tempDir, `root-test-${Date.now()}`);
const filePath = temporaryFile({rootDirectory});
t.true(filePath.startsWith(rootDirectory));
t.true(fs.existsSync(rootDirectory));
});
test('.file() with rootDirectory and parentDirectory', t => {
const rootDirectory = path.join(tempDir, `root-parent-test-${Date.now()}`);
const filePath = temporaryFile({rootDirectory, parentDirectory: 'my-app'});
t.true(filePath.startsWith(path.join(rootDirectory, 'my-app')));
});
test('.file() with rootDirectory and extension', t => {
const rootDirectory = path.join(tempDir, `root-ext-test-${Date.now()}`);
const filePath = temporaryFile({rootDirectory, extension: 'png'});
t.true(filePath.startsWith(rootDirectory));
t.true(filePath.endsWith('.png'));
});
test('.file() with rootDirectory and name', t => {
const rootDirectory = path.join(tempDir, `root-name-test-${Date.now()}`);
const filePath = temporaryFile({rootDirectory, name: 'unicorn.png'});
t.true(filePath.startsWith(rootDirectory));
t.is(path.basename(filePath), 'unicorn.png');
});
test('.directory() with rootDirectory', t => {
const rootDirectory = path.join(tempDir, `root-dir-test-${Date.now()}`);
const directory = temporaryDirectory({rootDirectory});
t.true(directory.startsWith(rootDirectory));
t.true(fs.statSync(directory).isDirectory());
});
test('.file() with rootDirectory - rejects relative paths', t => {
t.throws(() => temporaryFile({rootDirectory: 'relative/path'}), {
message: /must be an absolute path/,
});
});
test('.write() with rootDirectory', async t => {
const rootDirectory = path.join(tempDir, `root-write-test-${Date.now()}`);
const filePath = await temporaryWrite('unicorn', {rootDirectory});
t.is(fs.readFileSync(filePath, 'utf8'), 'unicorn');
t.true(filePath.startsWith(rootDirectory));
});
test('.writeSync() with rootDirectory', t => {
const rootDirectory = path.join(tempDir, `root-write-sync-test-${Date.now()}`);
const filePath = temporaryWriteSync('unicorn', {rootDirectory});
t.is(fs.readFileSync(filePath, 'utf8'), 'unicorn');
t.true(filePath.startsWith(rootDirectory));
});
test('.directory.task() with rootDirectory cleans up', async t => {
const rootDirectory = path.join(tempDir, `root-task-test-${Date.now()}`);
let temporaryDirectoryPath;
await temporaryDirectoryTask(async temporaryDirectory => {
temporaryDirectoryPath = temporaryDirectory;
t.true(temporaryDirectory.startsWith(rootDirectory));
}, {rootDirectory});
t.false(await pathExists(temporaryDirectoryPath));
});
// TODO: Use `unsafeFilenameFixtures` from `is-safe-filename` when targeting Node.js 20.
const unsafeFilenameFixtures = [
'',
' ',
'.',
'..',
' .',
'. ',
' ..',
'.. ',
'../',
'../foo',
'foo/../bar',
'foo/bar',
'foo\\bar',
'foo\0bar',
];
test('rejects unsafe path components', t => {
for (const fixture of unsafeFilenameFixtures) {
t.throws(() => temporaryFile({name: fixture}), {message: /Unsafe path component/});
t.throws(() => temporaryFile({extension: fixture}), {message: /Unsafe path component/});
// Empty string is valid for prefix (it's the default)
if (fixture !== '') {
t.throws(() => temporaryDirectory({prefix: fixture}), {message: /Unsafe path component/});
}
}
});