Skip to content

Commit

Permalink
Merge pull request #390 from codecov/fix/ignore-paths
Browse files Browse the repository at this point in the history
Negate paths
  • Loading branch information
mitchell-codecov authored Sep 30, 2021
2 parents 7e51690 + 6e7a2be commit b14492b
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 3 deletions.
19 changes: 18 additions & 1 deletion src/helpers/files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,10 @@ export function coverageFilePatterns(): string[] {
]
}

const EMPTY_STRING = '' as const

const isNegated = (path: string) => path.startsWith('!')

/**
*
* @param {string} projectRoot
Expand All @@ -180,7 +184,20 @@ export async function getCoverageFiles(
projectRoot: string,
coverageFilePatterns: string[],
): Promise<string[]> {
return glob(coverageFilePatterns.map(globstar), {
const globstar = (pattern: string) => `**/${pattern}`

return glob(coverageFilePatterns.map((pattern: string) => {
const parts = []

if (isNegated(pattern)) {
parts.push('!')
parts.push(globstar(pattern.substr(1)))
} else {
parts.push(globstar(pattern))
}

return parts.join(EMPTY_STRING)
}), {
cwd: projectRoot,
ignore: [...manualBlacklist(), ...globBlacklist()],
})
Expand Down
11 changes: 10 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,18 @@ export async function main(
coverageFilePaths = args.file
}
}
const isNegated = (path: string) => path.startsWith('!')
coverageFilePaths = coverageFilePaths.concat(await getCoverageFiles(
args.dir || projectRoot,
coverageFilePaths.length > 0 ? coverageFilePaths : coverageFilePatterns(),
(() => {
const numOfNegatedPaths = coverageFilePaths.filter(isNegated).length

if (coverageFilePaths.length > numOfNegatedPaths) {
return coverageFilePaths
} else {
return coverageFilePaths.concat(coverageFilePatterns())
}
})(),
))

// Remove invalid and duplicate file paths
Expand Down
5 changes: 5 additions & 0 deletions test/helpers/files.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,11 @@ describe('File Helpers', () => {
await fileHelpers.getCoverageFiles('.', ['index.test.ts']),
).toStrictEqual(['test/index.test.ts', 'test/providers/index.test.ts'])
})
it('can return a list of coverage files with a negated pattern', async () => {
expect(
await fileHelpers.getCoverageFiles('.', ['index.test.ts', '!test/providers']),
).toStrictEqual(['test/index.test.ts'])
})
describe('coverage file patterns', () => {
it('contains `jacoco*.xml`', () => {
expect(fileHelpers.coverageFilePatterns()).toContain('jacoco*.xml')
Expand Down
3 changes: 2 additions & 1 deletion test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ describe('Uploader Core', () => {
dryRun: 'true',
file: [
'cover*.txt',
'!fixtures/other'
],
name: 'customname',
token: 'abcdefg',
Expand All @@ -236,7 +237,7 @@ describe('Uploader Core', () => {
expect(log).toHaveBeenCalledWith(
expect.stringMatching(/Processing.*test\/fixtures\/coverage\.txt\.\.\./),
)
expect(log).toHaveBeenCalledWith(
expect(log).not.toHaveBeenCalledWith(
expect.stringMatching(/Processing.*test\/fixtures\/other\/coverage\.txt\.\.\./),
)
expect(log).not.toHaveBeenCalledWith(
Expand Down

0 comments on commit b14492b

Please sign in to comment.