Skip to content

Commit

Permalink
chore: move lint test helper into test-util
Browse files Browse the repository at this point in the history
  • Loading branch information
boneskull committed May 20, 2024
1 parent f7293b8 commit 4588cb4
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 48 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import {
createRuleRunner,
type NamedRuleRunner,
} from '@midnight-smoker/test-util';
import {RuleSeverities} from 'midnight-smoker/rule';
import {normalize} from 'node:path';
import unexpected from 'unexpected';
import noBannedFiles from '../../../src/rules/no-banned-files';
import {createRuleRunner, type NamedRuleRunner} from './helpers';

const expect = unexpected.clone();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import {
createRuleRunner,
type NamedRuleRunner,
} from '@midnight-smoker/test-util';
import {RuleSeverities} from 'midnight-smoker/rule';
import {normalize} from 'node:path';
import unexpected from 'unexpected';
import noMissingEntryPoint from '../../../src/rules/no-missing-entry-point';
import {createRuleRunner, type NamedRuleRunner} from './helpers';

const expect = unexpected.clone();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import {
createRuleRunner,
type NamedRuleRunner,
} from '@midnight-smoker/test-util';
import {normalize} from 'node:path';
import unexpected from 'unexpected';
import noMissingExports from '../../../src/rules/no-missing-exports';
import {createRuleRunner, type NamedRuleRunner} from './helpers';

const expect = unexpected.clone();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import {
createRuleRunner,
type NamedRuleRunner,
} from '@midnight-smoker/test-util';
import {RuleSeverities} from 'midnight-smoker/rule';
import {normalize} from 'node:path';
import unexpected from 'unexpected';
import noMissingPkgFiles from '../../../src/rules/no-missing-pkg-files';
import {createRuleRunner, type NamedRuleRunner} from './helpers';

const expect = unexpected.clone();

Expand Down
2 changes: 2 additions & 0 deletions packages/test-util/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ export * from './null-pkg-manager';
export * from './register';

export * from './snapshot';

export * from './lint';
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import {head} from 'lodash';
import {type SmokerOptions} from 'midnight-smoker';
import {
ComponentKinds,
DEFAULT_PKG_MANAGER_BIN,
Expand All @@ -11,61 +9,38 @@ import {
PluginMetadata,
createPluginAPI,
type PluginFactory,
type PluginRegistry,
} from 'midnight-smoker/plugin';
import {
DEFAULT_RULE_SEVERITY,
getDefaultRuleOptions,
type CheckResultFailed,
type CheckResultOk,
type RuleDefSchemaValue,
type RuleOptions,
type RuleResultFailed,
type RuleResultOk,
type SomeRuleDef,
type SomeRuleOptions,
} from 'midnight-smoker/rule';
import {type FileManager, type FileManagerOpts} from 'midnight-smoker/util';

/**
* Runs a {@link Rule} against a fixture.
* A rule runner function which can only run a single rule.
*
* @param rule - Rule to apply
* @param installPath - Path to installed package dir (test fixture)
* @param opts - Rule-specific options (not including `severity`). Will be
* merged over default options
* @returns This will be empty if there were no issues raised
* @see {@link createRuleRunner}
*/
// export async function applyRule<R extends SomeRule>(
// rule: R,
// installPath: string,
// opts?: RuleOptions<R['schema']>,
// ): Promise<readonly RuleIssue[] | undefined> {
// const config = {
// severity: rule.defaultSeverity,
// opts: {...rule.defaultOptions, ...opts},
// };
// // const ctx = await LintController.createRuleContext(rule, installPath, config);
// // await LintController.runRule(ctx, rule, config);
// return ctx.finalize();
// return;
// }

export interface CreateRuleRunnerOptions {
pluginRegistry?: PluginRegistry;
fileManager?: FileManager;
fileManagerOpts?: FileManagerOpts;
smokerOpts?: SmokerOptions;
}

export type NamedRuleRunner = (
installPath: string,
opts?: SomeRuleOptions,
) => Promise<RuleResultOk | RuleResultFailed[]>;
) => Promise<CheckResultOk | CheckResultFailed[]>;

/**
* A rule runner function which can run any rule defined by the plugin factory.
*
* @see {@link createRuleRunner}
*/
export type RuleRunner = (
name: string,
installPath: string,
opts?: SomeRuleOptions,
) => Promise<RuleResultOk | RuleResultFailed[]>;
) => Promise<CheckResultOk | CheckResultFailed[]>;

/**
* Factory function which creates a {@link NamedRuleRunner}.
Expand All @@ -79,13 +54,13 @@ export type RuleRunner = (
* If you have a `RuleDef` instead of a `PluginFactory`, use {@link runRule}
* instead.
*
* @param fn Plugin factory function
* @param factory Plugin factory function
* @param name Rule name
* @returns Rule runner function (can only run the rule specified by the `name`
* parameter)
*/
export async function createRuleRunner(
fn: PluginFactory,
factory: PluginFactory,
name: string,
): Promise<NamedRuleRunner>;

Expand All @@ -101,13 +76,15 @@ export async function createRuleRunner(
* If you have a `RuleDef` instead of a `PluginFactory`, use {@link runRule}
* instead.
*
* @param fn Plugin factory function
* @param factory Plugin factory function
* @returns Rule runner function (can run any rule defined by the plugin
* factory)
*/
export async function createRuleRunner(fn: PluginFactory): Promise<RuleRunner>;
export async function createRuleRunner(
factory: PluginFactory,
): Promise<RuleRunner>;

export async function createRuleRunner(fn: PluginFactory, name?: string) {
export async function createRuleRunner(factory: PluginFactory, name?: string) {
const ruleDefs: Map<string, SomeRuleDef> = new Map();
const metadata = PluginMetadata.createTransient('test-plugin');
const pluginApi = createPluginAPI(
Expand All @@ -120,7 +97,7 @@ export async function createRuleRunner(fn: PluginFactory, name?: string) {
metadata,
);
try {
await fn(pluginApi);
await factory(pluginApi);
} catch (err) {
throw new PluginInitError(fromUnknownError(err), metadata);
}
Expand Down Expand Up @@ -156,7 +133,7 @@ export async function runRule<T extends SomeRuleDef>(
def: T,
installPath: string,
opts?: RuleOptions<T['schema']>,
): Promise<RuleResultOk | RuleResultFailed[]> {
): Promise<CheckResultOk | CheckResultFailed[]> {
const plan = 1;
const defaultOpts = getDefaultRuleOptions(def.schema as RuleDefSchemaValue);
const someConfig = {
Expand Down Expand Up @@ -195,5 +172,5 @@ export async function runRule<T extends SomeRuleDef>(
if (output.length !== plan) {
throw new Error(`Expected exactly ${plan} result(s)`);
}
return head(output)!.result;
return output.shift()!.result;
}

0 comments on commit 4588cb4

Please sign in to comment.