-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
sync.js
225 lines (178 loc) · 6.54 KB
/
sync.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
import crypto from 'crypto';
import path from 'path';
import fs from 'graceful-fs';
import del from 'del';
import test from 'ava';
import uuid from 'uuid';
import sinon from 'sinon';
import m from '..';
import assertDateEqual from './helpers/assert';
import {buildEACCES, buildENOSPC, buildEBADF, buildEPERM} from './helpers/fs-errors';
const THREE_HUNDRED_KILO = (100 * 3 * 1024) + 1;
test.before(() => {
process.chdir(path.dirname(__dirname));
});
test.beforeEach(t => {
t.context.src = uuid.v4();
t.context.dest = uuid.v4();
t.context.creates = [t.context.src, t.context.dest];
});
test.afterEach.always(t => {
t.context.creates.forEach(path => del.sync(path));
});
test('throw an Error on missing `src`', t => {
t.throws(() => m.sync(), /`src`/);
});
test('throw an Error on missing `dest`', t => {
t.throws(() => m.sync('TARGET'), /`dest`/);
});
test('copy a file', t => {
m.sync('license', t.context.dest);
t.is(fs.readFileSync(t.context.dest, 'utf8'), fs.readFileSync('license', 'utf8'));
});
test('copy an empty file', t => {
fs.writeFileSync(t.context.src, '');
m.sync(t.context.src, t.context.dest);
t.is(fs.readFileSync(t.context.dest, 'utf8'), '');
});
test('copy big files', t => {
const buf = crypto.pseudoRandomBytes(THREE_HUNDRED_KILO);
fs.writeFileSync(t.context.src, buf);
m.sync(t.context.src, t.context.dest);
t.true(buf.equals(fs.readFileSync(t.context.dest)));
});
test('do not alter overwrite option', t => {
const opts = {};
m.sync('license', t.context.dest, opts);
t.false('overwrite' in opts);
});
test('overwrite when enabled', t => {
fs.writeFileSync(t.context.dest, '');
m.sync('license', t.context.dest, {overwrite: true});
t.is(fs.readFileSync(t.context.dest, 'utf8'), fs.readFileSync('license', 'utf8'));
});
test('overwrite when options are undefined', t => {
fs.writeFileSync(t.context.dest, '');
m.sync('license', t.context.dest);
t.is(fs.readFileSync(t.context.dest, 'utf8'), fs.readFileSync('license', 'utf8'));
});
test('do not overwrite when disabled', t => {
fs.writeFileSync(t.context.dest, '');
m.sync('license', t.context.dest, {overwrite: false});
t.is(fs.readFileSync(t.context.dest, 'utf8'), '');
});
test('do not create dest on unreadable src', t => {
const err = t.throws(() => m.sync('node_modules', t.context.dest));
t.is(err.name, 'CpFileError', err);
t.is(err.code, 'EISDIR', err);
t.throws(() => fs.statSync(t.context.dest), /ENOENT/);
});
test('do not create dest directory on unreadable src', t => {
const err = t.throws(() => m.sync('node_modules', 'subdir/' + uuid.v4()));
t.is(err.name, 'CpFileError', err);
t.is(err.code, 'EISDIR', err);
t.throws(() => fs.statSync('subdir'), /ENOENT/);
});
test('preserve timestamps', t => {
m.sync('license', t.context.dest);
const licenseStats = fs.lstatSync('license');
const tmpStats = fs.lstatSync(t.context.dest);
assertDateEqual(t, licenseStats.atime, tmpStats.atime);
assertDateEqual(t, licenseStats.mtime, tmpStats.mtime);
});
test('preserve mode', t => {
m.sync('license', t.context.dest);
const licenseStats = fs.lstatSync('license');
const tmpStats = fs.lstatSync(t.context.dest);
t.is(licenseStats.mode, tmpStats.mode);
});
test('preserve ownership', t => {
m.sync('license', t.context.dest);
const licenseStats = fs.lstatSync('license');
const tmpStats = fs.lstatSync(t.context.dest);
t.is(licenseStats.gid, tmpStats.gid);
t.is(licenseStats.uid, tmpStats.uid);
});
test('throw an Error if `src` does not exists', t => {
const err = t.throws(() => m.sync('NO_ENTRY', t.context.dest));
t.is(err.name, 'CpFileError', err);
t.is(err.code, 'ENOENT', err);
t.regex(err.message, /`NO_ENTRY`/, err);
t.regex(err.stack, /`NO_ENTRY`/, err);
});
test('rethrow mkdir EACCES errors', t => {
const dirPath = '/root/NO_ACCESS_' + uuid.v4();
const dest = dirPath + '/' + uuid.v4();
const mkdirError = buildEACCES(dirPath);
fs.mkdirSync = sinon.stub(fs, 'mkdirSync').throws(mkdirError);
const err = t.throws(() => m.sync('license', dest));
t.is(err.name, 'CpFileError', err);
t.is(err.errno, mkdirError.errno, err);
t.is(err.code, mkdirError.code, err);
t.is(err.path, mkdirError.path, err);
t.true(fs.mkdirSync.called);
fs.mkdirSync.restore();
});
test('rethrow ENOSPC errors', t => {
const noSpaceError = buildENOSPC();
fs.writeFileSync(t.context.src, '');
fs.writeSync = sinon.stub(fs, 'writeSync').throws(noSpaceError);
const err = t.throws(() => m.sync('license', t.context.dest));
t.is(err.name, 'CpFileError', err);
t.is(err.errno, noSpaceError.errno, err);
t.is(err.code, noSpaceError.code, err);
t.true(fs.writeSync.called);
fs.writeSync.restore();
});
test('rethrow stat errors', t => {
const fstatError = buildEBADF();
fs.writeFileSync(t.context.src, '');
fs.fstatSync = sinon.stub(fs, 'fstatSync').throws(fstatError);
const err = t.throws(() => m.sync(t.context.src, t.context.dest));
t.is(err.name, 'CpFileError', err);
t.is(err.errno, fstatError.errno, err);
t.is(err.code, fstatError.code, err);
t.true(fs.fstatSync.called);
fs.fstatSync.restore();
});
test('rethrow utimes errors', t => {
const futimesError = buildEBADF();
fs.futimesSync = sinon.stub(fs, 'futimesSync').throws(futimesError);
const err = t.throws(() => m.sync('license', t.context.dest));
t.is(err.name, 'CpFileError', err);
t.is(err.errno, futimesError.errno, err);
t.is(err.code, futimesError.code, err);
t.true(fs.futimesSync.called);
fs.futimesSync.restore();
});
test('rethrow EACCES errors of dest', t => {
const openError = buildEACCES(t.context.dest);
fs.openSync = sinon.stub(fs, 'openSync').throws(openError);
const err = t.throws(() => m.sync('license', t.context.dest));
t.is(err.name, 'CpFileError', err);
t.is(err.errno, openError.errno, err);
t.is(err.code, openError.code, err);
t.is(err.path, openError.path, err);
t.true(fs.openSync.called);
fs.openSync.restore();
});
test('rethrow chmod errors', t => {
const chmodError = buildEPERM(t.context.dest, 'chmod');
fs.chmodSync = sinon.stub(fs, 'chmodSync').throws(chmodError);
const err = t.throws(() => m.sync('license', t.context.dest));
t.is(err.name, 'CpFileError', err);
t.is(err.errno, chmodError.errno, err);
t.is(err.code, chmodError.code, err);
t.true(fs.chmodSync.called);
fs.chmodSync.restore();
});
test('rethrow chown errors', t => {
const chownError = buildEPERM(t.context.dest, 'chown');
fs.chownSync = sinon.stub(fs, 'chownSync').throws(chownError);
const err = t.throws(() => m.sync('license', t.context.dest));
t.is(err.name, 'CpFileError', err);
t.is(err.errno, chownError.errno, err);
t.is(err.code, chownError.code, err);
t.true(fs.chownSync.called);
fs.chownSync.restore();
});