|
1 |
| -const JsonFormatter = require('./json_formatter'); |
2 |
| -const fs = require('fs'); |
3 |
| -const path = require('path'); |
4 |
| -class HTMLFormatter extends JsonFormatter { |
| 1 | +const { Formatter } = require('@cucumber/cucumber'); |
| 2 | +const { write, readFileSync } = require('node:fs'); |
| 3 | +const path = require('node:path'); |
| 4 | + |
| 5 | +class HTMLStream { |
| 6 | + |
| 7 | + constructor(fd, ender) { |
| 8 | + this.fd = fd; |
| 9 | + this.position = 0; |
| 10 | + this.ender = Buffer.from(ender); |
| 11 | + } |
| 12 | + |
| 13 | + write(data) { |
| 14 | + const dataBuffer = Buffer.from(data); |
| 15 | + const buffer = Buffer.concat([dataBuffer, this.ender]); |
| 16 | + write(this.fd, buffer, 0, buffer.length, this.position, () => {}); |
| 17 | + this.position += dataBuffer.length; |
| 18 | + } |
| 19 | + |
| 20 | +} |
| 21 | + |
| 22 | +class HTMLFormatter extends Formatter { |
| 23 | + |
| 24 | + hooks = {}; |
5 | 25 |
|
6 | 26 | constructor(options) {
|
7 | 27 | super(options);
|
8 |
| - this.metadata = options.parsedArgvOptions.htmlConfig?.metadata ?? {}; |
9 |
| - const log = this.log.bind(this); |
10 |
| - this.log = function(json) { |
11 |
| - const htmlTemplate = fs.readFileSync(path.resolve(__dirname, './index.html'), 'utf-8'); |
12 |
| - log(htmlTemplate |
13 |
| - .replace('METADATA', () => JSON.stringify(this.metadata)) |
14 |
| - .replace('SOURCE_DATA', () => JSON.stringify(JSON.parse(json)))); |
| 28 | + const metadata = JSON.stringify(options.parsedArgvOptions.htmlConfig?.metadata ?? {}); |
| 29 | + const htmlTemplate = readFileSync(path.resolve(__dirname, './index.html'), 'utf-8') |
| 30 | + .replace('METADATA', metadata); |
| 31 | + const [left, right] = htmlTemplate.split('SOURCE_DATA'); |
| 32 | + this.htmlStream = new HTMLStream(this.stream.fd, right); |
| 33 | + this.htmlStream.write(left); |
| 34 | + options.eventBroadcaster.on('envelope', this.processEnvelope.bind(this)); |
| 35 | + } |
| 36 | + |
| 37 | + processEnvelope(envelope) { |
| 38 | + if (envelope.testCaseFinished) { |
| 39 | + return this.finishTest(envelope); |
| 40 | + } |
| 41 | + if (envelope.hook) { |
| 42 | + return this.hooks[envelope.hook.id] = envelope.hook; |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + finishTest(envelope) { |
| 47 | + if (envelope.testCaseFinished.willBeRetried) return; |
| 48 | + const testCase = this.eventDataCollector.getTestCaseAttempt(envelope.testCaseFinished.testCaseStartedId); |
| 49 | + const feature = { |
| 50 | + description: testCase.gherkinDocument.feature.description, |
| 51 | + id: 'feature' + testCase.pickle.id, |
| 52 | + line: testCase.gherkinDocument.feature.location.line, |
| 53 | + keyword: testCase.gherkinDocument.feature.keyword, |
| 54 | + name: testCase.gherkinDocument.feature.name, |
| 55 | + uri: testCase.gherkinDocument.uri, |
| 56 | + tags: this.formatTags(testCase.gherkinDocument.feature.tags) |
| 57 | + }; |
| 58 | + |
| 59 | + const steps = testCase.testCase.testSteps; |
| 60 | + for (const step of steps) { |
| 61 | + const pickle = testCase.pickle.steps.find(pickle => step.pickleStepId === pickle.id); |
| 62 | + step.name = pickle ? pickle.text : this.hookText(steps, step); |
| 63 | + step.arguments = pickle ? [{ ...(pickle.argument?.dataTable ?? pickle.argument?.docString) }] : undefined; |
| 64 | + const result = testCase.stepResults[step.id]; |
| 65 | + step.result = { |
| 66 | + status: result.status.toLowerCase(), |
| 67 | + duration: result.duration.seconds * 1_000_000_000 + result.duration.nanos, |
| 68 | + error_message: result.message |
| 69 | + }; |
| 70 | + step.embeddings = testCase.stepAttachments[step.id]?.map(attachment => ({ |
| 71 | + ...attachment, |
| 72 | + data: attachment.body, |
| 73 | + mime_type: attachment.mediaType |
| 74 | + })); |
15 | 75 | }
|
| 76 | + const scenario = { |
| 77 | + feature, |
| 78 | + steps, |
| 79 | + name: testCase.pickle.name, |
| 80 | + id: testCase.pickle.id, |
| 81 | + keyword: 'Scenario', |
| 82 | + tags: this.formatTags(testCase.pickle.tags), |
| 83 | + type: 'scenario' |
| 84 | + }; |
| 85 | + this.htmlStream.write(JSON.stringify(scenario) + ','); |
| 86 | + } |
| 87 | + |
| 88 | + formatTags(tags) { |
| 89 | + return tags.map(tag => ({ name: tag.name, line: tag.location?.line })); |
| 90 | + } |
| 91 | + |
| 92 | + hookText(steps, step) { |
| 93 | + const hook = this.hooks[step.hookId]; |
| 94 | + if (hook?.name) return hook.name; |
| 95 | + const stepsBefore = steps.slice(0, steps.findIndex((element) => element === step)); |
| 96 | + return stepsBefore.every(element => element.pickleStepId === undefined) ? 'Before' : 'After'; |
16 | 97 | }
|
17 | 98 |
|
18 | 99 | }
|
|
0 commit comments