Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions src/helpers/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,20 @@ export function validateURL(url: string): boolean {
return validator.isURL(url, { require_protocol: true })
}

export function validateFlags(flags: string): boolean {
export function isValidFlag(flag: string): boolean {
// eslint-disable-next-line no-useless-escape
const mask = /^[\w\.\-]{1,45}$/
Comment on lines 18 to 19
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// eslint-disable-next-line no-useless-escape
const mask = /^[\w\.\-]{1,45}$/

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mitchell-codecov Why are you deleting the mask?

return mask.test(flags)
return flag.length === 0 || mask.test(flag)
}

export function validateFlags(flags: string[]): void {
const invalidFlags = flags.filter(flag => isValidFlag(flag) !== true)

if (invalidFlags.length > 0) {
throw new Error(
`Flags must consist only of alphanumeric characters, '_', '-', or '.' and not exceed 45 characters. Received ${flags}`,
)
}
}

/**
Expand Down
11 changes: 3 additions & 8 deletions src/helpers/web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import {
UploaderInputs,
} from '../types'
import { info } from './logger'
import * as validateHelpers from './validate'

/**
*
Expand All @@ -32,15 +31,11 @@ export function populateBuildParams(
serviceParams.name = args.name || envs.CODECOV_NAME || ''
serviceParams.tag = args.tag || ''

let flags: string[]
if (typeof args.flags === 'object') {
flags = [...args.flags]
if (typeof args.flags === "string") {
serviceParams.flags = args.flags
} else {
flags = String(args.flags || '').split(',')
serviceParams.flags = args.flags.join(',')
}
serviceParams.flags = flags
.filter(flag => validateHelpers.validateFlags(flag))
.join(',')

serviceParams.parent = args.parent || ''
return serviceParams
Expand Down
10 changes: 10 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { generateGcovCoverageFiles } from './helpers/gcov'
import { generateXcodeCoverageFiles } from './helpers/xcode'
import { argAsArray } from './helpers/util'
import { checkSlug } from './helpers/checkSlug'
import { validateFlags } from './helpers/validate'

/**
*
Expand Down Expand Up @@ -119,6 +120,15 @@ export async function main(

info(generateHeader(getVersion()))

let flags: string[]
if (typeof args.flags === 'object') {
flags = [...args.flags]
} else {
flags = String(args.flags || '').split(',')
}

validateFlags(flags)

// #endregion
// #region == Step 2: detect if we are in a git repo
const projectRoot = args.rootDir || fetchGitRoot()
Expand Down
17 changes: 13 additions & 4 deletions test/helpers/validate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,27 @@ describe('Input Validators', () => {

describe('Flags', () => {
it('Should pass without a dash', () => {
expect(validate.validateFlags('moo')).toBe(true)
expect(validate.isValidFlag('moo')).toBe(true)
})
it('Should pass with a dash', () => {
expect(validate.validateFlags('moo-foor')).toBe(true)
expect(validate.isValidFlag('moo-foor')).toBe(true)
})

it('Should pass with a period in the middle', () => {
expect(validate.validateFlags('moo.foor')).toBe(true)
expect(validate.isValidFlag('moo.foor')).toBe(true)
})

it('Should pass with a dash at the start', () => {
expect(validate.validateFlags('-moo-foor')).toBe(true)
expect(validate.isValidFlag('-moo-foor')).toBe(true)
})

it("should throw when they they not match the pattern", () => {
// arrange
const invalidFlagName = "flag/subflag"
const expectedErrorMessage = "Flags must consist only of alphanumeric characters, '_', '-', or '.' and not exceed 45 characters. Received flag/subflag"

// act
expect(() => validate.validateFlags([invalidFlagName])).toThrowError(expectedErrorMessage)
})
})

Expand Down