Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 using System; | |
| 2 using System.Collections.Generic; | |
| 3 using System.Linq; | |
| 4 using System.Management; | |
| 5 using System.Text; | |
| 6 | |
| 7 using EnvDTE; | |
| 8 using EnvDTE80; | |
| 9 | |
| 10 namespace UnitTests | |
| 11 { | |
| 12 class TestUtilities | |
| 13 { | |
| 14 /// <summary> | |
| 15 /// Utility to start an instance of Visual Studio and get it's DTE | |
| 16 /// </summary> | |
| 17 /// <returns>DTE of the started instance</returns> | |
| 18 public static DTE2 StartVisualStudioInstance() | |
| 19 { | |
| 20 // Set up filter to handle threading events and automatically retry calls | |
| 21 // to dte which fail because dte is busy | |
| 22 ComMessageFilter.Register(); | |
| 23 | |
| 24 Type vsType = Type.GetTypeFromProgID("VisualStudio.DTE.10.0"); | |
| 25 DTE2 vs = Activator.CreateInstance(vsType) as DTE2; | |
|
Petr Hosek
2012/07/10 05:37:21
You should check for vs being null here.
tysand
2012/07/11 05:23:46
Done.
| |
| 26 vs.MainWindow.Visible = true; | |
| 27 return vs; | |
| 28 } | |
| 29 | |
| 30 | |
| 31 /// <summary> | |
| 32 /// Properly cleans up after StartVisualStudioInstance() | |
| 33 /// </summary> | |
| 34 /// <param name="dte">Dte instance returned by StartVisualStudioInstance </param> | |
| 35 public static void CleanUpVisualStudioInstance(DTE2 dte) | |
| 36 { | |
| 37 dte.Quit(); | |
| 38 | |
| 39 // Stop the message filter | |
| 40 ComMessageFilter.Revoke(); | |
| 41 } | |
| 42 | |
| 43 /// <summary> | |
| 44 /// Returns the text contained in the output window pane | |
| 45 /// </summary> | |
| 46 /// <param name="pane">Pane to get text from</param> | |
| 47 /// <returns>Text in the window</returns> | |
| 48 public static string GetPaneText(OutputWindowPane pane) | |
| 49 { | |
| 50 TextSelection selection = pane.TextDocument.Selection; | |
| 51 selection.StartOfDocument(false); | |
| 52 selection.EndOfDocument(true); | |
| 53 return selection.Text; | |
| 54 } | |
| 55 | |
| 56 /// <summary> | |
| 57 /// Starts a python process that just sleeps waiting to be killed | |
| 58 /// Can be used with DoesProcessExist to verify that a process started/e xited | |
| 59 /// </summary> | |
| 60 /// <param name="identifierString">A unique string to identify the proce ss via command line</param> | |
| 61 /// <param name="timeout">Time in seconds to wait before process exits o n its own</param> | |
| 62 /// <returns>The process object that was started</returns> | |
| 63 public static System.Diagnostics.Process StartProcessForKilling(String i dentifierString, int timeout) | |
| 64 { | |
| 65 string args = String.Format( | |
| 66 "-c \"import time; time.sleep({0}); print '{1}'\"", | |
| 67 timeout, identifierString); | |
| 68 System.Diagnostics.Process proc = new System.Diagnostics.Process(); | |
| 69 proc.StartInfo.CreateNoWindow = true; | |
| 70 proc.StartInfo.UseShellExecute = false; | |
| 71 proc.StartInfo.FileName = "python.exe"; | |
| 72 proc.StartInfo.Arguments = args; | |
| 73 proc.Start(); | |
| 74 return proc; | |
| 75 } | |
| 76 | |
| 77 /// <summary> | |
| 78 /// Returns true if there is a running process that has command line arg uments | |
| 79 /// containing the given strings. Search is case-insensitive | |
| 80 /// </summary> | |
| 81 /// <param name="commandLineIdentifiers">Strings to check for</param> | |
| 82 /// <param name="processName">Name of the process executable</param> | |
| 83 /// <returns>True if some process has the string in its command line arg uments</returns> | |
| 84 public static bool DoesProcessExist(String[] commandLineIdentifiers, Str ing processName) | |
|
Petr Hosek
2012/07/10 05:37:21
You could possible switch the order of arguments a
tysand
2012/07/11 05:23:46
Done.
| |
| 85 { | |
| 86 List<string> results = new List<string>(); | |
| 87 string query = String.Format("select CommandLine from Win32_Process where Name='{0}'", processName); | |
| 88 using (ManagementObjectSearcher searcher = new ManagementObjectSearc her(query)) | |
| 89 { | |
| 90 using (ManagementObjectCollection result = searcher.Get()) | |
|
Petr Hosek
2012/07/10 05:37:21
You can combine the two using statements together.
tysand
2012/07/11 05:23:46
Can't since they are of different types.
On 2012/0
| |
| 91 { | |
| 92 foreach (ManagementObject process in result) | |
| 93 { | |
| 94 string commandLine = process["CommandLine"] as string; | |
| 95 if (String.IsNullOrEmpty(commandLine)) | |
| 96 break; | |
| 97 | |
| 98 // Check if the command line contains each of the requir ed identifiers | |
| 99 bool found = true; | |
| 100 foreach (String identifier in commandLineIdentifiers) | |
| 101 if (commandLine.IndexOf(identifier, StringComparison .InvariantCultureIgnoreCase) == -1) | |
| 102 found = false; | |
| 103 if (found) | |
| 104 return true; | |
|
Petr Hosek
2012/07/10 05:37:21
You can use the LINQ here `return commandLineIdent
tysand
2012/07/11 05:23:46
Made this return true if All identifiers were foun
| |
| 105 } | |
| 106 } | |
| 107 } | |
| 108 return false; | |
| 109 } | |
| 110 | |
| 111 /// <summary> | |
| 112 /// Overload taking a string instead of string array | |
| 113 /// </summary> | |
| 114 public static bool DoesProcessExist(String commandLineIdentifier, String processName) | |
| 115 { | |
| 116 String[] arr = { commandLineIdentifier }; | |
| 117 return DoesProcessExist(arr, processName); | |
| 118 } | |
| 119 | |
| 120 /// <summary> | |
| 121 /// Sets the active configuration for the solution by specifying the con figuration name | |
| 122 /// and platform name. A solution configuration containing a project con figuration for | |
| 123 /// the specified project which has the config and platform names specif ied is selected. | |
| 124 /// </summary> | |
| 125 /// <param name="configurationName">Ex: "Debug" or "Release"</param> | |
| 126 /// <param name="platformName">Ex: "Win32" or "NaCl" or "PPAPI"</param> | |
| 127 public static void SetSolutionConfiguration(DTE2 dte, String projectUniq ueName, String configurationName, String platformName) | |
| 128 { | |
| 129 foreach (EnvDTE.SolutionConfiguration config in dte.Solution.Solutio nBuild.SolutionConfigurations) | |
| 130 { | |
| 131 EnvDTE.SolutionContext context = null; | |
| 132 try | |
| 133 { | |
| 134 context = config.SolutionContexts.Item(projectUniqueName); | |
| 135 } | |
| 136 catch (ArgumentException) | |
| 137 { | |
| 138 throw new Exception(String.Format("Project unique name not f ound in solution: {0}", projectUniqueName)); | |
| 139 } | |
| 140 | |
| 141 if (context.PlatformName == platformName && context.Configuratio nName == configurationName) | |
|
Petr Hosek
2012/07/10 05:37:21
You should probably check here that the context is
tysand
2012/07/11 05:23:46
Done.
| |
| 142 { | |
| 143 config.Activate(); | |
| 144 return; | |
| 145 } | |
| 146 } | |
| 147 throw new Exception(String.Format("Matching configuration not found for {0}: {1}|{2}", | |
| 148 projectUniqueName, platformName, configurationName)); | |
| 149 } | |
| 150 } | |
| 151 } | |
| OLD | NEW |