Skip to content

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

fix(github): escape workflow command data and properties like @actions/core (#43473)

Yury Semikhatsky964eb9a97a29
diff --git a/packages/playwright/src/reporters/github.ts b/packages/playwright/src/reporters/github.ts
index e21fae59e6..1cd371c66e 100644
--- a/packages/playwright/src/reporters/github.ts
+++ b/packages/playwright/src/reporters/github.ts
@@ -44,12 +44,11 @@ class GitHubLogger {
}
private _log(message: string, type: GitHubLogType = 'notice', options: GitHubLogOptions = {}) {
- message = message.replace(/\n/g, '%0A');
const configs = Object.entries(options)
- .map(([key, option]) => `${key}=${option}`)
+ .map(([key, option]) => `${key}=${escapeProperty(String(option))}`)
.join(',');
// eslint-disable-next-line no-restricted-properties
- process.stdout.write(stripAnsiEscapes(`::${type} ${configs}::${message}\n`));
+ process.stdout.write(stripAnsiEscapes(`::${type} ${configs}::${escapeData(message)}\n`));
}
debug(message: string, options?: GitHubLogOptions) {
@@ -69,6 +68,15 @@ class GitHubLogger {
}
}
+// Same escaping as @actions/core, see https://github.com/actions/toolkit/blob/main/packages/core/src/command.ts.
+function escapeData(s: string): string {
+ return s.replace(/%/g, '%25').replace(/\r/g, '%0D').replace(/\n/g, '%0A');
+}
+
+function escapeProperty(s: string): string {
+ return escapeData(s).replace(/:/g, '%3A').replace(/,/g, '%2C');
+}
+
export class GitHubReporter extends TerminalReporter {
githubLogger = new GitHubLogger();
private _failedTestCount = 0;
diff --git a/tests/playwright-test/reporter-github.spec.ts b/tests/playwright-test/reporter-github.spec.ts
index 6ace7068a1..256ad890d5 100644
--- a/tests/playwright-test/reporter-github.spec.ts
+++ b/tests/playwright-test/reporter-github.spec.ts
@@ -42,20 +42,30 @@ for (const useIntermediateMergeReport of [false, true] as const) {
expect(result.exitCode).toBe(0);
});
- test('print GitHub annotations for failed tests', async ({ runInlineTest }, testInfo) => {
+ test('print GitHub annotations for failed tests', {
+ annotation: { type: 'issue', description: 'https://github.com/microsoft/playwright/issues/43461' },
+ }, async ({ runInlineTest }, testInfo) => {
const result = await runInlineTest({
'a.test.js': `
const { test, expect } = require('@playwright/test');
test('example', async ({}) => {
expect(1 + 1).toBe(3);
});
+ test('login, then 100% off', async ({}) => {
+ throw new Error('GET /a%0Ab failed\\r\\nagain');
+ });
`
}, { retries: 3, reporter: 'github' }, { GITHUB_WORKSPACE: process.cwd() });
const text = result.output;
const testPath = relativeFilePath(testInfo.outputPath('a.test.js'));
- expect(text).toContain(`::error file=${testPath},title=a.test.js:3:11 › example,line=4,col=27:: 1) a.test.js:3:11 › example ──────────────────────────────────────────────────────────────────────%0A%0A Retry #1`);
- expect(text).toContain(`::error file=${testPath},title=a.test.js:3:11 › example,line=4,col=27:: 1) a.test.js:3:11 › example ──────────────────────────────────────────────────────────────────────%0A%0A Retry #2`);
- expect(text).toContain(`::error file=${testPath},title=a.test.js:3:11 › example,line=4,col=27:: 1) a.test.js:3:11 › example ──────────────────────────────────────────────────────────────────────%0A%0A Retry #3`);
+ expect(text).toContain(`::error file=${testPath},title=a.test.js%3A3%3A11 › example,line=4,col=27:: 1) a.test.js:3:11 › example ──────────────────────────────────────────────────────────────────────%0A%0A Retry #1`);
+ expect(text).toContain(`::error file=${testPath},title=a.test.js%3A3%3A11 › example,line=4,col=27:: 1) a.test.js:3:11 › example ──────────────────────────────────────────────────────────────────────%0A%0A Retry #2`);
+ expect(text).toContain(`::error file=${testPath},title=a.test.js%3A3%3A11 › example,line=4,col=27:: 1) a.test.js:3:11 › example ──────────────────────────────────────────────────────────────────────%0A%0A Retry #3`);
+ // Commas, percent signs and line breaks are escaped the way the GitHub Actions runner unescapes them.
+ // The column of a thrown error differs between Node and Bun, so do not pin it.
+ expect(text).toContain(`::error file=${testPath},title=a.test.js%3A6%3A11 › login%2C then 100%25 off,line=7,col=`);
+ expect(text).toContain(`:: 2) a.test.js:6:11 › login, then 100%25 off `);
+ expect(text).toContain(`Error: GET /a%250Ab failed%0D%0A again`);
expect(result.exitCode).toBe(1);
});