Skip to content

Commit a9dc37a

Browse files
authored
Revert "Deterministic usage formatter (#2633)" (#2688)
The original motivation for this was to easier create deterministic e2e tests for involving the usage formatter. However, a cuople of strategic waits was much simpler than I thought it would be and it's sufficiently deterministic [1]. Thus making the original PR superfluous. This reverts commit 1ec78f0. [1] badeball/cypress-cucumber-preprocessor@3c12732
1 parent 499bb43 commit a9dc37a

File tree

14 files changed

+58
-214
lines changed

14 files changed

+58
-214
lines changed

exports/root/report.api.md

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,6 @@ export class Formatter {
120120
protected stream: Writable;
121121
// (undocumented)
122122
protected supportCodeLibrary: SupportCodeLibrary;
123-
// (undocumented)
124-
protected usageOrder: UsageOrder;
125123
}
126124

127125
// @public (undocumented)
@@ -147,7 +145,6 @@ declare namespace formatterHelpers {
147145
formatLocation,
148146
formatSummary,
149147
getUsage,
150-
UsageOrder,
151148
GherkinDocumentParser,
152149
PickleParser
153150
}
@@ -182,7 +179,7 @@ function getStepKeyword({ pickleStep, gherkinStepMap, }: IGetStepKeywordRequest)
182179
function getStepKeywordType({ keyword, language, previousKeywordType, }: IGetStepKeywordTypeOptions): KeywordType;
183180

184181
// @public (undocumented)
185-
function getUsage({ stepDefinitions, eventDataCollector, order, }: IGetUsageRequest): IUsage[];
182+
function getUsage({ stepDefinitions, eventDataCollector, }: IGetUsageRequest): IUsage[];
186183

187184
declare namespace GherkinDocumentParser {
188185
export {
@@ -509,14 +506,6 @@ export class UsageJsonFormatter extends Formatter {
509506
replacer(key: string, value: any): any;
510507
}
511508

512-
// @public (undocumented)
513-
enum UsageOrder {
514-
// (undocumented)
515-
EXECUTION_TIME = "EXECUTION_TIME",
516-
// (undocumented)
517-
LOCATION = "LOCATION"
518-
}
519-
520509
// @public (undocumented)
521510
export const version: string;
522511

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@
7979
"John McLaughlin <john.mjhm@gmail.com>",
8080
"John Wright <johngeorge.wright@gmail.com>",
8181
"Johny Jose <johny@playlyfe.com>",
82-
"Jonas Amundsen (https://github.com/badeball)",
8382
"Jonathan Gomez <jonathanbgomez@gmail.com>",
8483
"Jonathan Kim <jkimbo@gmail.com>",
8584
"Josh Chisholm <joshuachisholm@gmail.com>",

src/formatter/helpers/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ export { KeywordType, getStepKeywordType } from './keyword_type'
77
export { formatIssue, isWarning, isFailure, isIssue } from './issue_helpers'
88
export { formatLocation } from './location_helpers'
99
export { formatSummary } from './summary_helpers'
10-
export { getUsage, UsageOrder } from './usage_helpers'
10+
export { getUsage } from './usage_helpers'
1111
export { GherkinDocumentParser, PickleParser }

src/formatter/helpers/usage_helpers/index.ts

Lines changed: 8 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,9 @@ export interface IUsage {
2222
uri: string
2323
}
2424

25-
export enum UsageOrder {
26-
EXECUTION_TIME = 'EXECUTION_TIME',
27-
LOCATION = 'LOCATION',
28-
}
29-
3025
export interface IGetUsageRequest {
3126
eventDataCollector: EventDataCollector
3227
stepDefinitions: StepDefinition[]
33-
order?: UsageOrder
3428
}
3529

3630
function buildEmptyMapping(
@@ -59,7 +53,7 @@ const unexecutedStatuses: readonly messages.TestStepResultStatus[] = [
5953
function buildMapping({
6054
stepDefinitions,
6155
eventDataCollector,
62-
}: Omit<IGetUsageRequest, 'order'>): Record<string, IUsage> {
56+
}: IGetUsageRequest): Record<string, IUsage> {
6357
const mapping = buildEmptyMapping(stepDefinitions)
6458
eventDataCollector.getTestCaseAttempts().forEach((testCaseAttempt) => {
6559
const pickleStepMap = getPickleStepMap(testCaseAttempt.pickle)
@@ -97,22 +91,15 @@ function normalizeDuration(duration?: messages.Duration): number {
9791
return messages.TimeConversion.durationToMilliseconds(duration)
9892
}
9993

100-
function buildResult(
101-
mapping: Record<string, IUsage>,
102-
order: UsageOrder
103-
): IUsage[] {
94+
function buildResult(mapping: Record<string, IUsage>): IUsage[] {
10495
return Object.keys(mapping)
10596
.map((stepDefinitionId) => {
10697
const { matches, ...rest } = mapping[stepDefinitionId]
10798
const sortedMatches = matches.sort((a: IUsageMatch, b: IUsageMatch) => {
10899
if (a.duration === b.duration) {
109100
return a.text < b.text ? -1 : 1
110101
}
111-
if (order === UsageOrder.EXECUTION_TIME) {
112-
return normalizeDuration(b.duration) - normalizeDuration(a.duration)
113-
} else {
114-
return a.text.localeCompare(b.text)
115-
}
102+
return normalizeDuration(b.duration) - normalizeDuration(a.duration)
116103
})
117104
const result = { matches: sortedMatches, ...rest }
118105
const durations: messages.Duration[] = matches
@@ -129,22 +116,16 @@ function buildResult(
129116
}
130117
return result
131118
})
132-
.sort((a: IUsage, b: IUsage) => {
133-
if (order === UsageOrder.EXECUTION_TIME) {
134-
return (
135-
normalizeDuration(b.meanDuration) - normalizeDuration(a.meanDuration)
136-
)
137-
} else {
138-
return a.uri.localeCompare(b.uri)
139-
}
140-
})
119+
.sort(
120+
(a: IUsage, b: IUsage) =>
121+
normalizeDuration(b.meanDuration) - normalizeDuration(a.meanDuration)
122+
)
141123
}
142124

143125
export function getUsage({
144126
stepDefinitions,
145127
eventDataCollector,
146-
order = UsageOrder.EXECUTION_TIME,
147128
}: IGetUsageRequest): IUsage[] {
148129
const mapping = buildMapping({ stepDefinitions, eventDataCollector })
149-
return buildResult(mapping, order)
130+
return buildResult(mapping)
150131
}

src/formatter/helpers/usage_helpers/index_spec.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { describe, it } from 'mocha'
22
import { expect } from 'chai'
33
import { getEnvelopesAndEventDataCollector } from '../../../../test/formatter_helpers'
44
import { buildSupportCodeLibrary } from '../../../../test/runtime_helpers'
5-
import { getUsage, UsageOrder } from './'
5+
import { getUsage } from './'
66

77
describe('Usage Helpers', () => {
88
describe('getUsage', () => {
@@ -23,7 +23,6 @@ describe('Usage Helpers', () => {
2323
const output = getUsage({
2424
eventDataCollector,
2525
stepDefinitions: supportCodeLibrary.stepDefinitions,
26-
order: UsageOrder.EXECUTION_TIME,
2726
})
2827

2928
// Assert
@@ -56,7 +55,6 @@ describe('Usage Helpers', () => {
5655
const output = getUsage({
5756
eventDataCollector,
5857
stepDefinitions: supportCodeLibrary.stepDefinitions,
59-
order: UsageOrder.EXECUTION_TIME,
6058
})
6159

6260
// Assert

src/formatter/index.ts

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { SupportCodeLibrary } from '../support_code_library_builder/types'
44
import { valueOrDefault } from '../value_checker'
55
import { FormatterPlugin } from '../plugin'
66
import { IColorFns } from './get_color_fns'
7-
import { EventDataCollector, UsageOrder } from './helpers'
7+
import { EventDataCollector } from './helpers'
88
import StepDefinitionSnippetBuilder from './step_definition_snippet_builder'
99
import { SnippetInterface } from './step_definition_snippet_builder/snippet_syntax'
1010

@@ -17,9 +17,6 @@ export interface FormatOptions {
1717
html?: {
1818
externalAttachments?: boolean
1919
}
20-
usage?: {
21-
order?: UsageOrder
22-
}
2320
rerun?: FormatRerunOptions
2421
snippetInterface?: SnippetInterface
2522
snippetSyntax?: string
@@ -54,7 +51,6 @@ export default class Formatter {
5451
protected stream: Writable
5552
protected supportCodeLibrary: SupportCodeLibrary
5653
protected printAttachments: boolean
57-
protected usageOrder: UsageOrder
5854
private readonly cleanup: IFormatterCleanupFn
5955
static readonly documentation: string
6056

@@ -71,10 +67,6 @@ export default class Formatter {
7167
options.parsedArgvOptions.printAttachments,
7268
true
7369
)
74-
this.usageOrder = valueOrDefault(
75-
options.parsedArgvOptions.usage?.order,
76-
UsageOrder.EXECUTION_TIME
77-
)
7870
}
7971

8072
async finished(): Promise<void> {

src/formatter/usage_formatter.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ export default class UsageFormatter extends Formatter {
2222
const usage = getUsage({
2323
stepDefinitions: this.supportCodeLibrary.stepDefinitions,
2424
eventDataCollector: this.eventDataCollector,
25-
order: this.usageOrder,
2625
})
2726
if (usage.length === 0) {
2827
this.log('No step definitions')

src/formatter/usage_formatter_spec.ts

Lines changed: 14 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,8 @@ import { expect } from 'chai'
33
import FakeTimers, { InstalledClock } from '@sinonjs/fake-timers'
44
import { reindent } from 'reindent-template-literals'
55
import timeMethods from '../time'
6-
import {
7-
getBasicUsageSupportCodeLibrary,
8-
getOrderedUsageSupportCodeLibrary,
9-
} from '../../test/fixtures/usage/usage_steps'
6+
import { getUsageSupportCodeLibrary } from '../../test/fixtures/usage_steps'
107
import { testFormatter } from '../../test/formatter_helpers'
11-
import { UsageOrder } from './helpers'
128

139
describe('UsageFormatter', () => {
1410
let clock: InstalledClock
@@ -37,7 +33,7 @@ describe('UsageFormatter', () => {
3733
describe('unused', () => {
3834
it('outputs the step definitions as unused', async () => {
3935
// Arrange
40-
const supportCodeLibrary = getBasicUsageSupportCodeLibrary(clock)
36+
const supportCodeLibrary = getUsageSupportCodeLibrary(clock)
4137

4238
// Act
4339
const output = await testFormatter({
@@ -51,11 +47,11 @@ describe('UsageFormatter', () => {
5147
┌────────────────┬──────────┬───────────────────┐
5248
│ Pattern / Text │ Duration │ Location │
5349
├────────────────┼──────────┼───────────────────┤
54-
│ abc │ UNUSED │ usage_steps.ts:13
50+
│ abc │ UNUSED │ usage_steps.ts:11
5551
├────────────────┼──────────┼───────────────────┤
56-
│ /def?/ │ UNUSED │ usage_steps.ts:18
52+
│ /def?/ │ UNUSED │ usage_steps.ts:16
5753
├────────────────┼──────────┼───────────────────┤
58-
│ ghi │ UNUSED │ usage_steps.ts:27
54+
│ ghi │ UNUSED │ usage_steps.ts:25
5955
└────────────────┴──────────┴───────────────────┘
6056
6157
`)
@@ -74,7 +70,7 @@ describe('UsageFormatter', () => {
7470
uri: 'a.feature',
7571
},
7672
]
77-
const supportCodeLibrary = getBasicUsageSupportCodeLibrary(clock)
73+
const supportCodeLibrary = getUsageSupportCodeLibrary(clock)
7874

7975
// Act
8076
const output = await testFormatter({
@@ -90,13 +86,13 @@ describe('UsageFormatter', () => {
9086
┌────────────────┬──────────┬───────────────────┐
9187
│ Pattern / Text │ Duration │ Location │
9288
├────────────────┼──────────┼───────────────────┤
93-
│ abc │ UNUSED │ usage_steps.ts:13
89+
│ abc │ UNUSED │ usage_steps.ts:11
9490
├────────────────┼──────────┼───────────────────┤
95-
│ /def?/ │ - │ usage_steps.ts:18
91+
│ /def?/ │ - │ usage_steps.ts:16
9692
│ de │ - │ a.feature:4 │
9793
│ def │ - │ a.feature:3 │
9894
├────────────────┼──────────┼───────────────────┤
99-
│ ghi │ UNUSED │ usage_steps.ts:27
95+
│ ghi │ UNUSED │ usage_steps.ts:25
10096
└────────────────┴──────────┴───────────────────┘
10197
10298
`)
@@ -105,15 +101,15 @@ describe('UsageFormatter', () => {
105101
})
106102

107103
describe('not in dry run', () => {
108-
it('outputs the step definition with durations', async () => {
104+
it('outputs the step definition without durations', async () => {
109105
// Arrange
110106
const sources = [
111107
{
112108
data: 'Feature: a\nScenario: b\nWhen def\nThen de',
113109
uri: 'a.feature',
114110
},
115111
]
116-
const supportCodeLibrary = getBasicUsageSupportCodeLibrary(clock)
112+
const supportCodeLibrary = getUsageSupportCodeLibrary(clock)
117113

118114
// Act
119115
const output = await testFormatter({
@@ -128,91 +124,19 @@ describe('UsageFormatter', () => {
128124
┌────────────────┬──────────┬───────────────────┐
129125
│ Pattern / Text │ Duration │ Location │
130126
├────────────────┼──────────┼───────────────────┤
131-
│ /def?/ │ 1.50ms │ usage_steps.ts:18
127+
│ /def?/ │ 1.50ms │ usage_steps.ts:16
132128
│ def │ 2.00ms │ a.feature:3 │
133129
│ de │ 1.00ms │ a.feature:4 │
134130
├────────────────┼──────────┼───────────────────┤
135-
│ abc │ UNUSED │ usage_steps.ts:13
131+
│ abc │ UNUSED │ usage_steps.ts:11
136132
├────────────────┼──────────┼───────────────────┤
137-
│ ghi │ UNUSED │ usage_steps.ts:27
133+
│ ghi │ UNUSED │ usage_steps.ts:25
138134
└────────────────┴──────────┴───────────────────┘
139135
140136
`)
141137
)
142138
})
143139
})
144-
145-
describe('sorting', () => {
146-
const sources = [
147-
{
148-
data: 'Feature: a\nScenario: a\nGiven foo\nThen bar',
149-
uri: 'a.feature',
150-
},
151-
{
152-
data: 'Feature: b\nScenario: b\nGiven foo\nThen bar',
153-
uri: 'b.feature',
154-
},
155-
]
156-
157-
it('defaults to order by execution time, decreasingly', async () => {
158-
const supportCodeLibrary = getOrderedUsageSupportCodeLibrary(clock)
159-
160-
// Act
161-
const output = await testFormatter({
162-
sources,
163-
supportCodeLibrary,
164-
type: 'usage',
165-
})
166-
167-
// Assert
168-
expect(output).to.eql(
169-
reindent(`
170-
┌────────────────┬──────────┬─────────────────┐
171-
│ Pattern / Text │ Duration │ Location │
172-
├────────────────┼──────────┼─────────────────┤
173-
│ foo │ 15.00ms │ foo_steps.ts:10 │
174-
│ foo │ 20.00ms │ b.feature:3 │
175-
│ foo │ 10.00ms │ a.feature:3 │
176-
├────────────────┼──────────┼─────────────────┤
177-
│ bar │ 3.00ms │ bar_steps.ts:10 │
178-
│ bar │ 4.00ms │ b.feature:4 │
179-
│ bar │ 2.00ms │ a.feature:4 │
180-
└────────────────┴──────────┴─────────────────┘
181-
182-
`)
183-
)
184-
})
185-
186-
it('can optionally order by location', async () => {
187-
const supportCodeLibrary = getOrderedUsageSupportCodeLibrary(clock)
188-
189-
// Act
190-
const output = await testFormatter({
191-
sources,
192-
supportCodeLibrary,
193-
type: 'usage',
194-
parsedArgvOptions: { usage: { order: UsageOrder.LOCATION } },
195-
})
196-
197-
// Assert
198-
expect(output).to.eql(
199-
reindent(`
200-
┌────────────────┬──────────┬─────────────────┐
201-
│ Pattern / Text │ Duration │ Location │
202-
├────────────────┼──────────┼─────────────────┤
203-
│ bar │ 3.00ms │ bar_steps.ts:10 │
204-
│ bar │ 2.00ms │ a.feature:4 │
205-
│ bar │ 4.00ms │ b.feature:4 │
206-
├────────────────┼──────────┼─────────────────┤
207-
│ foo │ 15.00ms │ foo_steps.ts:10 │
208-
│ foo │ 10.00ms │ a.feature:3 │
209-
│ foo │ 20.00ms │ b.feature:3 │
210-
└────────────────┴──────────┴─────────────────┘
211-
212-
`)
213-
)
214-
})
215-
})
216140
})
217141
})
218142
})

src/formatter/usage_json_formatter.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ export default class UsageJsonFormatter extends Formatter {
2121
const usage = getUsage({
2222
stepDefinitions: this.supportCodeLibrary.stepDefinitions,
2323
eventDataCollector: this.eventDataCollector,
24-
order: this.usageOrder,
2524
})
2625
this.log(JSON.stringify(usage, this.replacer, 2))
2726
}

0 commit comments

Comments
 (0)