-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
sync.js
116 lines (100 loc) · 2.49 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
import fs from 'fs';
import path from 'path';
import test from 'ava';
import tempy from 'tempy';
import gracefulFs from 'graceful-fs';
import {getFixture, assertDirectory, customFsOptions} from './helpers/util';
import makeDir from '..';
test('main', t => {
const dir = getFixture();
const madeDir = makeDir.sync(dir);
t.true(madeDir.length > 0);
assertDirectory(t, madeDir);
});
test('`fs` option - graceful-fs', t => {
const dir = getFixture();
makeDir.sync(dir, {fs: gracefulFs});
assertDirectory(t, dir);
});
test('`fs` option - custom', t => {
const dir = getFixture();
const madeDir = makeDir.sync(dir, customFsOptions);
t.true(madeDir.length > 0);
assertDirectory(t, madeDir);
});
test('`mode` option', t => {
const dir = getFixture();
const mode = 0o744;
makeDir.sync(dir, {mode});
assertDirectory(t, dir, mode);
// Ensure it's writable
makeDir.sync(dir);
assertDirectory(t, dir, mode);
});
test('dir exists', t => {
const dir = makeDir.sync(tempy.directory());
t.true(dir.length > 0);
assertDirectory(t, dir);
});
test('file exits', t => {
const fp = tempy.file();
fs.writeFileSync(fp, '');
t.throws(() => {
makeDir.sync(fp);
}, {code: 'EEXIST'});
});
test('parent dir is file', t => {
const fp = tempy.file();
fs.writeFileSync(fp, '');
const error = t.throws(() => {
makeDir.sync(fp + '/sub/dir');
});
t.regex(error.code, /ENOTDIR|EEXIST/);
});
test('root dir', t => {
if (process.platform === 'win32') {
// Do not assume that `C:` is current drive
t.throws(() => {
makeDir.sync('/');
}, {
code: 'EPERM',
message: /operation not permitted, mkdir '[A-Za-z]:\\'/
});
} else {
const mode = fs.statSync('/').mode & 0o777;
const dir = makeDir.sync('/');
t.true(dir.length > 0);
assertDirectory(t, dir, mode);
}
});
test('race two', t => {
const dir = getFixture();
makeDir.sync(dir);
makeDir.sync(dir);
assertDirectory(t, dir);
});
test('race many', t => {
const dir = getFixture();
for (let i = 0; i < 100; i++) {
makeDir.sync(dir);
}
assertDirectory(t, dir);
});
test('handles null bytes in path', t => {
const dir = path.join(tempy.directory(), 'foo\u0000bar');
const error = t.throws(() => {
makeDir.sync(dir);
}, /null bytes/);
t.regex(error.code, /ERR_INVALID_ARG_VALUE|ENOENT/);
});
if (process.platform === 'win32') {
test('handles non-existent root', t => {
// We assume the `o:\` drive doesn't exist on Windows
t.throws(() => {
makeDir.sync('o:\\foo');
}, {
code: 'EPERM',
message: /operation not permitted, mkdir/
});
});
}