-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
async.js
289 lines (233 loc) · 9.14 KB
/
async.js
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
import process from 'node:process';
import crypto from 'node:crypto';
import path from 'node:path';
import fs from 'node:fs';
import {fileURLToPath} from 'node:url';
import importFresh from 'import-fresh';
import clearModule from 'clear-module';
import {deleteSync} from 'del';
import test from 'ava';
import sinon from 'sinon';
import {copyFile} from '../index.js';
import assertDateEqual from './helpers/_assert.js';
import {buildEACCES, buildENOSPC, buildENOENT, buildEPERM, buildERRSTREAMWRITEAFTEREND} from './helpers/_fs-errors.js';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const THREE_HUNDRED_KILO = (100 * 3 * 1024) + 1;
test.before(() => {
process.chdir(path.dirname(__dirname));
deleteSync('temp'); // In case last test run failed.
fs.mkdirSync('temp');
});
test.after(() => {
deleteSync('temp');
});
test.beforeEach(t => {
t.context.source = path.join('temp', crypto.randomUUID());
t.context.destination = path.join('temp', crypto.randomUUID());
});
test('reject an Error on missing `source`', async t => {
await t.throwsAsync(copyFile(), {
message: /`source`/,
});
});
test('reject an Error on missing `destination`', async t => {
await t.throwsAsync(copyFile('TARGET'), {
message: /`destination`/,
});
});
test('copy a file', async t => {
await copyFile('license', t.context.destination);
t.is(fs.readFileSync(t.context.destination, 'utf8'), fs.readFileSync('license', 'utf8'));
});
test('copy an empty file', async t => {
fs.writeFileSync(t.context.source, '');
await copyFile(t.context.source, t.context.destination);
t.is(fs.readFileSync(t.context.destination, 'utf8'), '');
});
test('copy big files', async t => {
const buffer = crypto.randomBytes(THREE_HUNDRED_KILO);
fs.writeFileSync(t.context.source, buffer);
await copyFile(t.context.source, t.context.destination);
t.true(buffer.equals(fs.readFileSync(t.context.destination)));
});
test('do not alter overwrite option', async t => {
const options = {};
await copyFile('license', t.context.destination, options);
t.false('overwrite' in options);
});
test('overwrite when enabled', async t => {
fs.writeFileSync(t.context.destination, '');
await copyFile('license', t.context.destination, {overwrite: true});
t.is(fs.readFileSync(t.context.destination, 'utf8'), fs.readFileSync('license', 'utf8'));
});
test('overwrite when options are undefined', async t => {
fs.writeFileSync(t.context.destination, '');
await copyFile('license', t.context.destination);
t.is(fs.readFileSync(t.context.destination, 'utf8'), fs.readFileSync('license', 'utf8'));
});
test('do not overwrite when disabled', async t => {
fs.writeFileSync(t.context.destination, '');
const error = await t.throwsAsync(copyFile('license', t.context.destination, {overwrite: false}));
t.is(error.name, 'CopyFileError', error.message);
t.is(error.code, 'EEXIST', error.message);
});
if (process.platform !== 'win32') {
test('create directories with specified mode', async t => {
const directory = t.context.destination;
const destination = `${directory}/${crypto.randomUUID()}`;
const directoryMode = 0o700;
await copyFile('license', destination, {directoryMode});
const stat = fs.statSync(directory);
t.is(stat.mode & directoryMode, directoryMode); // eslint-disable-line no-bitwise
});
}
test('do not create `destination` on unreadable `source`', async t => {
const error = await t.throwsAsync(copyFile('node_modules', t.context.destination));
t.is(error.name, 'CopyFileError', error.message);
t.is(error.code, 'EISDIR', error.message);
t.throws(() => {
fs.statSync(t.context.destination);
}, {
message: /ENOENT/,
});
});
test('do not create `destination` directory on unreadable `source`', async t => {
const error = await t.throwsAsync(copyFile('node_modules', path.join('temp/subdir', crypto.randomUUID())));
t.is(error.name, 'CopyFileError', error.message);
t.is(error.code, 'EISDIR', error.message);
t.false(fs.existsSync('subdir'));
});
test('preserve timestamps', async t => {
await copyFile('license', t.context.destination);
const licenseStats = fs.lstatSync('license');
const temporaryStats = fs.lstatSync(t.context.destination);
assertDateEqual(t, licenseStats.atime, temporaryStats.atime);
assertDateEqual(t, licenseStats.mtime, temporaryStats.mtime);
});
test('preserve mode', async t => {
await copyFile('license', t.context.destination);
const licenseStats = fs.lstatSync('license');
const temporaryStats = fs.lstatSync(t.context.destination);
t.is(licenseStats.mode, temporaryStats.mode);
});
test('throw an Error if `source` does not exists', async t => {
const error = await t.throwsAsync(copyFile('NO_ENTRY', t.context.destination));
t.is(error.name, 'CopyFileError', error.message);
t.is(error.code, 'ENOENT', error.message);
t.regex(error.message, /`NO_ENTRY`/, error.message);
t.regex(error.stack, /`NO_ENTRY`/, error.message);
});
test.serial.failing('rethrow mkdir EACCES errors', async t => {
const directoryPath = `/root/NO_ACCESS_${crypto.randomUUID()}`;
const destination = path.join(directoryPath, crypto.randomUUID());
const mkdirError = buildEACCES(directoryPath);
fs.stat = sinon.stub(fs, 'stat').throws(mkdirError);
fs.mkdir = sinon.stub(fs, 'mkdir').throws(mkdirError);
const error = await t.throwsAsync(copyFile('license', destination));
t.is(error.name, 'CopyFileError', error.message);
t.is(error.errno, mkdirError.errno, error.message);
t.is(error.code, mkdirError.code, error.message);
t.is(error.path, mkdirError.path, error.message);
t.true(fs.mkdir.called || fs.stat.called);
fs.mkdir.restore();
fs.stat.restore();
});
test.serial.failing('rethrow ENOSPC errors', async t => {
const {createWriteStream} = fs;
const noSpaceError = buildENOSPC();
let isCalled = false;
fs.createWriteStream = (path, options) => {
const stream = createWriteStream(path, options);
if (path === t.context.destination) {
stream.on('pipe', () => {
if (!isCalled) {
isCalled = true;
stream.emit('error', noSpaceError);
}
});
}
return stream;
};
clearModule('../fs.js');
const uncached = importFresh('../index.js');
const error = await t.throwsAsync(uncached('license', t.context.destination));
t.is(error.name, 'CopyFileError', error.message);
t.is(error.errno, noSpaceError.errno, error.message);
t.is(error.code, noSpaceError.code, error.message);
t.true(isCalled);
fs.createWriteStream = createWriteStream;
});
test.serial.failing('rethrow stat errors', async t => {
const fstatError = buildENOENT();
fs.writeFileSync(t.context.source, '');
fs.lstat = sinon.stub(fs, 'lstat').throws(fstatError);
clearModule('../fs.js');
const uncached = importFresh('../index.js');
const error = await t.throwsAsync(uncached(t.context.source, t.context.destination));
t.is(error.name, 'CopyFileError', error.message);
t.is(error.errno, fstatError.errno, error.message);
t.is(error.code, fstatError.code, error.message);
t.true(fs.lstat.called);
fs.lstat.restore();
});
test.serial.failing('rethrow utimes errors', async t => {
const utimesError = buildENOENT();
fs.utimes = sinon.stub(fs, 'utimes').throws(utimesError);
clearModule('../fs.js');
const uncached = importFresh('../index.js');
const error = await t.throwsAsync(uncached('license', t.context.destination));
t.is(error.name, 'CopyFileError', error.message);
t.is(error.code, 'ENOENT', error.message);
t.true(fs.utimes.called);
fs.utimes.restore();
});
test.serial.failing('rethrow chmod errors', async t => {
const chmodError = buildEPERM(t.context.destination, 'chmod');
fs.chmod = sinon.stub(fs, 'chmod').throws(chmodError);
clearModule('../fs.js');
const uncached = importFresh('../index.js');
const error = await t.throwsAsync(uncached('license', t.context.destination));
t.is(error.name, 'CopyFileError', error.message);
t.is(error.code, chmodError.code, error.message);
t.is(error.path, chmodError.path, error.message);
t.true(fs.chmod.called);
fs.chmod.restore();
});
test.serial.failing('rethrow read after open errors', async t => {
const {createWriteStream, createReadStream} = fs;
let calledWriteEnd = 0;
let readStream;
const readError = buildERRSTREAMWRITEAFTEREND();
fs.createWriteStream = (...arguments_) => {
const stream = createWriteStream(...arguments_);
const {end} = stream;
stream.on('pipe', () => {
readStream.emit('error', readError);
});
stream.end = (...endArgs) => {
calledWriteEnd++;
return end.apply(stream, endArgs);
};
return stream;
};
fs.createReadStream = (...arguments_) => {
/* Fake stream */
readStream = createReadStream(...arguments_);
readStream.pause();
return readStream;
};
clearModule('../fs.js');
const uncached = importFresh('../index.js');
const error = await t.throwsAsync(uncached('license', t.context.destination));
t.is(error.name, 'CopyFileError', error.message);
t.is(error.code, readError.code, error.message);
t.is(error.errno, readError.errno, error.message);
t.is(calledWriteEnd, 1);
Object.assign(fs, {createWriteStream, createReadStream});
});
test('cwd option', async t => {
const error = await t.throwsAsync(copyFile('sync.js', t.context.destination));
t.is(error.name, 'CopyFileError');
t.is(error.code, 'ENOENT');
await t.notThrowsAsync(copyFile('sync.js', t.context.destination, {cwd: 'test'}));
});