Skip to content

fix: throw error when flags do not match pattern#831

Merged
mitchell-codecov merged 7 commits intomainfrom
drazisil-codecov/issue829
Aug 1, 2022
Merged

fix: throw error when flags do not match pattern#831
mitchell-codecov merged 7 commits intomainfrom
drazisil-codecov/issue829

Conversation

@drazisil-codecov
Copy link
Copy Markdown
Contributor

@drazisil-codecov drazisil-codecov commented Jul 25, 2022

Prior behavior was to strip out invalid flag names. The validation function will now throw.

Fixes #829

@drazisil-codecov drazisil-codecov requested a review from a team as a code owner July 25, 2022 13:46
@drazisil-codecov drazisil-codecov self-assigned this Jul 25, 2022
@codecov
Copy link
Copy Markdown

codecov bot commented Jul 25, 2022

Codecov Report

Merging #831 (59d606e) into main (fecf6b4) will decrease coverage by 0.12%.
The diff coverage is 85.71%.

@@            Coverage Diff             @@
##             main     #831      +/-   ##
==========================================
- Coverage   92.07%   91.94%   -0.13%     
==========================================
  Files          34       34              
  Lines        1173     1179       +6     
  Branches      240      243       +3     
==========================================
+ Hits         1080     1084       +4     
- Misses         63       64       +1     
- Partials       30       31       +1     
Flag Coverage Δ
alpine 91.94% <85.71%> (-0.13%) ⬇️
alpine-proxy 91.94% <85.71%> (-0.13%) ⬇️
alpine-without-git 91.94% <85.71%> (-0.13%) ⬇️
linux 91.94% <85.71%> (-0.13%) ⬇️
linux-without-git 91.94% <85.71%> (-0.13%) ⬇️
macos 91.94% <85.71%> (-0.13%) ⬇️
macos-without-git 91.94% <85.71%> (-0.13%) ⬇️
windows 91.94% <85.71%> (-0.13%) ⬇️
windows-without-git 91.94% <85.71%> (-0.13%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
src/index.ts 74.67% <60.00%> (-0.50%) ⬇️
src/helpers/validate.ts 100.00% <100.00%> (ø)
src/helpers/web.ts 80.00% <100.00%> (-0.96%) ⬇️

}

export function validateFlags(flags: string): boolean {
export function validateFlags(flag: string): boolean {
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
export function validateFlags(flag: string): boolean {
export function validateFlags(flags: string[]): void {

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.

This is being passed a single flag as part of a filter command, I believe, @mitchell-codecov

Comment on lines 18 to 19
// eslint-disable-next-line no-useless-escape
const mask = /^[\w\.\-]{1,45}$/
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?

// eslint-disable-next-line no-useless-escape
const mask = /^[\w\.\-]{1,45}$/
return mask.test(flags)
if (flag.length !== 0 && mask.test(flag) !== true) {
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
if (flag.length !== 0 && mask.test(flag) !== true) {
const invalidFlags = flags.filter(isValidFlag)
if (invalidFlags.length > 0) {

return mask.test(flags)
if (flag.length !== 0 && mask.test(flag) !== true) {
throw new Error(
`Flags must consist only of alphanumeric characters, '_', '-', or '.' and not exceed 45 characters. Received ${flag}`,
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
`Flags must consist only of alphanumeric characters, '_', '-', or '.' and not exceed 45 characters. Received ${flag}`,
`Flags must consist only of alphanumeric characters, '_', '-', or '.' and not exceed 45 characters. Received ${flags}`,

`Flags must consist only of alphanumeric characters, '_', '-', or '.' and not exceed 45 characters. Received ${flag}`,
)
}
return true
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
return true

@mitchell-codecov
Copy link
Copy Markdown
Contributor

mitchell-codecov commented Jul 26, 2022

With the above suggestions, I additionally recommend a new function for the validation:

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

function isValidFlag(flag: string): boolean {
  return flag.length > 0 && mask.test(flag)
}

* feat: validate `flags` earlier

* chore: create `isValidFlag`

* refactor: `validateFlags`

* refactor: use `validateFlags`

* refactor: `isValidFlag`

* fix: expose `isValidFlag`

* chore: remove unused import

* fix: filter on falsy values

* fix: allow empty strings

* fix: join flags

* fix: validate.test.ts
@mitchell-codecov mitchell-codecov merged commit cf2585c into main Aug 1, 2022
@mitchell-codecov mitchell-codecov deleted the drazisil-codecov/issue829 branch August 1, 2022 15:37
This was referenced Aug 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Flag silently ignored when contains invalid characters

2 participants