EditorLoadedTestAssemblyProvider.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. using System.Collections.Generic;
  2. using System.IO;
  3. using System.Linq;
  4. using UnityEditor.Scripting.ScriptCompilation;
  5. using UnityEngine.TestTools;
  6. using UnityEngine.TestTools.Utils;
  7. namespace UnityEditor.TestTools.TestRunner
  8. {
  9. internal class EditorLoadedTestAssemblyProvider : IEditorLoadedTestAssemblyProvider
  10. {
  11. private const string k_NunitAssemblyName = "nunit.framework";
  12. private const string k_TestRunnerAssemblyName = "UnityEngine.TestRunner";
  13. internal const string k_PerformanceTestingAssemblyName = "Unity.PerformanceTesting";
  14. private readonly IEditorAssembliesProxy m_EditorAssembliesProxy;
  15. private readonly ScriptAssembly[] m_AllEditorScriptAssemblies;
  16. private readonly PrecompiledAssembly[] m_AllPrecompiledAssemblies;
  17. public EditorLoadedTestAssemblyProvider(IEditorCompilationInterfaceProxy compilationInterfaceProxy, IEditorAssembliesProxy editorAssembliesProxy)
  18. {
  19. m_EditorAssembliesProxy = editorAssembliesProxy;
  20. m_AllEditorScriptAssemblies = compilationInterfaceProxy.GetAllEditorScriptAssemblies();
  21. m_AllPrecompiledAssemblies = compilationInterfaceProxy.GetAllPrecompiledAssemblies();
  22. }
  23. public List<IAssemblyWrapper> GetAssembliesGroupedByType(TestPlatform mode)
  24. {
  25. var assemblies = GetAssembliesGroupedByTypeAsync(mode);
  26. while (assemblies.MoveNext())
  27. {
  28. }
  29. return assemblies.Current.Where(pair => mode.IsFlagIncluded(pair.Key)).SelectMany(pair => pair.Value).ToList();
  30. }
  31. public IEnumerator<IDictionary<TestPlatform, List<IAssemblyWrapper>>> GetAssembliesGroupedByTypeAsync(TestPlatform mode)
  32. {
  33. IAssemblyWrapper[] loadedAssemblies = m_EditorAssembliesProxy.loadedAssemblies;
  34. IDictionary<TestPlatform, List<IAssemblyWrapper>> result = new Dictionary<TestPlatform, List<IAssemblyWrapper>>()
  35. {
  36. {TestPlatform.EditMode, new List<IAssemblyWrapper>() },
  37. {TestPlatform.PlayMode, new List<IAssemblyWrapper>() }
  38. };
  39. foreach (var loadedAssembly in loadedAssemblies)
  40. {
  41. if (loadedAssembly.GetReferencedAssemblies().Any(x => x.Name == k_NunitAssemblyName || x.Name == k_TestRunnerAssemblyName || x.Name == k_PerformanceTestingAssemblyName))
  42. {
  43. var assemblyName = new FileInfo(loadedAssembly.Location).Name;
  44. var scriptAssemblies = m_AllEditorScriptAssemblies.Where(x => x.Filename == assemblyName).ToList();
  45. var precompiledAssemblies = m_AllPrecompiledAssemblies.Where(x => new FileInfo(x.Path).Name == assemblyName).ToList();
  46. if (scriptAssemblies.Count < 1 && precompiledAssemblies.Count < 1)
  47. {
  48. continue;
  49. }
  50. var assemblyFlags = scriptAssemblies.Any() ? scriptAssemblies.Single().Flags : precompiledAssemblies.Single().Flags;
  51. var assemblyType = (assemblyFlags & AssemblyFlags.EditorOnly) == AssemblyFlags.EditorOnly ? TestPlatform.EditMode : TestPlatform.PlayMode;
  52. result[assemblyType].Add(loadedAssembly);
  53. yield return null;
  54. }
  55. }
  56. yield return result;
  57. }
  58. }
  59. }