From 4588cb41d5e6614b46a59cc6eb2463cd4c496d1f Mon Sep 17 00:00:00 2001 From: Christopher Hiller Date: Sun, 19 May 2024 18:57:38 -0700 Subject: [PATCH] chore: move lint test helper into test-util --- .../test/e2e/rules/no-banned-files.spec.ts | 5 +- .../e2e/rules/no-missing-entry-point.spec.ts | 5 +- .../test/e2e/rules/no-missing-exports.spec.ts | 5 +- .../e2e/rules/no-missing-pkg-files.spec.ts | 5 +- packages/test-util/src/index.ts | 2 + .../helpers.ts => test-util/src/lint.ts} | 65 ++++++------------- 6 files changed, 39 insertions(+), 48 deletions(-) rename packages/{plugin-default/test/e2e/rules/helpers.ts => test-util/src/lint.ts} (71%) diff --git a/packages/plugin-default/test/e2e/rules/no-banned-files.spec.ts b/packages/plugin-default/test/e2e/rules/no-banned-files.spec.ts index 6ced6f851..9e2b768c7 100644 --- a/packages/plugin-default/test/e2e/rules/no-banned-files.spec.ts +++ b/packages/plugin-default/test/e2e/rules/no-banned-files.spec.ts @@ -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(); diff --git a/packages/plugin-default/test/e2e/rules/no-missing-entry-point.spec.ts b/packages/plugin-default/test/e2e/rules/no-missing-entry-point.spec.ts index 0ba354dc7..dbdb958b8 100644 --- a/packages/plugin-default/test/e2e/rules/no-missing-entry-point.spec.ts +++ b/packages/plugin-default/test/e2e/rules/no-missing-entry-point.spec.ts @@ -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(); diff --git a/packages/plugin-default/test/e2e/rules/no-missing-exports.spec.ts b/packages/plugin-default/test/e2e/rules/no-missing-exports.spec.ts index 9312b9658..ace785d00 100644 --- a/packages/plugin-default/test/e2e/rules/no-missing-exports.spec.ts +++ b/packages/plugin-default/test/e2e/rules/no-missing-exports.spec.ts @@ -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(); diff --git a/packages/plugin-default/test/e2e/rules/no-missing-pkg-files.spec.ts b/packages/plugin-default/test/e2e/rules/no-missing-pkg-files.spec.ts index 4f7aa2a50..b6d15efad 100644 --- a/packages/plugin-default/test/e2e/rules/no-missing-pkg-files.spec.ts +++ b/packages/plugin-default/test/e2e/rules/no-missing-pkg-files.spec.ts @@ -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(); diff --git a/packages/test-util/src/index.ts b/packages/test-util/src/index.ts index 7ed19ed9b..38c4bff2e 100644 --- a/packages/test-util/src/index.ts +++ b/packages/test-util/src/index.ts @@ -11,3 +11,5 @@ export * from './null-pkg-manager'; export * from './register'; export * from './snapshot'; + +export * from './lint'; diff --git a/packages/plugin-default/test/e2e/rules/helpers.ts b/packages/test-util/src/lint.ts similarity index 71% rename from packages/plugin-default/test/e2e/rules/helpers.ts rename to packages/test-util/src/lint.ts index b3e66c050..329249229 100644 --- a/packages/plugin-default/test/e2e/rules/helpers.ts +++ b/packages/test-util/src/lint.ts @@ -1,5 +1,3 @@ -import {head} from 'lodash'; -import {type SmokerOptions} from 'midnight-smoker'; import { ComponentKinds, DEFAULT_PKG_MANAGER_BIN, @@ -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( -// rule: R, -// installPath: string, -// opts?: RuleOptions, -// ): Promise { -// 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; +) => Promise; +/** + * 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; +) => Promise; /** * Factory function which creates a {@link NamedRuleRunner}. @@ -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; @@ -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; +export async function createRuleRunner( + factory: PluginFactory, +): Promise; -export async function createRuleRunner(fn: PluginFactory, name?: string) { +export async function createRuleRunner(factory: PluginFactory, name?: string) { const ruleDefs: Map = new Map(); const metadata = PluginMetadata.createTransient('test-plugin'); const pluginApi = createPluginAPI( @@ -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); } @@ -156,7 +133,7 @@ export async function runRule( def: T, installPath: string, opts?: RuleOptions, -): Promise { +): Promise { const plan = 1; const defaultOpts = getDefaultRuleOptions(def.schema as RuleDefSchemaValue); const someConfig = { @@ -195,5 +172,5 @@ export async function runRule( if (output.length !== plan) { throw new Error(`Expected exactly ${plan} result(s)`); } - return head(output)!.result; + return output.shift()!.result; }