diff --git a/packages/glob/__tests__/internal-globber.test.ts b/packages/glob/__tests__/internal-globber.test.ts index 36dec23d7b..fe471e5700 100644 --- a/packages/glob/__tests__/internal-globber.test.ts +++ b/packages/glob/__tests__/internal-globber.test.ts @@ -97,6 +97,23 @@ describe('globber', () => { ]) }) + it('defaults to matchDirectories=true', async () => { + // Create the following layout: + // + // /folder-a + // /folder-a/file + const root = path.join(getTestTemp(), 'defaults-to-match-directories-true') + await fs.mkdir(path.join(root, 'folder-a'), {recursive: true}) + await fs.writeFile(path.join(root, 'folder-a', 'file'), 'test file content') + + const itemPaths = await glob(root, {}) + expect(itemPaths).toEqual([ + root, + path.join(root, 'folder-a'), + path.join(root, 'folder-a', 'file') + ]) + }) + it('does not match file with trailing slash when implicitDescendants=true', async () => { // Create the following layout: // @@ -361,6 +378,34 @@ describe('globber', () => { expect(itemPaths).toEqual([]) }) + it('does not return directories when match directories false', async () => { + // Create the following layout: + // /file-1 + // /dir-1 + // /dir-1/file-2 + // /dir-1/dir-2 + // /dir-1/dir-2/file-3 + const root = path.join( + getTestTemp(), + 'does-not-return-directories-when-match-directories-false' + ) + await fs.mkdir(path.join(root, 'dir-1', 'dir-2'), {recursive: true}) + await fs.writeFile(path.join(root, 'file-1'), '') + await fs.writeFile(path.join(root, 'dir-1', 'file-2'), '') + await fs.writeFile(path.join(root, 'dir-1', 'dir-2', 'file-3'), '') + + const pattern = `${root}${path.sep}**` + expect( + await glob(pattern, { + matchDirectories: false + }) + ).toEqual([ + path.join(root, 'dir-1', 'dir-2', 'file-3'), + path.join(root, 'dir-1', 'file-2'), + path.join(root, 'file-1') + ]) + }) + it('does not search paths that are not partial matches', async () => { // Create the following layout: // diff --git a/packages/glob/src/internal-glob-options-helper.ts b/packages/glob/src/internal-glob-options-helper.ts index 3c81b67194..c798b16510 100644 --- a/packages/glob/src/internal-glob-options-helper.ts +++ b/packages/glob/src/internal-glob-options-helper.ts @@ -8,6 +8,7 @@ export function getOptions(copy?: GlobOptions): GlobOptions { const result: GlobOptions = { followSymbolicLinks: true, implicitDescendants: true, + matchDirectories: true, omitBrokenSymbolicLinks: true } @@ -22,6 +23,11 @@ export function getOptions(copy?: GlobOptions): GlobOptions { core.debug(`implicitDescendants '${result.implicitDescendants}'`) } + if (typeof copy.matchDirectories === 'boolean') { + result.matchDirectories = copy.matchDirectories + core.debug(`matchDirectories '${result.matchDirectories}'`) + } + if (typeof copy.omitBrokenSymbolicLinks === 'boolean') { result.omitBrokenSymbolicLinks = copy.omitBrokenSymbolicLinks core.debug(`omitBrokenSymbolicLinks '${result.omitBrokenSymbolicLinks}'`) diff --git a/packages/glob/src/internal-glob-options.ts b/packages/glob/src/internal-glob-options.ts index 54d8b544c3..ac1e93f177 100644 --- a/packages/glob/src/internal-glob-options.ts +++ b/packages/glob/src/internal-glob-options.ts @@ -21,6 +21,14 @@ export interface GlobOptions { */ implicitDescendants?: boolean + /** + * Indicates whether matching directories should be included in the + * result set. + * + * @default true + */ + matchDirectories?: boolean + /** * Indicates whether broken symbolic should be ignored and omitted from the * result set. Otherwise an error will be thrown. diff --git a/packages/glob/src/internal-globber.ts b/packages/glob/src/internal-globber.ts index 98cbb8191a..3978d625bf 100644 --- a/packages/glob/src/internal-globber.ts +++ b/packages/glob/src/internal-globber.ts @@ -131,7 +131,7 @@ export class DefaultGlobber implements Globber { // Directory if (stats.isDirectory()) { // Matched - if (match & MatchKind.Directory) { + if (match & MatchKind.Directory && options.matchDirectories) { yield item.path } // Descend?