Skip to content

Commit cf5900f

Browse files
cherry-pick(#24213): Revert "fix: do not collide with other tests when test names have special chars (#23414)" (#24217)
This PR cherry-picks the following commits: - 57cca1d Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>
1 parent 74ec8c2 commit cf5900f

File tree

6 files changed

+23
-88
lines changed

6 files changed

+23
-88
lines changed

packages/playwright-test/src/matchers/toMatchSnapshot.ts

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import type { ImageComparatorOptions, Comparator } from 'playwright-core/lib/uti
2222
import { getComparator } from 'playwright-core/lib/utils';
2323
import type { PageScreenshotOptions } from 'playwright-core/types/types';
2424
import {
25-
serializeError, sanitizeForFilePath,
25+
addSuffixToFilePath, serializeError, sanitizeForFilePath,
2626
trimLongString, callLogText,
2727
expectTypes } from '../util';
2828
import { colors } from 'playwright-core/lib/utilsBundle';
@@ -440,14 +440,3 @@ function determineFileExtension(file: string | Buffer): string {
440440
function compareMagicBytes(file: Buffer, magicBytes: number[]): boolean {
441441
return Buffer.compare(Buffer.from(magicBytes), file.slice(0, magicBytes.length)) === 0;
442442
}
443-
444-
function addSuffixToFilePath(filePath: string, suffix: string, customExtension?: string, sanitize = false): string {
445-
const dirname = path.dirname(filePath);
446-
const ext = path.extname(filePath);
447-
const name = path.basename(filePath, ext);
448-
const base = path.join(dirname, name);
449-
const sanitizeForSnapshotFilePath = (s: string) => {
450-
return s.replace(/[\x00-\x2C\x2E-\x2F\x3A-\x40\x5B-\x60\x7B-\x7F]+/g, '-');
451-
};
452-
return (sanitize ? sanitizeForSnapshotFilePath(base) : base) + suffix + (customExtension || ext);
453-
}

packages/playwright-test/src/util.ts

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -195,18 +195,8 @@ export function expectTypes(receiver: any, types: string[], matcherName: string)
195195
}
196196
}
197197

198-
export function sanitizeForFilePath(input: string) {
199-
let nonTrivialSubstitute = false;
200-
let sanitized = input.replace(/[\x00-\x2C\x2E-\x2F\x3A-\x40\x5B-\x60\x7B-\x7F\x2A\-\*]+/g, substring => {
201-
if (substring !== ' ')
202-
nonTrivialSubstitute = true;
203-
return '-';
204-
});
205-
if (!nonTrivialSubstitute)
206-
return sanitized;
207-
// If we sanitized the beginning or end, remove it for cosmetic reasons.
208-
sanitized = sanitized.replace(/^-/, '').replace(/-$/, '');
209-
return sanitized + '-' + calculateSha1(input).substring(0, 6);
198+
export function sanitizeForFilePath(s: string) {
199+
return s.replace(/[\x00-\x2C\x2E-\x2F\x3A-\x40\x5B-\x60\x7B-\x7F]+/g, '-');
210200
}
211201

212202
export function trimLongString(s: string, length = 100) {
@@ -219,6 +209,14 @@ export function trimLongString(s: string, length = 100) {
219209
return s.substring(0, start) + middle + s.slice(-end);
220210
}
221211

212+
export function addSuffixToFilePath(filePath: string, suffix: string, customExtension?: string, sanitize = false): string {
213+
const dirname = path.dirname(filePath);
214+
const ext = path.extname(filePath);
215+
const name = path.basename(filePath, ext);
216+
const base = path.join(dirname, name);
217+
return (sanitize ? sanitizeForFilePath(base) : base) + suffix + (customExtension || ext);
218+
}
219+
222220
/**
223221
* Returns absolute path contained within parent directory.
224222
*/

tests/playwright-test/playwright.artifacts.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ const testFiles = {
5454
await page.click('text=Click me');
5555
});
5656
57-
test('shared failing', async ({ }) => {
57+
test('shared failing', async ({ }) => {
5858
await page.click('text=And me');
5959
expect(1).toBe(2);
6060
});

tests/playwright-test/reporter-attachment.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ test(`testInfo.attach name should be sanitized`, async ({ runInlineTest }) => {
235235
expect(result.failed).toBe(1);
236236

237237
expect(result.output).toContain('attachment #1: ../../../test (text/plain)');
238-
expect(result.output).toContain(`attachments${path.sep}test-8d909b-`);
238+
expect(result.output).toContain(`attachments${path.sep}-test`);
239239
});
240240

241241
test(`testInfo.attach name can be empty string`, async ({ runInlineTest }) => {

tests/playwright-test/reporter-raw.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ test('should use project name', async ({ runInlineTest }, testInfo) => {
4848
test('passes', async ({ page }, testInfo) => {});
4949
`,
5050
}, { reporter: 'dot,' + kRawReporterPath });
51-
const json = JSON.parse(fs.readFileSync(testInfo.outputPath('output', 'report', 'project-name-656a9b.report'), 'utf-8'));
51+
const json = JSON.parse(fs.readFileSync(testInfo.outputPath('output', 'report', 'project-name.report'), 'utf-8'));
5252
expect(json.project.name).toBe('project-name');
5353
expect(result.exitCode).toBe(0);
5454
});
@@ -173,7 +173,7 @@ test(`testInfo.attach should save attachments via path`, async ({ runInlineTest
173173
expect(result.attachments[0].name).toBe('example.png');
174174
expect(result.attachments[0].contentType).toBe('x-playwright/custom');
175175
const p = result.attachments[0].path;
176-
expect(p).toMatch(/[/\\]attachments[/\\]example-png-[0-9a-f]{6}-[0-9a-f]+\.json$/);
176+
expect(p).toMatch(/[/\\]attachments[/\\]example-png-[0-9a-f]+\.json$/);
177177
const contents = fs.readFileSync(p);
178178
expect(contents.toString()).toBe('We <3 Playwright!');
179179
}
@@ -255,5 +255,5 @@ test('dupe project names', async ({ runInlineTest }, testInfo) => {
255255
`,
256256
}, { reporter: 'dot,' + kRawReporterPath });
257257
const files = fs.readdirSync(testInfo.outputPath('test-results', 'report'));
258-
expect(new Set(files)).toEqual(new Set(['project-name-656a9b.report', 'project-name-656a9b-1.report', 'project-name-656a9b-2.report']));
258+
expect(new Set(files)).toEqual(new Set(['project-name.report', 'project-name-1.report', 'project-name-2.report']));
259259
});

tests/playwright-test/test-output-dir.spec.ts

Lines changed: 7 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -194,10 +194,10 @@ test('should include the project name', async ({ runInlineTest }) => {
194194
expect(result.output).toContain('my-test.spec.js-snapshots/bar-foo.txt');
195195

196196
// test1, run with bar
197-
expect(result.output).toContain('test-results/my-test-test-1-Bar-space-dc1461/bar.txt');
198-
expect(result.output).toContain('my-test.spec.js-snapshots/bar-Bar-space-dc1461.txt');
199-
expect(result.output).toContain('test-results/my-test-test-1-Bar-space-dc1461-retry1/bar.txt');
200-
expect(result.output).toContain('my-test.spec.js-snapshots/bar-Bar-space-dc1461.txt');
197+
expect(result.output).toContain('test-results/my-test-test-1-Bar-space-/bar.txt');
198+
expect(result.output).toContain('my-test.spec.js-snapshots/bar-Bar-space-.txt');
199+
expect(result.output).toContain('test-results/my-test-test-1-Bar-space--retry1/bar.txt');
200+
expect(result.output).toContain('my-test.spec.js-snapshots/bar-Bar-space-.txt');
201201

202202
// test2, run with empty
203203
expect(result.output).toContain('test-results/my-test-test-2/bar.txt');
@@ -212,8 +212,8 @@ test('should include the project name', async ({ runInlineTest }) => {
212212
expect(result.output).toContain('my-test.spec.js-snapshots/bar-foo-suffix.txt');
213213

214214
// test2, run with bar
215-
expect(result.output).toContain('test-results/my-test-test-2-Bar-space-dc1461/bar.txt');
216-
expect(result.output).toContain('my-test.spec.js-snapshots/bar-Bar-space-dc1461-suffix.txt');
215+
expect(result.output).toContain('test-results/my-test-test-2-Bar-space-/bar.txt');
216+
expect(result.output).toContain('my-test.spec.js-snapshots/bar-Bar-space--suffix.txt');
217217
});
218218

219219
test('should include path option in snapshot', async ({ runInlineTest }) => {
@@ -401,58 +401,6 @@ test('should allow nonAscii characters in the output dir', async ({ runInlineTes
401401
expect(outputDir).toBe(path.join(testInfo.outputDir, 'test-results', 'my-test-こんにちは世界'));
402402
});
403403

404-
test('should not collide with other tests when nonAscii characters are replaced', async ({ runInlineTest }) => {
405-
test.info().annotations.push({ type: 'issue', description: 'https://github.com/microsoft/playwright/issues/23386' });
406-
const result = await runInlineTest({
407-
'my-test.spec.js': `
408-
import { test, expect } from '@playwright/test';
409-
const specialChars = ['>', '=', '<', '+', '#', '-', '.', '!', '$', '%', '&', '\\'', '*', '/', '?', '^', '_', '\`', '{', '|', '}', '~', '(', ')', '[', ']', '@'];
410-
for (const char of specialChars) {
411-
test('test' + char, async ({}, testInfo) => {
412-
console.log('\\n%%' + testInfo.outputDir);
413-
});
414-
}
415-
`,
416-
});
417-
expect(result.exitCode).toBe(0);
418-
const outputDirs = result.outputLines;
419-
expect(outputDirs.length).toBe(27);
420-
expect(new Set(outputDirs).size).toBe(outputDirs.length);
421-
const forbiddenCharacters = ['\\', '/', ':', '*', '?', '"', '<', '>', '|', '%'];
422-
for (const outputDir of outputDirs) {
423-
const relativePath = path.relative(path.join(test.info().outputDir, 'test-results'), outputDir);
424-
for (const forbiddenCharacter of forbiddenCharacters)
425-
expect(relativePath).not.toContain(forbiddenCharacter);
426-
}
427-
});
428-
429-
test('should generate expected output dir names', async ({ runInlineTest }, testInfo) => {
430-
const runTests = async (fileName: string, testNames: string[]) => {
431-
const result = await runInlineTest({
432-
[fileName]: `
433-
import { test, expect } from '@playwright/test';
434-
${testNames.map(name => `test('${name}', async ({}, testInfo) => console.log('\\n%%' + testInfo.outputDir));`).join('\n')}
435-
`,
436-
});
437-
expect(result.exitCode).toBe(0);
438-
const outputDirs = result.outputLines.map(line => path.relative(path.join(testInfo.outputDir, 'test-results'), line));
439-
return outputDirs;
440-
};
441-
expect(await runTests('filename.spec.js', [
442-
'testing > foo',
443-
'testing multiple spaces',
444-
'testing multiple spaces',
445-
'!!!hello!!!',
446-
'dashes-are-used',
447-
])).toEqual([
448-
'filename-testing-foo-574286',
449-
'filename-testing-multiple-spaces',
450-
'filename-testing-multiple-spaces-f5d359',
451-
'filename-hello-8eb257',
452-
'filename-dashes-are-used-c67e31',
453-
]);
454-
});
455-
456404
test('should allow shorten long output dirs characters in the output dir', async ({ runInlineTest }, testInfo) => {
457405
const result = await runInlineTest({
458406
'very/deep/and/long/file/name/that/i/want/to/be/trimmed/my-test.spec.js': `
@@ -478,7 +426,7 @@ test('should not mangle double dashes', async ({ runInlineTest }, testInfo) => {
478426
`,
479427
});
480428
const outputDir = result.outputLines[0];
481-
expect(outputDir).toBe(path.join(testInfo.outputDir, 'test-results', 'my--file-my-test-68b7dd'));
429+
expect(outputDir).toBe(path.join(testInfo.outputDir, 'test-results', 'my--file-my--test'));
482430
});
483431

484432
test('should allow include the describe name the output dir', async ({ runInlineTest }, testInfo) => {

0 commit comments

Comments
 (0)