-
Notifications
You must be signed in to change notification settings - Fork 722
/
mkdir.js
203 lines (178 loc) · 6.82 KB
/
mkdir.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
import fs from 'fs';
import test from 'ava';
import shell from '..';
import common from '../src/common';
import utils from './utils/utils';
test.beforeEach(t => {
t.context.tmp = utils.getTempDir();
shell.config.resetForTesting();
shell.mkdir(t.context.tmp);
});
test.afterEach.always(t => {
shell.rm('-rf', t.context.tmp);
});
//
// Invalids
//
test('no args', t => {
const result = shell.mkdir();
t.truthy(shell.error());
t.is(result.code, 1);
t.is(result.stderr, 'mkdir: no paths given');
});
test('dir already exists', t => {
const mtime = common.statFollowLinks(t.context.tmp).mtime.toString();
const result = shell.mkdir(t.context.tmp); // dir already exists
t.truthy(shell.error());
t.is(result.code, 1);
t.is(result.stderr, `mkdir: path already exists: ${t.context.tmp}`);
t.is(common.statFollowLinks(t.context.tmp).mtime.toString(), mtime); // didn't mess with dir
});
test('Can\'t overwrite a broken link', t => {
const mtime = common.statNoFollowLinks('test/resources/badlink').mtime.toString();
const result = shell.mkdir('test/resources/badlink');
t.truthy(shell.error());
t.is(result.code, 1);
t.is(result.stderr, 'mkdir: path already exists: test/resources/badlink');
t.is(common.statNoFollowLinks('test/resources/badlink').mtime.toString(), mtime); // didn't mess with file
});
test('root path does not exist', t => {
t.falsy(fs.existsSync('/asdfasdf')); // sanity check
const result = shell.mkdir('/asdfasdf/foobar'); // root path does not exist
t.truthy(shell.error());
t.is(result.code, 1);
t.is(result.stderr, 'mkdir: no such file or directory: /asdfasdf');
t.falsy(fs.existsSync('/asdfasdf'));
t.falsy(fs.existsSync('/asdfasdf/foobar'));
});
test('try to overwrite file', t => {
t.truthy(common.statFollowLinks('test/resources/file1').isFile());
const result = shell.mkdir('test/resources/file1');
t.truthy(shell.error());
t.is(result.code, 1);
t.is(result.stderr, 'mkdir: path already exists: test/resources/file1');
t.truthy(common.statFollowLinks('test/resources/file1').isFile());
});
test('try to overwrite file, with -p', t => {
t.truthy(common.statFollowLinks('test/resources/file1').isFile());
const result = shell.mkdir('-p', 'test/resources/file1');
t.truthy(shell.error());
t.is(result.code, 1);
t.is(result.stderr, 'mkdir: cannot create directory test/resources/file1: File exists');
t.truthy(common.statFollowLinks('test/resources/file1').isFile());
});
test('try to make a subdirectory of a file', t => {
t.truthy(common.statFollowLinks('test/resources/file1').isFile());
const result = shell.mkdir('test/resources/file1/subdir');
t.truthy(shell.error());
t.is(result.code, 1);
t.is(result.stderr, 'mkdir: cannot create directory test/resources/file1/subdir: Not a directory');
t.truthy(common.statFollowLinks('test/resources/file1').isFile());
t.falsy(fs.existsSync('test/resources/file1/subdir'));
});
test('Check for invalid permissions', t => {
utils.skipOnWin(t, () => {
// This test case only works on unix, but should work on Windows as well
const dirName = `${t.context.tmp}/nowritedir`;
shell.mkdir(dirName);
t.falsy(shell.error());
shell.chmod('-w', dirName);
const result = shell.mkdir(dirName + '/foo');
t.is(result.code, 1);
t.is(
result.stderr,
`mkdir: cannot create directory ${t.context.tmp}/nowritedir/foo: Permission denied`
);
t.truthy(shell.error());
t.falsy(fs.existsSync(dirName + '/foo'));
});
});
//
// Valids
//
test('basic usage', t => {
t.falsy(fs.existsSync(`${t.context.tmp}/t1`));
const result = shell.mkdir(`${t.context.tmp}/t1`);
t.falsy(shell.error());
t.is(result.code, 0);
t.truthy(fs.existsSync(`${t.context.tmp}/t1`));
});
test('multiple dirs', t => {
t.falsy(fs.existsSync(`${t.context.tmp}/t2`));
t.falsy(fs.existsSync(`${t.context.tmp}/t3`));
const result = shell.mkdir(`${t.context.tmp}/t2`, `${t.context.tmp}/t3`);
t.falsy(shell.error());
t.is(result.code, 0);
t.truthy(fs.existsSync(`${t.context.tmp}/t2`));
t.truthy(fs.existsSync(`${t.context.tmp}/t3`));
});
test('one dir exists, the other does not', t => {
shell.mkdir(`${t.context.tmp}/t1`);
t.truthy(fs.existsSync(`${t.context.tmp}/t1`));
t.falsy(fs.existsSync(`${t.context.tmp}/t4`));
const result = shell.mkdir(`${t.context.tmp}/t1`, `${t.context.tmp}/t4`);
t.is(result.code, 1);
t.is(utils.numLines(shell.error()), 1);
t.truthy(fs.existsSync(`${t.context.tmp}/t1`));
t.truthy(fs.existsSync(`${t.context.tmp}/t4`));
});
test('-p flag', t => {
t.falsy(fs.existsSync(`${t.context.tmp}/a`));
const result = shell.mkdir('-p', `${t.context.tmp}/a/b/c`);
t.falsy(shell.error());
t.is(result.code, 0);
t.truthy(fs.existsSync(`${t.context.tmp}/a/b/c`));
});
test('-p flag: multiple dirs', t => {
const result = shell.mkdir('-p', `${t.context.tmp}/zzza`,
`${t.context.tmp}/zzzb`, `${t.context.tmp}/zzzc`);
t.falsy(shell.error());
t.is(result.code, 0);
t.truthy(fs.existsSync(`${t.context.tmp}/zzza`));
t.truthy(fs.existsSync(`${t.context.tmp}/zzzb`));
t.truthy(fs.existsSync(`${t.context.tmp}/zzzc`));
});
test('-p flag: multiple dirs, array syntax', t => {
const result = shell.mkdir('-p', [`${t.context.tmp}/yyya`,
`${t.context.tmp}/yyyb`, `${t.context.tmp}/yyyc`]);
t.falsy(shell.error());
t.is(result.code, 0);
t.truthy(fs.existsSync(`${t.context.tmp}/yyya`));
t.truthy(fs.existsSync(`${t.context.tmp}/yyyb`));
t.truthy(fs.existsSync(`${t.context.tmp}/yyyc`));
});
test('-p flag: subdirectory already exists', t => {
t.falsy(fs.existsSync(`${t.context.tmp}/d1`));
shell.mkdir('-p', `${t.context.tmp}/d1/d2/d3`);
t.truthy(fs.existsSync(`${t.context.tmp}/d1/d2/d3`));
const result = shell.mkdir('-p', `${t.context.tmp}/d1/d2`);
t.falsy(shell.error());
t.is(result.code, 0);
t.truthy(fs.existsSync(`${t.context.tmp}/d1/d2/d3`));
});
test('-p flag: create directory in subdirectory', t => {
t.falsy(fs.existsSync(`${t.context.tmp}/d1`));
shell.mkdir('-p', `${t.context.tmp}/d1/d2`);
t.truthy(fs.existsSync(`${t.context.tmp}/d1/d2`));
const result = shell.mkdir('-p', `${t.context.tmp}/d1/d2/d3`);
t.falsy(shell.error());
t.is(result.code, 0);
t.truthy(fs.existsSync(`${t.context.tmp}/d1/d2/d3`));
});
test('globbed dir', t => {
let result = shell.mkdir('-p', `${t.context.tmp}/mydir`);
t.falsy(shell.error());
t.is(result.code, 0);
t.truthy(fs.existsSync(`${t.context.tmp}/mydir`));
result = shell.mkdir('-p', `${t.context.tmp}/m*ir`);
t.falsy(shell.error());
t.is(result.code, 0);
t.truthy(fs.existsSync(`${t.context.tmp}/mydir`));
t.falsy(fs.existsSync(`${t.context.tmp}/m*ir`)); // doesn't create literal name
});
test('non-normalized paths are still ok with -p', t => {
const result = shell.mkdir('-p', `${t.context.tmp}/asdf/../asdf/./`);
t.falsy(shell.error());
t.is(result.code, 0);
t.truthy(fs.existsSync(`${t.context.tmp}/asdf`));
});