Skip to content

Commit 51c8f68

Browse files
authoredJan 19, 2022
Expose new generateGlobTasks and generateGlobTasksSync (#221)
1 parent 93f83f3 commit 51c8f68

9 files changed

+230
-95
lines changed
 

‎index.d.ts

+11-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {Options as FastGlobOptions, Entry} from 'fast-glob';
44
export type GlobEntry = Entry;
55

66
export interface GlobTask {
7-
readonly pattern: string;
7+
readonly patterns: string[];
88
readonly options: Options;
99
}
1010

@@ -141,6 +141,16 @@ Note that you should avoid running the same tasks multiple times as they contain
141141
export function generateGlobTasks(
142142
patterns: string | readonly string[],
143143
options?: Options
144+
): Promise<GlobTask[]>;
145+
146+
/**
147+
@see generateGlobTasks
148+
149+
@returns An object in the format `{pattern: string, options: object}`, which can be passed as arguments to [`fast-glob`](https://github.com/mrmlnc/fast-glob). This is useful for other globbing-related packages.
150+
*/
151+
export function generateGlobTasksSync(
152+
patterns: string | readonly string[],
153+
options?: Options
144154
): GlobTask[];
145155

146156
/**

‎index.js

+24-26
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ const normalizeOptions = (options = {}) => {
4949
return options;
5050
};
5151

52-
const normalizeArguments = fn => (patterns, options) => fn(toPatternsArray(patterns), normalizeOptions(options));
52+
const normalizeArguments = fn => async (patterns, options) => fn(toPatternsArray(patterns), normalizeOptions(options));
53+
const normalizeArgumentsSync = fn => (patterns, options) => fn(toPatternsArray(patterns), normalizeOptions(options));
5354

5455
const getFilter = async options => createFilterFunction(
5556
options.gitignore && await isGitIgnored({cwd: options.cwd, ignore: options.ignore}),
@@ -71,7 +72,7 @@ const createFilterFunction = isIgnored => {
7172
const unionFastGlobResults = (results, filter) => results.flat().filter(fastGlobResult => filter(fastGlobResult));
7273
const unionFastGlobStreams = (streams, filter) => merge2(streams).pipe(new FilterStream(fastGlobResult => filter(fastGlobResult)));
7374

74-
const generateGlobTasksInternal = (patterns, taskOptions) => {
75+
const convertNegativePatterns = (patterns, taskOptions) => {
7576
const globTasks = [];
7677
for (const [index, pattern] of patterns.entries()) {
7778
if (isNegative(pattern)) {
@@ -88,7 +89,7 @@ const generateGlobTasksInternal = (patterns, taskOptions) => {
8889
ignore: [...taskOptions.ignore, ...ignore],
8990
};
9091

91-
globTasks.push({pattern, options});
92+
globTasks.push({patterns: [pattern], options});
9293
}
9394

9495
return globTasks;
@@ -100,7 +101,7 @@ const getDirGlobOptions = (options, cwd) => ({
100101
});
101102

102103
const generateTasks = async (patterns, options) => {
103-
const globTasks = generateGlobTasksInternal(patterns, options);
104+
const globTasks = convertNegativePatterns(patterns, options);
104105

105106
const {cwd, expandDirectories} = options;
106107

@@ -113,24 +114,23 @@ const generateTasks = async (patterns, options) => {
113114

114115
return Promise.all(
115116
globTasks.map(async task => {
116-
const {pattern, options} = task;
117+
let {patterns, options} = task;
117118

118-
const [
119+
[
119120
patterns,
120-
ignore,
121+
options.ignore,
121122
] = await Promise.all([
122-
dirGlob(pattern, patternExpandOptions),
123+
dirGlob(patterns, patternExpandOptions),
123124
dirGlob(options.ignore, ignoreExpandOptions),
124125
]);
125126

126-
options.ignore = ignore;
127-
return {pattern: patterns, options};
127+
return {patterns, options};
128128
}),
129129
);
130130
};
131131

132132
const generateTasksSync = (patterns, options) => {
133-
const globTasks = generateGlobTasksInternal(patterns, options);
133+
const globTasks = convertNegativePatterns(patterns, options);
134134

135135
const {cwd, expandDirectories} = options;
136136

@@ -142,50 +142,48 @@ const generateTasksSync = (patterns, options) => {
142142
const ignoreExpandOptions = cwd ? {cwd} : undefined;
143143

144144
return globTasks.map(task => {
145-
const {pattern, options} = task;
146-
const patterns = dirGlob.sync(pattern, patternExpandOptions);
145+
let {patterns, options} = task;
146+
patterns = dirGlob.sync(patterns, patternExpandOptions);
147147
options.ignore = dirGlob.sync(options.ignore, ignoreExpandOptions);
148-
return {pattern: patterns, options};
148+
return {patterns, options};
149149
});
150150
};
151151

152-
export const globby = async (patterns, options) => {
153-
patterns = toPatternsArray(patterns);
154-
options = normalizeOptions(options);
155-
152+
export const globby = normalizeArguments(async (patterns, options) => {
156153
const [
157154
tasks,
158155
filter,
159156
] = await Promise.all([
160157
generateTasks(patterns, options),
161158
getFilter(options),
162159
]);
163-
const results = await Promise.all(tasks.map(task => fastGlob(task.pattern, task.options)));
160+
const results = await Promise.all(tasks.map(task => fastGlob(task.patterns, task.options)));
164161

165162
return unionFastGlobResults(results, filter);
166-
};
163+
});
167164

168-
export const globbySync = normalizeArguments((patterns, options) => {
165+
export const globbySync = normalizeArgumentsSync((patterns, options) => {
169166
const tasks = generateTasksSync(patterns, options);
170167
const filter = getFilterSync(options);
171-
const results = tasks.map(task => fastGlob.sync(task.pattern, task.options));
168+
const results = tasks.map(task => fastGlob.sync(task.patterns, task.options));
172169

173170
return unionFastGlobResults(results, filter);
174171
});
175172

176-
export const globbyStream = normalizeArguments((patterns, options) => {
173+
export const globbyStream = normalizeArgumentsSync((patterns, options) => {
177174
const tasks = generateTasksSync(patterns, options);
178175
const filter = getFilterSync(options);
179-
const streams = tasks.map(task => fastGlob.stream(task.pattern, task.options));
176+
const streams = tasks.map(task => fastGlob.stream(task.patterns, task.options));
180177

181178
return unionFastGlobStreams(streams, filter);
182179
});
183180

184-
export const isDynamicPattern = normalizeArguments(
181+
export const isDynamicPattern = normalizeArgumentsSync(
185182
(patterns, options) => patterns.some(pattern => fastGlob.isDynamicPattern(pattern, options)),
186183
);
187184

188-
export const generateGlobTasks = normalizeArguments(generateGlobTasksInternal);
185+
export const generateGlobTasks = normalizeArguments(generateTasks);
186+
export const generateGlobTasksSync = normalizeArgumentsSync(generateTasksSync);
189187

190188
export {
191189
isGitIgnored,

‎index.test-d.ts

+27-7
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
globbySync,
1010
globbyStream,
1111
generateGlobTasks,
12+
generateGlobTasksSync,
1213
isDynamicPattern,
1314
isGitIgnored,
1415
isGitIgnoredSync,
@@ -83,23 +84,42 @@ expectType<NodeJS.ReadableStream>(globbyStream('*.tmp', {ignore: ['**/b.tmp']}))
8384
})();
8485

8586
// GenerateGlobTasks
86-
expectType<GlobTask[]>(generateGlobTasks('*.tmp'));
87-
expectType<GlobTask[]>(generateGlobTasks(['a.tmp', '*.tmp', '!{c,d,e}.tmp']));
87+
expectType<Promise<GlobTask[]>>(generateGlobTasks('*.tmp'));
88+
expectType<Promise<GlobTask[]>>(generateGlobTasks(['a.tmp', '*.tmp', '!{c,d,e}.tmp']));
8889

89-
expectType<GlobTask[]>(generateGlobTasks('*.tmp', {expandDirectories: false}));
90-
expectType<GlobTask[]>(
90+
expectType<Promise<GlobTask[]>>(generateGlobTasks('*.tmp', {expandDirectories: false}));
91+
expectType<Promise<GlobTask[]>>(
9192
generateGlobTasks('*.tmp', {expandDirectories: ['a*', 'b*']}),
9293
);
93-
expectType<GlobTask[]>(
94+
expectType<Promise<GlobTask[]>>(
9495
generateGlobTasks('*.tmp', {
9596
expandDirectories: {
9697
files: ['a', 'b'],
9798
extensions: ['tmp'],
9899
},
99100
}),
100101
);
101-
expectType<GlobTask[]>(generateGlobTasks('*.tmp', {gitignore: true}));
102-
expectType<GlobTask[]>(generateGlobTasks('*.tmp', {ignore: ['**/b.tmp']}));
102+
expectType<Promise<GlobTask[]>>(generateGlobTasks('*.tmp', {gitignore: true}));
103+
expectType<Promise<GlobTask[]>>(generateGlobTasks('*.tmp', {ignore: ['**/b.tmp']}));
104+
105+
// GenerateGlobTasksSync
106+
expectType<GlobTask[]>(generateGlobTasksSync('*.tmp'));
107+
expectType<GlobTask[]>(generateGlobTasksSync(['a.tmp', '*.tmp', '!{c,d,e}.tmp']));
108+
109+
expectType<GlobTask[]>(generateGlobTasksSync('*.tmp', {expandDirectories: false}));
110+
expectType<GlobTask[]>(
111+
generateGlobTasksSync('*.tmp', {expandDirectories: ['a*', 'b*']}),
112+
);
113+
expectType<GlobTask[]>(
114+
generateGlobTasksSync('*.tmp', {
115+
expandDirectories: {
116+
files: ['a', 'b'],
117+
extensions: ['tmp'],
118+
},
119+
}),
120+
);
121+
expectType<GlobTask[]>(generateGlobTasksSync('*.tmp', {gitignore: true}));
122+
expectType<GlobTask[]>(generateGlobTasksSync('*.tmp', {ignore: ['**/b.tmp']}));
103123

104124
// IsDynamicPattern
105125
expectType<boolean>(isDynamicPattern('**'));

‎package.json

+5
Original file line numberDiff line numberDiff line change
@@ -81,5 +81,10 @@
8181
"ignores": [
8282
"fixtures"
8383
]
84+
},
85+
"ava": {
86+
"files": [
87+
"!tests/utilities.js"
88+
]
8489
}
8590
}

‎readme.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,16 @@ import {globbyStream} from 'globby';
110110

111111
### generateGlobTasks(patterns, options?)
112112

113-
Returns an `object[]` in the format `{pattern: string, options: Object}`, which can be passed as arguments to [`fast-glob`](https://github.com/mrmlnc/fast-glob). This is useful for other globbing-related packages.
113+
Returns an `Promise<object[]>` in the format `{patterns: string[], options: Object}`, which can be passed as arguments to [`fast-glob`](https://github.com/mrmlnc/fast-glob). This is useful for other globbing-related packages.
114114

115115
Note that you should avoid running the same tasks multiple times as they contain a file system cache. Instead, run this method each time to ensure file system changes are taken into consideration.
116116

117+
### generateGlobTasksSync(patterns, options?)
118+
119+
Returns an `object[]` in the format `{patterns: string[], options: Object}`, which can be passed as arguments to [`fast-glob`](https://github.com/mrmlnc/fast-glob). This is useful for other globbing-related packages.
120+
121+
Takes the same arguments as `generateGlobTasks`.
122+
117123
### isDynamicPattern(patterns, options?)
118124

119125
Returns a `boolean` of whether there are any special glob characters in the `patterns`.

‎tests/generate-glob-tasks.js

+101
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
import util from 'node:util';
2+
import process from 'node:process';
3+
import path from 'node:path';
4+
import test from 'ava';
5+
import {
6+
generateGlobTasks,
7+
generateGlobTasksSync,
8+
} from '../index.js';
9+
import {
10+
invalidPatterns,
11+
getPathValues,
12+
} from './utilities.js';
13+
14+
const runGenerateGlobTasks = async (t, patterns, options) => {
15+
const promiseResult = await generateGlobTasks(patterns, options);
16+
const syncResult = generateGlobTasksSync(patterns, options);
17+
18+
t.deepEqual(
19+
promiseResult,
20+
syncResult,
21+
'generateGlobTasksSync() result is different than generateGlobTasks()',
22+
);
23+
24+
return promiseResult;
25+
};
26+
27+
test('generateGlobTasks', async t => {
28+
const tasks = await runGenerateGlobTasks(t, ['*.tmp', '!b.tmp'], {ignore: ['c.tmp']});
29+
30+
t.is(tasks.length, 1);
31+
t.deepEqual(tasks[0].patterns, ['*.tmp']);
32+
t.deepEqual(tasks[0].options.ignore, ['c.tmp', 'b.tmp']);
33+
await t.notThrowsAsync(generateGlobTasks('*'));
34+
t.notThrows(() => generateGlobTasksSync('*'));
35+
});
36+
37+
// Rejected for being an invalid pattern
38+
for (const value of invalidPatterns) {
39+
const valueString = util.format(value);
40+
const message = 'Patterns must be a string or an array of strings';
41+
42+
test(`throws for invalid patterns input: ${valueString}`, async t => {
43+
await t.throwsAsync(generateGlobTasks(value), {instanceOf: TypeError, message});
44+
t.throws(() => generateGlobTasksSync(value), {instanceOf: TypeError, message});
45+
});
46+
}
47+
48+
test('throws when specifying a file as cwd', async t => {
49+
const error = {message: 'The `cwd` option must be a path to a directory'};
50+
51+
for (const file of getPathValues(path.resolve('fixtures/gitignore/bar.js'))) {
52+
// eslint-disable-next-line no-await-in-loop
53+
await t.throwsAsync(generateGlobTasks('*', {cwd: file}), error);
54+
t.throws(() => generateGlobTasksSync('*', {cwd: file}), error);
55+
}
56+
});
57+
58+
test('cwd', async t => {
59+
const cwd = process.cwd();
60+
for (const cwdDirectory of getPathValues(cwd)) {
61+
// eslint-disable-next-line no-await-in-loop
62+
const [task] = await runGenerateGlobTasks(t, ['*'], {cwd: cwdDirectory});
63+
t.is(task.options.cwd, cwd);
64+
}
65+
});
66+
67+
test('expandDirectories option', async t => {
68+
{
69+
const tasks = await runGenerateGlobTasks(t, ['fixtures'], {ignore: ['fixtures/negative']});
70+
t.is(tasks.length, 1);
71+
t.deepEqual(tasks[0].patterns, ['fixtures/**']);
72+
t.deepEqual(tasks[0].options.ignore, ['fixtures/negative/**']);
73+
}
74+
75+
{
76+
const tasks = await runGenerateGlobTasks(t, ['fixtures'], {ignore: ['fixtures/negative'], expandDirectories: false});
77+
t.is(tasks.length, 1);
78+
t.deepEqual(tasks[0].patterns, ['fixtures']);
79+
t.deepEqual(tasks[0].options.ignore, ['fixtures/negative']);
80+
}
81+
82+
{
83+
const tasks = await runGenerateGlobTasks(t, ['fixtures'], {expandDirectories: ['a*', 'b*']});
84+
t.is(tasks.length, 1);
85+
t.deepEqual(tasks[0].patterns, ['fixtures/**/a*', 'fixtures/**/b*']);
86+
t.deepEqual(tasks[0].options.ignore, []);
87+
}
88+
89+
{
90+
const tasks = await runGenerateGlobTasks(t, ['fixtures'], {
91+
expandDirectories: {
92+
files: ['a', 'b*'],
93+
extensions: ['tmp', 'txt'],
94+
},
95+
ignore: ['**/b.tmp'],
96+
});
97+
t.is(tasks.length, 1);
98+
t.deepEqual(tasks[0].patterns, ['fixtures/**/a.{tmp,txt}', 'fixtures/**/b*.{tmp,txt}']);
99+
t.deepEqual(tasks[0].options.ignore, ['**/b.tmp']);
100+
}
101+
});

‎gitignore.test.js ‎tests/gitignore.js

+16-16
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import path from 'node:path';
2-
import {fileURLToPath, pathToFileURL} from 'node:url';
32
import test from 'ava';
43
import slash from 'slash';
5-
import {isGitIgnored, isGitIgnoredSync} from './gitignore.js';
6-
7-
const __dirname = path.dirname(fileURLToPath(import.meta.url));
8-
const getPathValues = cwd => [cwd, pathToFileURL(cwd)];
4+
import {isGitIgnored, isGitIgnoredSync} from '../gitignore.js';
5+
import {
6+
PROJECT_ROOT,
7+
getPathValues,
8+
} from './utilities.js';
99

1010
test('gitignore', async t => {
11-
for (const cwd of getPathValues(path.join(__dirname, 'fixtures/gitignore'))) {
11+
for (const cwd of getPathValues(path.join(PROJECT_ROOT, 'fixtures/gitignore'))) {
1212
// eslint-disable-next-line no-await-in-loop
1313
const isIgnored = await isGitIgnored({cwd});
1414
const actual = ['foo.js', 'bar.js'].filter(file => !isIgnored(file));
@@ -18,7 +18,7 @@ test('gitignore', async t => {
1818
});
1919

2020
test('gitignore - mixed path styles', async t => {
21-
const directory = path.join(__dirname, 'fixtures/gitignore');
21+
const directory = path.join(PROJECT_ROOT, 'fixtures/gitignore');
2222
for (const cwd of getPathValues(directory)) {
2323
// eslint-disable-next-line no-await-in-loop
2424
const isIgnored = await isGitIgnored({cwd});
@@ -27,7 +27,7 @@ test('gitignore - mixed path styles', async t => {
2727
});
2828

2929
test('gitignore - os paths', async t => {
30-
const directory = path.join(__dirname, 'fixtures/gitignore');
30+
const directory = path.join(PROJECT_ROOT, 'fixtures/gitignore');
3131
for (const cwd of getPathValues(directory)) {
3232
// eslint-disable-next-line no-await-in-loop
3333
const isIgnored = await isGitIgnored({cwd});
@@ -36,7 +36,7 @@ test('gitignore - os paths', async t => {
3636
});
3737

3838
test('gitignore - sync', t => {
39-
for (const cwd of getPathValues(path.join(__dirname, 'fixtures/gitignore'))) {
39+
for (const cwd of getPathValues(path.join(PROJECT_ROOT, 'fixtures/gitignore'))) {
4040
const isIgnored = isGitIgnoredSync({cwd});
4141
const actual = ['foo.js', 'bar.js'].filter(file => !isIgnored(file));
4242
const expected = ['bar.js'];
@@ -47,7 +47,7 @@ test('gitignore - sync', t => {
4747
test('ignore ignored .gitignore', async t => {
4848
const ignore = ['**/.gitignore'];
4949

50-
for (const cwd of getPathValues(path.join(__dirname, 'fixtures/gitignore'))) {
50+
for (const cwd of getPathValues(path.join(PROJECT_ROOT, 'fixtures/gitignore'))) {
5151
// eslint-disable-next-line no-await-in-loop
5252
const isIgnored = await isGitIgnored({cwd, ignore});
5353
const actual = ['foo.js', 'bar.js'].filter(file => !isIgnored(file));
@@ -59,7 +59,7 @@ test('ignore ignored .gitignore', async t => {
5959
test('ignore ignored .gitignore - sync', t => {
6060
const ignore = ['**/.gitignore'];
6161

62-
for (const cwd of getPathValues(path.join(__dirname, 'fixtures/gitignore'))) {
62+
for (const cwd of getPathValues(path.join(PROJECT_ROOT, 'fixtures/gitignore'))) {
6363
const isIgnored = isGitIgnoredSync({cwd, ignore});
6464
const actual = ['foo.js', 'bar.js'].filter(file => !isIgnored(file));
6565
const expected = ['foo.js', 'bar.js'];
@@ -68,7 +68,7 @@ test('ignore ignored .gitignore - sync', t => {
6868
});
6969

7070
test('negative gitignore', async t => {
71-
for (const cwd of getPathValues(path.join(__dirname, 'fixtures/negative'))) {
71+
for (const cwd of getPathValues(path.join(PROJECT_ROOT, 'fixtures/negative'))) {
7272
// eslint-disable-next-line no-await-in-loop
7373
const isIgnored = await isGitIgnored({cwd});
7474
const actual = ['foo.js', 'bar.js'].filter(file => !isIgnored(file));
@@ -78,7 +78,7 @@ test('negative gitignore', async t => {
7878
});
7979

8080
test('negative gitignore - sync', t => {
81-
for (const cwd of getPathValues(path.join(__dirname, 'fixtures/negative'))) {
81+
for (const cwd of getPathValues(path.join(PROJECT_ROOT, 'fixtures/negative'))) {
8282
const isIgnored = isGitIgnoredSync({cwd});
8383
const actual = ['foo.js', 'bar.js'].filter(file => !isIgnored(file));
8484
const expected = ['foo.js'];
@@ -87,7 +87,7 @@ test('negative gitignore - sync', t => {
8787
});
8888

8989
test('multiple negation', async t => {
90-
for (const cwd of getPathValues(path.join(__dirname, 'fixtures/multiple-negation'))) {
90+
for (const cwd of getPathValues(path.join(PROJECT_ROOT, 'fixtures/multiple-negation'))) {
9191
// eslint-disable-next-line no-await-in-loop
9292
const isIgnored = await isGitIgnored({cwd});
9393

@@ -104,7 +104,7 @@ test('multiple negation', async t => {
104104
});
105105

106106
test('multiple negation - sync', t => {
107-
for (const cwd of getPathValues(path.join(__dirname, 'fixtures/multiple-negation'))) {
107+
for (const cwd of getPathValues(path.join(PROJECT_ROOT, 'fixtures/multiple-negation'))) {
108108
const isIgnored = isGitIgnoredSync({cwd});
109109

110110
const actual = [
@@ -120,7 +120,7 @@ test('multiple negation - sync', t => {
120120
});
121121

122122
test('check file', async t => {
123-
const directory = path.join(__dirname, 'fixtures/gitignore');
123+
const directory = path.join(PROJECT_ROOT, 'fixtures/gitignore');
124124
const ignoredFile = path.join(directory, 'foo.js');
125125
const isIgnored = await isGitIgnored({cwd: directory});
126126
const isIgnoredSync = isGitIgnoredSync({cwd: directory});

‎test.js ‎tests/globby.js

+17-44
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,20 @@ import process from 'node:process';
22
import fs from 'node:fs';
33
import path from 'node:path';
44
import util from 'node:util';
5-
import {fileURLToPath, pathToFileURL} from 'node:url';
65
import test from 'ava';
76
import getStream from 'get-stream';
87
import {
98
globby,
109
globbySync,
1110
globbyStream,
1211
isDynamicPattern,
13-
generateGlobTasks,
14-
} from './index.js';
12+
} from '../index.js';
13+
import {
14+
PROJECT_ROOT,
15+
getPathValues,
16+
invalidPatterns,
17+
} from './utilities.js';
1518

16-
const __dirname = path.dirname(fileURLToPath(import.meta.url));
1719
const cwd = process.cwd();
1820
const temporary = 'tmp';
1921

@@ -25,8 +27,6 @@ const fixture = [
2527
'e.tmp',
2628
];
2729

28-
const getCwdValues = cwd => [cwd, pathToFileURL(cwd)];
29-
3030
const stabilizeResult = result => result
3131
.map(fastGlobResult => {
3232
// In `objectMode`, `fastGlobResult.dirent` contains a function that makes `t.deepEqual` assertion fail.
@@ -68,14 +68,14 @@ test.before(() => {
6868

6969
for (const element of fixture) {
7070
fs.writeFileSync(element, '');
71-
fs.writeFileSync(path.join(__dirname, temporary, element), '');
71+
fs.writeFileSync(path.join(PROJECT_ROOT, temporary, element), '');
7272
}
7373
});
7474

7575
test.after(() => {
7676
for (const element of fixture) {
7777
fs.unlinkSync(element);
78-
fs.unlinkSync(path.join(__dirname, temporary, element));
78+
fs.unlinkSync(path.join(PROJECT_ROOT, temporary, element));
7979
}
8080

8181
fs.rmdirSync(temporary);
@@ -123,28 +123,19 @@ test('don\'t mutate the options object - async', async t => {
123123
t.pass();
124124
});
125125

126-
test('expose generateGlobTasks', t => {
127-
const tasks = generateGlobTasks(['*.tmp', '!b.tmp'], {ignore: ['c.tmp']});
128-
129-
t.is(tasks.length, 1);
130-
t.is(tasks[0].pattern, '*.tmp');
131-
t.deepEqual(tasks[0].options.ignore, ['c.tmp', 'b.tmp']);
132-
t.notThrows(() => generateGlobTasks('*'));
133-
});
134-
135126
test('expose isDynamicPattern', t => {
136127
t.true(isDynamicPattern('**'));
137128
t.true(isDynamicPattern(['**', 'path1', 'path2']));
138129
t.false(isDynamicPattern(['path1', 'path2']));
139130

140-
for (const cwdDirectory of getCwdValues(cwd)) {
131+
for (const cwdDirectory of getPathValues(cwd)) {
141132
t.true(isDynamicPattern('**', {cwd: cwdDirectory}));
142133
}
143134
});
144135

145136
test('expandDirectories option', async t => {
146137
t.deepEqual(await runGlobby(t, temporary), ['tmp/a.tmp', 'tmp/b.tmp', 'tmp/c.tmp', 'tmp/d.tmp', 'tmp/e.tmp']);
147-
for (const temporaryDirectory of getCwdValues(temporary)) {
138+
for (const temporaryDirectory of getPathValues(temporary)) {
148139
// eslint-disable-next-line no-await-in-loop
149140
t.deepEqual(await runGlobby(t, '**', {cwd: temporaryDirectory}), ['a.tmp', 'b.tmp', 'c.tmp', 'd.tmp', 'e.tmp']);
150141
}
@@ -189,7 +180,7 @@ test('expandDirectories and ignores option', async t => {
189180

190181
test.serial.failing('relative paths and ignores option', async t => {
191182
process.chdir(temporary);
192-
for (const cwd of getCwdValues(process.cwd())) {
183+
for (const cwd of getPathValues(process.cwd())) {
193184
// eslint-disable-next-line no-await-in-loop
194185
t.deepEqual(await runGlobby(t, '../tmp', {
195186
cwd,
@@ -201,32 +192,14 @@ test.serial.failing('relative paths and ignores option', async t => {
201192
});
202193

203194
// Rejected for being an invalid pattern
204-
for (const value of [
205-
{},
206-
[{}],
207-
true,
208-
[true],
209-
false,
210-
[false],
211-
null,
212-
[null],
213-
undefined,
214-
[undefined],
215-
Number.NaN,
216-
[Number.NaN],
217-
5,
218-
[5],
219-
function () {},
220-
[function () {}],
221-
]) {
195+
for (const value of invalidPatterns) {
222196
const valueString = util.format(value);
223197
const message = 'Patterns must be a string or an array of strings';
224198

225199
test(`throws for invalid patterns input: ${valueString}`, async t => {
226-
await t.throwsAsync(globby(t, value), {instanceOf: TypeError, message});
200+
await t.throwsAsync(globby(value), {instanceOf: TypeError, message});
227201
t.throws(() => globbySync(value), {instanceOf: TypeError, message});
228202
t.throws(() => globbyStream(value), {instanceOf: TypeError, message});
229-
t.throws(() => generateGlobTasks(value), {instanceOf: TypeError, message});
230203
t.throws(() => isDynamicPattern(value), {instanceOf: TypeError, message});
231204
});
232205
}
@@ -264,7 +237,7 @@ test('gitignore option and objectMode option', async t => {
264237
});
265238

266239
test('`{extension: false}` and `expandDirectories.extensions` option', async t => {
267-
for (const temporaryDirectory of getCwdValues(temporary)) {
240+
for (const temporaryDirectory of getPathValues(temporary)) {
268241
t.deepEqual(
269242
// eslint-disable-next-line no-await-in-loop
270243
await runGlobby(t, '*', {
@@ -291,7 +264,7 @@ test('`{extension: false}` and `expandDirectories.extensions` option', async t =
291264
test('throws when specifying a file as cwd', async t => {
292265
const error = {message: 'The `cwd` option must be a path to a directory'};
293266

294-
for (const file of getCwdValues(path.resolve('fixtures/gitignore/bar.js'))) {
267+
for (const file of getPathValues(path.resolve('fixtures/gitignore/bar.js'))) {
295268
// eslint-disable-next-line no-await-in-loop
296269
await t.throwsAsync(globby('.', {cwd: file}), error);
297270
// eslint-disable-next-line no-await-in-loop
@@ -304,7 +277,7 @@ test('throws when specifying a file as cwd', async t => {
304277
});
305278

306279
test('throws when specifying a file as cwd - isDynamicPattern', t => {
307-
for (const file of getCwdValues(path.resolve('fixtures/gitignore/bar.js'))) {
280+
for (const file of getPathValues(path.resolve('fixtures/gitignore/bar.js'))) {
308281
t.throws(() => {
309282
isDynamicPattern('.', {cwd: file});
310283
}, {message: 'The `cwd` option must be a path to a directory'});
@@ -316,7 +289,7 @@ test('throws when specifying a file as cwd - isDynamicPattern', t => {
316289
});
317290

318291
test('don\'t throw when specifying a non-existing cwd directory', async t => {
319-
for (const cwd of getCwdValues('/unknown')) {
292+
for (const cwd of getPathValues('/unknown')) {
320293
// eslint-disable-next-line no-await-in-loop
321294
const actual = await runGlobby(t, '.', {cwd});
322295
t.is(actual.length, 0);

‎tests/utilities.js

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import {fileURLToPath, pathToFileURL} from 'node:url';
2+
3+
export const PROJECT_ROOT = fileURLToPath(new URL('../', import.meta.url));
4+
export const getPathValues = path => [path, pathToFileURL(path)];
5+
export const invalidPatterns = [
6+
{},
7+
[{}],
8+
true,
9+
[true],
10+
false,
11+
[false],
12+
null,
13+
[null],
14+
undefined,
15+
[undefined],
16+
Number.NaN,
17+
[Number.NaN],
18+
5,
19+
[5],
20+
function () {},
21+
[function () {}],
22+
];

0 commit comments

Comments
 (0)
Please sign in to comment.