Skip to content

Commit

Permalink
feat: Support config.files filtering.
Browse files Browse the repository at this point in the history
fixes: #1395
  • Loading branch information
Jason3S committed Mar 1, 2024
1 parent 491994e commit ce05199
Show file tree
Hide file tree
Showing 11 changed files with 842 additions and 904 deletions.
79 changes: 9 additions & 70 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
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: 4 additions & 4 deletions action-src/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@
"dependencies": {
"@actions/core": "^1.10.1",
"@actions/github": "^6.0.0",
"@cspell/cspell-bundled-dicts": "^8.4.1",
"@cspell/cspell-types": "^8.4.1",
"@cspell/cspell-bundled-dicts": "^8.5.0",
"@cspell/cspell-types": "^8.5.0",
"@octokit/webhooks-types": "^7.3.2",
"@types/node": "^20.11.23",
"cspell": "^8.4.1",
"cspell-glob": "^8.4.1",
"cspell": "^8.5.0",
"cspell-glob": "^8.5.0",
"vscode-uri": "^3.0.8"
},
"files": [
Expand Down
66 changes: 30 additions & 36 deletions action-src/src/action.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import { beforeEach, describe, expect, test, vi } from 'vitest';

const configFile = path.resolve(root, 'cspell.json');

const timeout = 30000;

const debug = false;

const log: typeof console.log = debug ? console.log : () => undefined;
Expand Down Expand Up @@ -38,51 +36,48 @@ describe('Validate Action', () => {
${'bad inline'} | ${'bad_params/bad_inline.json'} | ${new AppError('Bad Configuration.')}
${'bad_incremental_files_only'} | ${'bad_params/bad_incremental_files_only.json'} | ${new AppError('Bad Configuration.')}
${'bad strict'} | ${'bad_params/bad_strict.json'} | ${new AppError('Bad Configuration.')}
`(
'$test',
async ({ file, expected }) => {
const context = createContextFromFile(file);
expect.assertions(1);
await expect(action(context)).rejects.toEqual(expected);
},
timeout,
);
`('$test', async ({ file, expected }) => {
const context = createContextFromFile(file);
expect.assertions(1);
await expect(action(context)).rejects.toEqual(expected);
});

test.each`
testName | file | expected
${'event pr 1594'} | ${'pr_1594_env.json'} | ${true}
${'event push main.js'} | ${'push.json'} | ${true}
${'event pull_request main.js'} | ${'pull_request.json'} | ${true}
${'event pull_request_with_files main.js'} | ${'pull_request_with_files.json'} | ${true}
`(
'$testName',
async ({ file, expected }) => {
const context = createContextFromFile(file);
expect.assertions(1);
await expect(action(context)).resolves.toBe(expected);
},
timeout,
);
`('$testName', async ({ file, expected }) => {
const context = createContextFromFile(file);
expect.assertions(1);
await expect(action(context)).resolves.toBe(expected);
});

test.only.each`
testName | file | expected
${'event pr 1594'} | ${'pr_1594_env.json'} | ${true}
`('$testName', async ({ file, expected }) => {
const context = createContextFromFile(file);
expect.assertions(1);
await expect(action(context)).resolves.toBe(expected);
});

test.each`
files | expected
${'**'} | ${false}
${'**/*.md'} | ${true}
`(
'check all $files',
async ({ files, expected }) => {
const warnings: string[] = [];
spyWarn.mockImplementation((msg: string) => warnings.push(msg));
const context = createContextFromFile('pull_request.json', {
INPUT_FILES: files,
INPUT_INCREMENTAL_FILES_ONLY: 'false',
});
await expect(action(context)).resolves.toBe(expected);
expect(warnings).toMatchSnapshot();
expect(spyStdout).toHaveBeenCalled();
},
timeout,
);
`('check all $files', async ({ files, expected }) => {
const warnings: string[] = [];
spyWarn.mockImplementation((msg: string) => warnings.push(msg));
const context = createContextFromFile('pull_request.json', {
INPUT_FILES: files,
INPUT_INCREMENTAL_FILES_ONLY: 'false',
});
await expect(action(context)).resolves.toBe(expected);
expect(warnings).toMatchSnapshot();
expect(spyStdout).toHaveBeenCalled();
});

test.each`
files | incremental | dot | contextFile | expected
Expand Down Expand Up @@ -112,7 +107,6 @@ describe('Validate Action', () => {
expect(spyStdout.mock.calls).toMatchSnapshot();
expect(spyStdout.mock.calls.map((call) => call.join('').trim()).filter((a) => !!a)).toMatchSnapshot();
},
timeout,
);
});

Expand Down
33 changes: 22 additions & 11 deletions action-src/src/action.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import path from 'node:path';
import { debug, info, error, warning, setFailed, setOutput } from '@actions/core';
import type { Context as GitHubContext } from '@actions/github/lib/context.js';
import type { RunResult } from 'cspell';
import * as glob from 'cspell-glob';
import * as path from 'path';
import { ActionParams, validateActionParams } from './ActionParams.js';
import { getActionParams } from './getActionParams.js';
import { CSpellReporterForGithubAction } from './reporter.js';
import { lint, LintOptions } from './spell.js';
import { gitListFilesForPullRequest, gitListFilesForPush } from './git.js';
import { gitListFilesForPullRequest, gitListFilesForPush, gitRoot } from './git.js';

import type { PushEvent, PullRequestEvent } from '@octokit/webhooks-types';

Expand All @@ -31,14 +31,19 @@ const checkDotMap = {
explicit: undefined,
} as const;

async function checkSpelling(params: ActionParams, files: string[]): Promise<RunResult | true> {
async function checkSpelling(
params: ActionParams,
globs: string[],
files: string[] | undefined,
): Promise<RunResult | true> {
const options: LintOptions = {
root: params.root || process.cwd(),
config: params.config || undefined,
checkDotFiles: checkDotMap[params.check_dot_files],
files,
};

if (!files.length) {
if (!globs.length && !files?.length) {
return true;
}

Expand All @@ -47,7 +52,7 @@ async function checkSpelling(params: ActionParams, files: string[]): Promise<Run
};

const collector = new CSpellReporterForGithubAction(params.inline, reporterOptions, core);
await lint(files, options, collector.reporter);
await lint(globs, options, collector.reporter);

return collector.result;
}
Expand All @@ -67,12 +72,17 @@ function isSupportedEvent(eventName: EventNames | string): eventName is EventNam
return supportedIncrementalEvents.has(eventName);
}

async function gatherFilesFromContext(context: Context): Promise<Set<string>> {
async function gatherGitCommitFilesFromContext(context: Context): Promise<string[] | undefined> {
if (context.useEventFiles) {
const eventFiles = await gatherFiles(context);
return filterFiles(context.files, eventFiles, context.dot);
if (!eventFiles) return undefined;
const files = filterFiles(context.files, eventFiles, context.dot);
const root = await gitRoot();
return [...files].map((f) => path.resolve(root, f));
}
}

async function gatherFileGlobsFromContext(context: Context): Promise<Set<string>> {
const files = new Set<string>(
context.files
.split('\n')
Expand All @@ -86,7 +96,7 @@ async function gatherFilesFromContext(context: Context): Promise<Set<string>> {
* Gather the set of files to be spell checked.
* @param context Context
*/
async function gatherFiles(context: Context): Promise<Set<string>> {
async function gatherFiles(context: Context): Promise<Set<string> | undefined> {
const eventName = context.githubContext.eventName;

// console.warn('gatherFiles %o', { context: context.githubContext, eventName });
Expand All @@ -102,7 +112,7 @@ async function gatherFiles(context: Context): Promise<Set<string>> {
core.warning('Unable to determine which files have changed, checking files: ' + defaultGlob);
}

return new Set();
return undefined;
}

function filterFiles(globPattern: string, files: Set<string>, dot: boolean): Set<string> {
Expand Down Expand Up @@ -145,8 +155,9 @@ export async function action(githubContext: GitHubContext): Promise<boolean> {
};

core.info(friendlyEventName(eventName));
const files = await gatherFilesFromContext(context);
const result = await checkSpelling(params, [...files]);
const fileList = await gatherGitCommitFilesFromContext(context);
const files = await gatherFileGlobsFromContext(context);
const result = await checkSpelling(params, fileList ? [] : [...files], fileList);
if (result === true) {
return true;
}
Expand Down
8 changes: 8 additions & 0 deletions action-src/src/git.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@ import {
gitListFilesForContext,
gitListFilesForPullRequest,
gitListFilesForPush,
gitRoot,
} from './git.js';

import { root } from './test/helper.js';

const urlFixtures = new URL('../fixtures/', import.meta.url);

const ac = expect.arrayContaining;
Expand Down Expand Up @@ -51,6 +54,11 @@ describe('git', () => {
test('gitDeepen', async () => {
await expect(gitDeepen(0)).resolves.toBeUndefined();
});

test('gitRoot', async () => {
const rootGit = await gitRoot();
expect(rootGit).toEqual(root);
});
});

function readFixtureFile(file: string | URL): Promise<string> {
Expand Down
22 changes: 14 additions & 8 deletions action-src/src/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,34 @@ const execP = promisify(exec);

export async function gitListCommits(count = 100, _since?: Date): Promise<string[]> {
const args = ['rev-list', 'HEAD', `-${count}`];
const cmd = `git ${args.join(' ')}`;
const cmdResult = await execP(cmd);
return cmdResult.stdout
const cmdResult = await runGit(args);
return cmdResult
.split('\n')
.map((a) => a.trim())
.filter((a) => !!a);
}

export async function gitDeepen(count: number): Promise<void> {
const args = ['fetch', `--deepen=${count}`];
const cmd = `git ${args.join(' ')}`;
await execP(cmd);
await runGit(args);
}

export async function gitListFiles(sha1: string, sha2?: string): Promise<string[]> {
const SHAs = [sha1, sha2].map(cleanSha).filter((a) => !!a);
if (!SHAs.length) return [];

const args = ['diff-tree', '--no-commit-id', '--name-only', '-r', ...SHAs];
const cmd = `git ${args.join(' ')}`;
const cmdResult = await execP(cmd);
return cmdResult.stdout
const cmdResult = await runGit(args);
return cmdResult
.split('\n')
.map((a) => a.trim())
.filter((a) => !!a);
}

export async function gitRoot(): Promise<string> {
return (await runGit(['rev-parse', '--show-toplevel'])).trim();
}

function cleanSha(sha: string | undefined): string {
if (!sha) return '';
const s = sha.trim().replace(/[^a-fA-F0-9]/g, '');
Expand Down Expand Up @@ -101,3 +102,8 @@ export class GitError extends Error {
this.name = 'GitError';
}
}

async function runGit(args: string[]): Promise<string> {
const { stdout } = await execP(`git ${args.join(' ')}`);
return stdout;
}

0 comments on commit ce05199

Please sign in to comment.