Skip to content

Commit

Permalink
fix: Escape testFailed service message parameters only once (#298)
Browse files Browse the repository at this point in the history
* fix: do not escape testFailed parameters

* fix: do not escape test name

as it is passed as message parameters to the escape function as well

* test: add test suite on escaping messages
  • Loading branch information
loiemilio authored Aug 2, 2024
1 parent 54d2922 commit 7e2504b
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 7 deletions.
13 changes: 6 additions & 7 deletions src/app/messages/test-message.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
import { type Test, type ErrorWithDiff } from 'vitest'
import { escape } from '../escape'
import { Message, type Parameters } from './message'

export class TestMessage extends Message {
constructor(test: Test) {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
super(test.file!.id, escape(test.name))
super(test.file!.id, test.name)
}

protected generate(type: string, parameters: Parameters = {}): string {
return this.generateTeamcityMessage(type, this.id, { ...parameters, name: this.name })
}

fail = (error: ErrorWithDiff | undefined): string => {
fail = (error: ErrorWithDiff): string => {
return this.generate('testFailed', {
message: escape(error?.message ?? ''),
details: escape(error?.stackStr ?? ''),
actual: escape(error?.actual as string ?? ''),
expected: escape(error?.expected as string ?? ''),
message: error.message ?? '',
details: error.stackStr ?? '',
actual: (error.actual as string) ?? '',
expected: (error.expected as string) ?? '',
})
}

Expand Down
66 changes: 66 additions & 0 deletions src/test/escape.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { describe, it, expect, type Test } from 'vitest'
import { escape } from '../app/escape'
import { SuitMessage } from '../app/messages/suite-message'
import { TestMessage } from '../app/messages/test-message'

const escapeMap = {
'\x1B1m': '',
'|': '||',
'\n': '|n',
'\r': '|r',
'[': '|[',
']': '|]',
'\u0085': '|x',
'\u2028': '|l',
'\u2029': '|p',
"'": "|'",
}

const testNumber = Math.random()
const expectedNumber = testNumber.toString()

const testString = Object.keys(escapeMap).join('')
const expectedString = Object.values(escapeMap).join('')

describe('Checking message escaping functionality', () => {
it('escaping of simple string or number is correct', () => {
const escapedString = escape(testString)
expect(escapedString).toStrictEqual(expectedString)

const escapedNumber = escape(testNumber)
expect(escapedNumber).toStrictEqual(expectedNumber)
})

it('suite message parameters are correctly escaped', () => {
const messageId = 'messageId'
const messageName = 'messageName'
const messageType = 'messageType'

const message = new SuitMessage(messageId, messageName)

const escapedParameters = message.generate(messageType, {
testString,
testNumber,
})

expect(escapedParameters).toStrictEqual(
`##teamcity[${messageType} flowId='${messageId}' testString='${expectedString}' testNumber='${expectedNumber}' name='${messageName}']`
)
})

it('test name is correctly escaped', () => {
const testName = testString
const fileId = 'fileId'

const test = new TestMessage({
name: testName,
file: {
id: fileId,
},
type: 'test',
} as unknown as Test)

const escapedMessage = test.started()
expect(escapedMessage).toStrictEqual(`##teamcity[testStarted flowId='${fileId}' name='${expectedString}']`)
})
})

0 comments on commit 7e2504b

Please sign in to comment.