private static StringBuilder CreateEnvironment(string pathExtension) { StringDictionary envVariables = new ProcessStartInfo().EnvironmentVariables; if (!string.IsNullOrEmpty(pathExtension)) { envVariables["PATH"] = Utils.GetExtendedPath(pathExtension); } var envVariablesList = new List <string>(); foreach (DictionaryEntry entry in envVariables) { envVariablesList.Add($"{entry.Key}={entry.Value}"); } envVariablesList.Sort(); var result = new StringBuilder(); foreach (string envVariable in envVariablesList) { result.Append(envVariable); result.Length++; } result.Length++; return(result); }
public void GetExtendedPath_NoExtension_ReturnsPath() { string result = Utils.GetExtendedPath(""); string path = Environment.GetEnvironmentVariable("PATH"); result.Should().Be(path); }
private int LaunchProcess(string workingDirectory, IDictionary <string, string> additionalEnvVars, string command, string param, bool printTestOutput, bool throwIfError, List <string> output) { var processStartInfo = new ProcessStartInfo(command, param) { StandardOutputEncoding = Encoding.Default, RedirectStandardOutput = true, RedirectStandardError = false, UseShellExecute = false, CreateNoWindow = true, WorkingDirectory = workingDirectory }; if (additionalEnvVars != null) { foreach (var entry in additionalEnvVars) { processStartInfo.EnvironmentVariables[entry.Key] = entry.Value; } } if (!string.IsNullOrEmpty(_pathExtension)) { var path = Utils.GetExtendedPath(_pathExtension); if (processStartInfo.EnvironmentVariables.ContainsKey("PATH")) { path += $";{processStartInfo.EnvironmentVariables["PATH"]}"; } processStartInfo.EnvironmentVariables["PATH"] = path; } Process process = Process.Start(processStartInfo); if (process != null) { _reportProcessId?.Invoke(process.Id); } try { var waiter = new ProcessWaiter(process); if (printTestOutput) { _logger.LogInfo(String.Format(Resources.OutputOfCommandMessage, "", command, param)); } ReadTheStream(process, output, printTestOutput, throwIfError); if (printTestOutput) { _logger.LogInfo(String.Format(Resources.EndOfOutputMessage, "")); } return(waiter.WaitForExit()); } finally { process?.Dispose(); } }
public void GetExtendedPath_WithExtension_ExtendsPath() { const string toAdd = @"c:\some\path\to\add"; string result = Utils.GetExtendedPath(toAdd); string path = Environment.GetEnvironmentVariable("PATH"); result.Should().HaveLength(path.Length + toAdd.Length + 1); result.Should().Contain(path); string[] pathParts = result.Split(';'); pathParts.Should().Contain(s => s.Equals(toAdd)); }
private int LaunchProcess(string workingDirectory, string command, string param, bool printTestOutput, bool throwIfError, List <string> output) { var processStartInfo = new ProcessStartInfo(command, param) { StandardOutputEncoding = Encoding.Default, RedirectStandardOutput = true, RedirectStandardError = false, UseShellExecute = false, CreateNoWindow = true, WorkingDirectory = workingDirectory }; if (!string.IsNullOrEmpty(_pathExtension)) { processStartInfo.EnvironmentVariables["PATH"] = Utils.GetExtendedPath(_pathExtension); } Process process = Process.Start(processStartInfo); if (process != null) { _reportProcessId?.Invoke(process.Id); } try { var waiter = new ProcessWaiter(process); if (printTestOutput) { _logger.LogInfo( ">>>>>>>>>>>>>>> Output of command '" + command + " " + param + "'"); } ReadTheStream(process, output, printTestOutput, throwIfError); if (printTestOutput) { _logger.LogInfo("<<<<<<<<<<<<<<< End of Output"); } return(waiter.WaitForExit()); } finally { process?.Dispose(); } }