From d8a84b9aab020ff83fa756a5781c6bc016f50c6d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jochen=20Kirst=C3=A4tter?= <7329802+jochenkirstaetter@users.noreply.github.com> Date: Thu, 29 Feb 2024 19:47:09 +0400 Subject: [PATCH] extend gcloud execution --- tests/ConfigurationFixture.cs | 77 ++++++++++++++++++++++++++++------- 1 file changed, 63 insertions(+), 14 deletions(-) diff --git a/tests/ConfigurationFixture.cs b/tests/ConfigurationFixture.cs index 9ddc8f1d..0fbcfb82 100644 --- a/tests/ConfigurationFixture.cs +++ b/tests/ConfigurationFixture.cs @@ -2,6 +2,7 @@ using System; using System.Diagnostics; using System.IO; +using System.Text; using Xunit; namespace Test.Mscc.GenerativeAI @@ -39,23 +40,71 @@ public ConfigurationFixture() if (string.IsNullOrEmpty(AccessToken)) AccessToken = Environment.GetEnvironmentVariable("GOOGLE_ACCESS_TOKEN"); if (string.IsNullOrEmpty(AccessToken)) - AccessToken = ReadAccessToken().TrimEnd(); + { + AccessToken = RunExternalExe("cmd.exe", "/c gcloud auth application-default print-access-token").TrimEnd(); + } } - private string ReadAccessToken() + private string RunExternalExe(string filename, string arguments = null) { - Process p = new Process(); - p.StartInfo.UseShellExecute = false; - p.StartInfo.CreateNoWindow = true; - p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; - p.StartInfo.RedirectStandardOutput = true; - p.StartInfo.FileName = "cmd.exe"; - p.StartInfo.Arguments = "/c gcloud auth application-default print-access-token"; - p.Start(); - var output = p.StandardOutput.ReadToEnd(); - p.WaitForExit(); - - return output; + var process = new Process(); + + process.StartInfo.FileName = filename; + if (!string.IsNullOrEmpty(arguments)) + { + process.StartInfo.Arguments = arguments; + } + + process.StartInfo.CreateNoWindow = true; + process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; + process.StartInfo.UseShellExecute = false; + + process.StartInfo.RedirectStandardError = true; + process.StartInfo.RedirectStandardOutput = true; + var stdOutput = new StringBuilder(); + process.OutputDataReceived += (sender, args) => stdOutput.AppendLine(args.Data); // Use AppendLine rather than Append since args.Data is one line of output, not including the newline character. + + string stdError = null; + try + { + process.Start(); + process.BeginOutputReadLine(); + stdError = process.StandardError.ReadToEnd(); + process.WaitForExit(); + } + catch (Exception e) + { + throw new Exception("OS error while executing " + Format(filename, arguments)+ ": " + e.Message, e); + } + + if (process.ExitCode == 0) + { + return stdOutput.ToString(); + } + else + { + var message = new StringBuilder(); + + if (!string.IsNullOrEmpty(stdError)) + { + message.AppendLine(stdError); + } + + if (stdOutput.Length != 0) + { + message.AppendLine("Std output:"); + message.AppendLine(stdOutput.ToString()); + } + + throw new Exception(Format(filename, arguments) + " finished with exit code = " + process.ExitCode + ": " + message); + } + } + + private string Format(string filename, string arguments) + { + return "'" + filename + + ((string.IsNullOrEmpty(arguments)) ? string.Empty : " " + arguments) + + "'"; } } } \ No newline at end of file