Repository: codecov/uploader
Pull request #1013 (2023-04-06 20:12:28)

Update fetchGitRoot to not halt execution
### What? 

This PR updates the `fetchGitRoot` function in `files.ts` to not error when attempting to `runExternalProgram`. Explicitly, this change involves: 

1. Updating the return statement to return the `currentWorkingDirectory` if `gitRoot == ""` 
2. Updating the `catch (error)` block to log the error, and then returning the `currentWorkingDirectory` instead of `throw new Error`. 
3. Updating the associated test for `fetchGitRoot()` inside of `files.tests.ts` 

thomasrockhu-codecov: @jimmy-lee I have a suspicion why the tests are failing, but I won't be able to get to this until later today or tomorrow morning. I'll merge this in and deploy a new version tomorrow afternoon
jimmy-lee: > @jimmy-lee I have a suspicion why the tests are failing, but I won't be able to get to this until later today or tomorrow morning. I'll merge this in and deploy a new version of the uploader tomorrow afternoon

Thank you! 
drazisil-codecov: Re-running tests.
thomasrockhu-codecov: @drazisil-codecov I think this is just access to CODECOV_TOKEN
drazisil-codecov: Enabling secret sharing with the fork in order to pass the CI. Will disable again after.
codecov[bot]: ## [Codecov](https://app.codecov.io/gh/codecov/uploader/compare/1013?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=codecov) Report
> Merging [#1013](https://app.codecov.io/gh/codecov/uploader/compare/1013?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=codecov) (bfea48d) into [main](https://codecov.io/gh/codecov/uploader/commit/977093be56039701d667d36f388b2852c58e401a?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=codecov) (977093b) will **increase** coverage by `0.01%`.
> The diff coverage is `83.33%`.

```diff
@@            Coverage Diff             @@
##             main    #1013      +/-   ##
==========================================
+ Coverage   92.32%   92.34%   +0.01%     
==========================================
  Files          35       35              
  Lines        1316     1319       +3     
  Branches      270      270              
==========================================
+ Hits         1215     1218       +3     
  Misses         69       69              
  Partials       32       32              
```

| Flag | Coverage Δ | |
|---|---|---|
| alpine | `92.26% <83.33%> (+0.01%)` | :arrow_up: |
| alpine-proxy | `92.26% <83.33%> (+0.01%)` | :arrow_up: |
| alpine-without-git | `92.26% <83.33%> (+0.01%)` | :arrow_up: |
| linux | `92.26% <83.33%> (+0.01%)` | :arrow_up: |
| linux-without-git | `92.26% <83.33%> (+0.01%)` | :arrow_up: |
| macos | `92.26% <83.33%> (+0.01%)` | :arrow_up: |
| macos-without-git | `92.26% <83.33%> (+0.01%)` | :arrow_up: |
| windows | `91.66% <83.33%> (+0.01%)` | :arrow_up: |
| windows-without-git | `91.66% <83.33%> (+0.01%)` | :arrow_up: |

Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=codecov#carryforward-flags-in-the-pull-request-comment) to find out more.

| [Impacted Files](https://app.codecov.io/gh/codecov/uploader/compare/1013?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=codecov) | Coverage Δ | |
|---|---|---|
| [src/helpers/files.ts](https://app.codecov.io/gh/codecov/uploader/compare/1013?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=codecov#diff-c3JjL2hlbHBlcnMvZmlsZXMudHM=) | `92.59% <83.33%> (+0.28%)` | :arrow_up: |


Patch:
diff --git a/README.md b/README.md
index 929be10..7e14b87 100644
--- a/README.md
+++ b/README.md
@@ -1,4 +1,4 @@
-# Codecov Uploader
+# Codecov Uploader 
 
 [![CircleCI](https://circleci.com/gh/codecov/uploader.svg?style=shield&circle-token=def755bf76a1d8c36436c3115530c7eac7fa30e0)](https://circleci.com/gh/codecov/uploader) [![codecov](https://codecov.io/gh/codecov/uploader/branch/master/graph/badge.svg?token=X1gImxfIya)](https://codecov.io/gh/codecov/uploader)
 [![FOSSA Status](https://app.fossa.com/api/projects/git%2Bgithub.com%2Fcodecov%2Fuploader.svg?type=shield)](https://app.fossa.com/projects/git%2Bgithub.com%2Fcodecov%2Fuploader?ref=badge_shield) [![Total alerts](https://img.shields.io/lgtm/alerts/g/codecov/uploader.svg?logo=lgtm&logoWidth=18)](https://lgtm.com/projects/g/codecov/uploader/alerts/)
diff --git a/src/helpers/files.ts b/src/helpers/files.ts
index bbb9f28..67f8b76 100644
--- a/src/helpers/files.ts
+++ b/src/helpers/files.ts
@@ -4,7 +4,7 @@ import fs from 'fs'
 import { readFile } from 'fs/promises'
 import { posix as path } from 'path'
 import { UploaderArgs } from '../types'
-import { logError, UploadLogger } from './logger'
+import { info, logError, UploadLogger } from './logger'
 import { runExternalProgram } from './util'
 import micromatch from "../vendor/micromatch/index.js";
 import { SPAWNPROCESSBUFFERSIZE } from './constants'
@@ -220,11 +220,13 @@ export async function getCoverageFiles(
 }
 
 export function fetchGitRoot(): string {
+  const currentWorkingDirectory = process.cwd() 
   try {
-    return (
-      runExternalProgram('git', ['rev-parse', '--show-toplevel'])) || process.cwd()
+    const gitRoot = runExternalProgram('git', ['rev-parse', '--show-toplevel'])
+    return (gitRoot != "" ? gitRoot : currentWorkingDirectory)
   } catch (error) {
-    throw new Error(`Error fetching git root. Please try using the -R flag. ${error}`)
+    info(`Error fetching git root. Defaulting to ${currentWorkingDirectory}. Please try using the -R flag. ${error}`)
+    return currentWorkingDirectory
   }
 }
 


Test patch:
diff --git a/test/helpers/files.test.ts b/test/helpers/files.test.ts
index c925701..424ec4b 100644
--- a/test/helpers/files.test.ts
+++ b/test/helpers/files.test.ts
@@ -35,13 +35,11 @@ describe('File Helpers', () => {
     expect(fileHelpers.fetchGitRoot()).toBe('gitRoot')
   })
 
-  it('errors when it cannot fetch the git root', () => {
+  it('returns cwd when it cannot fetch the git root', () => {
     const cwd = td.replace(process, 'cwd')
     td.replace(childProcess, 'spawnSync')
     td.when(cwd()).thenReturn({ stdout: 'fish' })
-    expect(() => {
-      fileHelpers.fetchGitRoot()
-    }).toThrow()
+    expect(fileHelpers.fetchGitRoot()).toEqual({stdout: 'fish'})
   })
 
   it('can get a file listing', async () => {