Няма описание
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. using System;
  2. using System.IO;
  3. using UnityEditor.TestRunner.CommandLineParser;
  4. using UnityEditor.TestTools.TestRunner.Api;
  5. namespace UnityEditor.TestTools.TestRunner.CommandLineTest
  6. {
  7. internal class SettingsBuilder : ISettingsBuilder
  8. {
  9. private ITestSettingsDeserializer m_TestSettingsDeserializer;
  10. private Action<string> m_LogAction;
  11. private Action<string> m_LogWarningAction;
  12. internal Func<string, bool> fileExistsCheck = File.Exists;
  13. private Func<bool> m_ScriptCompilationFailedCheck;
  14. internal Func<string, string[]> readAllLines = filePath => File.ReadAllText(filePath).Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.RemoveEmptyEntries);
  15. public SettingsBuilder(ITestSettingsDeserializer testSettingsDeserializer, Action<string> logAction, Action<string> logWarningAction, Func<bool> scriptCompilationFailedCheck)
  16. {
  17. m_LogAction = logAction;
  18. m_LogWarningAction = logWarningAction;
  19. m_ScriptCompilationFailedCheck = scriptCompilationFailedCheck;
  20. m_TestSettingsDeserializer = testSettingsDeserializer;
  21. }
  22. public Api.ExecutionSettings BuildApiExecutionSettings(string[] commandLineArgs)
  23. {
  24. var quit = false;
  25. string testPlatform = TestMode.EditMode.ToString();
  26. string[] testFilters = null;
  27. string[] testCategories = null;
  28. string testSettingsFilePath = null;
  29. int? playerHeartbeatTimeout = null;
  30. bool runSynchronously = false;
  31. string[] testAssemblyNames = null;
  32. string buildPlayerPath = string.Empty;
  33. string orderedTestListFilePath = null;
  34. int retry = 0;
  35. int repeat = 0;
  36. int randomOrderSeed = 0;
  37. var optionSet = new CommandLineOptionSet(
  38. new CommandLineOption("quit", () => { quit = true; }),
  39. new CommandLineOption("testPlatform", platform => { testPlatform = platform; }),
  40. new CommandLineOption("editorTestsFilter", filters => { testFilters = filters; }),
  41. new CommandLineOption("testFilter", filters => { testFilters = filters; }),
  42. new CommandLineOption("editorTestsCategories", catagories => { testCategories = catagories; }),
  43. new CommandLineOption("testCategory", catagories => { testCategories = catagories; }),
  44. new CommandLineOption("testSettingsFile", settingsFilePath => { testSettingsFilePath = settingsFilePath; }),
  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. new CommandLineOption("randomOrderSeed", seed => { randomOrderSeed = int.Parse(seed);}),
  51. new CommandLineOption("retry", n => { retry = int.Parse(n); }),
  52. new CommandLineOption("repeat", n => { repeat = int.Parse(n); })
  53. );
  54. optionSet.Parse(commandLineArgs);
  55. DisplayQuitWarningIfQuitIsGiven(quit);
  56. CheckForScriptCompilationErrors();
  57. var testSettings = GetTestSettings(testSettingsFilePath);
  58. var filter = new Filter
  59. {
  60. testMode = testPlatform.ToLower() == "editmode" ? TestMode.EditMode : TestMode.PlayMode,
  61. groupNames = testFilters,
  62. categoryNames = testCategories,
  63. assemblyNames = testAssemblyNames
  64. };
  65. var settings = new Api.ExecutionSettings
  66. {
  67. filters = new []{ filter },
  68. overloadTestRunSettings = new RunSettings(testSettings),
  69. ignoreTests = testSettings?.ignoreTests,
  70. featureFlags = testSettings?.featureFlags,
  71. targetPlatform = GetBuildTarget(testPlatform),
  72. runSynchronously = runSynchronously,
  73. playerSavePath = buildPlayerPath,
  74. orderedTestNames = GetOrderedTestList(orderedTestListFilePath),
  75. repeatCount = repeat,
  76. retryCount = retry,
  77. randomOrderSeed = randomOrderSeed
  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. private 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. private void CheckForScriptCompilationErrors()
  109. {
  110. if (m_ScriptCompilationFailedCheck())
  111. {
  112. throw new SetupException(SetupException.ExceptionType.ScriptCompilationFailed);
  113. }
  114. }
  115. private ITestSettings GetTestSettings(string testSettingsFilePath)
  116. {
  117. ITestSettings testSettings = null;
  118. if (!string.IsNullOrEmpty(testSettingsFilePath))
  119. {
  120. if (!fileExistsCheck(testSettingsFilePath))
  121. {
  122. throw new SetupException(SetupException.ExceptionType.TestSettingsFileNotFound, testSettingsFilePath);
  123. }
  124. testSettings = m_TestSettingsDeserializer.GetSettingsFromJsonFile(testSettingsFilePath);
  125. }
  126. return testSettings;
  127. }
  128. private string[] GetOrderedTestList(string orderedTestListFilePath)
  129. {
  130. if (!string.IsNullOrEmpty(orderedTestListFilePath))
  131. {
  132. if (!fileExistsCheck(orderedTestListFilePath))
  133. {
  134. throw new SetupException(SetupException.ExceptionType.OrderedTestListFileNotFound, orderedTestListFilePath);
  135. }
  136. return readAllLines(orderedTestListFilePath);
  137. }
  138. return null;
  139. }
  140. private static BuildTarget? GetBuildTarget(string testPlatform)
  141. {
  142. var testPlatformLower = testPlatform.ToLower();
  143. if (testPlatformLower == "editmode" || testPlatformLower == "playmode" || testPlatformLower == "editor" ||
  144. string.IsNullOrEmpty(testPlatformLower))
  145. {
  146. return null;
  147. }
  148. try
  149. {
  150. return (BuildTarget)Enum.Parse(typeof(BuildTarget), testPlatform, true);
  151. }
  152. catch (ArgumentException)
  153. {
  154. throw new SetupException(SetupException.ExceptionType.PlatformNotFound, testPlatform);
  155. }
  156. }
  157. }
  158. }