Skip to content

Commit

Permalink
feat: add useCWD option to args (#1284)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
joseph-sentry authored Dec 11, 2023
1 parent ccbdc35 commit 43dbfc0
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 13 deletions.
2 changes: 2 additions & 0 deletions HELPFILE
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions src/helpers/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
4 changes: 2 additions & 2 deletions src/helpers/files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/helpers/web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`

/**
*
Expand Down
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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.',
Expand Down
7 changes: 4 additions & 3 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions test/helpers/coveragepy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
)

Expand Down Expand Up @@ -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',
)

Expand Down
16 changes: 14 additions & 2 deletions test/helpers/files.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down
4 changes: 2 additions & 2 deletions test/helpers/token.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
)

Expand Down

0 comments on commit 43dbfc0

Please sign in to comment.