public void NoOutput() { ProcessRunner runner = new ProcessRunner(); runner.WorkingDirectory = Path.GetDirectoryName(GetConsoleAppFileName()); runner.Start(GetConsoleAppFileName()); runner.WaitForExit(); Assert.IsFalse(runner.IsRunning, "IsRunning should be false."); Assert.AreEqual(0, runner.ExitCode, "Exit code is incorrect."); Assert.AreEqual(String.Empty, runner.StandardOutput, "Should not be any output."); Assert.AreEqual(String.Empty, runner.StandardError, "Should not be any error output."); }
public void NonZeroExitCode() { ProcessRunner runner = new ProcessRunner(); runner.WorkingDirectory = Path.GetDirectoryName(GetConsoleAppFileName()); int expectedExitCode = 1; Assert.IsFalse(runner.IsRunning, "IsRunning should be false."); runner.Start(GetConsoleAppFileName(), String.Concat("-exitcode:", expectedExitCode.ToString())); runner.WaitForExit(); Assert.AreEqual(expectedExitCode, runner.ExitCode, "Exit code is incorrect."); Assert.IsFalse(runner.IsRunning, "IsRunning should be false."); }
public void SingleLineOfOutput() { ProcessRunner runner = new ProcessRunner(); runner.WorkingDirectory = Path.GetDirectoryName(GetConsoleAppFileName()); string echoText = "Test"; string expectedOutput = String.Concat(echoText, "\r\n"); runner.Start(GetConsoleAppFileName(), String.Concat("-echo:", echoText)); runner.WaitForExit(); Assert.IsFalse(runner.IsRunning, "IsRunning should be false."); Assert.AreEqual(0, runner.ExitCode, "Exit code is incorrect."); Assert.AreEqual(expectedOutput, runner.StandardOutput, "Should have some output."); Assert.AreEqual(String.Empty, runner.StandardError, "Should not be any error output."); }
public static void RunGit(string workingDir, string arguments, Action<int> finished) { GitMessageView.AppendLine(workingDir + "> git " + arguments); ProcessRunner runner = new ProcessRunner(); runner.WorkingDirectory = workingDir; runner.LogStandardOutputAndError = false; runner.OutputLineReceived += (sender, e) => GitMessageView.AppendLine(e.Line); runner.ErrorLineReceived += (sender, e) => GitMessageView.AppendLine(e.Line); runner.ProcessExited += delegate { GitMessageView.AppendLine("Done. (exit code " + runner.ExitCode + ")"); if (finished != null) finished(runner.ExitCode); }; runner.Start("cmd", "/c git " + arguments); }
public void ProcessExitEvent() { exitEvent = new AutoResetEvent(false); ProcessRunner runner = new ProcessRunner(); runner.WorkingDirectory = Path.GetDirectoryName(GetConsoleAppFileName()); string echoText = "Test"; string expectedOutput = String.Concat(echoText, "\r\n"); runner.ProcessExited += new EventHandler(OnProcessExited); runner.Start(GetConsoleAppFileName(), String.Concat("-echo:", echoText)); bool exited = exitEvent.WaitOne(2500, true); Assert.IsTrue(exited, "Timed out waiting for exit event."); Assert.AreEqual(0, exitCode, "Exit code should be zero."); Assert.AreEqual(expectedOutput, standardOutput, "Should have some output."); Assert.AreEqual(String.Empty, standardError, "Should not be any error output."); }
bool Get32BitFlags(string assembly) { assembly = Path.Combine(binPath, assembly); string corflags = FileUtility.GetSdkPath("corflags.exe"); Assert.IsNotNull(corflags, "corflags.exe not found, this test requires the .NET SDK!"); ProcessRunner pr = new ProcessRunner(); Console.WriteLine(corflags + " \"" + assembly + "\""); pr.Start(corflags, "\"" + assembly + "\""); if (!pr.WaitForExit(5000)) { pr.Kill(); throw new InvalidOperationException("Timeout running corflags"); } else { Console.WriteLine(pr.StandardOutput); Match m = Regex.Match(pr.StandardOutput, @"32BIT\s*:\s*([01])"); if (m.Success) { return m.Groups[1].Value == "1"; } else { throw new InvalidOperationException("Invalid corflags output"); } } }
internal static string GetBlobHash(string gitExe, string fileName) { if (!File.Exists(fileName)) return null; ProcessRunner runner = new ProcessRunner(); runner.WorkingDirectory = Path.GetDirectoryName(fileName); runner.Start(gitExe, "ls-tree HEAD " + Path.GetFileName(fileName)); string blobHash = null; runner.OutputLineReceived += delegate(object sender, LineReceivedEventArgs e) { string[] parts = e.Line.Split(new[] { " ", "\t" }, StringSplitOptions.RemoveEmptyEntries); if (parts.Length >= 3) { if (parts[2].Length == 40) blobHash = parts[2]; } }; runner.WaitForExit(); return blobHash; }
static void GitGetFiles(string wcRoot, GitStatusSet statusSet) { ProcessRunner runner = new ProcessRunner(); runner.WorkingDirectory = wcRoot; runner.LogStandardOutputAndError = false; runner.OutputLineReceived += delegate(object sender, LineReceivedEventArgs e) { if (!string.IsNullOrEmpty(e.Line)) { statusSet.AddEntry(e.Line, GitStatus.OK); } }; string command = "git ls-files"; bool hasErrors = false; runner.ErrorLineReceived += delegate(object sender, LineReceivedEventArgs e) { if (!hasErrors) { hasErrors = true; GitMessageView.AppendLine(runner.WorkingDirectory + "> " + command); } GitMessageView.AppendLine(e.Line); }; runner.Start("cmd", "/c " + command); runner.WaitForExit(); }
static void GitGetStatus(string wcRoot, GitStatusSet statusSet) { string command = "git status --porcelain --untracked-files=no"; bool hasErrors = false; ProcessRunner runner = new ProcessRunner(); runner.WorkingDirectory = wcRoot; runner.LogStandardOutputAndError = false; runner.OutputLineReceived += delegate(object sender, LineReceivedEventArgs e) { if (!string.IsNullOrEmpty(e.Line)) { Match m = statusParseRegex.Match(e.Line); if (m.Success) { statusSet.AddEntry(m.Groups[2].Value, StatusFromText(m.Groups[1].Value)); } else { if (!hasErrors) { hasErrors = true; GitMessageView.AppendLine(runner.WorkingDirectory + "> " + command); } GitMessageView.AppendLine("unknown output: " + e.Line); } } }; runner.ErrorLineReceived += delegate(object sender, LineReceivedEventArgs e) { if (!hasErrors) { hasErrors = true; GitMessageView.AppendLine(runner.WorkingDirectory + "> " + command); } GitMessageView.AppendLine(e.Line); }; runner.Start("cmd", "/c " + command); runner.WaitForExit(); }
private static void GitGetFiles(string wcRoot, GitStatusSet statusSet) { var git = Git.FindGit(); if (git == null) return; var runner = new ProcessRunner(); runner.WorkingDirectory = wcRoot; runner.LogStandardOutputAndError = false; runner.OutputLineReceived += delegate (object sender, LineReceivedEventArgs e) { if (!string.IsNullOrEmpty(e.Line)) { statusSet.AddEntry(e.Line, GitStatus.OK); } }; var command = "ls-files"; var hasErrors = false; runner.ErrorLineReceived += delegate (object sender, LineReceivedEventArgs e) { if (!hasErrors) { hasErrors = true; GitMessageView.AppendLine(runner.WorkingDirectory + "> git " + command); } GitMessageView.AppendLine(e.Line); }; runner.Start(git, command); runner.WaitForExit(); }
private static void GitGetStatus(string wcRoot, GitStatusSet statusSet) { var git = Git.FindGit(); if (git == null) return; var command = "status --porcelain --untracked-files=no"; var hasErrors = false; var runner = new ProcessRunner(); runner.WorkingDirectory = wcRoot; runner.LogStandardOutputAndError = false; runner.OutputLineReceived += delegate (object sender, LineReceivedEventArgs e) { if (!string.IsNullOrEmpty(e.Line)) { var m = StatusParseRegex.Match(e.Line); if (m.Success) { statusSet.AddEntry(m.Groups[2].Value, StatusFromText(m.Groups[1].Value)); } else { if (!hasErrors) { // in front of first output line, print the command line we invoked hasErrors = true; GitMessageView.AppendLine(runner.WorkingDirectory + "> git " + command); } GitMessageView.AppendLine("unknown output: " + e.Line); } } }; runner.ErrorLineReceived += delegate (object sender, LineReceivedEventArgs e) { if (!hasErrors) { hasErrors = true; GitMessageView.AppendLine(runner.WorkingDirectory + "> git " + command); } GitMessageView.AppendLine(e.Line); }; runner.Start(git, command); runner.WaitForExit(); }
void RunTool(ExternalTool tool) { // Set these to somewhat useful values in case StingParser.Parse() passes when being called on one of them. string command = tool.Command; string args = tool.Arguments; // This needs it's own try/catch because if parsing these messages fail, the catch block after // the second try would also throw because MessageService.ShowError() calls StringParser.Parse() try { command = StringParser.Parse(tool.Command); args = StringParser.Parse(tool.Arguments); } catch (Exception ex) { MessageService.ShowError("${res:XML.MainMenu.ToolMenu.ExternalTools.ExecutionFailed} '" + ex.Message); return; } if (tool.PromptForArguments) { args = MessageService.ShowInputBox(tool.MenuCommand, "${res:XML.MainMenu.ToolMenu.ExternalTools.EnterArguments}", args); if (args == null) return; } try { if (tool.UseOutputPad) { ProcessRunner processRunner = new ProcessRunner(); processRunner.LogStandardOutputAndError = false; processRunner.ProcessExited += ProcessExitEvent; processRunner.OutputLineReceived += process_OutputLineReceived; processRunner.ErrorLineReceived += process_OutputLineReceived; processRunner.WorkingDirectory = StringParser.Parse(tool.InitialDirectory); if (args == null || args.Length == 0 || args.Trim('"', ' ').Length == 0) { processRunner.Start(command); } else { processRunner.Start(command, args); } } else { ProcessStartInfo startinfo; if (args == null || args.Length == 0 || args.Trim('"', ' ').Length == 0) { startinfo = new ProcessStartInfo(command); } else { startinfo = new ProcessStartInfo(command, args); } startinfo.WorkingDirectory = StringParser.Parse(tool.InitialDirectory); Process process = new Process(); process.StartInfo = startinfo; process.Start(); } } catch (Exception ex) { MessageService.ShowError("${res:XML.MainMenu.ToolMenu.ExternalTools.ExecutionFailed} '" + command + " " + args + "'\n" + ex.Message); } }
string GetBlobHash(string fileName) { ProcessRunner runner = new ProcessRunner(); runner.WorkingDirectory = Path.GetDirectoryName(fileName); runner.Start("cmd", "/c git ls-tree HEAD " + Path.GetFileName(fileName)); runner.WaitForExit(); string output = runner.StandardOutput.Trim(); string[] parts = output.Split(new[] { " ", "\t" }, StringSplitOptions.RemoveEmptyEntries); if (parts.Length < 3) return null; return parts[2]; }
public void LargeAmountOfOutput() { ProcessRunner runner = new ProcessRunner(); runner.WorkingDirectory = Path.GetDirectoryName(GetConsoleAppFileName()); string filename = "test.txt"; string fullFilename = Path.Combine(runner.WorkingDirectory, filename); try { string outputText = GetOutputText(); CreateTextFile(fullFilename, outputText); runner.Start(GetConsoleAppFileName(), String.Concat("-file:", filename)); bool exited = runner.WaitForExit(5000); Assert.IsTrue(exited, "App did not exit."); Assert.IsFalse(runner.IsRunning, "IsRunning should be false."); Assert.AreEqual(0, runner.ExitCode, "Exit code is incorrect."); Assert.AreEqual(outputText, runner.StandardOutput, "Should have some output."); Assert.AreEqual(String.Empty, runner.StandardError, "Should not be any error output."); } finally { if (File.Exists(fullFilename)) { File.Delete(fullFilename); } } }
static void GitGetStatus(string wcRoot, GitStatusSet statusSet) { string command = "git status --porcelain --untracked-files=no"; bool hasErrors = false; ProcessRunner runner = new ProcessRunner(); runner.WorkingDirectory = wcRoot; runner.LogStandardOutputAndError = false; string commandPrompt = wcRoot + ">@"; // C:\work\SD>@git.exe %* runner.OutputLineReceived += delegate(object sender, LineReceivedEventArgs e) { if (!string.IsNullOrEmpty(e.Line)) { Match m = statusParseRegex.Match(e.Line); if (m.Success) { statusSet.AddEntry(m.Groups[2].Value, StatusFromText(m.Groups[1].Value)); } else if (!e.Line.StartsWith(commandPrompt, StringComparison.Ordinal)) { // Suppress "unknown output" produced by git.cmd when git is installed // in the PATH but the git unix tools aren't if (!hasErrors) { // in front of first output line, print the command line we invoked hasErrors = true; GitMessageView.AppendLine(runner.WorkingDirectory + "> " + command); } GitMessageView.AppendLine("unknown output: " + e.Line); } } }; runner.ErrorLineReceived += delegate(object sender, LineReceivedEventArgs e) { if (!hasErrors) { hasErrors = true; GitMessageView.AppendLine(runner.WorkingDirectory + "> " + command); } GitMessageView.AppendLine(e.Line); }; runner.Start("cmd", "/c " + command); runner.WaitForExit(); }
/// <summary> /// This handler gets called when a tool in the Tool menu is clicked on. /// </summary> /// <param name="sender">The MenuCommand that sent the event.</param> /// <param name="e">Event arguments.</param> void ToolEvt(object sender, EventArgs e) { MenuCommand item = (MenuCommand)sender; // TODO: ToolLoader.Tool should get a string indexor. Overloading List or making it a Dictionary<string,ExternalTool> would work. for (int i = 0; i < ToolLoader.Tool.Count; ++i) { if (item.Text != ToolLoader.Tool[i].ToString()) { continue; } ExternalTool tool = (ExternalTool)ToolLoader.Tool[i]; // Set these to somewhat useful values in case StingParser.Parse() passes when being called on one of them. string command = tool.Command; string args = tool.Arguments; // This needs it's own try/catch because if parsing these messages fail, the catch block after // the second try would also throw because MessageService.ShowError() calls StringParser.Parse() try { command = StringParser.Parse(tool.Command); args = StringParser.Parse(tool.Arguments); } catch (Exception ex) { MessageService.ShowError("${res:XML.MainMenu.ToolMenu.ExternalTools.ExecutionFailed} '" + ex.Message); return; } if (tool.PromptForArguments) { args = MessageService.ShowInputBox(tool.MenuCommand, "${res:XML.MainMenu.ToolMenu.ExternalTools.EnterArguments}", args); if (args == null) return; } try { if (tool.UseOutputPad) { ProcessRunner processRunner = new ProcessRunner(); processRunner.LogStandardOutputAndError = false; processRunner.ProcessExited += ProcessExitEvent; processRunner.OutputLineReceived += process_OutputLineReceived; processRunner.ErrorLineReceived += process_OutputLineReceived; processRunner.WorkingDirectory = StringParser.Parse(tool.InitialDirectory); if (args == null || args.Length == 0 || args.Trim('"', ' ').Length == 0) { processRunner.Start(command); } else { processRunner.Start(command, args); } } else { ProcessStartInfo startinfo; if (args == null || args.Length == 0 || args.Trim('"', ' ').Length == 0) { startinfo = new ProcessStartInfo(command); } else { startinfo = new ProcessStartInfo(command, args); } startinfo.WorkingDirectory = StringParser.Parse(tool.InitialDirectory); Process process = new Process(); process.StartInfo = startinfo; process.Start(); } } catch (Exception ex) { MessageService.ShowError("${res:XML.MainMenu.ToolMenu.ExternalTools.ExecutionFailed} '" + command + " " + args + "'\n" + ex.Message); } return; } }