暂无描述
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

SyncTestRunEventsHandler.cs 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. #if TEST_FRAMEWORK
  2. using System;
  3. using System.Reflection;
  4. using UnityEditor;
  5. using UnityEngine;
  6. namespace Packages.Rider.Editor.UnitTesting
  7. {
  8. internal class SyncTestRunEventsHandler : ScriptableSingleton<SyncTestRunEventsHandler>
  9. {
  10. [SerializeField] private string m_SessionId;
  11. [SerializeField] private string m_HandlerCodeBase;
  12. [SerializeField] private string m_HandlerTypeName;
  13. [SerializeField] private string[] m_HandlerDependencies;
  14. [SerializeField] private bool m_RunInitialized;
  15. private object m_Handler;
  16. private MethodInfo m_OnSessionStartedMethodInfo;
  17. private MethodInfo m_OnTestStartedMethodInfo;
  18. private MethodInfo m_OnTestFinishedMethodInfo;
  19. private MethodInfo m_OnSessionFinishedMethodInfo;
  20. internal void InitRun(string sessionId, string handlerCodeBase, string handlerTypeName, string[] handlerDependencies)
  21. {
  22. if (PluginSettings.SelectedLoggingLevel >= LoggingLevel.TRACE)
  23. Debug.Log("Rider Test Runner: initializing sync callbacks handler: " +
  24. $"sessionId={sessionId}, " +
  25. $"codeBase={handlerCodeBase}, " +
  26. $"typeName={handlerTypeName}, " +
  27. $"dependencies={(handlerDependencies == null ? "" : string.Join("; ", handlerDependencies))}");
  28. m_SessionId = sessionId;
  29. m_HandlerCodeBase = handlerCodeBase;
  30. m_HandlerTypeName = handlerTypeName;
  31. m_HandlerDependencies = handlerDependencies;
  32. m_RunInitialized = true;
  33. CreateHandlerInstance();
  34. SafeInvokeHandlerMethod(m_OnSessionStartedMethodInfo, Array.Empty<object>());
  35. }
  36. private void OnEnable()
  37. {
  38. if (m_RunInitialized)
  39. CreateHandlerInstance();
  40. }
  41. internal void OnTestStarted(string testId)
  42. {
  43. if (m_RunInitialized)
  44. SafeInvokeHandlerMethod(m_OnTestStartedMethodInfo, new object[] {testId});
  45. }
  46. internal void OnTestFinished()
  47. {
  48. if (m_RunInitialized)
  49. SafeInvokeHandlerMethod(m_OnTestFinishedMethodInfo, Array.Empty<object>());
  50. }
  51. internal void OnRunFinished()
  52. {
  53. if (!m_RunInitialized)
  54. return;
  55. SafeInvokeHandlerMethod(m_OnSessionFinishedMethodInfo, Array.Empty<object>());
  56. CleanUp();
  57. m_RunInitialized = false;
  58. }
  59. private void SafeInvokeHandlerMethod(MethodInfo methodInfo, object[] args)
  60. {
  61. try
  62. {
  63. methodInfo?.Invoke(m_Handler, args);
  64. }
  65. catch (Exception e)
  66. {
  67. Debug.LogException(e);
  68. }
  69. }
  70. private void CreateHandlerInstance()
  71. {
  72. try
  73. {
  74. if (m_HandlerDependencies != null)
  75. foreach (var dependency in m_HandlerDependencies)
  76. {
  77. if (PluginSettings.SelectedLoggingLevel >= LoggingLevel.TRACE)
  78. Debug.Log($"Rider Test Runner: loading assembly from {dependency}");
  79. Assembly.LoadFrom(dependency);
  80. }
  81. if (PluginSettings.SelectedLoggingLevel >= LoggingLevel.TRACE)
  82. Debug.Log($"Rider Test Runner: loading assembly from {m_HandlerCodeBase}");
  83. var assembly = Assembly.LoadFrom(m_HandlerCodeBase);
  84. var type = assembly.GetType(m_HandlerTypeName);
  85. if (type == null)
  86. {
  87. Debug.LogError($"Rider Test Runner: type '{m_HandlerTypeName}' not found in assembly '{assembly.FullName}'");
  88. return;
  89. }
  90. if (PluginSettings.SelectedLoggingLevel >= LoggingLevel.TRACE)
  91. Debug.Log($"Rider Test Runner: creating instance of type '{type.AssemblyQualifiedName}'");
  92. m_Handler = Activator.CreateInstance(type, m_SessionId);
  93. m_OnSessionStartedMethodInfo = type.GetMethod("OnSessionStarted", BindingFlags.Instance | BindingFlags.Public);
  94. if (m_OnSessionStartedMethodInfo == null)
  95. {
  96. Debug.LogError($"Rider Test Runner: OnSessionStarted method not found in type='{type.AssemblyQualifiedName}'");
  97. return;
  98. }
  99. m_OnTestStartedMethodInfo = type.GetMethod("OnTestStarted", BindingFlags.Instance | BindingFlags.Public);
  100. if (m_OnTestStartedMethodInfo == null)
  101. {
  102. Debug.LogError($"Rider Test Runner: OnTestStarted method not found in type='{type.AssemblyQualifiedName}'");
  103. return;
  104. }
  105. m_OnTestFinishedMethodInfo = type.GetMethod("OnTestFinished", BindingFlags.Instance | BindingFlags.Public);
  106. if (m_OnTestFinishedMethodInfo == null)
  107. {
  108. Debug.LogError($"Rider Test Runner: OnTestFinished method not found in type='{type.AssemblyQualifiedName}'");
  109. return;
  110. }
  111. m_OnSessionFinishedMethodInfo = type.GetMethod("OnSessionFinished", BindingFlags.Instance | BindingFlags.Public);
  112. if (m_OnSessionFinishedMethodInfo == null)
  113. Debug.LogError($"Rider Test Runner: OnSessionFinished method not found in type='{type.AssemblyQualifiedName}'");
  114. }
  115. catch (Exception e)
  116. {
  117. Debug.LogException(e);
  118. }
  119. }
  120. private void CleanUp()
  121. {
  122. m_Handler = null;
  123. m_OnSessionStartedMethodInfo = null;
  124. m_OnSessionFinishedMethodInfo = null;
  125. m_OnTestStartedMethodInfo = null;
  126. m_OnTestFinishedMethodInfo = null;
  127. }
  128. }
  129. }
  130. #endif