Skip to content

Commit

Permalink
remove old Runtime class and related interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
davidjgoss committed Aug 21, 2024
1 parent c209c37 commit 27df645
Show file tree
Hide file tree
Showing 16 changed files with 108 additions and 201 deletions.
2 changes: 1 addition & 1 deletion src/api/run_cucumber.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ Running from: ${__dirname}
supportCodeLibrary,
options: options.runtime,
})
const success = await runtime.start()
const success = await runtime.run()
await pluginManager.cleanup()
await cleanupFormatters()

Expand Down
6 changes: 2 additions & 4 deletions src/api/runtime.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import { EventEmitter } from 'node:events'
import { IdGenerator } from '@cucumber/messages'
import { IRuntime } from '../runtime'
import { EventDataCollector } from '../formatter/helpers'
import { SupportCodeLibrary } from '../support_code_library_builder/types'
import { ILogger } from '../logger'
import { Coordinator } from '../runtime/coordinator'
import { Runtime, Coordinator, RuntimeAdapter } from '../runtime'
import { ChildProcessAdapter } from '../runtime/parallel/adapter'
import { IFilterablePickle } from '../filter'
import { InProcessAdapter } from '../runtime/serial/adapter'
import { RuntimeAdapter } from '../runtime/types'
import { IRunEnvironment, IRunOptionsRuntime } from './types'

export async function makeRuntime({
Expand All @@ -29,7 +27,7 @@ export async function makeRuntime({
filteredPickles: ReadonlyArray<IFilterablePickle>
supportCodeLibrary: SupportCodeLibrary
options: IRunOptionsRuntime
}): Promise<IRuntime> {
}): Promise<Runtime> {
const adapter: RuntimeAdapter =
options.parallel > 0
? new ChildProcessAdapter(
Expand Down
4 changes: 2 additions & 2 deletions src/formatter/helpers/summary_helpers_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ import { getTestCaseAttempts } from '../../../test/formatter_helpers'
import { getBaseSupportCodeLibrary } from '../../../test/fixtures/steps'
import timeMethods, { durationBetweenTimestamps } from '../../time'
import { buildSupportCodeLibrary } from '../../../test/runtime_helpers'
import { IRuntimeOptions } from '../../runtime'
import { RuntimeOptions } from '../../runtime'
import { SupportCodeLibrary } from '../../support_code_library_builder/types'
import { doesNotHaveValue } from '../../value_checker'
import { formatSummary } from './summary_helpers'

interface ITestFormatSummaryOptions {
runtimeOptions?: Partial<IRuntimeOptions>
runtimeOptions?: Partial<RuntimeOptions>
sourceData: string
supportCodeLibrary?: SupportCodeLibrary
testRunStarted?: messages.TestRunStarted
Expand Down
4 changes: 2 additions & 2 deletions src/formatter/progress_bar_formatter_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ import {
import { buildSupportCodeLibrary } from '../../test/runtime_helpers'
import { getBaseSupportCodeLibrary } from '../../test/fixtures/steps'
import timeMethods from '../time'
import { IRuntimeOptions } from '../runtime'
import { RuntimeOptions } from '../runtime'
import { SupportCodeLibrary } from '../support_code_library_builder/types'
import { doesHaveValue, doesNotHaveValue } from '../value_checker'
import ProgressBarFormatter from './progress_bar_formatter'
import FormatterBuilder from './builder'
import { EventDataCollector } from './helpers'

interface ITestProgressBarFormatterOptions {
runtimeOptions?: Partial<IRuntimeOptions>
runtimeOptions?: Partial<RuntimeOptions>
shouldStopFn: (envelope: messages.Envelope) => boolean
sources?: ITestSource[]
supportCodeLibrary?: SupportCodeLibrary
Expand Down
8 changes: 4 additions & 4 deletions src/runtime/coordinator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import { assembleTestCases } from '../assemble'
import { SupportCodeLibrary } from '../support_code_library_builder/types'
import { RuntimeAdapter } from './types'
import { timestamp } from './stopwatch'
import { IRuntime } from './index'
import { Runtime } from './index'

export class Coordinator implements IRuntime {
export class Coordinator implements Runtime {
constructor(
private eventBroadcaster: EventEmitter,
private newId: IdGenerator.NewId,
Expand All @@ -16,7 +16,7 @@ export class Coordinator implements IRuntime {
private adapter: RuntimeAdapter
) {}

async start(): Promise<boolean> {
async run(): Promise<boolean> {
this.eventBroadcaster.emit('envelope', {
testRunStarted: {
timestamp: timestamp(),
Expand All @@ -30,7 +30,7 @@ export class Coordinator implements IRuntime {
supportCodeLibrary: this.supportCodeLibrary,
})

const success = await this.adapter.start(assembledTestCases)
const success = await this.adapter.run(assembledTestCases)

this.eventBroadcaster.emit('envelope', {
testRunFinished: {
Expand Down
6 changes: 3 additions & 3 deletions src/runtime/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as messages from '@cucumber/messages'
import { formatLocation } from '../formatter/helpers/location_helpers'
import { PickleTagFilter } from '../pickle_filter'
import StepDefinition from '../models/step_definition'
import { IRuntimeOptions } from '.'
import { RuntimeOptions } from '.'

export function getAmbiguousStepException(
stepDefinitions: StepDefinition[]
Expand Down Expand Up @@ -47,7 +47,7 @@ export function getAmbiguousStepException(

export function retriesForPickle(
pickle: messages.Pickle,
options: IRuntimeOptions
options: RuntimeOptions
): number {
if (!options.retry) {
return 0
Expand All @@ -69,7 +69,7 @@ export function retriesForPickle(

export function shouldCauseFailure(
status: messages.TestStepResultStatus,
options: IRuntimeOptions
options: RuntimeOptions
): boolean {
if (options.dryRun) {
return false
Expand Down
135 changes: 2 additions & 133 deletions src/runtime/index.ts
Original file line number Diff line number Diff line change
@@ -1,133 +1,2 @@
import { EventEmitter } from 'node:events'
import * as messages from '@cucumber/messages'
import { IdGenerator } from '@cucumber/messages'
import { JsonObject } from 'type-fest'
import { EventDataCollector } from '../formatter/helpers'
import { SupportCodeLibrary } from '../support_code_library_builder/types'
import { assembleTestCasesByPickleId } from '../assemble'
import { retriesForPickle, shouldCauseFailure } from './helpers'
import { makeRunTestRunHooks, RunsTestRunHooks } from './run_test_run_hooks'
import { IStopwatch, create, timestamp } from './stopwatch'
import TestCaseRunner from './test_case_runner'

export interface IRuntime {
start: () => Promise<boolean>
}

export interface INewRuntimeOptions {
eventBroadcaster: EventEmitter
eventDataCollector: EventDataCollector
newId: IdGenerator.NewId
options: IRuntimeOptions
pickleIds: string[]
supportCodeLibrary: SupportCodeLibrary
}

export interface IRuntimeOptions {
dryRun: boolean
failFast: boolean
filterStacktraces: boolean
retry: number
retryTagFilter: string
strict: boolean
worldParameters: JsonObject
}

export default class Runtime implements IRuntime {
private readonly eventBroadcaster: EventEmitter
private readonly eventDataCollector: EventDataCollector
private readonly stopwatch: IStopwatch
private readonly newId: IdGenerator.NewId
private readonly options: IRuntimeOptions
private readonly pickleIds: string[]
private readonly supportCodeLibrary: SupportCodeLibrary
private success: boolean
private readonly runTestRunHooks: RunsTestRunHooks

constructor({
eventBroadcaster,
eventDataCollector,
newId,
options,
pickleIds,
supportCodeLibrary,
}: INewRuntimeOptions) {
this.eventBroadcaster = eventBroadcaster
this.eventDataCollector = eventDataCollector
this.stopwatch = create()
this.newId = newId
this.options = options
this.pickleIds = pickleIds
this.supportCodeLibrary = supportCodeLibrary
this.success = true
this.runTestRunHooks = makeRunTestRunHooks(
this.options.dryRun,
this.supportCodeLibrary.defaultTimeout,
this.options.worldParameters,
(name, location) => `${name} hook errored, process exiting: ${location}`
)
}

async runTestCase(
pickleId: string,
testCase: messages.TestCase
): Promise<void> {
const pickle = this.eventDataCollector.getPickle(pickleId)
const retries = retriesForPickle(pickle, this.options)
const skip = this.options.dryRun || (this.options.failFast && !this.success)
const testCaseRunner = new TestCaseRunner({
eventBroadcaster: this.eventBroadcaster,
gherkinDocument: this.eventDataCollector.getGherkinDocument(pickle.uri),
newId: this.newId,
pickle,
testCase,
retries,
skip,
filterStackTraces: this.options.filterStacktraces,
supportCodeLibrary: this.supportCodeLibrary,
worldParameters: this.options.worldParameters,
})
const status = await testCaseRunner.run()
if (shouldCauseFailure(status, this.options)) {
this.success = false
}
}

async start(): Promise<boolean> {
const testRunStarted: messages.Envelope = {
testRunStarted: {
timestamp: timestamp(),
},
}
this.eventBroadcaster.emit('envelope', testRunStarted)
this.stopwatch.start()
await this.runTestRunHooks(
this.supportCodeLibrary.beforeTestRunHookDefinitions,
'a BeforeAll'
)
const assembledTestCases = await assembleTestCasesByPickleId({
eventBroadcaster: this.eventBroadcaster,
newId: this.newId,
pickles: this.pickleIds.map((pickleId) =>
this.eventDataCollector.getPickle(pickleId)
),
supportCodeLibrary: this.supportCodeLibrary,
})
for (const pickleId of this.pickleIds) {
await this.runTestCase(pickleId, assembledTestCases[pickleId])
}
await this.runTestRunHooks(
this.supportCodeLibrary.afterTestRunHookDefinitions.slice(0).reverse(),
'an AfterAll'
)
this.stopwatch.stop()
const testRunFinished: messages.Envelope = {
testRunFinished: {
timestamp: timestamp(),
success: this.success,
},
}
this.eventBroadcaster.emit('envelope', testRunFinished)
return this.success
}
}
export * from './coordinator'
export * from './types'
2 changes: 1 addition & 1 deletion src/runtime/parallel/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ export class ChildProcessAdapter implements RuntimeAdapter {
}
}

async start(
async run(
assembledTestCases: ReadonlyArray<AssembledTestCase>
): Promise<boolean> {
this.todo = Array.from(assembledTestCases)
Expand Down
4 changes: 2 additions & 2 deletions src/runtime/parallel/command_types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Envelope } from '@cucumber/messages'
import { IRuntimeOptions } from '../index'
import { RuntimeOptions } from '../index'
import { ISupportCodeCoordinates } from '../../api'
import { AssembledTestCase } from '../../assemble'

Expand All @@ -14,7 +14,7 @@ export interface IWorkerCommand {
export interface IWorkerCommandInitialize {
supportCodeCoordinates: ISupportCodeCoordinates
supportCodeIds?: ICanonicalSupportCodeIds
options: IRuntimeOptions
options: RuntimeOptions
}

export interface ICanonicalSupportCodeIds {
Expand Down
4 changes: 2 additions & 2 deletions src/runtime/parallel/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { SupportCodeLibrary } from '../../support_code_library_builder/types'
import { doesHaveValue } from '../../value_checker'
import tryRequire from '../../try_require'
import { Worker } from '../worker'
import { IRuntimeOptions } from '../index'
import { RuntimeOptions } from '../index'
import { AssembledTestCase } from '../../assemble'
import {
ICoordinatorReport,
Expand All @@ -29,7 +29,7 @@ export class ChildProcessWorker {
private readonly eventBroadcaster: EventEmitter
private readonly newId: IdGenerator.NewId
private readonly sendMessage: IMessageSender
private options: IRuntimeOptions
private options: RuntimeOptions
private supportCodeLibrary: SupportCodeLibrary
private worker: Worker

Expand Down
6 changes: 3 additions & 3 deletions src/runtime/serial/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { IdGenerator } from '@cucumber/messages'
import { RuntimeAdapter } from '../types'
import { AssembledTestCase } from '../../assemble'
import { Worker } from '../worker'
import { IRuntimeOptions } from '../index'
import { RuntimeOptions } from '../index'
import { SupportCodeLibrary } from '../../support_code_library_builder/types'

export class InProcessAdapter implements RuntimeAdapter {
Expand All @@ -12,7 +12,7 @@ export class InProcessAdapter implements RuntimeAdapter {
constructor(
eventBroadcaster: EventEmitter,
newId: IdGenerator.NewId,
options: IRuntimeOptions,
options: RuntimeOptions,
supportCodeLibrary: SupportCodeLibrary
) {
this.#worker = new Worker(
Expand All @@ -24,7 +24,7 @@ export class InProcessAdapter implements RuntimeAdapter {
)
}

async start(
async run(
assembledTestCases: ReadonlyArray<AssembledTestCase>
): Promise<boolean> {
await this.#worker.runBeforeAllHooks()
Expand Down
17 changes: 16 additions & 1 deletion src/runtime/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
import { JsonObject } from 'type-fest'
import { AssembledTestCase } from '../assemble'

export interface RuntimeOptions {
dryRun: boolean
failFast: boolean
filterStacktraces: boolean
retry: number
retryTagFilter: string
strict: boolean
worldParameters: JsonObject
}

export interface Runtime {
run: () => Promise<boolean>
}

export interface RuntimeAdapter {
start(assembledTestCases: ReadonlyArray<AssembledTestCase>): Promise<boolean>
run(assembledTestCases: ReadonlyArray<AssembledTestCase>): Promise<boolean>
}
4 changes: 2 additions & 2 deletions src/runtime/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { SupportCodeLibrary } from '../support_code_library_builder/types'
import TestCaseRunner from './test_case_runner'
import { retriesForPickle, shouldCauseFailure } from './helpers'
import { makeRunTestRunHooks, RunsTestRunHooks } from './run_test_run_hooks'
import { IRuntimeOptions } from './index'
import { RuntimeOptions } from './index'

export class Worker {
#success: boolean = true
Expand All @@ -15,7 +15,7 @@ export class Worker {
private readonly workerId: string | undefined,
private readonly eventBroadcaster: EventEmitter,
private readonly newId: IdGenerator.NewId,
private readonly options: IRuntimeOptions,
private readonly options: RuntimeOptions,
private readonly supportCodeLibrary: SupportCodeLibrary
) {
this.runTestRunHooks = makeRunTestRunHooks(
Expand Down
Loading

0 comments on commit 27df645

Please sign in to comment.