diff options
| author | matt korwel <[email protected]> | 2025-06-16 08:27:29 -0700 |
|---|---|---|
| committer | GitHub <[email protected]> | 2025-06-16 15:27:29 +0000 |
| commit | df938d6ee833ded59f6d12528105e6165ed76a92 (patch) | |
| tree | 712b82037a378b74f5fd68ed5b06f1aae56a88c9 /integration-tests/run-tests.js | |
| parent | a600588c200f85e9f3bfe46ef7f836387e7349e5 (diff) | |
Preflight and integration npx (#1096)
Diffstat (limited to 'integration-tests/run-tests.js')
| -rw-r--r-- | integration-tests/run-tests.js | 122 |
1 files changed, 122 insertions, 0 deletions
diff --git a/integration-tests/run-tests.js b/integration-tests/run-tests.js new file mode 100644 index 00000000..d4b9cf49 --- /dev/null +++ b/integration-tests/run-tests.js @@ -0,0 +1,122 @@ +/** + * @license + * Copyright 2025 Google LLC + * SPDX-License-Identifier: Apache-2.0 + */ + +import { spawnSync } from 'child_process'; +import { spawn } from 'child_process'; +import { mkdirSync, rmSync, createWriteStream } from 'fs'; +import { join, dirname, basename } from 'path'; +import { fileURLToPath } from 'url'; +import { glob } from 'glob'; + +async function main() { + const __dirname = dirname(fileURLToPath(import.meta.url)); + const rootDir = join(__dirname, '..'); + const integrationTestsDir = join(rootDir, '.integration-tests'); + + if (process.env.GEMINI_SANDBOX === 'docker' && !process.env.IS_DOCKER) { + console.log('Building sandbox for Docker...'); + const buildResult = spawnSync('npm', ['run', 'build:all'], { + stdio: 'inherit', + }); + if (buildResult.status !== 0) { + console.error('Sandbox build failed.'); + process.exit(1); + } + } + + const runId = `${Date.now()}`; + const runDir = join(integrationTestsDir, runId); + + mkdirSync(runDir, { recursive: true }); + + const args = process.argv.slice(2); + const keepOutput = + process.env.KEEP_OUTPUT === 'true' || args.includes('--keep-output'); + if (keepOutput) { + const keepOutputIndex = args.indexOf('--keep-output'); + if (keepOutputIndex > -1) { + args.splice(keepOutputIndex, 1); + } + console.log(`Keeping output for test run in: ${runDir}`); + } + + const verbose = args.includes('--verbose'); + if (verbose) { + const verboseIndex = args.indexOf('--verbose'); + if (verboseIndex > -1) { + args.splice(verboseIndex, 1); + } + } + + const testPatterns = + args.length > 0 + ? args.map((arg) => `integration-tests/${arg}.test.js`) + : ['integration-tests/*.test.js']; + const testFiles = glob.sync(testPatterns, { cwd: rootDir, absolute: true }); + + for (const testFile of testFiles) { + const testFileName = basename(testFile); + console.log(`\tFound test file: ${testFileName}`); + } + + let allTestsPassed = true; + + for (const testFile of testFiles) { + const testFileName = basename(testFile); + const testFileDir = join(runDir, testFileName); + mkdirSync(testFileDir, { recursive: true }); + + console.log( + `------------- Running test file: ${testFileName} ------------------------------`, + ); + + const child = spawn('node', ['--test', testFile], { + stdio: 'pipe', + env: { + ...process.env, + INTEGRATION_TEST_FILE_DIR: testFileDir, + KEEP_OUTPUT: keepOutput.toString(), + TEST_FILE_NAME: testFileName, + }, + }); + + if (verbose) { + child.stdout.pipe(process.stdout); + child.stderr.pipe(process.stderr); + } + + if (keepOutput) { + const outputFile = join(testFileDir, 'output.log'); + const outputStream = createWriteStream(outputFile); + child.stdout.pipe(outputStream); + child.stderr.pipe(outputStream); + console.log(`Output for ${testFileName} written to: ${outputFile}`); + } else if (!verbose) { + child.stdout.pipe(process.stdout); + child.stderr.pipe(process.stderr); + } + + const exitCode = await new Promise((resolve) => { + child.on('close', resolve); + }); + + if (exitCode !== 0) { + console.error(`Test file failed: ${testFileName}`); + allTestsPassed = false; + } + } + + if (!keepOutput) { + rmSync(runDir, { recursive: true, force: true }); + } + + if (!allTestsPassed) { + console.error('One or more test files failed.'); + process.exit(1); + } +} + +main(); |
