Skip to content

Commit

Permalink
Sort file array prior to removing files
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisblossom committed Jul 8, 2019
1 parent 9e7550b commit 0ea196a
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 6 deletions.
10 changes: 8 additions & 2 deletions index.js
Expand Up @@ -27,7 +27,9 @@ module.exports = async (patterns, {force, dryRun, ...options} = {}) => {
...options
};

const files = await globby(patterns, options);
const files = (await globby(patterns, options))
.sort()
.reverse();

const mapper = async file => {
if (!force) {
Expand All @@ -54,7 +56,11 @@ module.exports.sync = (patterns, {force, dryRun, ...options} = {}) => {
...options
};

return globby.sync(patterns, options).map(file => {
const files = globby.sync(patterns, options)
.sort()
.reverse();

return files.map(file => {
if (!force) {
safeCheck(file);
}
Expand Down
51 changes: 47 additions & 4 deletions test.js
Expand Up @@ -88,9 +88,9 @@ test('don\'t delete files, but return them - async', async t => {
});
exists(t, ['1.tmp', '2.tmp', '3.tmp', '4.tmp', '.dot.tmp']);
t.deepEqual(deletedFiles, [
path.join(t.context.tmp, '2.tmp'),
path.join(t.context.tmp, '4.tmp'),
path.join(t.context.tmp, '3.tmp'),
path.join(t.context.tmp, '4.tmp')
path.join(t.context.tmp, '2.tmp')
]);
});

Expand All @@ -101,8 +101,51 @@ test('don\'t delete files, but return them - sync', t => {
});
exists(t, ['1.tmp', '2.tmp', '3.tmp', '4.tmp', '.dot.tmp']);
t.deepEqual(deletedFiles, [
path.join(t.context.tmp, '2.tmp'),
path.join(t.context.tmp, '4.tmp'),
path.join(t.context.tmp, '3.tmp'),
path.join(t.context.tmp, '4.tmp')
path.join(t.context.tmp, '2.tmp')
]);
});

// Currently this only testable locally on an osx machine.
// https://github.com/sindresorhus/del/issues/68
test.serial('does not throw EINVAL - async', async t => {
const nestedFile = path.resolve(t.context.tmp, 'a/b/c/nested.js');
const totalAttempts = 200;

let count = 0;
while (count !== totalAttempts) {
makeDir.sync(nestedFile);

// eslint-disable-next-line no-await-in-loop
await del('**/*', {
cwd: t.context.tmp,
dot: true
});

count += 1;
}

notExists(t, [...fixtures, 'a']);
t.is(count, totalAttempts);
});

test.serial('does not throw EINVAL - sync', t => {
const nestedFile = path.resolve(t.context.tmp, 'a/b/c/nested.js');
const totalAttempts = 200;

let count = 0;
while (count !== totalAttempts) {
makeDir.sync(nestedFile);

del.sync('**/*', {
cwd: t.context.tmp,
dot: true
});

count += 1;
}

notExists(t, [...fixtures, 'a']);
t.is(count, totalAttempts);
});

0 comments on commit 0ea196a

Please sign in to comment.