-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
async.js
101 lines (84 loc) · 2.23 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
import fs from 'fs';
import path from 'path';
import test from 'ava';
import tempy from 'tempy';
import gracefulFs from 'graceful-fs';
import {getFixture, assertDir} from './helpers/util';
import m from '..';
test('main', async t => {
const dir = getFixture();
const madeDir = await m(dir);
t.true(madeDir.length > 0);
assertDir(t, madeDir);
});
test('`fs` option', async t => {
const dir = getFixture();
await m(dir, {fs: gracefulFs});
assertDir(t, dir);
});
test('`mode` option', async t => {
const dir = getFixture();
const mode = 0o744;
await m(dir, {mode});
assertDir(t, dir, mode);
// Ensure it's writable
await m(dir);
assertDir(t, dir, mode);
});
test('dir exists', async t => {
const dir = await m(tempy.directory());
t.true(dir.length > 0);
assertDir(t, dir);
});
test('file exits', async t => {
const fp = tempy.file();
fs.writeFileSync(fp, '');
const err = await t.throws(m(fp));
t.is(err.code, 'EEXIST');
});
test('root dir', async t => {
const dir = await m('/');
t.true(dir.length > 0);
assertDir(t, dir);
});
test('race two', async t => {
const dir = getFixture();
await Promise.all([m(dir), m(dir)]);
assertDir(t, dir);
});
test('race many', async t => {
const dir = getFixture();
const all = [];
for (let i = 0; i < 100; i++) {
all.push(m(dir));
}
await Promise.all(all);
assertDir(t, dir);
});
test('handles null bytes in path', async t => {
const dir = path.join(tempy.directory(), 'foo\u0000bar');
const err = await t.throws(m(dir), /null bytes/);
t.regex(err.code, /ERR_INVALID_ARG_VALUE|ENOENT/);
});
test.serial('handles invalid path characters', async t => {
// We do this to please `nyc`
const {platform} = process;
Object.defineProperty(process, 'platform', {
value: 'win32'
});
// Also to please `nyc`
await m(tempy.directory());
const dir = path.join(tempy.directory(), 'foo"bar');
const err = await t.throws(m(dir), /invalid characters/);
t.is(err.code, 'EINVAL');
Object.defineProperty(process, 'platform', {
value: platform
});
});
if (process.platform === 'win32') {
test('handles non-existent root', async t => {
// We assume the `o:\` drive doesn't exist on Windows
const err = await t.throws(m('o:\\foo'), /no such file or directory/);
t.is(err.code, 'ENOENT');
});
}