Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Support config.files filtering. #1605

Merged
merged 9 commits into from Mar 3, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
79 changes: 9 additions & 70 deletions .vscode/launch.json
Expand Up @@ -6,75 +6,15 @@
"configurations": [
{
"type": "node",
"name": "vscode-jest-tests",
"request": "launch",
"runtimeArgs": [
"jest"
],
"cwd": "${workspaceFolder}/action-src",
"args": [
"--runInBand"
],
"runtimeExecutable": "yarn",
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
"disableOptimisticBPs": true,
},
{
"type": "node",
"name": "Jest Current File",
"request": "launch",
"runtimeArgs": [
"jest"
],
"cwd": "${workspaceFolder}/action-src",
"args": [
"${fileBasename}",
"--runInBand"
],
"runtimeExecutable": "yarn",
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
"disableOptimisticBPs": true,
},
{
"type": "node",
"request": "launch",
"name": "XX Jest Current File",
"name": "Vitest: Current Test File",
"autoAttachChildProcesses": true,
"skipFiles": [],
"program": "${fileWorkspaceFolder}/node_modules/vitest/vitest.mjs",
"args": ["run", "--test-timeout=600000", "${fileBasenameNoExtension}"],
"cwd": "${workspaceFolder}/action-src",
"args": [
"--runInBand",
"${fileBasename}"
],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
"disableOptimisticBPs": true,
"skipFiles": [
"<node_internals>/**"
],
"windows": {
"program": "${workspaceFolder}/action-src/yarn",
}
},
{
"type": "node",
"request": "launch",
"name": "Jest Test All",
"program": "${workspaceFolder}/action-src/yarn",
"cwd": "${workspaceFolder}",
"args": [
"jest",
"--runInBand"
],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
"disableOptimisticBPs": true,
"skipFiles": [
"<node_internals>/**"
],
"windows": {
"program": "${workspaceFolder}/action-src/yarn",
}
"smartStep": true,
"console": "integratedTerminal"
},
{
"type": "node",
Expand All @@ -83,10 +23,9 @@
"skipFiles": [
"<node_internals>/**"
],
"program": "${workspaceFolder}/action/lib/main_root.js",
"program": "${workspaceFolder}/action/lib/main_root.cjs",
"cwd": "${workspaceFolder}",
"env": {
"INPUT_GITHUB_TOKEN": "$GITHUB_TOKEN",
"GITHUB_EVENT_PATH": "./fixtures/pull_request_payload.json",
"GITHUB_EVENT_NAME": "pull_request",
"GITHUB_SHA": "fac78ee45538f198c00ae651db5aedc7336f7ccc",
Expand All @@ -99,7 +38,7 @@
"GITHUB_RUN_ID": "421485606"
},
"outFiles": [
"${workspaceFolder}/action/**/*.js"
"${workspaceFolder}/action/**/*.cjs"
]
}
]
Expand Down
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/cspell.json
@@ -1,5 +1,5 @@
{
"version": "0.1",
"version": "0.2",
"words": ["camelcase", "octokit", "pollyjs", "repos", "shrinkwrap", "streetsidesoftware"],
"ignorePaths": ["node_modules", "action/", "cspell.json", "__recordings__", "**/*.json"],
"overrides": [
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
4 changes: 4 additions & 0 deletions action-src/fixtures/sampleCode/ts/cspell.config.yaml
@@ -0,0 +1,4 @@
import:
- "../../cspell.json"
files:
- "*.ts"
2 changes: 1 addition & 1 deletion action-src/package.json
Expand Up @@ -29,7 +29,7 @@
"@actions/core": "^1.10.1",
"@actions/github": "^6.0.0",
"@cspell/cspell-bundled-dicts": "^8.5.0",
"@cspell/cspell-types": "^8.4.1",
"@cspell/cspell-types": "^8.5.0",
"@octokit/webhooks-types": "^7.3.2",
"@types/node": "^20.11.23",
"cspell": "^8.5.0",
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
60 changes: 31 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,12 +61,13 @@ const defaultActionParams: ActionParams = {
strict: 'true',
verbose: 'false',
check_dot_files: 'explicit',
use_cspell_files: 'false',
};

type ValidationFunction = (params: ActionParamsInput) => string | undefined;

export function applyDefaults(params: ActionParamsInput): ActionParamsInput {
const results = { ...params };
export function applyDefaults(params: Partial<ActionParamsInput>): ActionParamsInput {
const results = { ...defaultActionParams, ...params };
const alias = results as Record<string, string>;
for (const [key, value] of Object.entries(defaultActionParams)) {
alias[key] = alias[key] || value;
Expand All @@ -74,35 +87,22 @@ 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 toActionParams(params: Partial<ActionParamsInput>): ActionParams {
const p = applyDefaults(params);
validateActionParams(p, () => undefined);
return p;
}

export function validateActionParams(
Expand All @@ -112,10 +112,12 @@ export function validateActionParams(
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