diff --git a/index.js b/index.js index 56bde1e..c2d785a 100644 --- a/index.js +++ b/index.js @@ -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 diff --git a/package.json b/package.json index 5bfa0a2..5f57eb8 100644 --- a/package.json +++ b/package.json @@ -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": [ diff --git a/test.js b/test.js index 8579ce6..8c0a718 100644 --- a/test.js +++ b/test.js @@ -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'; @@ -18,73 +18,79 @@ 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'); @@ -92,22 +98,22 @@ test('expose generateGlobTasks', t => { }); 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'] @@ -117,22 +123,22 @@ 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']); @@ -140,7 +146,7 @@ test('expandDirectories and ignores option', t => { 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'] }), []); @@ -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: [ @@ -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'}); });