/// <summary> /// Invokes a call to gvfs using the arguments specified /// </summary> /// <param name="args">The arguments to use when invoking gvfs</param> /// <param name="expectedExitCode"> /// What the expected exit code should be. /// >= than 0 to check the exit code explicitly /// -1 = Fail if the exit code is 0 /// -2 = Do not check the exit code (Default) /// </param> /// <param name="trace">What to set the GIT_TRACE environment variable to</param> /// <param name="standardInput">What to write to the standard input stream</param> /// <param name="internalParameter">The internal parameter to set in the arguments</param> /// <returns></returns> private string CallGVFS(string args, int expectedExitCode = DoNotCheckExitCode, string trace = null, string standardInput = null, string internalParameter = null) { ProcessStartInfo processInfo = null; processInfo = new ProcessStartInfo(this.pathToGVFS); if (internalParameter == null) { internalParameter = GVFSHelpers.GetInternalParameter(); } processInfo.Arguments = args + " " + TestConstants.InternalUseOnlyFlag + " " + internalParameter; processInfo.WindowStyle = ProcessWindowStyle.Hidden; processInfo.UseShellExecute = false; processInfo.RedirectStandardOutput = true; if (standardInput != null) { processInfo.RedirectStandardInput = true; } if (processInfo.WorkingDirectory != null) { TestContext.Progress.WriteLine($"WorkingDir: cd {processInfo.WorkingDirectory}"); } if (trace != null) { processInfo.EnvironmentVariables["GIT_TRACE"] = trace; } TestContext.Progress.WriteLine($"Invoking: {processInfo.FileName} {processInfo.Arguments}"); using (Process process = Process.Start(processInfo)) { if (standardInput != null) { process.StandardInput.Write(standardInput); process.StandardInput.Close(); } string result = process.StandardOutput.ReadToEnd(); process.WaitForExit(); if (expectedExitCode >= SuccessExitCode) { process.ExitCode.ShouldEqual(expectedExitCode, result); } else if (expectedExitCode == ExitCodeShouldNotBeZero) { process.ExitCode.ShouldNotEqual(SuccessExitCode, "Exit code should not be zero"); } ProcessHelper.WritePrefixedOutputLines("stdout> ", result); return(result); } }
public static ProcessResult InvokeProcess(string executionWorkingDirectory, string command, Dictionary <string, string> environmentVariables = null, Stream inputStream = null) { ProcessStartInfo processInfo = new ProcessStartInfo(Properties.Settings.Default.PathToGit); processInfo.WorkingDirectory = executionWorkingDirectory; processInfo.UseShellExecute = false; processInfo.RedirectStandardOutput = true; processInfo.RedirectStandardError = true; processInfo.Arguments = command; if (inputStream != null) { processInfo.RedirectStandardInput = true; } if (processInfo.WorkingDirectory != null) { TestContext.Progress.WriteLine($"WorkingDir: cd {processInfo.WorkingDirectory}"); } processInfo.EnvironmentVariables["GIT_TERMINAL_PROMPT"] = "0"; if (environmentVariables != null) { foreach (string key in environmentVariables.Keys) { TestContext.Progress.WriteLine($"Env: set {key}={environmentVariables[key]}"); processInfo.EnvironmentVariables[key] = environmentVariables[key]; } } TestContext.Progress.WriteLine($"Invoking: {processInfo.FileName} {processInfo.Arguments}"); ProcessResult result = ProcessHelper.Run(processInfo, inputStream: inputStream); ProcessHelper.WritePrefixedOutputLines("stdout> ", result.Output); ProcessHelper.WritePrefixedOutputLines("stderr> ", result.Errors); return(result); }