From ce5afc0d0768bbce8ab343eda899d42a0bc70c47 Mon Sep 17 00:00:00 2001 From: Philip Pfaffe Date: Tue, 9 Apr 2024 13:08:58 +0000 Subject: [PATCH] [testing] Use fixed paths to refer to snapshots/screenshots MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This avoid special casing and passing golden locations around. In order to nicely reference snapshots, we move them next to the test files since snapshots are looked up by test name anyways. Bug: b:333423685 Change-Id: I02188633f25dffe8f863d64b1e769cbb5563b2b6 Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/5435761 Reviewed-by: Simon Zünd Reviewed-by: Jack Franklin Commit-Queue: Philip Pfaffe --- PRESUBMIT.py | 2 +- scripts/test/check_obsolete_goldens.js | 5 ++--- .../application/cookies_test.json | 0 .../recorder/recorder_test.json | 0 .../e2e/{snapshots => }/recorder/ui_test.json | 0 .../recorder/injected}/injected_test.json | 0 test/shared/screenshots.ts | 5 ++--- test/shared/snapshots.ts | 20 +++++++++++-------- 8 files changed, 17 insertions(+), 15 deletions(-) rename test/e2e/{snapshots => }/application/cookies_test.json (100%) rename test/e2e/{snapshots => }/recorder/recorder_test.json (100%) rename test/e2e/{snapshots => }/recorder/ui_test.json (100%) rename test/interactions/{snapshots => panels/recorder/injected}/injected_test.json (100%) diff --git a/PRESUBMIT.py b/PRESUBMIT.py index 976efff0fb..4d19266512 100644 --- a/PRESUBMIT.py +++ b/PRESUBMIT.py @@ -538,7 +538,7 @@ def _CheckObsoleteScreenshotGoldens(input_api, output_api): 'scripts', 'test', 'check_obsolete_goldens.js') - script_args = ["--interaction-test-root", interaction_test_root_path] + script_args = [] errors_from_script = _checkWithNodeScript(input_api, output_api, script_path, script_args) results.extend(errors_from_script) diff --git a/scripts/test/check_obsolete_goldens.js b/scripts/test/check_obsolete_goldens.js index 769a099379..79b54b259a 100644 --- a/scripts/test/check_obsolete_goldens.js +++ b/scripts/test/check_obsolete_goldens.js @@ -10,14 +10,13 @@ const exec = util.promisify(childProcess.exec); const yargsObject = require('yargs') - .option('interaction-test-root', {type: 'string', desc: 'file path of the interaction test root directory.'}) .option( 'remove-files', {type: 'boolean', desc: 'Set to true to have obsolete goldens removed.', default: false}) .argv; const shouldRemoveFiles = yargsObject['remove-files'] === true; -const interactionTestRoot = yargsObject['interaction-test-root']; - +const SOURCE_ROOT = path.resolve(__dirname, path.join('..', '..')); +const interactionTestRoot = path.join(SOURCE_ROOT, 'test', 'interactions'); const GOLDENS_LOCATION = path.join(interactionTestRoot, 'goldens'); function findScreenshotsToCheck(folder) { diff --git a/test/e2e/snapshots/application/cookies_test.json b/test/e2e/application/cookies_test.json similarity index 100% rename from test/e2e/snapshots/application/cookies_test.json rename to test/e2e/application/cookies_test.json diff --git a/test/e2e/snapshots/recorder/recorder_test.json b/test/e2e/recorder/recorder_test.json similarity index 100% rename from test/e2e/snapshots/recorder/recorder_test.json rename to test/e2e/recorder/recorder_test.json diff --git a/test/e2e/snapshots/recorder/ui_test.json b/test/e2e/recorder/ui_test.json similarity index 100% rename from test/e2e/snapshots/recorder/ui_test.json rename to test/e2e/recorder/ui_test.json diff --git a/test/interactions/snapshots/injected_test.json b/test/interactions/panels/recorder/injected/injected_test.json similarity index 100% rename from test/interactions/snapshots/injected_test.json rename to test/interactions/panels/recorder/injected/injected_test.json diff --git a/test/shared/screenshots.ts b/test/shared/screenshots.ts index b52ad61d37..81130d76c0 100644 --- a/test/shared/screenshots.ts +++ b/test/shared/screenshots.ts @@ -27,11 +27,10 @@ import {ScreenshotError} from '../shared/screenshot-error.js'; * goldens from there. */ const testRunnerCWD = getTestRunnerConfigSetting('cwd', ''); -const testSuiteSourceDir = getTestRunnerConfigSetting('test-suite-source-dir', ''); -if (!testRunnerCWD || !testSuiteSourceDir) { +if (!testRunnerCWD) { throw new Error('--cwd and --test-suite-source-dir must be provided when running the screenshot tests.'); } -const GOLDENS_FOLDER = path.join(testRunnerCWD, testSuiteSourceDir, 'goldens', platform); +const GOLDENS_FOLDER = path.join(testRunnerCWD, 'test', 'interactions', 'goldens', platform); /** * It's assumed that the image_diff binaries are in CWD/third_party/image_diff/{platform}/image_diff diff --git a/test/shared/snapshots.ts b/test/shared/snapshots.ts index 29277e46b0..d048e654ba 100644 --- a/test/shared/snapshots.ts +++ b/test/shared/snapshots.ts @@ -4,22 +4,23 @@ import {assert} from 'chai'; import {existsSync, mkdirSync, readFileSync, writeFileSync} from 'fs'; -import {basename, dirname, extname, join, normalize} from 'path'; +import {dirname, format, join, parse, relative} from 'path'; import {getTestRunnerConfigSetting} from '../conductor/test_runner_config.js'; +const TARGET = getTestRunnerConfigSetting('target', ''); const CWD = getTestRunnerConfigSetting('cwd', ''); -const TEST_SUITE_SOURCE_DIR = getTestRunnerConfigSetting('test-suite-source-dir', ''); const TEST_SUITE_PATH = getTestRunnerConfigSetting('test-suite-path', ''); -if (!CWD || !TEST_SUITE_SOURCE_DIR) { - throw new Error('--cwd and --test-suite-source-dir must be provided when running the snapshot tests.'); +if (!TARGET) { + throw new Error('Unknown target'); +} +if (!CWD) { + throw new Error('--cwd must be provided when running the snapshot tests.'); } if (!TEST_SUITE_PATH) { throw new Error('--test-suite-path must be specified'); } -const SNAPSHOTS_DIR = join(CWD, TEST_SUITE_SOURCE_DIR, 'snapshots'); - const UPDATE_SNAPSHOTS = Boolean(process.env['UPDATE_SNAPSHOTS']); let currentTestPath: string|undefined; @@ -45,7 +46,7 @@ beforeEach(function() { currentTestTitle = currentTestTitle.slice(0, -match[1].length); } - currentTestPath = testPath && normalize(testPath.trim()); + currentTestPath = this.currentTest.file; snapshotIndex = 0; } @@ -78,7 +79,10 @@ const restoreSnapshots = () => { }; const getSnapshotPath = (testPath: string) => { - return join(SNAPSHOTS_DIR, dirname(testPath), `${basename(testPath, extname(testPath))}.json`); + const relativeTestPath = parse(relative(join(CWD, 'out', TARGET, 'gen'), testPath)); + relativeTestPath.ext = '.json'; + relativeTestPath.base = `${relativeTestPath.name}.json`; + return join(CWD, format(relativeTestPath)); }; const getOrUpdateSnapshot = (value: unknown, options: SnapshotOptions) => {