@devries-cameron20 / composio Mirror · read-only / commits / 6325230db
Fork 0

Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

Mirrored from https://github.com/Camo67/composio.git

Synced Nov 22, 08:15 UTC

chore: unify runCmd logic

jkomyno committed Feb 18, 2026 at 18:10 UTC 6325230dbc1042e2371a338005a7fd1c91d50983
1 file changed +131 -153
ts/e2e-tests/_utils/src/runner.ts
+131 -153
@@ -24,6 +24,7 @@ import {
24 resolveDenoVersionMetaList,
25 resolveCliVersionMetaList,
26 } from './config';
27 +import type { ExecResult } from './image-lifecycle';
28 import {
29 ensureNodeImage,
30 runNodeContainer,
@@ -348,36 +349,45 @@ interface VersionExecutionContext {
349 }
350
351 // ============================================================================
351 -// Node.js Docker Executors
352 +// Shared Docker Executor Factories
353 // ============================================================================
354
355 /**
355 - * Creates Docker execution utilities for a specific Node.js version.
356 + * A function that runs a command inside a Docker container.
357 + * All runtime-specific container runners (Node, Deno, CLI) share this signature
358 + * for the subset of options used by `createRunCmd`.
359 */
357 -function createNodeDockerExecutors(
358 - config: RunE2EInternalConfig,
359 - nodeVersion: string,
360 - imageTag: string,
361 - repoRoot: string,
362 - logManager: DebugLogManager
363 -) {
364 - const { cwd, suiteName, env, usesFixtures } = config;
365 - const containerEnv = buildContainerEnv(env);
366 - const effectiveCwd = usesFixtures ? `${cwd}/fixtures` : cwd;
360 +type ContainerRunner = (options: {
361 + imageTag: string;
362 + cmd: string;
363 + cwd: string;
364 + env: Record<string, string | undefined>;
365 + name: string;
366 + remove?: boolean;
367 + volumes?: Array<{ volume: string; target: string; readonly: boolean }>;
368 +}) => Promise<ExecResult>;
369
368 - const context: VersionExecutionContext = {
369 - phases: [],
370 - versionStartTime: Date.now(),
371 - imageTag,
372 - };
370 +/**
371 + * Creates a `runCmd` function that executes a command in a Docker container,
372 + * tracks phase results, and optionally captures files from the container.
373 + */
374 +function createRunCmd(options: {
375 + runContainer: ContainerRunner;
376 + containerPrefix: string;
377 + effectiveCwd: string;
378 + containerEnv: Record<string, string | undefined>;
379 + imageTag: string;
380 + context: VersionExecutionContext;
381 +}): (input: RunCmdInput) => Promise<E2ETestResult | E2ETestResultWithFiles> {
382 + const { runContainer, containerPrefix, effectiveCwd, containerEnv, imageTag, context } = options;
383
374 - const runCmd = async (input: RunCmdInput): Promise<E2ETestResult | E2ETestResultWithFiles> => {
384 + return async (input: RunCmdInput) => {
385 const { command, files } = normalizeRunCmdInput(input);
386 const shouldCapture = Array.isArray(files) && files.length > 0;
377 - const containerName = `e2e-${suiteName}-node-${nodeVersion.replace(/\./g, '-')}-${Date.now()}`;
387 + const containerName = `${containerPrefix}-${Date.now()}`;
388 const startTime = Date.now();
389
380 - const result = await runNodeContainer({
390 + const result = await runContainer({
391 imageTag,
392 cmd: command,
393 cwd: effectiveCwd,
@@ -416,6 +426,70 @@ function createNodeDockerExecutors(
426
427 return result;
428 };
429 +}
430 +
431 +/**
432 + * Creates a `finalizeVersionLog` function that writes the version's
433 + * phase results to the debug log.
434 + */
435 +function createFinalizeVersionLog(options: {
436 + runtime: RuntimeKind;
437 + version: string;
438 + context: VersionExecutionContext;
439 + logManager: DebugLogManager;
440 +}): () => Promise<void> {
441 + const { runtime, version, context, logManager } = options;
442 +
443 + return async () => {
444 + const totalDurationMs = Date.now() - context.versionStartTime;
445 + const allPassed = context.phases.every(p => p.exitCode === 0);
446 + const status: 'pass' | 'fail' = allPassed ? 'pass' : 'fail';
447 +
448 + await logManager.writeVersionSection({
449 + runtime,
450 + version,
451 + status,
452 + imageTag: context.imageTag,
453 + phases: context.phases,
454 + totalDurationMs,
455 + });
456 + };
457 +}
458 +
459 +// ============================================================================
460 +// Node.js Docker Executors
461 +// ============================================================================
462 +
463 +/**
464 + * Creates Docker execution utilities for a specific Node.js version.
465 + */
466 +function createNodeDockerExecutors(
467 + config: RunE2EInternalConfig,
468 + nodeVersion: string,
469 + imageTag: string,
470 + repoRoot: string,
471 + logManager: DebugLogManager
472 +) {
473 + const { cwd, suiteName, env, usesFixtures } = config;
474 + const containerEnv = buildContainerEnv(env);
475 + const effectiveCwd = usesFixtures ? `${cwd}/fixtures` : cwd;
476 +
477 + const context: VersionExecutionContext = {
478 + phases: [],
479 + versionStartTime: Date.now(),
480 + imageTag,
481 + };
482 +
483 + const containerPrefix = `e2e-${suiteName}-node-${nodeVersion.replace(/\./g, '-')}`;
484 +
485 + const runCmd = createRunCmd({
486 + runContainer: runNodeContainer,
487 + containerPrefix,
488 + effectiveCwd,
489 + containerEnv,
490 + imageTag,
491 + context,
492 + });
493
494 /**
495 * Unified fixture runner with optional setup phase.
@@ -434,7 +508,7 @@ function createNodeDockerExecutors(
508 }
509
510 const volumeName = generateVolumeName(suiteName, nodeVersion);
437 - const containerBaseName = `e2e-${suiteName}-node-${nodeVersion.replace(/\./g, '-')}-${Date.now()}`;
511 + const containerBaseName = `${containerPrefix}-${Date.now()}`;
512 const volumeTarget = `${effectiveCwd.startsWith('/') ? effectiveCwd : `/app/${effectiveCwd}`}/node_modules`;
513
514 try {
@@ -495,20 +569,12 @@ function createNodeDockerExecutors(
569 }
570 }
571
498 - const finalizeVersionLog = async (): Promise<void> => {
499 - const totalDurationMs = Date.now() - context.versionStartTime;
500 - const allPassed = context.phases.every(p => p.exitCode === 0);
501 - const status: 'pass' | 'fail' = allPassed ? 'pass' : 'fail';
502 -
503 - await logManager.writeVersionSection({
504 - runtime: 'node',
505 - version: nodeVersion,
506 - status,
507 - imageTag: context.imageTag,
508 - phases: context.phases,
509 - totalDurationMs,
510 - });
511 - };
572 + const finalizeVersionLog = createFinalizeVersionLog({
573 + runtime: 'node',
574 + version: nodeVersion,
575 + context,
576 + logManager,
577 + });
578
579 return { runCmd, runFixture, finalizeVersionLog, context };
580 }
@@ -537,51 +603,16 @@ function createDenoDockerExecutors(
603 imageTag,
604 };
605
540 - const runCmd = async (input: RunCmdInput): Promise<E2ETestResult | E2ETestResultWithFiles> => {
541 - const { command, files } = normalizeRunCmdInput(input);
542 - const shouldCapture = Array.isArray(files) && files.length > 0;
543 - const containerName = `e2e-${suiteName}-deno-${denoVersion.replace(/\./g, '-')}-${Date.now()}`;
544 - const startTime = Date.now();
545 -
546 - const result = await runDenoContainer({
547 - imageTag,
548 - cmd: command,
549 - cwd: effectiveCwd,
550 - env: containerEnv,
551 - name: containerName,
552 - remove: !shouldCapture,
553 - });
554 -
555 - const durationMs = Date.now() - startTime;
556 -
557 - context.phases.push({
558 - phase: 'fixture',
559 - phaseIndex: context.phases.length + 1,
560 - totalPhases: context.phases.length + 1,
561 - command,
562 - containerName,
563 - durationMs,
564 - exitCode: result.exitCode,
565 - stdout: result.stdout,
566 - stderr: result.stderr,
567 - });
568 -
569 - if (shouldCapture) {
570 - const containerCwd = toContainerCwd(effectiveCwd);
571 - try {
572 - const captured = await captureFilesFromContainer({
573 - containerName,
574 - containerCwd,
575 - files: files as string[],
576 - });
577 - return { ...result, files: captured };
578 - } finally {
579 - await dockerRm(containerName);
580 - }
581 - }
606 + const containerPrefix = `e2e-${suiteName}-deno-${denoVersion.replace(/\./g, '-')}`;
607
583 - return result;
584 - };
608 + const runCmd = createRunCmd({
609 + runContainer: runDenoContainer,
610 + containerPrefix,
611 + effectiveCwd,
612 + containerEnv,
613 + imageTag,
614 + context,
615 + });
616
617 function runFixture(options: { filename: string }): Promise<E2ETestResult>;
618 function runFixture(options: { filename: string; setup: string }): Promise<E2ETestResultWithSetup>;
@@ -597,7 +628,7 @@ function createDenoDockerExecutors(
628
629 // Deno with setup: use Docker volumes for caching
630 const volumeName = generateVolumeName(suiteName, denoVersion);
600 - const containerBaseName = `e2e-${suiteName}-deno-${denoVersion.replace(/\./g, '-')}-${Date.now()}`;
631 + const containerBaseName = `${containerPrefix}-${Date.now()}`;
632 // Deno caches to DENO_DIR, but we also support node_modules for npm packages
633 const volumeTarget = `${effectiveCwd.startsWith('/') ? effectiveCwd : `/app/${effectiveCwd}`}/node_modules`;
634
@@ -659,20 +690,12 @@ function createDenoDockerExecutors(
690 }
691 }
692
662 - const finalizeVersionLog = async (): Promise<void> => {
663 - const totalDurationMs = Date.now() - context.versionStartTime;
664 - const allPassed = context.phases.every(p => p.exitCode === 0);
665 - const status: 'pass' | 'fail' = allPassed ? 'pass' : 'fail';
666 -
667 - await logManager.writeVersionSection({
668 - runtime: 'deno',
669 - version: denoVersion,
670 - status,
671 - imageTag: context.imageTag,
672 - phases: context.phases,
673 - totalDurationMs,
674 - });
675 - };
693 + const finalizeVersionLog = createFinalizeVersionLog({
694 + runtime: 'deno',
695 + version: denoVersion,
696 + context,
697 + logManager,
698 + });
699
700 return { runCmd, runFixture, finalizeVersionLog, context };
701 }
@@ -701,70 +724,25 @@ function createCliDockerExecutors(
724 imageTag,
725 };
726
704 - const runCmd = async (input: RunCmdInput): Promise<E2ETestResult | E2ETestResultWithFiles> => {
705 - const { command, files } = normalizeRunCmdInput(input);
706 - const shouldCapture = Array.isArray(files) && files.length > 0;
707 - const containerName = `e2e-${suiteName}-cli-${cliVersion.replace(/\./g, '-')}-${Date.now()}`;
708 - const startTime = Date.now();
709 -
710 - const result = await runCliContainer({
711 - imageTag,
712 - cmd: command,
713 - cwd: effectiveCwd,
714 - env: containerEnv,
715 - name: containerName,
716 - remove: !shouldCapture,
717 - });
718 -
719 - const durationMs = Date.now() - startTime;
720 -
721 - context.phases.push({
722 - phase: 'fixture',
723 - phaseIndex: context.phases.length + 1,
724 - totalPhases: context.phases.length + 1,
725 - command,
726 - containerName,
727 - durationMs,
728 - exitCode: result.exitCode,
729 - stdout: result.stdout,
730 - stderr: result.stderr,
731 - });
732 -
733 - if (shouldCapture) {
734 - const containerCwd = toContainerCwd(effectiveCwd);
735 - try {
736 - const captured = await captureFilesFromContainer({
737 - containerName,
738 - containerCwd,
739 - files: files as string[],
740 - });
741 - return { ...result, files: captured };
742 - } finally {
743 - await dockerRm(containerName);
744 - }
745 - }
746 -
747 - return result;
748 - };
727 + const runCmd = createRunCmd({
728 + runContainer: runCliContainer,
729 + containerPrefix: `e2e-${suiteName}-cli-${cliVersion.replace(/\./g, '-')}`,
730 + effectiveCwd,
731 + containerEnv,
732 + imageTag,
733 + context,
734 + });
735
736 const runFixture = ((_: RunFixtureOptions) => {
737 throw new Error('runFixture is not supported for CLI runtime tests');
738 }) as DefineTestsContext['runFixture'];
739
754 - const finalizeVersionLog = async (): Promise<void> => {
755 - const totalDurationMs = Date.now() - context.versionStartTime;
756 - const allPassed = context.phases.every(p => p.exitCode === 0);
757 - const status: 'pass' | 'fail' = allPassed ? 'pass' : 'fail';
758 -
759 - await logManager.writeVersionSection({
760 - runtime: 'cli',
761 - version: cliVersion,
762 - status,
763 - imageTag: context.imageTag,
764 - phases: context.phases,
765 - totalDurationMs,
766 - });
767 - };
740 + const finalizeVersionLog = createFinalizeVersionLog({
741 + runtime: 'cli',
742 + version: cliVersion,
743 + context,
744 + logManager,
745 + });
746
747 return { runCmd, runFixture, finalizeVersionLog, context };
748 }