Нет описания
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

SettingsBuilder.cs 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. using System;
  2. using System.IO;
  3. using UnityEditor.TestRunner.CommandLineParser;
  4. using UnityEditor.TestTools.TestRunner.Api;
  5. using UnityEditor.TestTools.TestRunner.GUI;
  6. namespace UnityEditor.TestTools.TestRunner.CommandLineTest
  7. {
  8. internal class SettingsBuilder : ISettingsBuilder
  9. {
  10. private ITestSettingsDeserializer m_TestSettingsDeserializer;
  11. private Action<string> m_LogAction;
  12. private Action<string> m_LogWarningAction;
  13. internal Func<string, bool> fileExistsCheck = File.Exists;
  14. private Func<bool> m_ScriptCompilationFailedCheck;
  15. internal Func<string, string[]> readAllLines = filePath => File.ReadAllText(filePath).Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.RemoveEmptyEntries);
  16. public SettingsBuilder(ITestSettingsDeserializer testSettingsDeserializer, Action<string> logAction, Action<string> logWarningAction, Func<bool> scriptCompilationFailedCheck)
  17. {
  18. m_LogAction = logAction;
  19. m_LogWarningAction = logWarningAction;
  20. m_ScriptCompilationFailedCheck = scriptCompilationFailedCheck;
  21. m_TestSettingsDeserializer = testSettingsDeserializer;
  22. }
  23. public Api.ExecutionSettings BuildApiExecutionSettings(string[] commandLineArgs)
  24. {
  25. var quit = false;
  26. string testPlatform = TestMode.EditMode.ToString();
  27. string[] testFilters = null;
  28. string[] testCategories = null;
  29. string testSettingsFilePath = null;
  30. int testRepetitions = 1;
  31. int? playerHeartbeatTimeout = null;
  32. bool runSynchronously = false;
  33. string[] testAssemblyNames = null;
  34. string buildPlayerPath = string.Empty;
  35. string orderedTestListFilePath = null;
  36. var optionSet = new CommandLineOptionSet(
  37. new CommandLineOption("quit", () => { quit = true; }),
  38. new CommandLineOption("testPlatform", platform => { testPlatform = platform; }),
  39. new CommandLineOption("editorTestsFilter", filters => { testFilters = filters; }),
  40. new CommandLineOption("testFilter", filters => { testFilters = filters; }),
  41. new CommandLineOption("editorTestsCategories", catagories => { testCategories = catagories; }),
  42. new CommandLineOption("testCategory", catagories => { testCategories = catagories; }),
  43. new CommandLineOption("testSettingsFile", settingsFilePath => { testSettingsFilePath = settingsFilePath; }),
  44. new CommandLineOption("testRepetitions", reps => { testRepetitions = int.Parse(reps); }),
  45. new CommandLineOption("playerHeartbeatTimeout", timeout => { playerHeartbeatTimeout = int.Parse(timeout); }),
  46. new CommandLineOption("runSynchronously", () => { runSynchronously = true; }),
  47. new CommandLineOption("assemblyNames", assemblyNames => { testAssemblyNames = assemblyNames; }),
  48. new CommandLineOption("buildPlayerPath", buildPath => { buildPlayerPath = buildPath; }),
  49. new CommandLineOption("orderedTestListFile", filePath => { orderedTestListFilePath = filePath; })
  50. );
  51. optionSet.Parse(commandLineArgs);
  52. DisplayQuitWarningIfQuitIsGiven(quit);
  53. CheckForScriptCompilationErrors();
  54. LogParametersForRun(testPlatform, testFilters, testCategories, testSettingsFilePath);
  55. var testSettings = GetTestSettings(testSettingsFilePath);
  56. var filter = new Filter()
  57. {
  58. groupNames = testFilters,
  59. categoryNames = testCategories,
  60. assemblyNames = testAssemblyNames
  61. };
  62. var buildTarget = SetFilterAndGetBuildTarget(testPlatform, filter);
  63. RerunCallbackData.instance.runFilters = new []{new UITestRunnerFilter()
  64. {
  65. categoryNames = filter.categoryNames,
  66. groupNames = filter.groupNames,
  67. testRepetitions = testRepetitions
  68. }};
  69. RerunCallbackData.instance.testMode = filter.testMode;
  70. var settings = new Api.ExecutionSettings()
  71. {
  72. filters = new []{filter},
  73. overloadTestRunSettings = new RunSettings(testSettings),
  74. targetPlatform = buildTarget,
  75. runSynchronously = runSynchronously,
  76. playerSavePath = buildPlayerPath,
  77. orderedTestNames = GetOrderedTestList(orderedTestListFilePath)
  78. };
  79. if (playerHeartbeatTimeout != null)
  80. {
  81. settings.playerHeartbeatTimeout = playerHeartbeatTimeout.Value;
  82. }
  83. return settings;
  84. }
  85. public ExecutionSettings BuildExecutionSettings(string[] commandLineArgs)
  86. {
  87. string resultFilePath = null;
  88. string deviceLogsDirectory = null;
  89. var optionSet = new CommandLineOptionSet(
  90. new CommandLineOption("editorTestsResultFile", filePath => { resultFilePath = filePath; }),
  91. new CommandLineOption("testResults", filePath => { resultFilePath = filePath; }),
  92. new CommandLineOption("deviceLogs", dirPath => { deviceLogsDirectory = dirPath; })
  93. );
  94. optionSet.Parse(commandLineArgs);
  95. return new ExecutionSettings()
  96. {
  97. TestResultsFile = resultFilePath,
  98. DeviceLogsDirectory = deviceLogsDirectory
  99. };
  100. }
  101. void DisplayQuitWarningIfQuitIsGiven(bool quitIsGiven)
  102. {
  103. if (quitIsGiven)
  104. {
  105. m_LogWarningAction("Running tests from command line arguments will not work when \"quit\" is specified.");
  106. }
  107. }
  108. void CheckForScriptCompilationErrors()
  109. {
  110. if (m_ScriptCompilationFailedCheck())
  111. {
  112. throw new SetupException(SetupException.ExceptionType.ScriptCompilationFailed);
  113. }
  114. }
  115. void LogParametersForRun(string testPlatform, string[] testFilters, string[] testCategories, string testSettingsFilePath)
  116. {
  117. m_LogAction("Running tests for " + testPlatform);
  118. if (testFilters != null && testFilters.Length > 0)
  119. {
  120. m_LogAction("With test filter: " + string.Join(", ", testFilters));
  121. }
  122. if (testCategories != null && testCategories.Length > 0)
  123. {
  124. m_LogAction("With test categories: " + string.Join(", ", testCategories));
  125. }
  126. if (!string.IsNullOrEmpty(testSettingsFilePath))
  127. {
  128. m_LogAction("With test settings file: " + testSettingsFilePath);
  129. }
  130. }
  131. ITestSettings GetTestSettings(string testSettingsFilePath)
  132. {
  133. ITestSettings testSettings = null;
  134. if (!string.IsNullOrEmpty(testSettingsFilePath))
  135. {
  136. if (!fileExistsCheck(testSettingsFilePath))
  137. {
  138. throw new SetupException(SetupException.ExceptionType.TestSettingsFileNotFound, testSettingsFilePath);
  139. }
  140. testSettings = m_TestSettingsDeserializer.GetSettingsFromJsonFile(testSettingsFilePath);
  141. }
  142. return testSettings;
  143. }
  144. private string[] GetOrderedTestList(string orderedTestListFilePath)
  145. {
  146. if (!string.IsNullOrEmpty(orderedTestListFilePath))
  147. {
  148. if (!fileExistsCheck(orderedTestListFilePath))
  149. {
  150. throw new SetupException(SetupException.ExceptionType.OrderedTestListFileNotFound, orderedTestListFilePath);
  151. }
  152. return readAllLines(orderedTestListFilePath);
  153. }
  154. return null;
  155. }
  156. static BuildTarget? SetFilterAndGetBuildTarget(string testPlatform, Filter filter)
  157. {
  158. BuildTarget? buildTarget = null;
  159. if (testPlatform.ToLower() == "editmode")
  160. {
  161. filter.testMode = TestMode.EditMode;
  162. }
  163. else if (testPlatform.ToLower() == "playmode")
  164. {
  165. filter.testMode = TestMode.PlayMode;
  166. }
  167. else
  168. {
  169. try
  170. {
  171. buildTarget = (BuildTarget)Enum.Parse(typeof(BuildTarget), testPlatform, true);
  172. filter.testMode = TestMode.PlayMode;
  173. }
  174. catch (ArgumentException)
  175. {
  176. throw new SetupException(SetupException.ExceptionType.PlatformNotFound, testPlatform);
  177. }
  178. }
  179. return buildTarget;
  180. }
  181. }
  182. }