Skip to content

Commit bf91cbb

Browse files
committed
empty ProgressPrinter impl and test
1 parent 561e454 commit bf91cbb

File tree

2 files changed

+132
-0
lines changed

2 files changed

+132
-0
lines changed
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
import fs from 'node:fs'
2+
import * as path from 'node:path'
3+
import { Writable } from 'node:stream'
4+
import { pipeline } from 'node:stream/promises'
5+
6+
import { NdjsonToMessageStream } from '@cucumber/message-streams'
7+
import { Envelope, TestStepResultStatus } from '@cucumber/messages'
8+
import { expect } from 'chai'
9+
import { globbySync } from 'globby'
10+
11+
import { ProgressPrinter } from './ProgressPrinter.js'
12+
import { CUCUMBER_THEME } from './theme.js'
13+
import type { Options } from './types.js'
14+
15+
describe('ProgressPrinter', async () => {
16+
const ndjsonFiles = globbySync(`*.ndjson`, {
17+
cwd: path.join(import.meta.dirname, '..', '..', 'testdata', 'src'),
18+
absolute: true,
19+
})
20+
21+
const variants: ReadonlyArray<{ name: string; options: Options }> = [
22+
{
23+
name: 'cucumber',
24+
options: {
25+
attachments: true,
26+
featuresAndRules: true,
27+
theme: CUCUMBER_THEME,
28+
},
29+
},
30+
{
31+
name: 'plain',
32+
options: {
33+
attachments: true,
34+
featuresAndRules: true,
35+
theme: {
36+
status: {
37+
icon: {
38+
[TestStepResultStatus.AMBIGUOUS]: '✘',
39+
[TestStepResultStatus.FAILED]: '✘',
40+
[TestStepResultStatus.PASSED]: '✔',
41+
[TestStepResultStatus.PENDING]: '■',
42+
[TestStepResultStatus.SKIPPED]: '↷',
43+
[TestStepResultStatus.UNDEFINED]: '■',
44+
[TestStepResultStatus.UNKNOWN]: ' ',
45+
},
46+
},
47+
},
48+
},
49+
},
50+
]
51+
52+
// just enough so Node.js internals consider it a color-supporting stream
53+
const fakeStream = {
54+
_writableState: {},
55+
isTTY: true,
56+
getColorDepth: () => 3,
57+
} as unknown as NodeJS.WritableStream
58+
59+
for (const { name, options } of variants) {
60+
describe(name, () => {
61+
for (const ndjsonFile of ndjsonFiles) {
62+
const [suiteName] = path.basename(ndjsonFile).split('.')
63+
64+
it(suiteName, async () => {
65+
let content = ''
66+
const printer = new ProgressPrinter(
67+
fakeStream,
68+
(chunk) => {
69+
content += chunk
70+
},
71+
{
72+
attachments: true,
73+
featuresAndRules: true,
74+
theme: CUCUMBER_THEME,
75+
...options,
76+
}
77+
)
78+
79+
await pipeline(
80+
fs.createReadStream(ndjsonFile, { encoding: 'utf-8' }),
81+
new NdjsonToMessageStream(),
82+
new Writable({
83+
objectMode: true,
84+
write(envelope: Envelope, _: BufferEncoding, callback) {
85+
printer.update(envelope)
86+
callback()
87+
},
88+
})
89+
)
90+
91+
const expectedOutput = fs.readFileSync(
92+
ndjsonFile.replace('.ndjson', `.${name}.progress.log`),
93+
{
94+
encoding: 'utf-8',
95+
}
96+
)
97+
98+
expect(content).to.eq(expectedOutput)
99+
})
100+
}
101+
})
102+
}
103+
})

javascript/src/ProgressPrinter.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { Envelope, TestRunFinished } from '@cucumber/messages'
2+
import { Query } from '@cucumber/query'
3+
4+
import type { Options } from './types.js'
5+
6+
export class ProgressPrinter {
7+
private readonly println: (content?: string) => void
8+
private readonly query: Query = new Query()
9+
10+
constructor(
11+
private readonly stream: NodeJS.WritableStream,
12+
private readonly print: (content: string) => void,
13+
private readonly options: Required<Options>
14+
) {
15+
this.println = (content: string = '') => this.print(`${content}\n`)
16+
}
17+
18+
update(message: Envelope) {
19+
this.query.update(message)
20+
21+
if (message.testRunFinished) {
22+
this.handleTestRunFinished(message.testRunFinished)
23+
}
24+
}
25+
26+
private handleTestRunFinished(testRunFinished: TestRunFinished) {
27+
this.println('Progress!')
28+
}
29+
}

0 commit comments

Comments
 (0)