Skip to content

Commit

Permalink
Add directory filtering to globber (#728)
Browse files Browse the repository at this point in the history
Co-authored-by: Thomas Boop <52323235+thboop@users.noreply.github.com>
  • Loading branch information
MatisseHack and thboop committed Jun 1, 2021
1 parent 51dc07a commit 439eace
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 1 deletion.
45 changes: 45 additions & 0 deletions packages/glob/__tests__/internal-globber.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,23 @@ describe('globber', () => {
])
})

it('defaults to matchDirectories=true', async () => {
// Create the following layout:
// <root>
// <root>/folder-a
// <root>/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:
// <root>
Expand Down Expand Up @@ -361,6 +378,34 @@ describe('globber', () => {
expect(itemPaths).toEqual([])
})

it('does not return directories when match directories false', async () => {
// Create the following layout:
// <root>/file-1
// <root>/dir-1
// <root>/dir-1/file-2
// <root>/dir-1/dir-2
// <root>/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:
// <root>
Expand Down
6 changes: 6 additions & 0 deletions packages/glob/src/internal-glob-options-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export function getOptions(copy?: GlobOptions): GlobOptions {
const result: GlobOptions = {
followSymbolicLinks: true,
implicitDescendants: true,
matchDirectories: true,
omitBrokenSymbolicLinks: true
}

Expand All @@ -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}'`)
Expand Down
8 changes: 8 additions & 0 deletions packages/glob/src/internal-glob-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion packages/glob/src/internal-globber.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down

0 comments on commit 439eace

Please sign in to comment.