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.

GuiHelper.cs 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Reflection;
  6. using System.Text.RegularExpressions;
  7. using Unity.CodeEditor;
  8. using UnityEditor.Utils;
  9. using UnityEngine;
  10. namespace UnityEditor.TestTools.TestRunner.GUI
  11. {
  12. internal class GuiHelper : IGuiHelper
  13. {
  14. public GuiHelper(IMonoCecilHelper monoCecilHelper, IAssetsDatabaseHelper assetsDatabaseHelper)
  15. {
  16. MonoCecilHelper = monoCecilHelper;
  17. AssetsDatabaseHelper = assetsDatabaseHelper;
  18. GetCSFiles = (dirPath, fileExtension) =>
  19. {
  20. return Directory.GetFiles(dirPath, $"*{fileExtension}", SearchOption.AllDirectories)
  21. .Select(Paths.UnifyDirectorySeparator);
  22. };
  23. }
  24. internal Func<string, string, IEnumerable<string>> GetCSFiles;
  25. protected IMonoCecilHelper MonoCecilHelper { get; private set; }
  26. public IAssetsDatabaseHelper AssetsDatabaseHelper { get; private set; }
  27. public IExternalCodeEditor Editor { get; internal set; }
  28. private const string FileExtension = ".cs";
  29. public void OpenScriptInExternalEditor(Type type, MethodInfo method)
  30. {
  31. var fileOpenInfo = GetFileOpenInfo(type, method);
  32. if (string.IsNullOrEmpty(fileOpenInfo.FilePath))
  33. {
  34. Debug.LogWarning("Failed to open test method source code in external editor. Inconsistent filename and yield return operator in target method.");
  35. return;
  36. }
  37. if (fileOpenInfo.LineNumber == 1)
  38. {
  39. Debug.LogWarning("Failed to get a line number for unity test method. So please find it in opened file in external editor.");
  40. }
  41. if (!fileOpenInfo.FilePath.Contains("Assets"))
  42. {
  43. (Editor ?? CodeEditor.CurrentEditor).OpenProject(fileOpenInfo.FilePath, fileOpenInfo.LineNumber, 1);
  44. }
  45. else
  46. {
  47. AssetsDatabaseHelper.OpenAssetInItsDefaultExternalEditor(fileOpenInfo.FilePath, fileOpenInfo.LineNumber);
  48. }
  49. }
  50. public IFileOpenInfo GetFileOpenInfo(Type type, MethodInfo method)
  51. {
  52. var fileOpenInfo = MonoCecilHelper.TryGetCecilFileOpenInfo(type, method);
  53. if (string.IsNullOrEmpty(fileOpenInfo.FilePath))
  54. {
  55. var dirPath = Paths.UnifyDirectorySeparator(Application.dataPath);
  56. var allCsFiles = GetCSFiles(dirPath, FileExtension);
  57. var fileName = allCsFiles.FirstOrDefault(x =>
  58. x.Split(Path.DirectorySeparatorChar).Last().Equals(string.Concat(GetTestFileName(type), FileExtension)));
  59. fileOpenInfo.FilePath = fileName ?? string.Empty;
  60. }
  61. if (!fileOpenInfo.FilePath.Contains("Assets"))
  62. {
  63. return fileOpenInfo;
  64. }
  65. fileOpenInfo.FilePath = FilePathToAssetsRelativeAndUnified(fileOpenInfo.FilePath);
  66. return fileOpenInfo;
  67. }
  68. internal static string GetTestFileName(Type type)
  69. {
  70. //This handles the case of a test in a nested class, getting the name of the base class
  71. if (type.FullName != null && type.Namespace != null && type.FullName.Contains("+"))
  72. {
  73. var removedNamespace = type.FullName.Substring(type.Namespace.Length + 1);
  74. return removedNamespace.Substring(0, removedNamespace.IndexOf("+", StringComparison.Ordinal));
  75. }
  76. return type.Name;
  77. }
  78. public string FilePathToAssetsRelativeAndUnified(string filePath)
  79. {
  80. if (string.IsNullOrEmpty(filePath))
  81. return string.Empty;
  82. #if UNITY_2021_3_OR_NEWER
  83. return Path.GetRelativePath(Directory.GetCurrentDirectory(), filePath);
  84. #else
  85. filePath = Paths.UnifyDirectorySeparator(filePath);
  86. var length = Paths.UnifyDirectorySeparator(Application.dataPath).Length - "Assets".Length;
  87. return filePath.Substring(length);
  88. #endif
  89. }
  90. public bool OpenScriptInExternalEditor(string stacktrace)
  91. {
  92. if (string.IsNullOrEmpty(stacktrace))
  93. return false;
  94. var regex = new Regex("in (?<path>.*):{1}(?<line>[0-9]+)");
  95. var matchingLines = stacktrace.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries).Where(x => regex.IsMatch(x)).ToList();
  96. if (!matchingLines.Any())
  97. return false;
  98. var fileOpenInfos = matchingLines
  99. .Select(x => regex.Match(x))
  100. .Select(x =>
  101. new FileOpenInfo
  102. {
  103. FilePath = x.Groups["path"].Value,
  104. LineNumber = int.Parse(x.Groups["line"].Value)
  105. }).ToList();
  106. var fileOpenInfo = fileOpenInfos
  107. .FirstOrDefault(openInfo => !string.IsNullOrEmpty(openInfo.FilePath) && File.Exists(openInfo.FilePath));
  108. if (fileOpenInfo == null)
  109. {
  110. return false;
  111. }
  112. var filePath = FilePathToAssetsRelativeAndUnified(fileOpenInfo.FilePath);
  113. AssetsDatabaseHelper.OpenAssetInItsDefaultExternalEditor(filePath, fileOpenInfo.LineNumber);
  114. return true;
  115. }
  116. }
  117. }