Repository: codecov/uploader
Pull request #390 (2021-09-28 17:02:59)

Negate paths


Patch:
diff --git a/src/helpers/files.ts b/src/helpers/files.ts
index 16ab397..54058f0 100644
--- a/src/helpers/files.ts
+++ b/src/helpers/files.ts
@@ -170,6 +170,10 @@ export function coverageFilePatterns(): string[] {
   ]
 }
 
+const EMPTY_STRING = '' as const
+
+const isNegated = (path: string) => path.startsWith('!')
+
 /**
  *
  * @param {string} projectRoot
@@ -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()],
   })
diff --git a/src/index.ts b/src/index.ts
index 025284a..f5b9f3f 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -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


Test patch:
diff --git a/test/helpers/files.test.ts b/test/helpers/files.test.ts
index c5737b6..0a214ee 100644
--- a/test/helpers/files.test.ts
+++ b/test/helpers/files.test.ts
@@ -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')
diff --git a/test/index.test.ts b/test/index.test.ts
index e5f600f..3753790 100644
--- a/test/index.test.ts
+++ b/test/index.test.ts
@@ -227,6 +227,7 @@ describe('Uploader Core', () => {
       dryRun: 'true',
       file: [
         'cover*.txt',
+        '!fixtures/other'
       ],
       name: 'customname',
       token: 'abcdefg',
@@ -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(