diff --git a/cli/azd/extensions/azure.ai.agents/test/integrationTests/testUtilities/test_utils.go b/cli/azd/extensions/azure.ai.agents/test/integrationTests/testUtilities/test_utils.go index d813f6fff00..e09998ad13a 100644 --- a/cli/azd/extensions/azure.ai.agents/test/integrationTests/testUtilities/test_utils.go +++ b/cli/azd/extensions/azure.ai.agents/test/integrationTests/testUtilities/test_utils.go @@ -185,15 +185,34 @@ func executeAzdCommand(ctx context.Context, cli *azdcli.CLI, testSuite *Integrat cli.WorkingDirectory = testSuite.AzdProjectDir result, err := cli.RunCommandWithStdIn(ctx, "", args...) - output := result.Stdout + result.Stderr + + var output string + if result != nil { + output = result.Stdout + result.Stderr + } LogCommandOutput(strings.Join(args, " "), []byte(output)) - if err != nil || result.ExitCode != 0 { - Logf("Command failed with error: %v, exit code: %d", err, result.ExitCode) + if err != nil { + var exitCode int = -1 // Default to -1 when result is nil (command failed to start) + if result != nil { + exitCode = result.ExitCode + } + Logf("Command failed with error: %v, exit code: %d", err, exitCode) + return "", &InitError{ + Message: output, + Err: fmt.Errorf("exit code %d: %w", exitCode, err), + } + } + + // Defensive check: handle case where err is nil but exit code is non-zero + // This shouldn't normally happen based on RunCommandWithStdIn implementation, + // but we check defensively in case the implementation changes + if result != nil && result.ExitCode != 0 { + Logf("Command failed with exit code: %d", result.ExitCode) return "", &InitError{ Message: output, - Err: fmt.Errorf("exit code %d: %w", result.ExitCode, err), + Err: fmt.Errorf("exit code %d", result.ExitCode), } } Logf("Command completed successfully") @@ -201,7 +220,7 @@ func executeAzdCommand(ctx context.Context, cli *azdcli.CLI, testSuite *Integrat return output, nil } -// executeAzdCommand executes an azd command and returns output and agent version if available +// executeAzdCommandWithExec executes an azd command using exec.Command and returns the output func executeAzdCommandWithExec(ctx context.Context, testSuite *IntegrationTestSuite, timeout time.Duration, args []string) (string, error) { ctx, cancel := context.WithTimeout(ctx, timeout) defer cancel()