|
| 1 | +// Copyright (c) 2016-present Invertase Limited & Contributors |
| 2 | + |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this library except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | + |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +// This script replaces mock response files for Vertex AI unit tests with a fresh |
| 16 | +// clone of the shared repository of Vertex AI test data. |
| 17 | + |
| 18 | +// eslint-disable-next-line @typescript-eslint/no-require-imports |
| 19 | +import { execSync } from 'child_process'; |
| 20 | +import { join } from 'path'; |
| 21 | +import { existsSync } from 'fs'; |
| 22 | +import { rimrafSync } from 'rimraf'; |
| 23 | + |
| 24 | +// Our test data repository |
| 25 | +const REPO_NAME = 'vertexai-sdk-test-data'; |
| 26 | +const REPO_LINK = `https://github.com/FirebaseExtended/${REPO_NAME}.git`; |
| 27 | +const TEST_DATA_ROOT = join(__dirname, '..', 'packages', 'ai', '__tests__', 'test-utils'); |
| 28 | + |
| 29 | +// Get tags from repository, sorted by tag name, and coerce result to a string, then trim it |
| 30 | +const repoTags = ( |
| 31 | + execSync(`git ls-remote --tags --sort=version:refname "${REPO_LINK}"`) + '' |
| 32 | +).trim(); |
| 33 | + |
| 34 | +// Fish out just the tag name from the last line (since they are sorted already) |
| 35 | +const latestTag = repoTags.split('/').at(-1); |
| 36 | +if (latestTag === undefined) { |
| 37 | + console.error('Unable to determine latest test data tag.'); |
| 38 | + process.exit(1); |
| 39 | +} |
| 40 | + |
| 41 | +// Create the test data directory based on the latest tag |
| 42 | +const cloneDirName = `${REPO_NAME}_${latestTag}`; |
| 43 | +const cloneDirPath = join(TEST_DATA_ROOT, cloneDirName); |
| 44 | + |
| 45 | +// Clean out any test data that isn't the latest test data |
| 46 | +rimrafSync(TEST_DATA_ROOT, { |
| 47 | + preserveRoot: false, |
| 48 | + filter: (path: string, _) => !path.endsWith('.ts') && !path.includes(cloneDirName), |
| 49 | +}); |
| 50 | + |
| 51 | +// Exit if our intended latest data clone target already exists |
| 52 | +if (existsSync(cloneDirPath)) { |
| 53 | + console.log('AI mock responses data exists locally already. Exiting fetch script.'); |
| 54 | + process.exit(0); |
| 55 | +} |
| 56 | + |
| 57 | +// Clone the latest test data |
| 58 | +console.log(`Fetching AI mock responses data...`); |
| 59 | +execSync( |
| 60 | + `git -c advice.detachedHead=false clone --branch ${latestTag} ${REPO_LINK} ${cloneDirPath}`, |
| 61 | +); |
0 commit comments