From b7dcc2bb162d569a4d97bc980a7ef32dcb712ea0 Mon Sep 17 00:00:00 2001 From: Andrey Lushnikov Date: Mon, 10 Jul 2023 09:45:24 -0700 Subject: [PATCH] cherry-pick(#24127): fix: do not create empty directories for successful snapshot tests Fixes https://github.com/microsoft/playwright/issues/15600 --- packages/playwright-test/src/matchers/toMatchSnapshot.ts | 4 ++-- packages/playwright-test/src/worker/testInfo.ts | 5 +++++ tests/playwright-test/to-have-screenshot.spec.ts | 1 + 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/packages/playwright-test/src/matchers/toMatchSnapshot.ts b/packages/playwright-test/src/matchers/toMatchSnapshot.ts index 87ad7d3e93630..6c25e04c8e7e2 100644 --- a/packages/playwright-test/src/matchers/toMatchSnapshot.ts +++ b/packages/playwright-test/src/matchers/toMatchSnapshot.ts @@ -124,8 +124,8 @@ class SnapshotHelper { const inputPathSegments = Array.isArray(name) ? name : [addSuffixToFilePath(name, '', undefined, true)]; const outputPathSegments = Array.isArray(name) ? name : [addSuffixToFilePath(name, actualModifier, undefined, true)]; this.snapshotPath = snapshotPathResolver(...inputPathSegments); - const inputFile = testInfo.outputPath(...inputPathSegments); - const outputFile = testInfo.outputPath(...outputPathSegments); + const inputFile = testInfo._getOutputPath(...inputPathSegments); + const outputFile = testInfo._getOutputPath(...outputPathSegments); this.expectedPath = addSuffixToFilePath(inputFile, '-expected'); this.previousPath = addSuffixToFilePath(outputFile, '-previous'); this.actualPath = addSuffixToFilePath(outputFile, '-actual'); diff --git a/packages/playwright-test/src/worker/testInfo.ts b/packages/playwright-test/src/worker/testInfo.ts index c4b04bd5b92cb..0d497fe294d7d 100644 --- a/packages/playwright-test/src/worker/testInfo.ts +++ b/packages/playwright-test/src/worker/testInfo.ts @@ -382,7 +382,12 @@ export class TestInfoImpl implements TestInfo { } outputPath(...pathSegments: string[]){ + const outputPath = this._getOutputPath(...pathSegments); fs.mkdirSync(this.outputDir, { recursive: true }); + return outputPath; + } + + _getOutputPath(...pathSegments: string[]){ const joinedPath = path.join(...pathSegments); const outputPath = getContainedPath(this.outputDir, joinedPath); if (outputPath) diff --git a/tests/playwright-test/to-have-screenshot.spec.ts b/tests/playwright-test/to-have-screenshot.spec.ts index cdd48b84adf54..a0c05f5a9a73c 100644 --- a/tests/playwright-test/to-have-screenshot.spec.ts +++ b/tests/playwright-test/to-have-screenshot.spec.ts @@ -283,6 +283,7 @@ test('should support clip option for page', async ({ runInlineTest }, testInfo) }); ` }); + expect(fs.existsSync(testInfo.outputPath('test-results', 'a-is-a-test'))).toBe(false); expect(result.exitCode).toBe(0); });