MCPcopy Index your code
hub / github.com/devcontainers/cli / doScenario

Function doScenario

src/spec-node/featuresCLI/testCommandImpl.ts:322–378  ·  view source on GitHub ↗
(pathToTestDir: string, targetFeatureOrGlobal: string, args: FeaturesTestCommandInput, testResults: TestResult[] = [])

Source from the content-addressed store, hash-verified

320}
321
322async function doScenario(pathToTestDir: string, targetFeatureOrGlobal: string, args: FeaturesTestCommandInput, testResults: TestResult[] = []): Promise<TestResult[]> {
323 const { collectionFolder, cliHost, filter } = args;
324 const scenariosPath = path.join(pathToTestDir, 'scenarios.json');
325
326 if (!(await cliHost.isFile(scenariosPath))) {
327 log(`No scenario file found at '${scenariosPath}'. Skipping...`, { prefix: '⚠️', });
328 return testResults;
329 }
330
331 // Read in scenarios.json
332 const scenariosBuffer = await cliHost.readFile(scenariosPath);
333 // Parse to json
334 let scenarios: Scenarios = {};
335 let errors: jsonc.ParseError[] = [];
336 scenarios = jsonc.parse(scenariosBuffer.toString(), errors);
337 if (errors.length > 0) {
338 // Print each jsonc error
339 errors.forEach(error => {
340 log(`${jsonc.printParseErrorCode(error.error)}`, { prefix: '⚠️' });
341 });
342 fail(`Failed to parse scenarios.json at ${scenariosPath}`);
343 return []; // We never reach here, we exit via fail()
344 }
345
346 // For EACH scenario: Spin up a container and exec the scenario test script
347 for (const [scenarioName, scenarioConfig] of Object.entries(scenarios)) {
348
349 if (filter && !scenarioName.includes(filter)) {
350 continue;
351 }
352
353 log(`Running scenario: ${scenarioName}`);
354
355 // Check if we have a scenario test script, otherwise skip.
356 if (!(await cliHost.isFile(path.join(pathToTestDir, `${scenarioName}.sh`)))) {
357 fail(`No scenario test script found at path '${path.join(pathToTestDir, `${scenarioName}.sh`)}'. Either add a script to the test folder, or remove from scenarios.json.`);
358 }
359
360 // Create Container
361 const workspaceFolder = await generateProjectFromScenario(cliHost, collectionFolder, scenarioName, scenarioConfig, targetFeatureOrGlobal);
362 const params = await generateDockerParams(workspaceFolder, args);
363 await createContainerFromWorkingDirectory(params, workspaceFolder, args);
364
365 // Move the entire test directory for the given Feature into the workspaceFolder
366 await cpDirectoryLocal(pathToTestDir, workspaceFolder);
367
368 // Move the test library script into the workspaceFolder
369 await cliHost.writeFile(path.join(workspaceFolder, TEST_LIBRARY_SCRIPT_NAME), Buffer.from(testLibraryScript));
370
371 // Execute Test
372 testResults.push({
373 testName: scenarioName,
374 result: await execTest(`${scenarioName}.sh`, workspaceFolder, cliHost)
375 });
376 }
377 return testResults;
378}
379

Callers 2

runGlobalFeatureTestsFunction · 0.85
runFeatureTestsFunction · 0.85

Calls 9

logFunction · 0.85
failFunction · 0.85
generateDockerParamsFunction · 0.85
execTestFunction · 0.85
isFileMethod · 0.65
readFileMethod · 0.65
writeFileMethod · 0.65

Tested by

no test coverage detected