Skip to content

Commit ee273df

Browse files
committed
Sort file array prior to removing files
1 parent 9e7550b commit ee273df

File tree

2 files changed

+81
-6
lines changed

2 files changed

+81
-6
lines changed

index.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ module.exports = async (patterns, {force, dryRun, ...options} = {}) => {
2727
...options
2828
};
2929

30-
const files = await globby(patterns, options);
30+
const files = (await globby(patterns, options))
31+
.sort((a, b) => b.localeCompare(a));
3132

3233
const mapper = async file => {
3334
if (!force) {
@@ -54,7 +55,10 @@ module.exports.sync = (patterns, {force, dryRun, ...options} = {}) => {
5455
...options
5556
};
5657

57-
return globby.sync(patterns, options).map(file => {
58+
const files = globby.sync(patterns, options)
59+
.sort((a, b) => b.localeCompare(a));
60+
61+
return files.map(file => {
5862
if (!force) {
5963
safeCheck(file);
6064
}

test.js

Lines changed: 75 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,9 @@ test('don\'t delete files, but return them - async', async t => {
8888
});
8989
exists(t, ['1.tmp', '2.tmp', '3.tmp', '4.tmp', '.dot.tmp']);
9090
t.deepEqual(deletedFiles, [
91-
path.join(t.context.tmp, '2.tmp'),
91+
path.join(t.context.tmp, '4.tmp'),
9292
path.join(t.context.tmp, '3.tmp'),
93-
path.join(t.context.tmp, '4.tmp')
93+
path.join(t.context.tmp, '2.tmp')
9494
]);
9595
});
9696

@@ -101,8 +101,79 @@ test('don\'t delete files, but return them - sync', t => {
101101
});
102102
exists(t, ['1.tmp', '2.tmp', '3.tmp', '4.tmp', '.dot.tmp']);
103103
t.deepEqual(deletedFiles, [
104-
path.join(t.context.tmp, '2.tmp'),
104+
path.join(t.context.tmp, '4.tmp'),
105105
path.join(t.context.tmp, '3.tmp'),
106-
path.join(t.context.tmp, '4.tmp')
106+
path.join(t.context.tmp, '2.tmp')
107107
]);
108108
});
109+
110+
// Currently this only testable locally on an osx machine.
111+
// https://github.com/sindresorhus/del/issues/68
112+
test.serial('does not throw EINVAL - async', async t => {
113+
await del('**/*', {
114+
cwd: t.context.tmp,
115+
dot: true
116+
});
117+
118+
const nestedFile = path.resolve(t.context.tmp, 'a/b/c/nested.js');
119+
const totalAttempts = 200;
120+
121+
let count = 0;
122+
while (count !== totalAttempts) {
123+
makeDir.sync(nestedFile);
124+
125+
// eslint-disable-next-line no-await-in-loop
126+
const removed = await del('**/*', {
127+
cwd: t.context.tmp,
128+
dot: true
129+
});
130+
131+
const expected = [
132+
path.resolve(t.context.tmp, 'a/b/c/nested.js'),
133+
path.resolve(t.context.tmp, 'a/b/c'),
134+
path.resolve(t.context.tmp, 'a/b'),
135+
path.resolve(t.context.tmp, 'a')
136+
];
137+
138+
t.deepEqual(removed, expected);
139+
140+
count += 1;
141+
}
142+
143+
notExists(t, [...fixtures, 'a']);
144+
t.is(count, totalAttempts);
145+
});
146+
147+
test.serial('does not throw EINVAL - sync', t => {
148+
del.sync('**/*', {
149+
cwd: t.context.tmp,
150+
dot: true
151+
});
152+
153+
const nestedFile = path.resolve(t.context.tmp, 'a/b/c/nested.js');
154+
const totalAttempts = 200;
155+
156+
let count = 0;
157+
while (count !== totalAttempts) {
158+
makeDir.sync(nestedFile);
159+
160+
const removed = del.sync('**/*', {
161+
cwd: t.context.tmp,
162+
dot: true
163+
});
164+
165+
const expected = [
166+
path.resolve(t.context.tmp, 'a/b/c/nested.js'),
167+
path.resolve(t.context.tmp, 'a/b/c'),
168+
path.resolve(t.context.tmp, 'a/b'),
169+
path.resolve(t.context.tmp, 'a')
170+
];
171+
172+
t.deepEqual(removed, expected);
173+
174+
count += 1;
175+
}
176+
177+
notExists(t, [...fixtures, 'a']);
178+
t.is(count, totalAttempts);
179+
});

0 commit comments

Comments
 (0)