Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add directory filtering to globber #728

Merged
merged 2 commits into from
Jun 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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