Skip to content

Commit

Permalink
Add option use_cspell_files
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason3S committed Mar 3, 2024
1 parent b0ceef5 commit ae1a0ea
Show file tree
Hide file tree
Showing 16 changed files with 218 additions and 220 deletions.
8 changes: 3 additions & 5 deletions README.md
Expand Up @@ -32,11 +32,6 @@ jobs:
# files: |
# **/*.{ts,js}
# !dist/**/*.{ts,js}
# # Hidden directories need an explicit .* to be included
# .*/**/*.yml
#
# To not check hidden files, use:
# files: "**"
#
# Default: ALL files
files: ''
Expand Down Expand Up @@ -70,6 +65,9 @@ jobs:
# Log progress and other information during the action execution.
# Default: false
verbose: false

# Use the `files` setting found in the CSpell configuration instead of `input.files`.
use_cspell_files: false
```

## Yarn 2 - PlugNPlay
Expand Down
2 changes: 1 addition & 1 deletion action-src/fixtures/pull_request_payload.json
Expand Up @@ -285,7 +285,7 @@
"watchers": 1,
"watchers_count": 1
},
"sha": "245401caa9cb567e577bd4af251789e7e87c726d",
"sha": "779c8bde2ff3f09f0c09633ca17a9dbfb5b07528",
"user": {
"avatar_url": "https://avatars0.githubusercontent.com/u/50543896?v=4",
"events_url": "https://api.github.com/users/streetsidesoftware/events{/privacy}",
Expand Down
13 changes: 3 additions & 10 deletions action-src/src/ActionParams.test.ts
Expand Up @@ -15,22 +15,15 @@ describe('ActionParams', () => {
${{ incremental_files_only: 'sure' }} | ${'Invalid incremental_files_only setting, must be one of (true, false)'}
${{ config: 'config_not_found' }} | ${'Configuration file "config_not_found" not found.'}
${{ root: 'root_not_found' }} | ${'Root path does not exist: "root_not_found"'}
${{ inline: 'swizzle' }} | ${'Invalid inline level (swizzle), must be one of (error, warning, none)'}
${{ inline: 'swizzle' }} | ${'Invalid inline setting, must be one of (error, warning, none)'}
${{ strict: 'sure' }} | ${'Invalid strict setting, must be one of (true, false)'}
${{ use_cspell_files: 'sure' }} | ${'Invalid use_cspell_files setting, must be one of (true, false)'}
${{ check_dot_files: 'sure' }} | ${'Invalid check_dot_files setting, must be one of (true, false, explicit)'}
`('validateActionParams Errors $params', ({ params, expected }) => {
const logger = vi.fn();
expect(() => validateActionParams(ap(params), logger)).toThrow();
expect(logger).toHaveBeenCalledWith(expected);
});

test.each`
params
${{ github_token: 'token' }}
`('validateActionParams $params', ({ params }) => {
const logger = vi.fn();
expect(() => validateActionParams(ap(params), logger)).not.toThrow();
expect(logger).not.toHaveBeenCalled();
});
});

function ap(p: Partial<ActionParamsInput>): ActionParamsInput {
Expand Down
54 changes: 25 additions & 29 deletions action-src/src/ActionParams.ts
Expand Up @@ -11,16 +11,22 @@ export type TrueFalse = 'true' | 'false';
export interface ActionParamsInput extends Record<keyof ActionParams, string> {}

export interface ActionParams {
/**
* Files or glob patterns to check.
*/
files: string;
incremental_files_only: TrueFalse;
config: string;
root: string;
/**
* @default 'warning'
*/
inline: InlineWorkflowCommand;
/**
* Determines if the action should be failed if any spelling issues are found.
*
* Allowed values are: true, false
* @default 'warning'
* @default 'false'
*/
strict: TrueFalse;
/**
Expand All @@ -38,6 +44,12 @@ export interface ActionParams {
* @default 'explicit'
*/
check_dot_files: TrueFalse | 'explicit';

/**
* Use the `files` setting in the CSpell configuration to determine the files to check.
* @default 'false'
*/
use_cspell_files: TrueFalse;
}

const defaultActionParams: ActionParams = {
Expand All @@ -49,6 +61,7 @@ const defaultActionParams: ActionParams = {
strict: 'true',
verbose: 'false',
check_dot_files: 'explicit',
use_cspell_files: 'false',
};

type ValidationFunction = (params: ActionParamsInput) => string | undefined;
Expand All @@ -74,48 +87,31 @@ function validateRoot(params: ActionParamsInput) {
return !success ? `Root path does not exist: "${root}"` : undefined;
}

function validateInlineLevel(params: ActionParamsInput) {
const inline = params.inline;
const success = isInlineWorkflowCommand(inline);
return !success ? `Invalid inline level (${inline}), must be one of (error, warning, none)` : undefined;
function validateTrueFalse(key: keyof ActionParamsInput): ValidationFunction {
return validateOptions(key, ['true', 'false']);
}

const validateStrict = validateTrueFalse('strict', 'Invalid strict setting, must be one of (true, false)');
const validateIncrementalFilesOnly = validateTrueFalse(
'incremental_files_only',
'Invalid incremental_files_only setting, must be one of (true, false)',
);
const validateVerbose = validateTrueFalse('verbose', 'Invalid verbose setting, must be one of (true, false)');

function validateTrueFalse(key: keyof ActionParamsInput, msg: string): ValidationFunction {
function validateOptions(key: keyof ActionParamsInput, options: string[]): ValidationFunction {
return (params: ActionParamsInput) => {
const value = params[key];
const success = value === 'true' || value === 'false';
return !success ? msg : undefined;
const success = options.includes(value);
return !success ? `Invalid ${key} setting, must be one of (${options.join(', ')})` : undefined;
};
}

const inlineWorkflowCommandSet: Record<InlineWorkflowCommand | string, boolean | undefined> = {
error: true,
warning: true,
none: true,
};

function isInlineWorkflowCommand(cmd: InlineWorkflowCommand | string): cmd is InlineWorkflowCommand {
return !!inlineWorkflowCommandSet[cmd];
}

export function validateActionParams(
params: ActionParamsInput | ActionParams,
logError: (msg: string) => void,
): asserts params is ActionParams {
const validations: ValidationFunction[] = [
validateConfig,
validateRoot,
validateInlineLevel,
validateStrict,
validateIncrementalFilesOnly,
validateVerbose,
validateOptions('inline', ['error', 'warning', 'none']),
validateTrueFalse('strict'),
validateTrueFalse('incremental_files_only'),
validateTrueFalse('verbose'),
validateTrueFalse('use_cspell_files'),
validateOptions('check_dot_files', ['true', 'false', 'explicit']),
];
const success = validations
.map((fn) => fn(params))
Expand Down

0 comments on commit ae1a0ea

Please sign in to comment.