Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

alternative stack trace approach #2119

Merged
merged 16 commits into from
Nov 12, 2022
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 36 additions & 13 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,6 @@
"node": "12 || 14 || 16 || 17 || 18"
},
"dependencies": {
"@cspotcode/source-map-support": "^0.8.0",
"@cucumber/ci-environment": "9.1.0",
"@cucumber/cucumber-expressions": "16.0.0",
"@cucumber/gherkin": "24.0.0",
Expand All @@ -212,6 +211,7 @@
"commander": "^9.0.0",
"duration": "^0.2.2",
"durations": "^3.4.2",
"error-stack-parser": "^2.1.4",
"figures": "^3.2.0",
"glob": "^7.1.6",
"has-ansi": "^4.0.1",
Expand All @@ -225,7 +225,6 @@
"progress": "^2.0.3",
"resolve-pkg": "^2.0.0",
"semver": "7.3.7",
"stack-chain": "^2.0.0",
"string-argv": "^0.3.1",
"strip-ansi": "6.0.1",
"supports-color": "^8.1.1",
Expand Down
38 changes: 38 additions & 0 deletions src/filter_stack_trace.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import path from 'path'
import { valueOrDefault } from './value_checker'
import { StackFrame } from 'error-stack-parser'

const projectRootPath = path.join(__dirname, '..')
const projectChildDirs = ['src', 'lib', 'node_modules']

export function isFileNameInCucumber(fileName: string): boolean {
return projectChildDirs.some((dir) =>
fileName.startsWith(path.join(projectRootPath, dir))
)
}

export function filterStackTrace(frames: StackFrame[]): StackFrame[] {
if (isErrorInCucumber(frames)) {
return frames
}
const index = frames.findIndex((x) => isFrameInCucumber(x))
if (index === -1) {
return frames
}
return frames.slice(0, index)
}

function isErrorInCucumber(frames: StackFrame[]): boolean {
const filteredFrames = frames.filter((x) => !isFrameInNode(x))
return filteredFrames.length > 0 && isFrameInCucumber(filteredFrames[0])
}

function isFrameInCucumber(frame: StackFrame): boolean {
const fileName = valueOrDefault(frame.getFileName(), '')
return isFileNameInCucumber(fileName)
}

function isFrameInNode(frame: StackFrame): boolean {
const fileName = valueOrDefault(frame.getFileName(), '')
return !fileName.includes(path.sep)
}
22 changes: 22 additions & 0 deletions src/runtime/format_error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { format } from 'assertion-error-formatter'
import errorStackParser from 'error-stack-parser'
import { filterStackTrace } from '../filter_stack_trace'

export function formatError(error: Error, filterStackTraces: boolean): string {
let filteredStack: string
if (filterStackTraces) {
try {
filteredStack = filterStackTrace(errorStackParser.parse(error))
.map((f) => f.source)
.join('\n')
} catch {
// if we weren't able to parse and filter, we'll settle for the original
}
}
return format(error, {
colorFns: {
errorStack: (stack: string) =>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm kind of abusing the assertion-error-formatter API here, which doesn't feel great. It could maybe do with a formatErrorStack option where you can provide a function that rewrites the stack as desired?

filteredStack ? `\n${filteredStack}` : stack,
},
})
}
10 changes: 1 addition & 9 deletions src/runtime/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import * as messages from '@cucumber/messages'
import { IdGenerator } from '@cucumber/messages'
import { EventEmitter } from 'events'
import { EventDataCollector } from '../formatter/helpers'
import StackTraceFilter from '../stack_trace_filter'
import { ISupportCodeLibrary } from '../support_code_library_builder/types'
import { assembleTestCases } from './assemble_test_cases'
import { retriesForPickle, shouldCauseFailure } from './helpers'
Expand Down Expand Up @@ -40,7 +39,6 @@ export default class Runtime implements IRuntime {
private readonly newId: IdGenerator.NewId
private readonly options: IRuntimeOptions
private readonly pickleIds: string[]
private readonly stackTraceFilter: StackTraceFilter
private readonly supportCodeLibrary: ISupportCodeLibrary
private success: boolean
private runTestRunHooks: RunsTestRunHooks
Expand All @@ -59,7 +57,6 @@ export default class Runtime implements IRuntime {
this.newId = newId
this.options = options
this.pickleIds = pickleIds
this.stackTraceFilter = new StackTraceFilter()
this.supportCodeLibrary = supportCodeLibrary
this.success = true
this.runTestRunHooks = makeRunTestRunHooks(
Expand All @@ -85,6 +82,7 @@ export default class Runtime implements IRuntime {
testCase,
retries,
skip,
filterStackTraces: this.options.filterStacktraces,
supportCodeLibrary: this.supportCodeLibrary,
worldParameters: this.options.worldParameters,
})
Expand All @@ -95,9 +93,6 @@ export default class Runtime implements IRuntime {
}

async start(): Promise<boolean> {
if (this.options.filterStacktraces) {
this.stackTraceFilter.filter()
}
const testRunStarted: messages.Envelope = {
testRunStarted: {
timestamp: this.stopwatch.timestamp(),
Expand Down Expand Up @@ -132,9 +127,6 @@ export default class Runtime implements IRuntime {
},
}
this.eventBroadcaster.emit('envelope', testRunFinished)
if (this.options.filterStacktraces) {
this.stackTraceFilter.unfilter()
}
return this.success
}
}
10 changes: 1 addition & 9 deletions src/runtime/parallel/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { IdGenerator } from '@cucumber/messages'
import { duration } from 'durations'
import { EventEmitter } from 'events'
import { pathToFileURL } from 'url'
import StackTraceFilter from '../../stack_trace_filter'
import supportCodeLibraryBuilder from '../../support_code_library_builder'
import { ISupportCodeLibrary } from '../../support_code_library_builder/types'
import { doesHaveValue } from '../../value_checker'
Expand Down Expand Up @@ -33,7 +32,6 @@ export default class Worker {
private filterStacktraces: boolean
private readonly newId: IdGenerator.NewId
private readonly sendMessage: IMessageSender
private readonly stackTraceFilter: StackTraceFilter
private supportCodeLibrary: ISupportCodeLibrary
private worldParameters: any
private runTestRunHooks: RunsTestRunHooks
Expand All @@ -55,7 +53,6 @@ export default class Worker {
this.exit = exit
this.sendMessage = sendMessage
this.eventBroadcaster = new EventEmitter()
this.stackTraceFilter = new StackTraceFilter()
this.eventBroadcaster.on('envelope', (envelope: messages.Envelope) => {
this.sendMessage({
jsonEnvelope: JSON.stringify(envelope),
Expand All @@ -81,9 +78,6 @@ export default class Worker {

this.worldParameters = options.worldParameters
this.filterStacktraces = filterStacktraces
if (this.filterStacktraces) {
this.stackTraceFilter.filter()
}
this.runTestRunHooks = makeRunTestRunHooks(
options.dryRun,
this.supportCodeLibrary.defaultTimeout,
Expand All @@ -102,9 +96,6 @@ export default class Worker {
this.supportCodeLibrary.afterTestRunHookDefinitions,
'an AfterAll'
)
if (this.filterStacktraces) {
this.stackTraceFilter.unfilter()
}
this.exit(0)
}

Expand Down Expand Up @@ -137,6 +128,7 @@ export default class Worker {
testCase,
retries,
skip,
filterStackTraces: this.filterStacktraces,
supportCodeLibrary: this.supportCodeLibrary,
worldParameters: this.worldParameters,
})
Expand Down
6 changes: 4 additions & 2 deletions src/runtime/step_runner.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
import Time from '../time'
import UserCodeRunner from '../user_code_runner'
import * as messages from '@cucumber/messages'
import { format } from 'assertion-error-formatter'
import { ITestCaseHookParameter } from '../support_code_library_builder/types'
import { IDefinition, IGetInvocationDataResponse } from '../models/definition'
import {
doesHaveValue,
doesNotHaveValue,
valueOrDefault,
} from '../value_checker'
import { formatError } from './format_error'

const { beginTiming, endTiming } = Time

export interface IRunOptions {
defaultTimeout: number
filterStackTraces: boolean
hookParameter: ITestCaseHookParameter
step: messages.PickleStep
stepDefinition: IDefinition
Expand All @@ -22,6 +23,7 @@ export interface IRunOptions {

export async function run({
defaultTimeout,
filterStackTraces,
hookParameter,
step,
stepDefinition,
Expand Down Expand Up @@ -68,7 +70,7 @@ export async function run({
} else if (result === 'pending') {
status = messages.TestStepResultStatus.PENDING
} else if (doesHaveValue(error)) {
message = format(error)
message = formatError(error, filterStackTraces)
status = messages.TestStepResultStatus.FAILED
} else {
status = messages.TestStepResultStatus.PASSED
Expand Down
Loading