Repository: codecov/uploader
Pull request #535 (2021-12-06 21:35:50)

chore: swap spawnSync calls for helper function runExternalProgram
Note: the specified encoding should be redundant since we call toString on the buffer response in `runExternalProgram`

https://github.com/codecov/uploader/blob/722c8191c20cda5fba026f889069ed40ef8da4e6/src/helpers/util.ts#L15


Patch:
diff --git a/src/ci_providers/provider_githubactions.ts b/src/ci_providers/provider_githubactions.ts
index 0a20130..488d4ed 100644
--- a/src/ci_providers/provider_githubactions.ts
+++ b/src/ci_providers/provider_githubactions.ts
@@ -3,7 +3,7 @@
  */
 import { IServiceParams, UploaderEnvs, UploaderInputs } from '../types'
 
-import childProcess from 'child_process'
+import { runExternalProgram } from "../helpers/util"
 import { info } from '../helpers/logger'
 
 export function detect(envs: UploaderEnvs): boolean {
@@ -71,10 +71,7 @@ function _getSHA(inputs: UploaderInputs): string {
   let commit = envs.GITHUB_SHA
   if (pr && pr !== '' && !args.sha) {
     const mergeCommitRegex = /^[a-z0-9]{40} [a-z0-9]{40}$/
-    const mergeCommitMessage = childProcess
-      .spawnSync('git', ['show', '--no-patch', '--format="%P"'])
-      .stdout.toString()
-      .trimRight()
+    const mergeCommitMessage = runExternalProgram('git', ['show', '--no-patch', '--format="%P"'])
     if (mergeCommitRegex.exec(mergeCommitMessage)) {
       const mergeCommit = mergeCommitMessage.split(' ')[1]
       info(`    Fixing merge commit SHA ${commit} -> ${mergeCommit}`)
diff --git a/src/helpers/files.ts b/src/helpers/files.ts
index 82c63e4..4f21b14 100644
--- a/src/helpers/files.ts
+++ b/src/helpers/files.ts
@@ -1,10 +1,11 @@
-import childProcess, { spawnSync } from 'child_process'
+import { spawnSync } from 'child_process'
 import glob from 'fast-glob'
 import fs from 'fs'
 import { readFile } from 'fs/promises'
 import { posix as path } from 'path'
 import { UploaderArgs } from '../types'
 import { logError, UploadLogger, verbose } from './logger'
+import { runExternalProgram } from './util'
 import micromatch from "micromatch";
 
 export const MARKER_NETWORK_END = '\n<<<<<< network\n'
@@ -213,10 +214,7 @@ export async function getCoverageFiles(
 export function fetchGitRoot(): string {
   try {
     return (
-      childProcess.spawnSync('git', ['rev-parse', '--show-toplevel'], {
-        encoding: 'utf-8',
-      }).stdout || process.cwd()
-    ).trimRight()
+      runExternalProgram('git', ['rev-parse', '--show-toplevel'])) || process.cwd()
   } catch (error) {
     throw new Error('Error fetching git root. Please try using the -R flag.')
   }
diff --git a/src/helpers/git.ts b/src/helpers/git.ts
index af17711..9c58164 100644
--- a/src/helpers/git.ts
+++ b/src/helpers/git.ts
@@ -1,4 +1,4 @@
-import childProcess from 'child_process'
+import { runExternalProgram } from './util'
 
 export function parseSlug(slug: string): string {
   // origin    https://github.com/torvalds/linux.git (fetch)
@@ -25,11 +25,8 @@ export function parseSlugFromRemoteAddr(remoteAddr?: string): string {
   let slug = ''
   if (!remoteAddr) {
     remoteAddr = (
-      childProcess.spawnSync('git', ['config', '--get', 'remote.origin.url'])
-        .stdout || ''
+      runExternalProgram('git', ['config', '--get', 'remote.origin.url']) || ''
     )
-      .toString()
-      .trimRight()
   }
   if (remoteAddr) {
     slug = parseSlug(remoteAddr)


Test patch:
diff --git a/test/helpers/files.test.ts b/test/helpers/files.test.ts
index 1511538..9daedfb 100644
--- a/test/helpers/files.test.ts
+++ b/test/helpers/files.test.ts
@@ -27,7 +27,7 @@ describe('File Helpers', () => {
     const spawnSync = td.replace(childProcess, 'spawnSync')
     td.when(cwd()).thenReturn({ stdout: 'fish' })
     td.when(
-      spawnSync('git', ['rev-parse', '--show-toplevel'], { encoding: 'utf-8' }),
+      spawnSync('git', ['rev-parse', '--show-toplevel']),
     ).thenReturn({ stdout: 'gitRoot' })
 
     expect(fileHelpers.fetchGitRoot()).toBe('gitRoot')