Skip to content

Commit

Permalink
Meta tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Feb 16, 2019
1 parent d77a623 commit eb26c7e
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 53 deletions.
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ const globToTask = task => glob => {
if (options.ignore && Array.isArray(options.ignore) && options.expandDirectories) {
options.ignore = dirGlob.sync(options.ignore);
}

return {
pattern: glob,
options
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,12 @@
"slash": "^2.0.0"
},
"devDependencies": {
"ava": "^1.0.1",
"ava": "^1.2.1",
"glob-stream": "^6.1.0",
"globby": "sindresorhus/globby#master",
"matcha": "^0.7.0",
"rimraf": "^2.6.3",
"xo": "^0.23.0"
"xo": "^0.24.0"
},
"xo": {
"ignores": [
Expand Down
108 changes: 57 additions & 51 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import fs from 'fs';
import util from 'util';
import path from 'path';
import test from 'ava';
import m from '.';
import globby from '.';

const cwd = process.cwd();
const tmp = 'tmp';
Expand All @@ -18,96 +18,102 @@ test.before(() => {
if (!fs.existsSync(tmp)) {
fs.mkdirSync(tmp);
}
fixture.forEach(fs.writeFileSync.bind(fs));
fixture.forEach(x => fs.writeFileSync(path.join(__dirname, tmp, x)));

for (const element of fixture) {
fs.writeFileSync(element);
fs.writeFileSync(path.join(__dirname, tmp, element));
}
});

test.after(() => {
fixture.forEach(fs.unlinkSync.bind(fs));
fixture.forEach(x => fs.unlinkSync(path.join(__dirname, tmp, x)));
for (const element of fixture) {
fs.unlinkSync(element);
fs.unlinkSync(path.join(__dirname, tmp, element));
}

fs.rmdirSync(tmp);
});

test('glob - async', async t => {
t.deepEqual(await m('*.tmp'), ['a.tmp', 'b.tmp', 'c.tmp', 'd.tmp', 'e.tmp']);
t.deepEqual((await globby('*.tmp')).sort(), ['a.tmp', 'b.tmp', 'c.tmp', 'd.tmp', 'e.tmp']);
});

test('glob - async - multiple file paths', t => {
t.deepEqual(m.sync(['a.tmp', 'b.tmp']), ['a.tmp', 'b.tmp']);
t.deepEqual(globby.sync(['a.tmp', 'b.tmp']), ['a.tmp', 'b.tmp']);
});

test('glob with multiple patterns - async', async t => {
t.deepEqual(await m(['a.tmp', '*.tmp', '!{c,d,e}.tmp']), ['a.tmp', 'b.tmp']);
t.deepEqual(await globby(['a.tmp', '*.tmp', '!{c,d,e}.tmp']), ['a.tmp', 'b.tmp']);
});

test('respect patterns order - async', async t => {
t.deepEqual(await m(['!*.tmp', 'a.tmp']), ['a.tmp']);
t.deepEqual(await globby(['!*.tmp', 'a.tmp']), ['a.tmp']);
});

test('respect patterns order - sync', t => {
t.deepEqual(m.sync(['!*.tmp', 'a.tmp']), ['a.tmp']);
t.deepEqual(globby.sync(['!*.tmp', 'a.tmp']), ['a.tmp']);
});

test('glob - sync', t => {
t.deepEqual(m.sync('*.tmp'), ['a.tmp', 'b.tmp', 'c.tmp', 'd.tmp', 'e.tmp']);
t.deepEqual(m.sync(['a.tmp', '*.tmp', '!{c,d,e}.tmp']), ['a.tmp', 'b.tmp']);
t.deepEqual(m.sync(['!*.tmp', 'a.tmp']), ['a.tmp']);
t.deepEqual(globby.sync('*.tmp'), ['a.tmp', 'b.tmp', 'c.tmp', 'd.tmp', 'e.tmp']);
t.deepEqual(globby.sync(['a.tmp', '*.tmp', '!{c,d,e}.tmp']), ['a.tmp', 'b.tmp']);
t.deepEqual(globby.sync(['!*.tmp', 'a.tmp']), ['a.tmp']);
});

test('glob - sync - multiple file paths', t => {
t.deepEqual(m.sync(['a.tmp', 'b.tmp']), ['a.tmp', 'b.tmp']);
t.deepEqual(globby.sync(['a.tmp', 'b.tmp']), ['a.tmp', 'b.tmp']);
});

test('return [] for all negative patterns - sync', t => {
t.deepEqual(m.sync(['!a.tmp', '!b.tmp']), []);
t.deepEqual(globby.sync(['!a.tmp', '!b.tmp']), []);
});

test('return [] for all negative patterns - async', async t => {
t.deepEqual(await m(['!a.tmp', '!b.tmp']), []);
t.deepEqual(await globby(['!a.tmp', '!b.tmp']), []);
});

test('cwd option', t => {
process.chdir(tmp);
t.deepEqual(m.sync('*.tmp', {cwd}), ['a.tmp', 'b.tmp', 'c.tmp', 'd.tmp', 'e.tmp']);
t.deepEqual(m.sync(['a.tmp', '*.tmp', '!{c,d,e}.tmp'], {cwd}), ['a.tmp', 'b.tmp']);
t.deepEqual(globby.sync('*.tmp', {cwd}), ['a.tmp', 'b.tmp', 'c.tmp', 'd.tmp', 'e.tmp']);
t.deepEqual(globby.sync(['a.tmp', '*.tmp', '!{c,d,e}.tmp'], {cwd}), ['a.tmp', 'b.tmp']);
process.chdir(cwd);
});

test('don\'t mutate the options object - async', async t => {
await m(['*.tmp', '!b.tmp'], Object.freeze({ignore: Object.freeze([])}));
await globby(['*.tmp', '!b.tmp'], Object.freeze({ignore: Object.freeze([])}));
t.pass();
});

test('don\'t mutate the options object - sync', t => {
m.sync(['*.tmp', '!b.tmp'], Object.freeze({ignore: Object.freeze([])}));
globby.sync(['*.tmp', '!b.tmp'], Object.freeze({ignore: Object.freeze([])}));
t.pass();
});

test('expose generateGlobTasks', t => {
const tasks = m.generateGlobTasks(['*.tmp', '!b.tmp'], {ignore: ['c.tmp']});
const tasks = globby.generateGlobTasks(['*.tmp', '!b.tmp'], {ignore: ['c.tmp']});

t.is(tasks.length, 1);
t.is(tasks[0].pattern, '*.tmp');
t.deepEqual(tasks[0].options.ignore, ['c.tmp', 'b.tmp']);
});

test('expose hasMagic', t => {
t.true(m.hasMagic('**'));
t.true(m.hasMagic(['**', 'path1', 'path2']));
t.false(m.hasMagic(['path1', 'path2']));
t.true(globby.hasMagic('**'));
t.true(globby.hasMagic(['**', 'path1', 'path2']));
t.false(globby.hasMagic(['path1', 'path2']));
});

test('expandDirectories option', t => {
t.deepEqual(m.sync(tmp), ['tmp/a.tmp', 'tmp/b.tmp', 'tmp/c.tmp', 'tmp/d.tmp', 'tmp/e.tmp']);
t.deepEqual(m.sync('**', {cwd: tmp}), ['a.tmp', 'b.tmp', 'c.tmp', 'd.tmp', 'e.tmp']);
t.deepEqual(m.sync(tmp, {expandDirectories: ['a*', 'b*']}), ['tmp/a.tmp', 'tmp/b.tmp']);
t.deepEqual(m.sync(tmp, {
t.deepEqual(globby.sync(tmp), ['tmp/a.tmp', 'tmp/b.tmp', 'tmp/c.tmp', 'tmp/d.tmp', 'tmp/e.tmp']);
t.deepEqual(globby.sync('**', {cwd: tmp}), ['a.tmp', 'b.tmp', 'c.tmp', 'd.tmp', 'e.tmp']);
t.deepEqual(globby.sync(tmp, {expandDirectories: ['a*', 'b*']}), ['tmp/a.tmp', 'tmp/b.tmp']);
t.deepEqual(globby.sync(tmp, {
expandDirectories: {
files: ['a', 'b'],
extensions: ['tmp']
}
}), ['tmp/a.tmp', 'tmp/b.tmp']);
t.deepEqual(m.sync(tmp, {
t.deepEqual(globby.sync(tmp, {
expandDirectories: {
files: ['a', 'b'],
extensions: ['tmp']
Expand All @@ -117,30 +123,30 @@ test('expandDirectories option', t => {
});

test('expandDirectories:true and onlyFiles:true option', t => {
t.deepEqual(m.sync(tmp, {onlyFiles: true}), ['tmp/a.tmp', 'tmp/b.tmp', 'tmp/c.tmp', 'tmp/d.tmp', 'tmp/e.tmp']);
t.deepEqual(globby.sync(tmp, {onlyFiles: true}), ['tmp/a.tmp', 'tmp/b.tmp', 'tmp/c.tmp', 'tmp/d.tmp', 'tmp/e.tmp']);
});

test.failing('expandDirectories:true and onlyFiles:false option', t => {
// Node-glob('tmp/**') => ['tmp', 'tmp/a.tmp', 'tmp/b.tmp', 'tmp/c.tmp', 'tmp/d.tmp', 'tmp/e.tmp']
// Fast-glob('tmp/**') => ['tmp/a.tmp', 'tmp/b.tmp', 'tmp/c.tmp', 'tmp/d.tmp', 'tmp/e.tmp']
// See https://github.com/mrmlnc/fast-glob/issues/47
t.deepEqual(m.sync(tmp, {onlyFiles: false}), ['tmp', 'tmp/a.tmp', 'tmp/b.tmp', 'tmp/c.tmp', 'tmp/d.tmp', 'tmp/e.tmp']);
t.deepEqual(globby.sync(tmp, {onlyFiles: false}), ['tmp', 'tmp/a.tmp', 'tmp/b.tmp', 'tmp/c.tmp', 'tmp/d.tmp', 'tmp/e.tmp']);
});

test('expandDirectories and ignores option', t => {
t.deepEqual(m.sync('tmp', {
t.deepEqual(globby.sync('tmp', {
ignore: ['tmp']
}), []);

t.deepEqual(m.sync('tmp/**', {
t.deepEqual(globby.sync('tmp/**', {
expandDirectories: false,
ignore: ['tmp']
}), ['tmp/a.tmp', 'tmp/b.tmp', 'tmp/c.tmp', 'tmp/d.tmp', 'tmp/e.tmp']);
});

test.failing('relative paths and ignores option', t => {
process.chdir(tmp);
t.deepEqual(m.sync('../tmp', {
t.deepEqual(globby.sync('../tmp', {
cwd: process.cwd(),
ignore: ['tmp']
}), []);
Expand Down Expand Up @@ -170,55 +176,55 @@ test.failing('relative paths and ignores option', t => {
const msg = 'Patterns must be a string or an array of strings';

test(`rejects the promise for invalid patterns input: ${valstring} - async`, async t => {
await t.throwsAsync(m(v), TypeError);
await t.throwsAsync(m(v), msg);
await t.throwsAsync(globby(v), TypeError);
await t.throwsAsync(globby(v), msg);
});

test(`throws for invalid patterns input: ${valstring}`, t => {
t.throws(() => m.sync(v), TypeError);
t.throws(() => m.sync(v), msg);
t.throws(() => globby.sync(v), TypeError);
t.throws(() => globby.sync(v), msg);
});

test(`generateGlobTasks throws for invalid patterns input: ${valstring}`, t => {
t.throws(() => m.generateGlobTasks(v), TypeError);
t.throws(() => m.generateGlobTasks(v), msg);
t.throws(() => globby.generateGlobTasks(v), TypeError);
t.throws(() => globby.generateGlobTasks(v), msg);
});
});

test('gitignore option defaults to false', async t => {
const actual = await m('*', {onlyFiles: false});
const actual = await globby('*', {onlyFiles: false});
t.true(actual.indexOf('node_modules') > -1);
});

test('gitignore option defaults to false - sync', t => {
const actual = m.sync('*', {onlyFiles: false});
const actual = globby.sync('*', {onlyFiles: false});
t.true(actual.indexOf('node_modules') > -1);
});

test('respects gitignore option true', async t => {
const actual = await m('*', {gitignore: true, onlyFiles: false});
const actual = await globby('*', {gitignore: true, onlyFiles: false});
t.false(actual.indexOf('node_modules') > -1);
});

test('respects gitignore option true - sync', t => {
const actual = m.sync('*', {gitignore: true, onlyFiles: false});
const actual = globby.sync('*', {gitignore: true, onlyFiles: false});
t.false(actual.indexOf('node_modules') > -1);
});

test('respects gitignore option false', async t => {
const actual = await m('*', {gitignore: false, onlyFiles: false});
const actual = await globby('*', {gitignore: false, onlyFiles: false});
t.true(actual.indexOf('node_modules') > -1);
});

test('respects gitignore option false - sync', t => {
const actual = m.sync('*', {gitignore: false, onlyFiles: false});
const actual = globby.sync('*', {gitignore: false, onlyFiles: false});
t.true(actual.indexOf('node_modules') > -1);
});

// https://github.com/sindresorhus/globby/issues/97
test.failing('`{extension: false}` and `expandDirectories.extensions` option', t => {
t.deepEqual(
m.sync(tmp, {
globby.sync(tmp, {
extension: false,
expandDirectories: {
extensions: [
Expand All @@ -240,17 +246,17 @@ test.failing('`{extension: false}` and `expandDirectories.extensions` option', t
// https://github.com/sindresorhus/globby/issues/105
test.failing('throws ENOTDIR when specifying a file as cwd - async', async t => {
const isFile = path.resolve('fixtures/gitignore/bar.js');
await t.throwsAsync(m('.', {cwd: isFile}), {code: 'ENOTDIR'});
await t.throwsAsync(m('*', {cwd: isFile}), {code: 'ENOTDIR'});
await t.throwsAsync(globby('.', {cwd: isFile}), {code: 'ENOTDIR'});
await t.throwsAsync(globby('*', {cwd: isFile}), {code: 'ENOTDIR'});
});

// https://github.com/sindresorhus/globby/issues/105
test.failing('throws ENOTDIR when specifying a file as cwd - sync', t => {
const isFile = path.resolve('fixtures/gitignore/bar.js');
t.throws(() => {
m.sync('.', {cwd: isFile});
globby.sync('.', {cwd: isFile});
}, {code: 'ENOTDIR'});
t.throws(() => {
m.sync('*', {cwd: isFile});
globby.sync('*', {cwd: isFile});
}, {code: 'ENOTDIR'});
});

0 comments on commit eb26c7e

Please sign in to comment.