暫無描述
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.

FolderPathTestCompilationContextProvider.cs 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. using System;
  2. using System.Linq;
  3. using UnityEditor.Scripting.ScriptCompilation;
  4. namespace UnityEditor.TestTools.TestRunner.GUI.TestAssets
  5. {
  6. /// <inheritdoc />
  7. internal class FolderPathTestCompilationContextProvider : IFolderPathTestCompilationContextProvider
  8. {
  9. internal const string nUnitLibraryFilename = "nunit.framework.dll";
  10. private static ICustomScriptAssemblyMappingFinder s_CustomScriptAssemblyMappingFinder;
  11. internal static ICustomScriptAssemblyMappingFinder CustomScriptAssemblyMappingFinder
  12. {
  13. private get => s_CustomScriptAssemblyMappingFinder ?? (s_CustomScriptAssemblyMappingFinder = new CustomScriptAssemblyMappingFinder());
  14. set => s_CustomScriptAssemblyMappingFinder = value;
  15. }
  16. /// <summary>
  17. /// Checks if the provided folder path belongs to a Custom Test Assembly.
  18. /// A Custom Test Assembly is defined by a valid reference to the precompiled NUnit library.
  19. /// </summary>
  20. /// <param name="folderPath">The folder path to check.</param>
  21. /// <returns>True if a custom test assembly associated with the provided folder can be found; false otherwise.</returns>
  22. /// <exception cref="ArgumentNullException">The <paramref name="folderPath" /> string argument is null.</exception>
  23. public bool FolderPathBelongsToCustomTestAssembly(string folderPath)
  24. {
  25. if (folderPath == null)
  26. {
  27. throw new ArgumentNullException(nameof(folderPath));
  28. }
  29. var customScriptAssembly = CustomScriptAssemblyMappingFinder.FindCustomScriptAssemblyFromFolderPath(folderPath);
  30. var assemblyIsCustomTestAssembly = customScriptAssembly != null
  31. && customScriptAssembly.HasPrecompiledReference(nUnitLibraryFilename);
  32. return assemblyIsCustomTestAssembly;
  33. }
  34. /// <summary>
  35. /// Checks if the provided folder path belongs to an assembly capable of compiling Test Scripts.
  36. /// Unless the <see cref="PlayerSettings.playModeTestRunnerEnabled" /> setting is enabled,
  37. /// a Test Script can only be compiled in a Custom Test Assembly
  38. /// or an (implicit or explicit) <see cref="AssemblyFlags.EditorOnly" /> assembly.
  39. /// </summary>
  40. /// <param name="folderPath">The folder path to check.</param>
  41. /// <returns>True if Test Scripts can be successfully compiled when added to this folder path; false otherwise.</returns>
  42. /// <exception cref="ArgumentNullException">The <paramref name="folderPath" /> string argument is null.</exception>
  43. public bool TestScriptWillCompileInFolderPath(string folderPath)
  44. {
  45. if (folderPath == null)
  46. {
  47. throw new ArgumentNullException(nameof(folderPath));
  48. }
  49. if (PlayerSettings.playModeTestRunnerEnabled)
  50. {
  51. return true;
  52. }
  53. var customScriptAssembly = CustomScriptAssemblyMappingFinder.FindCustomScriptAssemblyFromFolderPath(folderPath);
  54. if (customScriptAssembly != null)
  55. {
  56. var assemblyCanCompileTestScripts = customScriptAssembly.HasPrecompiledReference(nUnitLibraryFilename)
  57. || customScriptAssembly.HasAssemblyFlag(AssemblyFlags.EditorOnly);
  58. return assemblyCanCompileTestScripts;
  59. }
  60. var isImplicitEditorAssembly = FolderPathBelongsToImplicitEditorAssembly(folderPath);
  61. return isImplicitEditorAssembly;
  62. }
  63. /// <summary>
  64. /// Checks if the provided folder path is a special editor path that belongs to an implicit editor assembly.
  65. /// </summary>
  66. /// <param name="folderPath">The folder path to check.</param>
  67. /// <returns>True if the folder path belongs to an implicit editor assembly; false otherwise.</returns>
  68. /// <exception cref="ArgumentNullException">The <paramref name="folderPath" /> string argument is null.</exception>
  69. internal static bool FolderPathBelongsToImplicitEditorAssembly(string folderPath)
  70. {
  71. if (folderPath == null)
  72. {
  73. throw new ArgumentNullException(nameof(folderPath));
  74. }
  75. const char unityPathSeparator = '/';
  76. var unityFormatPath = folderPath.Replace('\\', unityPathSeparator);
  77. var folderComponents = unityFormatPath.Split(unityPathSeparator);
  78. var folderComponentsIncludeEditorFolder = folderComponents.Any(n => n.ToLower().Equals("editor"));
  79. return folderComponentsIncludeEditorFolder;
  80. }
  81. }
  82. }