Keine Beschreibung
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

Executer.cs 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using UnityEditor.TestRunner.TestLaunchers;
  5. using UnityEditor.TestTools.TestRunner.Api;
  6. using UnityEngine;
  7. namespace UnityEditor.TestTools.TestRunner.CommandLineTest
  8. {
  9. internal class Executer : IExecuter
  10. {
  11. internal IRunData runData = RunData.instance;
  12. private ITestRunnerApi m_TestRunnerApi;
  13. private ISettingsBuilder m_SettingsBuilder;
  14. private Action<string, object[]> m_LogErrorFormat;
  15. private Action<Exception> m_LogException;
  16. private Action<string> m_LogMessage;
  17. private Action<int> m_ExitEditorApplication;
  18. private Func<bool> m_ScriptCompilationFailedCheck;
  19. private Func<bool> m_IsRunActive;
  20. public Executer(ITestRunnerApi testRunnerApi, ISettingsBuilder settingsBuilder, Action<string, object[]> logErrorFormat, Action<Exception> logException, Action<string> logMessage, Action<int> exitEditorApplication, Func<bool> scriptCompilationFailedCheck, Func<bool> isRunActive)
  21. {
  22. m_TestRunnerApi = testRunnerApi;
  23. m_SettingsBuilder = settingsBuilder;
  24. m_LogErrorFormat = logErrorFormat;
  25. m_LogException = logException;
  26. m_LogMessage = logMessage;
  27. m_ExitEditorApplication = exitEditorApplication;
  28. m_ScriptCompilationFailedCheck = scriptCompilationFailedCheck;
  29. m_IsRunActive = isRunActive;
  30. }
  31. public string InitializeAndExecuteRun(string[] commandLineArgs)
  32. {
  33. Api.ExecutionSettings executionSettings;
  34. try
  35. {
  36. executionSettings = m_SettingsBuilder.BuildApiExecutionSettings(commandLineArgs);
  37. if (executionSettings.targetPlatform.HasValue)
  38. RemotePlayerLogController.instance.SetBuildTarget(executionSettings.targetPlatform.Value);
  39. }
  40. catch (SetupException exception)
  41. {
  42. HandleSetupException(exception);
  43. return string.Empty;
  44. }
  45. try
  46. {
  47. // It is important that the message starts with "Running tests for ", otherwise TestCleanConsole will fail.
  48. m_LogMessage($"Running tests for {executionSettings}");
  49. return m_TestRunnerApi.Execute(executionSettings);
  50. }
  51. catch (Exception exception)
  52. {
  53. m_LogException(exception);
  54. ExitApplication(ReturnCodes.RunError, "Exception when starting test run.");
  55. return string.Empty;
  56. }
  57. }
  58. public void ExitIfRunIsCompleted()
  59. {
  60. if (m_IsRunActive())
  61. {
  62. return;
  63. }
  64. var runState = runData.RunState;
  65. var returnCode = s_StateReturnCodes[runState];
  66. var reason = s_StateMessages[runState] ?? runData.RunErrorMessage;
  67. ExitApplication(returnCode, reason);
  68. }
  69. private void ExitApplication(ReturnCodes returnCode, string reason)
  70. {
  71. var returnCodeInt = (int)returnCode;
  72. m_LogMessage($"Test run completed. Exiting with code {returnCodeInt} ({returnCode}). {reason}");
  73. m_ExitEditorApplication(returnCodeInt);
  74. }
  75. public ExecutionSettings BuildExecutionSettings(string[] commandLineArgs)
  76. {
  77. return m_SettingsBuilder.BuildExecutionSettings(commandLineArgs);
  78. }
  79. internal enum ReturnCodes
  80. {
  81. Ok = 0,
  82. Failed = 2,
  83. RunError = 3,
  84. PlatformNotFoundReturnCode = 4
  85. }
  86. public void SetUpCallbacks(ExecutionSettings executionSettings)
  87. {
  88. RemotePlayerLogController.instance.SetLogsDirectory(executionSettings.DeviceLogsDirectory);
  89. var resultSavingCallback = ScriptableObject.CreateInstance<ResultsSavingCallbacks>();
  90. resultSavingCallback.m_ResultFilePath = executionSettings.TestResultsFile;
  91. var logSavingCallback = ScriptableObject.CreateInstance<LogSavingCallbacks>();
  92. TestRunnerApi.RegisterTestCallback(resultSavingCallback);
  93. TestRunnerApi.RegisterTestCallback(logSavingCallback);
  94. TestRunnerApi.RegisterTestCallback(new RunStateCallbacks());
  95. }
  96. public void ExitOnCompileErrors()
  97. {
  98. if (m_ScriptCompilationFailedCheck())
  99. {
  100. var handling = s_ExceptionHandlingMapping.First(h => h.m_ExceptionType == SetupException.ExceptionType.ScriptCompilationFailed);
  101. m_LogErrorFormat(handling.m_Message, new object[0]);
  102. ExitApplication(handling.m_ReturnCode, handling.m_Message);
  103. }
  104. }
  105. private void HandleSetupException(SetupException exception)
  106. {
  107. ExceptionHandling handling = s_ExceptionHandlingMapping.FirstOrDefault(h => h.m_ExceptionType == exception.Type) ?? new ExceptionHandling(exception.Type, "Unknown command line test run error. " + exception.Type, ReturnCodes.RunError);
  108. m_LogErrorFormat(handling.m_Message, exception.Details);
  109. ExitApplication(handling.m_ReturnCode, handling.m_Message);
  110. }
  111. private class ExceptionHandling
  112. {
  113. internal SetupException.ExceptionType m_ExceptionType;
  114. internal string m_Message;
  115. internal ReturnCodes m_ReturnCode;
  116. public ExceptionHandling(SetupException.ExceptionType exceptionType, string message, ReturnCodes returnCode)
  117. {
  118. m_ExceptionType = exceptionType;
  119. m_Message = message;
  120. m_ReturnCode = returnCode;
  121. }
  122. }
  123. private static ExceptionHandling[] s_ExceptionHandlingMapping = {
  124. new ExceptionHandling(SetupException.ExceptionType.ScriptCompilationFailed, "Scripts had compilation errors.", ReturnCodes.RunError),
  125. new ExceptionHandling(SetupException.ExceptionType.PlatformNotFound, "Test platform not found ({0}).", ReturnCodes.PlatformNotFoundReturnCode),
  126. new ExceptionHandling(SetupException.ExceptionType.TestSettingsFileNotFound, "Test settings file not found at {0}.", ReturnCodes.RunError),
  127. new ExceptionHandling(SetupException.ExceptionType.OrderedTestListFileNotFound, "Ordered test list file not found at {0}.", ReturnCodes.RunError)
  128. };
  129. private static IDictionary<TestRunState, string> s_StateMessages = new Dictionary<TestRunState, string>()
  130. {
  131. {TestRunState.NoCallbacksReceived, "No callbacks received."},
  132. {TestRunState.OneOrMoreTestsExecutedWithNoFailures, "Run completed."},
  133. {TestRunState.OneOrMoreTestsExecutedWithOneOrMoreFailed, "One or more tests failed."},
  134. {TestRunState.CompletedJobWithoutAnyTestsExecuted, "No tests were executed."},
  135. {TestRunState.RunError, null}
  136. };
  137. private static IDictionary<TestRunState, ReturnCodes> s_StateReturnCodes = new Dictionary<TestRunState, ReturnCodes>()
  138. {
  139. {TestRunState.NoCallbacksReceived, ReturnCodes.RunError},
  140. {TestRunState.OneOrMoreTestsExecutedWithNoFailures, ReturnCodes.Ok},
  141. {TestRunState.OneOrMoreTestsExecutedWithOneOrMoreFailed, ReturnCodes.Failed},
  142. {TestRunState.CompletedJobWithoutAnyTestsExecuted, ReturnCodes.Ok},
  143. {TestRunState.RunError, ReturnCodes.RunError}
  144. };
  145. }
  146. }