Repository: codecov/uploader
Pull request #658 (2022-03-09 01:33:27)

fix: add helper arg array method, pass in extra gcov args


Patch:
diff --git a/src/helpers/gcov.ts b/src/helpers/gcov.ts
index dd449e1..91cda05 100644
--- a/src/helpers/gcov.ts
+++ b/src/helpers/gcov.ts
@@ -3,7 +3,7 @@ import glob from 'fast-glob'
 import { manualBlocklist } from '../../src/helpers/files'
 import { isProgramInstalled, runExternalProgram } from "./util"
 
-export async function generateGcovCoverageFiles(projectRoot: string, include: string[] = [], ignore: string[] = [], gcovArgs = '',): Promise<string> {
+export async function generateGcovCoverageFiles(projectRoot: string, include: string[] = [], ignore: string[] = [], gcovArgs: string[] = []): Promise<string> {
     if (!isProgramInstalled('gcov')) {
         throw new Error('gcov is not installed, cannot process files')
     }
@@ -15,5 +15,5 @@ export async function generateGcovCoverageFiles(projectRoot: string, include: st
     if (!files.length) {
         throw new Error('No gcov files found')
     }
-    return runExternalProgram('gcov', ['-pb', gcovArgs, ...files]);
+    return runExternalProgram('gcov', ['-pb', ...gcovArgs, ...files]);
 }
diff --git a/src/helpers/util.ts b/src/helpers/util.ts
index f5d172f..0e9919b 100644
--- a/src/helpers/util.ts
+++ b/src/helpers/util.ts
@@ -18,3 +18,11 @@ export function runExternalProgram(
 export function isSetAndNotEmpty(val: string | undefined): boolean {
   return typeof val !== 'undefined' && val !== ''
 }
+
+export function argAsArray<T>(args?: T | T[]): T[] {
+  const result: T[] = []
+  if (typeof args === "undefined") {
+    return result
+  }
+  return result.concat(args)
+}
diff --git a/src/index.ts b/src/index.ts
index dd03d45..050dbab 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -23,6 +23,7 @@ import {
   removeFile,
 } from './helpers/files'
 import { generateGcovCoverageFiles } from './helpers/gcov'
+import { argAsArray } from './helpers/util'
 
 /**
  *
@@ -158,18 +159,15 @@ export async function main(
 
   if (args.gcov) {
     UploadLogger.verbose('Running gcov...')
-    const gcovLogs = await generateGcovCoverageFiles(projectRoot)
+    const gcovInclude: string[] = argAsArray(args.gcovInclude)
+    const gcovIgnore: string[] = argAsArray(args.gcovIgnore)
+    const gcovArgs: string[] = argAsArray(args.gcovArgs)
+    const gcovLogs = await generateGcovCoverageFiles(projectRoot, gcovInclude, gcovIgnore, gcovArgs)
     UploadLogger.verbose(`${gcovLogs}`)
   }
   
   let coverageFilePaths: string[] = []
-  if (args.file) {
-    if (typeof args.file === 'string') {
-      requestedPaths = [args.file]
-    } else {
-      requestedPaths = args.file
-    }
-  }
+  requestedPaths = argAsArray(args.file)
 
   coverageFilePaths = requestedPaths
 
diff --git a/src/types.ts b/src/types.ts
index 84c6507..90799e5 100644
--- a/src/types.ts
+++ b/src/types.ts
@@ -27,9 +27,9 @@ export interface UploaderArgs {
   source?: string // Track wrappers of the uploader
   changelog?: string // Displays the changelog and exits
   gcov?: string // Run with gcov support
-  gcovIgnore?: string // Paths to ignore during gcov gathering
-  gcovInclude?: string // Paths to include during gcov gathering
-  gcovArgs?: string // Extra arguments to pass to gcov
+  gcovIgnore?:  string | string[] // Paths to ignore during gcov gathering
+  gcovInclude?:  string | string[] // Paths to include during gcov gathering
+  gcovArgs?:  string | string[] // Extra arguments to pass to gcov
 }
 
 export type UploaderEnvs = NodeJS.Dict<string>


Test patch:
diff --git a/test/helpers/gcov.test.ts b/test/helpers/gcov.test.ts
index b2c0c20..23ce823 100644
--- a/test/helpers/gcov.test.ts
+++ b/test/helpers/gcov.test.ts
@@ -34,7 +34,7 @@ describe('generateGcovCoverageFiles()', () => {
         })
 
         const projectRoot = process.cwd()
-        expect(await generateGcovCoverageFiles(projectRoot, [], [], 'NEWGCOVARG')).toEqual("Matched")
+        expect(await generateGcovCoverageFiles(projectRoot, [], [], ['NEWGCOVARG'])).toEqual("Matched")
     })
 
     it('should return an error when no gcno files found', async () => {
@@ -57,6 +57,6 @@ describe('generateGcovCoverageFiles()', () => {
         td.when(spawnSync('gcov')).thenReturn({ error: "Command 'gcov' not found" })
 
         const projectRoot = process.cwd()
-        await expect(generateGcovCoverageFiles(projectRoot, [], [], 'NEWGCOVARG')).rejects.toThrowError(/gcov is not installed/)
+        await expect(generateGcovCoverageFiles(projectRoot, [], [], ['NEWGCOVARG'])).rejects.toThrowError(/gcov is not installed/)
     })
 })