Skip to content

Commit

Permalink
feat: Add ability to treat flagged words as errors (#1814)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason3S authored Jul 1, 2024
1 parent 80487bb commit 66d5354
Show file tree
Hide file tree
Showing 10 changed files with 382 additions and 20 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ jobs:
# Default: warning
inline: warning

# Reports flagged / forbidden words as errors.
# If true, errors will still be reported even if `inline` is "none"
treat_flagged_words_as_errors: false

# Generate Spelling suggestions.
suggestions: false

Expand Down
7 changes: 7 additions & 0 deletions action-src/src/ActionParams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ export interface ActionParams {
* @default 'warning'
*/
inline: InlineWorkflowCommand;
/**
* Determines if flagged words should be treated as errors.
* @default 'false'
*/
treat_flagged_words_as_errors: TrueFalse;
/**
* Determines if the action should be failed if any spelling issues are found.
*
Expand Down Expand Up @@ -65,6 +70,7 @@ const defaultActionParams: ActionParams = {
config: '',
root: '',
inline: 'warning',
treat_flagged_words_as_errors: 'false',
strict: 'true',
verbose: 'false',
check_dot_files: 'explicit',
Expand Down Expand Up @@ -121,6 +127,7 @@ export function validateActionParams(
validateConfig,
validateRoot,
validateOptions('inline', ['error', 'warning', 'none']),
validateTrueFalse('treat_flagged_words_as_errors'),
validateTrueFalse('strict'),
validateTrueFalse('incremental_files_only'),
validateTrueFalse('verbose'),
Expand Down
297 changes: 297 additions & 0 deletions action-src/src/__snapshots__/action.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -882,3 +882,300 @@ exports[`Validate Action > check files "'sampleCode/**'" incremental: true sugs:
"::error::4 spelling issues found in 1 of the 4 files checked.",
]
`;

exports[`Validate Action > check files flag errors "'sampleCode/**'" sugs: true 'pull_request_with_files.json', "'error'" 1`] = `
[
"fixtures/sampleCode/samples_with_errors/withErrors.ts:5:19 Unknown word (Functon) Suggestions: (functor, function, Function, futon, fulton)",
"fixtures/sampleCode/samples_with_errors/withErrors.ts:5:27 Unknown word (countt) Suggestions: (count, Count, counts, county, cont)",
"fixtures/sampleCode/samples_with_errors/withErrors.ts:9:15 Forbidden word (blacklist) Suggestions: (denylist*, backlist, blocklist, blockList, BlockList)",
"fixtures/sampleCode/samples_with_errors/withErrors.ts:9:53 Misspelled word (colours) Suggestions: (colors*, coolers, color, coors, clouds)",
]
`;

exports[`Validate Action > check files flag errors "'sampleCode/**'" sugs: true 'pull_request_with_files.json', "'error'" 2`] = `[]`;

exports[`Validate Action > check files flag errors "'sampleCode/**'" sugs: true 'pull_request_with_files.json', "'error'" 3`] = `
[
[
"Pull Request
",
],
[
"::error file=fixtures/sampleCode/samples_with_errors/withErrors.ts,line=5,col=19::Unknown word (Functon) Suggestions: (functor, function, Function, futon, fulton)
",
],
[
"::error file=fixtures/sampleCode/samples_with_errors/withErrors.ts,line=5,col=27::Unknown word (countt) Suggestions: (count, Count, counts, county, cont)
",
],
[
"::error file=fixtures/sampleCode/samples_with_errors/withErrors.ts,line=9,col=15::Forbidden word (blacklist) Suggestions: (denylist*, backlist, blocklist, blockList, BlockList)
",
],
[
"::error file=fixtures/sampleCode/samples_with_errors/withErrors.ts,line=9,col=53::Misspelled word (colours) Suggestions: (colors*, coolers, color, coors, clouds)
",
],
[
"Files checked: 4, Issues found: 4 in 1 files.
",
],
[
"
",
],
[
"::set-output name=success::false
",
],
[
"
",
],
[
"::set-output name=number_of_files_checked::4
",
],
[
"
",
],
[
"::set-output name=number_of_issues::4
",
],
[
"
",
],
[
"::set-output name=number_of_files_with_issues::1
",
],
[
"
",
],
[
"::set-output name=files_with_issues::["fixtures/sampleCode/samples_with_errors/withErrors.ts"]
",
],
[
"
",
],
[
"::set-output name=result::{"success":false,"number_of_issues":4,"number_of_files_checked":4,"files_with_issues":["fixtures/sampleCode/samples_with_errors/withErrors.ts"]}
",
],
]
`;

exports[`Validate Action > check files flag errors "'sampleCode/**'" sugs: true 'pull_request_with_files.json', "'error'" 4`] = `
[
"Pull Request",
"::error file=fixtures/sampleCode/samples_with_errors/withErrors.ts,line=5,col=19::Unknown word (Functon) Suggestions: (functor, function, Function, futon, fulton)",
"::error file=fixtures/sampleCode/samples_with_errors/withErrors.ts,line=5,col=27::Unknown word (countt) Suggestions: (count, Count, counts, county, cont)",
"::error file=fixtures/sampleCode/samples_with_errors/withErrors.ts,line=9,col=15::Forbidden word (blacklist) Suggestions: (denylist*, backlist, blocklist, blockList, BlockList)",
"::error file=fixtures/sampleCode/samples_with_errors/withErrors.ts,line=9,col=53::Misspelled word (colours) Suggestions: (colors*, coolers, color, coors, clouds)",
"Files checked: 4, Issues found: 4 in 1 files.",
"::set-output name=success::false",
"::set-output name=number_of_files_checked::4",
"::set-output name=number_of_issues::4",
"::set-output name=number_of_files_with_issues::1",
"::set-output name=files_with_issues::["fixtures/sampleCode/samples_with_errors/withErrors.ts"]",
"::set-output name=result::{"success":false,"number_of_issues":4,"number_of_files_checked":4,"files_with_issues":["fixtures/sampleCode/samples_with_errors/withErrors.ts"]}",
]
`;

exports[`Validate Action > check files flag errors "'sampleCode/**'" sugs: true 'pull_request_with_files.json', "'none'" 1`] = `
[
"fixtures/sampleCode/samples_with_errors/withErrors.ts:9:15 Forbidden word (blacklist) Suggestions: (denylist*, backlist, blocklist, blockList, BlockList)",
]
`;

exports[`Validate Action > check files flag errors "'sampleCode/**'" sugs: true 'pull_request_with_files.json', "'none'" 2`] = `[]`;

exports[`Validate Action > check files flag errors "'sampleCode/**'" sugs: true 'pull_request_with_files.json', "'none'" 3`] = `
[
[
"Pull Request
",
],
[
"::error file=fixtures/sampleCode/samples_with_errors/withErrors.ts,line=9,col=15::Forbidden word (blacklist) Suggestions: (denylist*, backlist, blocklist, blockList, BlockList)
",
],
[
"Files checked: 4, Issues found: 4 in 1 files.
",
],
[
"
",
],
[
"::set-output name=success::false
",
],
[
"
",
],
[
"::set-output name=number_of_files_checked::4
",
],
[
"
",
],
[
"::set-output name=number_of_issues::4
",
],
[
"
",
],
[
"::set-output name=number_of_files_with_issues::1
",
],
[
"
",
],
[
"::set-output name=files_with_issues::["fixtures/sampleCode/samples_with_errors/withErrors.ts"]
",
],
[
"
",
],
[
"::set-output name=result::{"success":false,"number_of_issues":4,"number_of_files_checked":4,"files_with_issues":["fixtures/sampleCode/samples_with_errors/withErrors.ts"]}
",
],
]
`;

exports[`Validate Action > check files flag errors "'sampleCode/**'" sugs: true 'pull_request_with_files.json', "'none'" 4`] = `
[
"Pull Request",
"::error file=fixtures/sampleCode/samples_with_errors/withErrors.ts,line=9,col=15::Forbidden word (blacklist) Suggestions: (denylist*, backlist, blocklist, blockList, BlockList)",
"Files checked: 4, Issues found: 4 in 1 files.",
"::set-output name=success::false",
"::set-output name=number_of_files_checked::4",
"::set-output name=number_of_issues::4",
"::set-output name=number_of_files_with_issues::1",
"::set-output name=files_with_issues::["fixtures/sampleCode/samples_with_errors/withErrors.ts"]",
"::set-output name=result::{"success":false,"number_of_issues":4,"number_of_files_checked":4,"files_with_issues":["fixtures/sampleCode/samples_with_errors/withErrors.ts"]}",
]
`;

exports[`Validate Action > check files flag errors "'sampleCode/**'" sugs: true 'pull_request_with_files.json', "'warning'" 1`] = `
[
"fixtures/sampleCode/samples_with_errors/withErrors.ts:5:19 Unknown word (Functon) Suggestions: (functor, function, Function, futon, fulton)",
"fixtures/sampleCode/samples_with_errors/withErrors.ts:5:27 Unknown word (countt) Suggestions: (count, Count, counts, county, cont)",
"fixtures/sampleCode/samples_with_errors/withErrors.ts:9:15 Forbidden word (blacklist) Suggestions: (denylist*, backlist, blocklist, blockList, BlockList)",
"fixtures/sampleCode/samples_with_errors/withErrors.ts:9:53 Misspelled word (colours) Suggestions: (colors*, coolers, color, coors, clouds)",
]
`;

exports[`Validate Action > check files flag errors "'sampleCode/**'" sugs: true 'pull_request_with_files.json', "'warning'" 2`] = `[]`;

exports[`Validate Action > check files flag errors "'sampleCode/**'" sugs: true 'pull_request_with_files.json', "'warning'" 3`] = `
[
[
"Pull Request
",
],
[
"::warning file=fixtures/sampleCode/samples_with_errors/withErrors.ts,line=5,col=19::Unknown word (Functon) Suggestions: (functor, function, Function, futon, fulton)
",
],
[
"::warning file=fixtures/sampleCode/samples_with_errors/withErrors.ts,line=5,col=27::Unknown word (countt) Suggestions: (count, Count, counts, county, cont)
",
],
[
"::error file=fixtures/sampleCode/samples_with_errors/withErrors.ts,line=9,col=15::Forbidden word (blacklist) Suggestions: (denylist*, backlist, blocklist, blockList, BlockList)
",
],
[
"::warning file=fixtures/sampleCode/samples_with_errors/withErrors.ts,line=9,col=53::Misspelled word (colours) Suggestions: (colors*, coolers, color, coors, clouds)
",
],
[
"Files checked: 4, Issues found: 4 in 1 files.
",
],
[
"
",
],
[
"::set-output name=success::false
",
],
[
"
",
],
[
"::set-output name=number_of_files_checked::4
",
],
[
"
",
],
[
"::set-output name=number_of_issues::4
",
],
[
"
",
],
[
"::set-output name=number_of_files_with_issues::1
",
],
[
"
",
],
[
"::set-output name=files_with_issues::["fixtures/sampleCode/samples_with_errors/withErrors.ts"]
",
],
[
"
",
],
[
"::set-output name=result::{"success":false,"number_of_issues":4,"number_of_files_checked":4,"files_with_issues":["fixtures/sampleCode/samples_with_errors/withErrors.ts"]}
",
],
]
`;

exports[`Validate Action > check files flag errors "'sampleCode/**'" sugs: true 'pull_request_with_files.json', "'warning'" 4`] = `
[
"Pull Request",
"::warning file=fixtures/sampleCode/samples_with_errors/withErrors.ts,line=5,col=19::Unknown word (Functon) Suggestions: (functor, function, Function, futon, fulton)",
"::warning file=fixtures/sampleCode/samples_with_errors/withErrors.ts,line=5,col=27::Unknown word (countt) Suggestions: (count, Count, counts, county, cont)",
"::error file=fixtures/sampleCode/samples_with_errors/withErrors.ts,line=9,col=15::Forbidden word (blacklist) Suggestions: (denylist*, backlist, blocklist, blockList, BlockList)",
"::warning file=fixtures/sampleCode/samples_with_errors/withErrors.ts,line=9,col=53::Misspelled word (colours) Suggestions: (colors*, coolers, color, coors, clouds)",
"Files checked: 4, Issues found: 4 in 1 files.",
"::set-output name=success::false",
"::set-output name=number_of_files_checked::4",
"::set-output name=number_of_issues::4",
"::set-output name=number_of_files_with_issues::1",
"::set-output name=files_with_issues::["fixtures/sampleCode/samples_with_errors/withErrors.ts"]",
"::set-output name=result::{"success":false,"number_of_issues":4,"number_of_files_checked":4,"files_with_issues":["fixtures/sampleCode/samples_with_errors/withErrors.ts"]}",
]
`;
28 changes: 28 additions & 0 deletions action-src/src/action.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,34 @@ describe('Validate Action', () => {
expect(spyStdout.mock.calls.map((call) => call.join('').trim()).filter((a) => !!a)).toMatchSnapshot();
},
);

test.each`
files | suggestions | inline | contextFile | expected
${'sampleCode/**'} | ${true} | ${'warning'} | ${'pull_request_with_files.json'} | ${false}
${'sampleCode/**'} | ${true} | ${'error'} | ${'pull_request_with_files.json'} | ${false}
${'sampleCode/**'} | ${true} | ${'none'} | ${'pull_request_with_files.json'} | ${false}
`(
'check files flag errors "$files" sugs: $suggestions $contextFile, "$inline"',
async ({ files, suggestions, contextFile, inline, expected }) => {
const warnings: string[] = [];
spyWarn.mockImplementation((msg: string) => warnings.push(msg));
const params = {
INPUT_FILES: files,
INPUT_INCREMENTAL_FILES_ONLY: 'false',
INPUT_INLINE: inline,
INPUT_ROOT: path.resolve(sourceDir, 'fixtures'),
INPUT_CONFIG: path.resolve(sourceDir, 'fixtures/cspell.json'),
INPUT_SUGGESTIONS: suggestions ? 'true' : 'false',
INPUT_TREAT_FLAGGED_WORDS_AS_ERRORS: 'true',
};
const context = createContextFromFile(contextFile, params);
await expect(action(context)).resolves.toBe(expected);
expect(warnings).toMatchSnapshot();
expect(spyLog.mock.calls).toMatchSnapshot();
expect(spyStdout.mock.calls).toMatchSnapshot();
expect(spyStdout.mock.calls.map((call) => call.join('').trim()).filter((a) => !!a)).toMatchSnapshot();
},
);
});

function cleanEnv() {
Expand Down
1 change: 1 addition & 0 deletions action-src/src/checkSpelling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ async function checkSpelling(

const reporterOptions = {
verbose: params.verbose === 'true',
treatFlaggedWordsAsErrors: params.treat_flagged_words_as_errors === 'true',
};

const collector = new CSpellReporterForGithubAction(params.inline, reporterOptions, core);
Expand Down
Loading

0 comments on commit 66d5354

Please sign in to comment.