From 43dbfc037ce506fa602b868679829a497d8a415b Mon Sep 17 00:00:00 2001 From: joseph-sentry <136376984+joseph-sentry@users.noreply.github.com> Date: Mon, 11 Dec 2023 09:47:24 -0500 Subject: [PATCH] feat: add useCWD option to args (#1284) * feat: add useCWD option to args This option is being added to allow users to manually decide to use the current working directory instead of the git root when fetching the git root automatically. * chore: change useCWD to useCwd * test: change string to compare in useCwd fetchGitRoot test * test: fix token.test.ts fetchGitRoot call Signed-off-by: joseph-sentry --- HELPFILE | 2 ++ src/helpers/cli.ts | 7 +++++++ src/helpers/files.ts | 4 ++-- src/helpers/web.ts | 2 +- src/index.ts | 3 ++- src/types.ts | 7 ++++--- test/helpers/coveragepy.test.ts | 4 ++-- test/helpers/files.test.ts | 16 ++++++++++++++-- test/helpers/token.test.ts | 4 ++-- 9 files changed, 36 insertions(+), 13 deletions(-) diff --git a/HELPFILE b/HELPFILE index 1cb6ac1dd..ae09ff320 100644 --- a/HELPFILE +++ b/HELPFILE @@ -57,6 +57,8 @@ Options: through [string] [default: ""] -u, --url Change the upload host (Enterprise use) [string] [default: "https://codecov.io"] + --useCwd, --uc Use the current working directory instead of the + git root [boolean] [default: false] -v, --verbose Run with verbose logging [boolean] -X, --feature Toggle functionalities. Separate multiple ones by comma: -X network,search diff --git a/src/helpers/cli.ts b/src/helpers/cli.ts index db8419d4e..0d00aec14 100644 --- a/src/helpers/cli.ts +++ b/src/helpers/cli.ts @@ -190,6 +190,13 @@ const args: ICLIArgument[] = [ description: 'Change the upload host (Enterprise use)', default: 'https://codecov.io', }, + { + alias: 'uc', + name: 'useCwd', + type: 'boolean', + default: false, + description: 'Use the current working directory instead of the git root', + }, { alias: 'v', name: 'verbose', diff --git a/src/helpers/files.ts b/src/helpers/files.ts index 05ef7b928..f107bed8b 100644 --- a/src/helpers/files.ts +++ b/src/helpers/files.ts @@ -221,11 +221,11 @@ export async function getCoverageFiles( }) } -export function fetchGitRoot(): string { +export function fetchGitRoot(useCwd: boolean): string { const currentWorkingDirectory = process.cwd() try { const gitRoot = runExternalProgram('git', ['rev-parse', '--show-toplevel']) - return (gitRoot != "" ? gitRoot : currentWorkingDirectory) + return (gitRoot != "" && !useCwd ? gitRoot : currentWorkingDirectory) } catch (error) { info(`Error fetching git root. Defaulting to ${currentWorkingDirectory}. Please try using the -R flag. ${error}`) return currentWorkingDirectory diff --git a/src/helpers/web.ts b/src/helpers/web.ts index 9f88dd37a..0b241cfc1 100644 --- a/src/helpers/web.ts +++ b/src/helpers/web.ts @@ -19,7 +19,7 @@ import { sleep } from './util' const maxRetries = 4 const baseBackoffDelayMs = 1000 // Adjust this value based on your needs. -export const userAgent: string = `codecov-uploader/${version}` +export const userAgent = `codecov-uploader/${version}` /** * diff --git a/src/index.ts b/src/index.ts index 017cb4559..2547aa265 100644 --- a/src/index.ts +++ b/src/index.ts @@ -83,6 +83,7 @@ function dryRun( * @param {boolean} args.clean Move discovered coverage reports to the trash * @param {string} args.feature Toggle features * @param {string} args.source Track wrappers of the uploader + * @param {string} args.useCwd Use current working directory as the automatically detected project root */ export async function main( args: UploaderArgs, @@ -135,7 +136,7 @@ export async function main( // #endregion // #region == Step 2: detect if we are in a git repo - const projectRoot = args.rootDir || fetchGitRoot() + const projectRoot = args.rootDir || fetchGitRoot(args.useCwd || false) if (projectRoot === '') { info( '=> No git repo detected. Please use the -R flag if the below detected directory is not correct.', diff --git a/src/types.ts b/src/types.ts index 920292c50..18282eb2b 100644 --- a/src/types.ts +++ b/src/types.ts @@ -13,9 +13,9 @@ export interface UploaderArgs { flags: string | string[] // Flag the upload to group coverage metrics fullReport?: string // Specify the path to a previously uploaded Codecov report gcov?: string // Run with gcov support - gcovArgs?: string | 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 + gcovIgnore?: string | string[] // Paths to ignore during gcov gathering + gcovInclude?: string | string[] // Paths to include during gcov gathering gcovExecutable?: string // gcov executable to run. name?: string // Custom defined name of the upload. Visible in Codecov UI networkFilter?: string // Specify a prefix on the files listed in the network section of the Codecov report. Useful for upload-specific path fixing @@ -34,6 +34,7 @@ export interface UploaderArgs { token?: string // Codecov upload token upstream: string // Upstream proxy to connect to url?: string // Change the upload host (Enterprise use) + useCwd?: boolean verbose?: string // Run with verbose logging xcode?: string // Run with xcode support xcodeArchivePath?: string // Specify the xcode archive path. Likely specified as the -resultBundlePath and should end in .xcresult diff --git a/test/helpers/coveragepy.test.ts b/test/helpers/coveragepy.test.ts index 1e4050922..8f93c5e3d 100644 --- a/test/helpers/coveragepy.test.ts +++ b/test/helpers/coveragepy.test.ts @@ -12,7 +12,7 @@ describe('generateCoveragePyFile()', () => { it('should run when coveragepy is asked for', async () => { const fixturesCoveragePyDir = path.join( - fileHelpers.fetchGitRoot(), + fileHelpers.fetchGitRoot(false), 'test/fixtures/coveragepy', ) @@ -50,7 +50,7 @@ describe('generateCoveragePyFile()', () => { it('should return a log when there are no dotcoverage files', async () => { const fixturesYamlDir = path.join( - fileHelpers.fetchGitRoot(), + fileHelpers.fetchGitRoot(false), 'test/fixtures/yaml', ) diff --git a/test/helpers/files.test.ts b/test/helpers/files.test.ts index 15dfa430f..eab9d11b2 100644 --- a/test/helpers/files.test.ts +++ b/test/helpers/files.test.ts @@ -32,14 +32,26 @@ describe('File Helpers', () => { spawnSync('git', ['rev-parse', '--show-toplevel'], { maxBuffer: SPAWNPROCESSBUFFERSIZE }), ).thenReturn({ stdout: Buffer.from('gitRoot') }) - expect(fileHelpers.fetchGitRoot()).toBe('gitRoot') + expect(fileHelpers.fetchGitRoot(false)).toBe('gitRoot') }) it('returns cwd when it cannot fetch the git root', () => { const cwd = td.replace(process, 'cwd') td.replace(childProcess, 'spawnSync') td.when(cwd()).thenReturn('fish') - expect(fileHelpers.fetchGitRoot()).toEqual('fish') + expect(fileHelpers.fetchGitRoot(false)).toEqual('fish') + }) + + it('returns cwd when its input is true even if it can fetch the git root', () => { + const cwd = td.replace(process, 'cwd') + const spawnSync = td.replace(childProcess, 'spawnSync') + td.when(cwd()).thenReturn('CWD') + td.when( + spawnSync('git', ['rev-parse', '--show-toplevel'], { maxBuffer: SPAWNPROCESSBUFFERSIZE }), + ).thenReturn({ stdout: Buffer.from('gitRoot') }) + + expect(fileHelpers.fetchGitRoot(true)).toEqual('CWD') + }) it('can get a file listing', async () => { diff --git a/test/helpers/token.test.ts b/test/helpers/token.test.ts index 891869e24..2bfcde8bd 100644 --- a/test/helpers/token.test.ts +++ b/test/helpers/token.test.ts @@ -8,11 +8,11 @@ import { createEmptyArgs } from '../test_helpers' describe('Get tokens', () => { const fixturesDir = path.join( - fileHelpers.fetchGitRoot(), + fileHelpers.fetchGitRoot(false), 'test/fixtures/yaml', ) const invalidFixturesDir = path.join( - fileHelpers.fetchGitRoot(), + fileHelpers.fetchGitRoot(false), 'test/fixtures/invalid_yaml', )