No Description
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.

BuildActionTaskBase.cs 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using NUnit.Framework.Interfaces;
  5. using UnityEngine;
  6. using UnityEngine.TestTools.Logging;
  7. namespace UnityEditor.TestTools.TestRunner.TestRun.Tasks
  8. {
  9. internal abstract class BuildActionTaskBase<T> : TestTaskBase
  10. {
  11. private string typeName;
  12. internal IAttributeFinder attributeFinder;
  13. internal Action<string> logAction = Debug.Log;
  14. internal Func<ILogScope> logScopeProvider = () => new LogScope();
  15. internal Func<Type, object> createInstance = Activator.CreateInstance;
  16. protected BuildActionTaskBase(IAttributeFinder attributeFinder)
  17. {
  18. this.attributeFinder = attributeFinder;
  19. typeName = typeof(T).Name;
  20. }
  21. protected abstract void Action(T target);
  22. public override IEnumerator Execute(TestJobData testJobData)
  23. {
  24. if (testJobData.testTree == null)
  25. {
  26. throw new Exception($"Test tree is not available for {GetType().Name}.");
  27. }
  28. var enumerator = ExecuteMethods(testJobData.testTree, testJobData.testFilter, testJobData.TargetRuntimePlatform ?? Application.platform);
  29. while (enumerator.MoveNext())
  30. {
  31. yield return null;
  32. }
  33. }
  34. private IEnumerator ExecuteMethods(ITest testTree, ITestFilter testRunnerFilter, RuntimePlatform targetPlatform)
  35. {
  36. var exceptions = new List<Exception>();
  37. foreach (var targetClassType in attributeFinder.Search(testTree, testRunnerFilter, targetPlatform))
  38. {
  39. try
  40. {
  41. var targetClass = (T)createInstance(targetClassType);
  42. logAction($"Executing {typeName} for: {targetClassType.FullName}.");
  43. using (var logScope = logScopeProvider())
  44. {
  45. Action(targetClass);
  46. logScope.EvaluateLogScope(true);
  47. }
  48. }
  49. catch (Exception ex)
  50. {
  51. exceptions.Add(ex);
  52. }
  53. }
  54. if (exceptions.Count > 0)
  55. {
  56. throw new AggregateException($"One or more exceptions when executing {typeName}.", exceptions);
  57. }
  58. yield break;
  59. }
  60. }
  61. }