Skip to content

Commit

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

# Reports flagged / forbidden words as errors.
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
105 changes: 105 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,108 @@ 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/**'" incremental: false sugs: true 'pull_request_with_files.json', dot: "'explicit'" 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/**'" incremental: false sugs: true 'pull_request_with_files.json', dot: "'explicit'" 2`] = `[]`;

exports[`Validate Action > check files flag errors "'sampleCode/**'" incremental: false sugs: true 'pull_request_with_files.json', dot: "'explicit'" 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/**'" incremental: false sugs: true 'pull_request_with_files.json', dot: "'explicit'" 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"]}",
]
`;
26 changes: 26 additions & 0 deletions action-src/src/action.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,32 @@ describe('Validate Action', () => {
expect(spyStdout.mock.calls.map((call) => call.join('').trim()).filter((a) => !!a)).toMatchSnapshot();
},
);

test.each`
files | incremental | suggestions | dot | contextFile | expected
${'sampleCode/**'} | ${false} | ${true} | ${'explicit'} | ${'pull_request_with_files.json'} | ${false}
`(
'check files flag errors "$files" incremental: $incremental sugs: $suggestions $contextFile, dot: "$dot"',
async ({ files, incremental, suggestions, contextFile, dot, expected }) => {
const warnings: string[] = [];
spyWarn.mockImplementation((msg: string) => warnings.push(msg));
const params = {
INPUT_FILES: files,
INPUT_INCREMENTAL_FILES_ONLY: incremental ? 'true' : 'false',
INPUT_CHECK_DOT_FILES: dot,
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
6 changes: 4 additions & 2 deletions action-src/src/getActionParams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,21 @@ import { getInput } from '@actions/core';
import { ActionParamsInput, applyDefaults, TrueFalse } from './ActionParams.js';

export function getActionParams(): ActionParamsInput {
return applyDefaults({
const params: ActionParamsInput = {
// github_token: getInput('github_token', { required: true }),
files: getInput('files'),
incremental_files_only: tf(getInput('incremental_files_only')),
config: getInput('config'),
root: getInput('root'),
inline: getInput('inline').toLowerCase(),
treat_flagged_words_as_errors: tf(getInput('treat_flagged_words_as_errors')),
strict: tf(getInput('strict')),
verbose: tf(getInput('verbose')),
check_dot_files: tf(getInput('check_dot_files')),
use_cspell_files: tf(getInput('use_cspell_files')),
suggestions: tf(getInput('suggestions')),
});
};
return applyDefaults(params);
}

function tf(v: string | boolean | number): TrueFalse | string {
Expand Down
16 changes: 10 additions & 6 deletions action-src/src/reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export type ReportIssueCommand = 'error' | 'warning' | 'none';

export interface ReporterOptions {
verbose: boolean;
treatFlaggedWordsAsErrors: boolean;
}

export class CSpellReporterForGithubAction {
Expand Down Expand Up @@ -106,23 +107,26 @@ ${error.stack}
Object.assign(this.result, result);
this.finished = true;
const command = this.reportIssueCommand;

if (!['error', 'warning'].includes(command)) {
return;
}
const errorCommand = this.options.treatFlaggedWordsAsErrors ? 'error' : command;

const cwd = process.cwd();

this.issues.forEach((item) => {
const isError = item.isFlagged || false;
const hasPreferred = item.suggestionsEx?.some((s) => s.isPreferred) || false;
const msgPrefix = item.isFlagged ? 'Forbidden word' : hasPreferred ? 'Misspelled word' : 'Unknown word';
const msgPrefix = isError ? 'Forbidden word' : hasPreferred ? 'Misspelled word' : 'Unknown word';
const suggestions = item.suggestionsEx?.map((s) => s.word + (s.isPreferred ? '*' : '')).join(', ') || '';
const sugMsg = suggestions ? ` Suggestions: (${suggestions})` : '';
const message = `${msgPrefix} (${item.text})${sugMsg}`;
const cmd = isError ? errorCommand : command;

if (!['error', 'warning'].includes(cmd)) {
return;

Check warning on line 124 in action-src/src/reporter.ts

View check run for this annotation

Codecov / codecov/patch

action-src/src/reporter.ts#L124

Added line #L124 was not covered by tests
}

// format: ::warning file={name},line={line},col={col}::{message}
issueCommand(
command,
cmd,
{
file: relative(cwd, item.uri || ''),
line: item.row,
Expand Down
5 changes: 5 additions & 0 deletions action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ inputs:
Allowed values are: warning, error, none
default: warning
required: false
treat_flagged_words_as_errors:
description: >
Treat flagged / forbidden words as errors.
Allowed values are: true, false
default: "false"
strict:
description: |
Determines if the action should be failed if any spelling issues are found.
Expand Down

0 comments on commit 3dd9a11

Please sign in to comment.