/// <summary> /// Recovers development environment from test case running. /// This has to be executed from UI thread. /// </summary> /// <param name="testInformation"></param> public static void PostRunTestCase(TestInformation testInformation) { try { if (testInformation.Debug) { if (!testRunners.ContainsKey(testInformation.AssemblyPath)) { return; } RunnerInformation runnerInformation = testRunners[testInformation.AssemblyPath]; // Detaching the NunitRunner process from debugger. DTE dte = (DTE)Package.GetGlobalService(typeof(DTE)); foreach (EnvDTE.Process localProcess in dte.Debugger.DebuggedProcesses) { if (localProcess.ProcessID == runnerInformation.Process.Id) { localProcess.Detach(true); } } } } catch (Exception ex) { Trace.TraceError("Error detaching process from debugger: " + ex.ToString()); } }
/// <summary> /// Prepares development environment for test case running. /// This has to be executed from UI thread. /// </summary> /// <param name="testInformation"></param> public static void PreRunTestCase(TestInformation testInformation) { testInformation.Stop = false; try { if (testInformation.Debug) { if (!testRunners.ContainsKey(testInformation.AssemblyPath)) { return; } RunnerInformation runnerInformation = testRunners[testInformation.AssemblyPath]; // Attaching the NunitRunner process to debugger. DTE dte = (DTE)Package.GetGlobalService(typeof(DTE)); foreach (EnvDTE.Process localProcess in dte.Debugger.LocalProcesses) { if (localProcess.ProcessID == runnerInformation.Process.Id) { int processId = runnerInformation.Process.Id; string localProcessName = localProcess.Name; localProcess.Attach(); } } } } catch (Exception ex) { Trace.TraceError("Error attaching process to debugger: " + ex.ToString()); } }
/// <summary> /// Runs a single test case in separate NunitRunner process synchronously. /// </summary> /// <param name="testInformation">Information identifying the test case and containing place holders for result information.</param> /// <param name="debug">Set to true to enable debug mode.</param> public static void RunTestCase(TestInformation testInformation, bool explicitRun) { if (!testRunners.ContainsKey(testInformation.AssemblyPath)) { return; } RunnerInformation runnerInformation = testRunners[testInformation.AssemblyPath]; try { runnerInformation.Client.RunTest(testInformation, explicitRun); } catch (Exception e) { Trace.TraceError("Error running test:" + e.ToString()); } }
/// <summary> /// Starts or restarts runner process and lists test cases it hosts. /// </summary> /// <param name="assemblyPath">Path to assembly to list test cases from.</param> /// <param name="type">The type.</param> /// <returns></returns> public static IList <string> StartRunner(string assemblyPath, PlatformType type) { // If runner already exists then shutdown the existing runner. if (testRunners.ContainsKey(assemblyPath)) { RunnerInformation runnerInformation = testRunners[assemblyPath]; try { runnerInformation.Client.Disconnect(); } catch (Exception) { } try { runnerInformation.Process.WaitForExit(200); } catch (Exception) { } try { if (!runnerInformation.Process.HasExited) { runnerInformation.Process.Kill(); } } catch (Exception) { } testRunners.Remove(assemblyPath); } try { // Parse the directory and file information from path. string directory = Path.GetDirectoryName(assemblyPath).ToString(); string fileName = Path.GetFileName(assemblyPath); // Starting a new NunitRunner process. // determine what item ProcessStartInfo startInfo = new ProcessStartInfo(); startInfo.FileName = Path.Combine( Directory.GetParent(Assembly.GetExecutingAssembly().Location).ToString(), GetNunitRunner(type)); startInfo.WorkingDirectory = directory; startInfo.Arguments = "serve " + fileName; startInfo.RedirectStandardOutput = false; startInfo.RedirectStandardInput = false; startInfo.UseShellExecute = false; startInfo.CreateNoWindow = true; RunnerInformation runnerInformation = new RunnerInformation(); runnerInformation.Process = System.Diagnostics.Process.Start(startInfo); // determine which mode we are in runnerInformation.Client = new RunnerClient(runnerInformation.Process); if (runnerInformation.Client.TestCases.Count > 0) { testRunners.Add(assemblyPath, runnerInformation); } else { runnerInformation.Client.Disconnect(); } return(runnerInformation.Client.TestCases); } catch (Exception e) { Trace.TraceError("Failed to list tests for " + assemblyPath + ": " + e.ToString()); return(new List <string>()); } }
public string RunCodeGame(RunnerInformation player1Info, RunnerInformation player2Info, Field field, IEnumerable <Bot> bots1, IEnumerable <Bot> bots2, FinishGameCondition finishCondition) { CSharpRunnerInformation csRunInfo1 = player1Info as CSharpRunnerInformation; CSharpRunnerInformation csRunInfo2 = player2Info as CSharpRunnerInformation; this.verifyRunParameters(csRunInfo1, csRunInfo2); string dirPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "CompilerTempFiles"); string fileName = generateUniqueFileName(dirPath); using (FileStream fs = new FileStream(Path.Combine(dirPath, fileName), FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite)) { BotJournalFileHelper.WriteFieldToFile(fs, field); StreamWriter sw = new StreamWriter(fs); sw.Write($" { csRunInfo1.PlayerName } ;"); sw.Flush(); BotJournalFileWatcher botFileWatcher = new BotJournalFileWatcher(dirPath, fileName); Process player1Process = null, player2Process = null; botFileWatcher.CommandEdited += (sender, e) => { GameCommand command = e.NewCommand; Console.WriteLine("File changed"); if (command.BotId == null) { return; } Console.WriteLine("user wrote full command"); try { IActionHandler actionHandler = ActionHandlersProvider.GetActionHandler(command.ActionType); Field newField = actionHandler.ApplyStep(command.StepParams, field); if (!newField.Equals(field)) { field = newField; fs.Position = 0; BotJournalFileHelper.WriteFieldToFile(fs, field); } } catch (ArgumentException) { } using (FileStream s = new FileStream(Path.Combine(dirPath, fileName), FileMode.Append, FileAccess.Write, FileShare.ReadWrite)) { sw = new StreamWriter(s); if (command.PlayerName.Trim().Equals(player1Info.PlayerName)) { sw.Write($" { player2Info.PlayerName } ;"); } else { sw.Write($" { player1Info.PlayerName } ;"); } sw.Flush(); } if (finishCondition.IsFinished(field, botFileWatcher.CommandCount)) { player1Process.Kill(); player2Process.Kill(); raiseFinishGameEvent(Path.Combine(dirPath, fileName)); botFileWatcher.Dispose(); } Console.WriteLine("user command processed"); }; player1Process = Process.Start(csRunInfo1.PathToExecutable, $" { dirPath } { fileName } { csRunInfo1.PlayerName } { generateStringParameterForBots(bots1) }"); player2Process = Process.Start(csRunInfo2.PathToExecutable, $" { dirPath } { fileName } { csRunInfo2.PlayerName } { generateStringParameterForBots(bots2) } "); } return(string.Empty); }