private ProcessHandler LauchProcessHandler() { var processHandler = new ProcessHandler(); ProcessStartInfo startInfo = new ProcessStartInfo { UseShellExecute = false, RedirectStandardOutput = true, RedirectStandardInput = true, RedirectStandardError = true, }; if (LocalUseDotnetCLI) { startInfo.FileName = "dotnet"; startInfo.Arguments = $"run --project {appPath} {arguments}"; } else { startInfo.FileName = appPath; startInfo.Arguments = arguments; } processHandler.Process.StartInfo = startInfo; processHandler.Process.OutputDataReceived += (object sender, System.Diagnostics.DataReceivedEventArgs e) => processHandler.Stdoutx.Append(e.Data); processHandler.Process.ErrorDataReceived += (object sender, System.Diagnostics.DataReceivedEventArgs e) => processHandler.Stderrx.Append(e.Data); processHandler.Process.Start(); processHandler.Process.BeginOutputReadLine(); processHandler.Process.BeginErrorReadLine(); return(processHandler); }
/// <summary> /// Inputing all strings from testInputPath into standard input of target application. /// </summary> /// <param name="startingInputs">Input to be sent to the target application before test input are entered.</param> /// <returns>true if there are no exception. On exception, current testInput, exception and standard output are returned.</returns> public ConcurrentBag <IO_Exception_Check_Result> Start(string[] startingInputs) { if (startRunning) { throw new StartAlreadyRunning("Start() is already running. Please use the StartExited event."); } else { startRunning = true; List <string> testInputArray = JsonConvert.DeserializeObject <List <string> >(System.IO.File.ReadAllText(testInputPath)); int testInputArrayLength = testInputArray.Count; var results = new ConcurrentBag <IO_Exception_Check_Result>(); var processes = new ProcessHandler[testInputArrayLength]; if (LocalUseDotnetCLI) { // Build the .net application once and then sets the --no-build so it isn't rebuilt Dotnet_CLI_build(); arguments += "--no-build"; } // Launches process for each item in the testInputArray for (int i = 0; i < testInputArrayLength; i++) { processes[i] = LauchProcessHandler(); try { foreach (var input in startingInputs) { processes[i].Process.StandardInput.WriteLine(input); } for (int x = 0; x < 20; x++) { processes[i].Process.StandardInput.WriteLine(testInputArray[i]); } } catch (Exception e) { // TODO: Add proper logging functionatly. Debug.WriteLine(e); } } Console.WriteLine("launched"); // Cleans up processes and adds any exceptions to results for (int i = 0; i < processes.Length; i++) { try { if (!processes[i].Process.WaitForExit(timeout)) { processes[i].Process.Kill(); } } catch (Exception e) { // TODO: Add proper logging functionatly. Debug.WriteLine(e); } if (!string.IsNullOrEmpty(processes[i].Stderrx.ToString())) { results.Add(new IO_Exception_Check_Result(testInputArray[i], processes[i].Stdoutx.ToString(), processes[i].Stderrx.ToString())); } else { if (!ReturnOnlyExceptions) { results.Add(new IO_Exception_Check_Result(testInputArray[i], processes[i].Stdoutx.ToString())); } } } startRunning = false; StartExited?.Invoke(this, System.EventArgs.Empty); return(results); } }