Skip to content

Commit

Permalink
chore: tests
Browse files Browse the repository at this point in the history
  • Loading branch information
boneskull committed May 28, 2024
1 parent 567ed48 commit db21e49
Show file tree
Hide file tree
Showing 46 changed files with 1,534 additions and 615 deletions.
5 changes: 1 addition & 4 deletions .wallaby.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,11 @@ module.exports = () => {
pattern: './packages/midnight-smoker/data/*.json',
instrument: false,
},
{
pattern: './packages/midnight-smoker/test/**/*.ts',
instrument: false,
},
{
pattern: './packages/midnight-smoker/test/**/fixture/**/*',
instrument: false,
},
'./packages/midnight-smoker/test/**/*.ts',
'./packages/midnight-smoker/src/**/*.ts',
'./packages/midnight-smoker/package.json',
// './packages/midnight-smoker/src/package.json',
Expand Down
2 changes: 1 addition & 1 deletion packages/midnight-smoker/src/cli/command/run-script-cmd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/
import {handleRejection} from '#cli/cli-util';
import {Smoker} from '#smoker';
import {castArray} from '#util/schema-util';
import {castArray} from '#util/util';
import Debug from 'debug';
import type {ArgumentsCamelCase, Argv, InferredOptionTypes} from 'yargs';
import {BaseCommand} from './base-cmd';
Expand Down
4 changes: 2 additions & 2 deletions packages/midnight-smoker/src/error/base-error.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {isZodError} from '#util/error-util';
import {castArray} from '#util/schema-util';
import {castArray} from '#util/util';
import {italic, white, whiteBright, yellow} from 'chalk';
import Debug from 'debug';
import {format, formatWithOptions} from 'node:util';
Expand Down Expand Up @@ -126,7 +126,7 @@ export abstract class AggregateSmokerError<Context extends object | void = void>
isZodError(err) ? fromZodError(err) : err,
);
super(errs, message);
this.context = context;
this.context = context ?? undefined;
this.errors = errs;
this.code = getErrorCode(this);
}
Expand Down
20 changes: 20 additions & 0 deletions packages/midnight-smoker/src/error/cleanup-error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import {BaseSmokerError} from './base-error';

/**
* Thrown when `Smoker.cleanup()` fails.
*
* @group Errors
*/

export class CleanupError extends BaseSmokerError<
{
dir: string;
},
NodeJS.ErrnoException
> {
public readonly id = 'CleanupError';

constructor(message: string, dir: string, error: NodeJS.ErrnoException) {
super(message, {dir}, error);
}
}
7 changes: 5 additions & 2 deletions packages/midnight-smoker/src/error/codes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,16 @@
export const ErrorCodes = {
CleanupError: 'ESMOKER_CLEANUP',
ComponentCollisionError: 'ESMOKER_COMPONENTIDCOLLISION',
DirCreationError: 'ESMOKER_DIRCREATION',
TempDirError: 'ESMOKER_DIRCREATION',
DisallowedPluginError: 'ESMOKER_DISALLOWEDPLUGIN',
DuplicatePluginError: 'ESMOKER_DUPLICATEPLUGIN',
ExecError: 'ESMOKER_EXEC',
InstallError: 'ESMOKER_INSTALL',
InvalidArgError: 'ESMOKER_INVALIDARG',
InvalidComponentError: 'ESMOKER_INVALIDCOMPONENT',
InvalidPluginError: 'ESMOKER_INVALIDPLUGIN',
LifecycleError: 'ESMOKER_LIFECYCLE',
MachineError: 'ESMOKER_MACHINE',
MissingPackageJsonError: 'ESMOKER_MISSINGPACKAGEJSON',
NotImplementedError: 'ESMOKER_NOTIMPLEMENTED',
PackageManagerError: 'ESMOKER_PACKAGEMANAGER',
Expand All @@ -30,10 +32,11 @@ export const ErrorCodes = {
PluginInitError: 'ESMOKER_PLUGININIT',
PluginResolutionError: 'ESMOKER_PLUGINRESOLUTION',
ReporterError: 'ESMOKER_REPORTER',
ReporterListenerError: 'ESMOKER_REPORTERLISTENER',
RuleError: 'ESMOKER_RULEERROR',
RunScriptError: 'ESMOKER_RUNSCRIPT',
ScriptFailedError: 'ESMOKER_SCRIPTFAILED',
SmokeFailedError: 'ESMOKER_SMOKEFAILED',
SmokeError: 'ESMOKER_SMOKE',
SmokerReferenceError: 'ESMOKER_REFERENCE',
UnknownDistTagError: 'ESMOKER_UNKNOWNDISTTAG',
UnknownScriptError: 'ESMOKER_UNKNOWNSCRIPT',
Expand Down
16 changes: 11 additions & 5 deletions packages/midnight-smoker/src/error/create-dir-error.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
import {type StaticPkgManagerSpec} from '#schema/static-pkg-manager-spec';
import {isString} from 'lodash';
import {BaseSmokerError} from './base-error';

/**
* @group Errors
*/

export class DirCreationError extends BaseSmokerError<
{prefix: string},
export class TempDirError extends BaseSmokerError<
{spec: string},
NodeJS.ErrnoException
> {
public readonly id = 'DirCreationError';
public readonly id = 'TempDirError';

constructor(message: string, prefix: string, error: NodeJS.ErrnoException) {
super(message, {prefix}, error);
constructor(
message: string,
spec: string | StaticPkgManagerSpec,
error: NodeJS.ErrnoException,
) {
super(message, {spec: isString(spec) ? spec : spec.spec}, error);
}
}
4 changes: 3 additions & 1 deletion packages/midnight-smoker/src/error/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ export * from './base-error';

export * from './codes';

export * from './cleanup-error';

export * from './component-collision-error';

export * from './create-dir-error';
Expand Down Expand Up @@ -56,7 +58,7 @@ export * from './script-bailed';

export * from './script-failed-error';

export * from './smoker-error';
export * from './smoke-error';

export * from './smoker-reference-error';

Expand Down
47 changes: 31 additions & 16 deletions packages/midnight-smoker/src/error/install-error.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import type {ExecError} from '#error/exec-error';
import {type PkgManagerSpec} from '#pkg-manager/pkg-manager-spec';
import {ExecError} from '#error/exec-error';
import {isExecResult, type ExecResult} from '#schema/exec-result';
import {type StaticPkgManagerSpec} from '#schema/static-pkg-manager-spec';
import {isExecaError, isSmokerError} from '#util/error-util';
import {red} from 'chalk';
import {isString} from 'lodash';
import {BaseSmokerError} from './base-error';
import {fromUnknownError} from './from-unknown-error';

/**
* @group Errors
Expand All @@ -11,32 +15,43 @@ export class InstallError extends BaseSmokerError<
pkgManager: string;
pkgSpec: string;
cwd: string;
exitCode?: number;
output?: string;
error?: object;
result?: ExecResult;
},
ExecError | undefined
ExecError | Error | undefined
> {
public readonly id = 'InstallError';

constructor(
message: string,
pkgManager: string | PkgManagerSpec,
pkgManager: string | StaticPkgManagerSpec,
pkgSpec: string,
cwd: string,
{
error,
exitCode,
output,
}: {error?: object; exitCode?: number; output?: string} = {},
execError?: ExecError,
rawResult: unknown,
) {
const pmSpec = isString(pkgManager) ? pkgManager : pkgManager.spec;
let error: ExecError | Error | undefined;
let result: ExecResult | undefined;

if (isExecaError(rawResult)) {
error = isSmokerError(ExecError, rawResult)
? rawResult
: new ExecError(rawResult);
} else if (!isExecResult(rawResult)) {
error = fromUnknownError(rawResult);
} else {
result = rawResult;
}
super(
`Package manager ${pkgManager} failed to install "${pkgSpec}" in dir ${cwd}: ${red(
`Package manager ${pmSpec} failed to install "${pkgSpec}" in dir ${cwd}: ${red(
message,
)}`,
{pkgManager: `${pkgManager}`, pkgSpec, cwd, exitCode, output, error},
execError,
{
pkgManager: pmSpec,
pkgSpec,
cwd,
result,
},
error,
);
}
}
37 changes: 37 additions & 0 deletions packages/midnight-smoker/src/error/lifecycle-error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import {type StaticPluginMetadata} from '#schema/static-plugin-metadata';
import {BaseSmokerError} from './base-error';

export type LifecycleStage = 'setup' | 'teardown';

export type LifecycleComponentKind = 'reporter' | 'pkg-manager';

export class LifecycleError extends BaseSmokerError<
{
name: string;
stage: LifecycleStage;
plugin: StaticPluginMetadata;
kind: LifecycleComponentKind;
},
Error
> {
public readonly id = 'LifecycleError';

constructor(
error: Error,
stage: LifecycleStage,
kind: LifecycleComponentKind,
name: string,
plugin: StaticPluginMetadata,
) {
super(
`Error during lifecycle hook "${stage}"`,
{
name,
kind,
stage,
plugin,
},
error,
);
}
}
43 changes: 43 additions & 0 deletions packages/midnight-smoker/src/error/machine-error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import {castArray} from 'lodash';
import {AggregateSmokerError} from './base-error';

/**
* Generic aggregate error for machines
*
* @group Errors
*/
export class MachineError extends AggregateSmokerError<{
machineId: string;
}> {
public readonly id = 'MachineError';

public override readonly context: {
machineId: string;
};

static originators = new WeakMap<Error, string>();

constructor(message: string, errors: Error[] | Error, machineId: string) {
errors = castArray(errors);
for (const error of errors) {
MachineError.originators.set(error, machineId);
}
super(message, errors, {machineId});
this.context = {machineId};
}

/**
* Clone this instance with additional errors and options.
*
* @param error Zero or more errors to append to the aggregate
* @param options Results of linting and running scripts, if any
* @returns New instance of `SmokeError` with the given errors and options
*/
clone(error: Error | Error[] = []): MachineError {
return new MachineError(
this.message,
[...this.errors, ...castArray(error)],
this.context.machineId,
);
}
}
54 changes: 26 additions & 28 deletions packages/midnight-smoker/src/error/pack-error.ts
Original file line number Diff line number Diff line change
@@ -1,43 +1,40 @@
import {type PkgManagerSpec} from '#pkg-manager/pkg-manager-spec';
import {type StaticPkgManagerSpec} from '#schema/static-pkg-manager-spec';
import {type WorkspaceInfo} from '#schema/workspaces';
import {red} from 'chalk';
import {isString} from 'lodash';
import {BaseSmokerError} from './base-error';
import {fromUnknownError} from './from-unknown-error';

/**
* @group Errors
*/
export class PackError extends BaseSmokerError<{
spec: string;
dest: string;
cwd?: string;
exitCode?: number;
output?: string;
error?: object;
workspace: WorkspaceInfo;
}> {
export class PackError extends BaseSmokerError<
{
spec: string;
dest: string;
workspace: WorkspaceInfo;
},
Error | undefined
> {
public readonly id = 'PackError';

constructor(
message: string,
pkgManager: string | PkgManagerSpec,
pkgManager: string | StaticPkgManagerSpec,
workspace: WorkspaceInfo,
dest: string,
{
cwd,
error,
exitCode,
output,
}: {cwd?: string; error?: object; exitCode?: number; output?: string} = {},
error?: unknown,
) {
super(`Package manager ${pkgManager} failed to pack: ${red(message)}`, {
error,
spec: `${pkgManager}`,
cwd,
dest,
exitCode,
output,
workspace,
});
const pmSpec = isString(pkgManager) ? pkgManager : pkgManager.spec;
super(
`Package manager ${pmSpec} failed to pack: ${red(message)}`,
{
spec: pmSpec,
dest,
workspace,
},
fromUnknownError(error),
);
}
}

Expand All @@ -56,11 +53,12 @@ export class PackParseError extends BaseSmokerError<

constructor(
message: string,
pkgManager: string | PkgManagerSpec,
pkgManager: string | StaticPkgManagerSpec,
workspace: WorkspaceInfo,
error: SyntaxError,
output: string,
) {
super(message, {pkgManager: `${pkgManager}`, output, workspace}, error);
const pmSpec = isString(pkgManager) ? pkgManager : pkgManager.spec;
super(message, {pkgManager: pmSpec, output, workspace}, error);
}
}
Loading

0 comments on commit db21e49

Please sign in to comment.