From 439eaced077b820cbf01183f71a220813514a95b Mon Sep 17 00:00:00 2001 From: Matisse Hack Date: Tue, 1 Jun 2021 12:57:03 -0700 Subject: [PATCH] Add directory filtering to globber (#728) Co-authored-by: Thomas Boop <52323235+thboop@users.noreply.github.com> --- .../glob/__tests__/internal-globber.test.ts | 45 +++++++++++++++++++ .../glob/src/internal-glob-options-helper.ts | 6 +++ packages/glob/src/internal-glob-options.ts | 8 ++++ packages/glob/src/internal-globber.ts | 2 +- 4 files changed, 60 insertions(+), 1 deletion(-) diff --git a/packages/glob/__tests__/internal-globber.test.ts b/packages/glob/__tests__/internal-globber.test.ts index 36dec23d..fe471e57 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 3c81b671..c798b165 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 54d8b544..ac1e93f1 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 98cbb819..3978d625 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?