inicial
This commit is contained in:
+109
@@ -0,0 +1,109 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using NUnit.Framework.Interfaces;
|
||||
using UnityEngine;
|
||||
using UnityEngine.TestTools;
|
||||
|
||||
namespace UnityEditor.TestTools.TestRunner
|
||||
{
|
||||
internal abstract class AttributeFinderBase : IAttributeFinder
|
||||
{
|
||||
public abstract IEnumerable<Type> Search(ITest tests, ITestFilter filter, RuntimePlatform testTargetPlatform);
|
||||
}
|
||||
|
||||
internal interface IAttributeFinder
|
||||
{
|
||||
IEnumerable<Type> Search(ITest tests, ITestFilter filter, RuntimePlatform testTargetPlatform);
|
||||
}
|
||||
|
||||
internal abstract class AttributeFinderBase<T1, T2> : AttributeFinderBase where T2 : Attribute
|
||||
{
|
||||
private readonly Func<T2, Type> m_TypeSelector;
|
||||
protected AttributeFinderBase(Func<T2, Type> typeSelector)
|
||||
{
|
||||
m_TypeSelector = typeSelector;
|
||||
}
|
||||
|
||||
public override IEnumerable<Type> Search(ITest tests, ITestFilter filter, RuntimePlatform testTargetPlatform)
|
||||
{
|
||||
var selectedTests = new List<ITest>();
|
||||
GetMatchingTests(tests, filter, ref selectedTests, testTargetPlatform);
|
||||
|
||||
var result = new List<Type>();
|
||||
result.AddRange(GetTypesFromPrebuildAttributes(selectedTests));
|
||||
result.AddRange(GetTypesFromInterface(selectedTests, testTargetPlatform));
|
||||
|
||||
return result.Distinct();
|
||||
}
|
||||
|
||||
private static void GetMatchingTests(ITest tests, ITestFilter filter, ref List<ITest> resultList, RuntimePlatform testTargetPlatform)
|
||||
{
|
||||
foreach (var test in tests.Tests)
|
||||
{
|
||||
if (IsTestEnabledOnPlatform(test, testTargetPlatform))
|
||||
{
|
||||
if (test.IsSuite)
|
||||
{
|
||||
GetMatchingTests(test, filter, ref resultList, testTargetPlatform);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (filter.Pass(test))
|
||||
resultList.Add(test);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static bool IsTestEnabledOnPlatform(ITest test, RuntimePlatform testTargetPlatform)
|
||||
{
|
||||
if (test.Method == null)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
var attributesFromMethods = test.Method.GetCustomAttributes<UnityPlatformAttribute>(true);
|
||||
var attributesFromTypes = test.Method.TypeInfo.GetCustomAttributes<UnityPlatformAttribute>(true);
|
||||
|
||||
if (attributesFromMethods.Length == 0 && attributesFromTypes.Length == 0)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!attributesFromMethods.All(a => a.IsPlatformSupported(testTargetPlatform)))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!attributesFromTypes.All(a => a.IsPlatformSupported(testTargetPlatform)))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private IEnumerable<Type> GetTypesFromPrebuildAttributes(IEnumerable<ITest> tests)
|
||||
{
|
||||
var allAssemblies = AppDomain.CurrentDomain.GetAssemblies();
|
||||
allAssemblies = allAssemblies.Where(x => x.GetReferencedAssemblies().Any(z => z.Name == "UnityEditor.TestRunner")).ToArray();
|
||||
var attributesFromAssemblies = allAssemblies.SelectMany(assembly => assembly.GetCustomAttributes(typeof(T2), true).OfType<T2>());
|
||||
var attributesFromMethods = tests.SelectMany(t => t.Method.GetCustomAttributes<T2>(true).Select(attribute => attribute));
|
||||
var attributesFromTypes = tests.SelectMany(t => t.Method.TypeInfo.GetCustomAttributes<T2>(true).Select(attribute => attribute));
|
||||
|
||||
var result = new List<T2>();
|
||||
result.AddRange(attributesFromAssemblies);
|
||||
result.AddRange(attributesFromMethods);
|
||||
result.AddRange(attributesFromTypes);
|
||||
|
||||
return result.Select(m_TypeSelector).Where(type => type != null);
|
||||
}
|
||||
|
||||
private static IEnumerable<Type> GetTypesFromInterface(IEnumerable<ITest> selectedTests, RuntimePlatform testTargetPlatform)
|
||||
{
|
||||
var typesWithInterfaces = selectedTests.Where(t => typeof(T1).IsAssignableFrom(t.Method.TypeInfo.Type) && IsTestEnabledOnPlatform(t, testTargetPlatform));
|
||||
return typesWithInterfaces.Select(t => t.Method.TypeInfo.Type);
|
||||
}
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 5d4de3d4682a8d641907cc75e4fb950e
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
+46
@@ -0,0 +1,46 @@
|
||||
using System;
|
||||
|
||||
namespace UnityEditor.TestTools.TestRunner
|
||||
{
|
||||
internal class DelayedCallback
|
||||
{
|
||||
private Action m_Callback;
|
||||
private double m_CallbackTime;
|
||||
private double m_Delay;
|
||||
|
||||
public DelayedCallback(Action function, double timeFromNow)
|
||||
{
|
||||
m_Callback = function;
|
||||
m_CallbackTime = EditorApplication.timeSinceStartup + timeFromNow;
|
||||
m_Delay = timeFromNow;
|
||||
EditorApplication.update += Update;
|
||||
}
|
||||
|
||||
public void Clear()
|
||||
{
|
||||
EditorApplication.update -= Update;
|
||||
m_CallbackTime = 0.0;
|
||||
m_Callback = null;
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
if (EditorApplication.timeSinceStartup > m_CallbackTime)
|
||||
{
|
||||
// Clear state before firing callback to ensure reset (callback could call ExitGUI)
|
||||
var callback = m_Callback;
|
||||
Clear();
|
||||
|
||||
callback?.Invoke();
|
||||
}
|
||||
}
|
||||
|
||||
public void Reset()
|
||||
{
|
||||
if (m_Callback != null)
|
||||
{
|
||||
m_CallbackTime = EditorApplication.timeSinceStartup + m_Delay;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b9d121df8c444236a5b38ccfadfdd1a7
|
||||
timeCreated: 1583140472
|
||||
+247
@@ -0,0 +1,247 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using NUnit.Framework.Interfaces;
|
||||
using UnityEditor.TestTools.TestRunner.GUI;
|
||||
using UnityEngine;
|
||||
|
||||
namespace UnityEditor.TestRunner.TestLaunchers
|
||||
{
|
||||
internal static class FilePathMetaInfo
|
||||
{
|
||||
[Serializable]
|
||||
private struct FileReference
|
||||
{
|
||||
public string FilePath;
|
||||
public int LineNumber;
|
||||
}
|
||||
|
||||
private enum PathType
|
||||
{
|
||||
ProjectRepositoryPath,
|
||||
ProjectPath,
|
||||
}
|
||||
|
||||
public static void TryCreateFile(ITest runnerLoadedTest, BuildPlayerOptions playerBuildOptions)
|
||||
{
|
||||
try
|
||||
{
|
||||
var metaFileDestinationPath = GetMetaDestinationPath(playerBuildOptions);
|
||||
var repositoryPath = GetPathFromArgs(PathType.ProjectRepositoryPath);
|
||||
// if no path is given, early out so we do not pollute the build player folder with the file path file.
|
||||
if (string.IsNullOrEmpty(repositoryPath))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// Create a dictionary for the test names and their file paths
|
||||
var testFilePaths = new Dictionary<string, FileReference>();
|
||||
RecursivelyPopulateFileReferences(runnerLoadedTest, testFilePaths, repositoryPath, new GuiHelper(new MonoCecilHelper(), new AssetsDatabaseHelper()));
|
||||
SaveToJsonFile(testFilePaths, metaFileDestinationPath);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Debug.LogWarning("Saving test file path meta info failed: " + e.Message);
|
||||
}
|
||||
}
|
||||
|
||||
// This function serializes dictionary to json file, all the logic would not be necessary if Unity was able to serialize Dictionaries, or if we could use Newtonsoft.Json.
|
||||
// This function could be changed later on, or we can use different data structure than Dictionary.
|
||||
private static void SaveToJsonFile(Dictionary<string, FileReference> testFilePaths, string metaFileDestinationPath)
|
||||
{
|
||||
using (var fileStream = File.CreateText(Path.Combine(metaFileDestinationPath, "TestFileReferences.json")))
|
||||
{
|
||||
fileStream.WriteLine("{");
|
||||
|
||||
foreach (var testFilePath in testFilePaths)
|
||||
{
|
||||
fileStream.WriteLine($" \"{JavaScriptStringEncode(testFilePath.Key)}\": {{");
|
||||
fileStream.WriteLine($" \"filePath\": \"{JavaScriptStringEncode(testFilePath.Value.FilePath)}\",");
|
||||
fileStream.WriteLine($" \"lineNumber\": {testFilePath.Value.LineNumber}");
|
||||
// check if it is the last element in the dictionary
|
||||
if (testFilePath.Key != testFilePaths.Keys.Last())
|
||||
{
|
||||
fileStream.WriteLine(" },");
|
||||
}
|
||||
else
|
||||
{
|
||||
fileStream.WriteLine(" }");
|
||||
}
|
||||
}
|
||||
|
||||
fileStream.WriteLine("}");
|
||||
}
|
||||
}
|
||||
|
||||
private static string GetMetaDestinationPath(BuildPlayerOptions playerBuildOptions)
|
||||
{
|
||||
// If we are Auto-Running the player, use project path instead of player build path because it will be wiped out after successful run.
|
||||
if ((playerBuildOptions.options & BuildOptions.AutoRunPlayer) != 0)
|
||||
{
|
||||
return Path.Combine(GetPathFromArgs(PathType.ProjectPath));
|
||||
}
|
||||
|
||||
// if the buildOutputPath is for a file, then get the directory of it
|
||||
return File.Exists(playerBuildOptions.locationPathName) ? Path.GetDirectoryName(playerBuildOptions.locationPathName) : playerBuildOptions.locationPathName;
|
||||
}
|
||||
|
||||
private static void RecursivelyPopulateFileReferences(ITest test, Dictionary<string, FileReference> testFilePaths, string repositoryPath, IGuiHelper guiHelper)
|
||||
{
|
||||
if (test.HasChildren)
|
||||
{
|
||||
foreach (var child in test.Tests)
|
||||
{
|
||||
RecursivelyPopulateFileReferences(child, testFilePaths, repositoryPath, guiHelper);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
var testMethod = test.Method;
|
||||
if (testMethod == null)
|
||||
{
|
||||
testMethod = test.Parent.Method;
|
||||
if (testMethod == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
var methodInfo = test.Method.MethodInfo;
|
||||
var type = test.TypeInfo.Type;
|
||||
var fileOpenInfo = guiHelper.GetFileOpenInfo(type, methodInfo);
|
||||
var filePathString = Path.Combine(repositoryPath, fileOpenInfo.FilePath);
|
||||
var lineNumber = fileOpenInfo.LineNumber;
|
||||
var fileReference = new FileReference
|
||||
{
|
||||
FilePath = filePathString,
|
||||
LineNumber = lineNumber
|
||||
};
|
||||
// Cannot be simplified with .TryAdd because Unity 2020.3 and below does not have it.
|
||||
if (!testFilePaths.ContainsKey(test.FullName))
|
||||
{
|
||||
testFilePaths.Add(test.FullName, fileReference);
|
||||
}
|
||||
}
|
||||
|
||||
private static string GetPathFromArgs(PathType type)
|
||||
{
|
||||
var commandLineArgs = Environment.GetCommandLineArgs();
|
||||
|
||||
string lookFor;
|
||||
switch (type)
|
||||
{
|
||||
case PathType.ProjectRepositoryPath:
|
||||
lookFor = "-projectRepositoryPath";
|
||||
break;
|
||||
case PathType.ProjectPath:
|
||||
lookFor = "-projectPath";
|
||||
break;
|
||||
default:
|
||||
throw new ArgumentException("Invalid PathType");
|
||||
}
|
||||
|
||||
for (var i = 0; i < commandLineArgs.Length; i++)
|
||||
{
|
||||
if (commandLineArgs[i].Equals(lookFor))
|
||||
{
|
||||
return commandLineArgs[i + 1];
|
||||
}
|
||||
}
|
||||
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
// Below implementation is copy-paste from HttpUtility.JavaScriptStringEncode
|
||||
private static string JavaScriptStringEncode(string value) {
|
||||
if (String.IsNullOrEmpty(value)) {
|
||||
return String.Empty;
|
||||
}
|
||||
|
||||
StringBuilder b = null;
|
||||
int startIndex = 0;
|
||||
int count = 0;
|
||||
for (int i = 0; i < value.Length; i++) {
|
||||
char c = value[i];
|
||||
|
||||
// Append the unhandled characters (that do not require special treament)
|
||||
// to the string builder when special characters are detected.
|
||||
if (CharRequiresJavaScriptEncoding(c)) {
|
||||
if (b == null) {
|
||||
b = new StringBuilder(value.Length + 5);
|
||||
}
|
||||
|
||||
if (count > 0) {
|
||||
b.Append(value, startIndex, count);
|
||||
}
|
||||
|
||||
startIndex = i + 1;
|
||||
count = 0;
|
||||
}
|
||||
|
||||
switch (c) {
|
||||
case '\r':
|
||||
b.Append("\\r");
|
||||
break;
|
||||
case '\t':
|
||||
b.Append("\\t");
|
||||
break;
|
||||
case '\"':
|
||||
b.Append("\\\"");
|
||||
break;
|
||||
case '\\':
|
||||
b.Append("\\\\");
|
||||
break;
|
||||
case '\n':
|
||||
b.Append("\\n");
|
||||
break;
|
||||
case '\b':
|
||||
b.Append("\\b");
|
||||
break;
|
||||
case '\f':
|
||||
b.Append("\\f");
|
||||
break;
|
||||
default:
|
||||
if (CharRequiresJavaScriptEncoding(c)) {
|
||||
AppendCharAsUnicodeJavaScript(b, c);
|
||||
}
|
||||
else {
|
||||
count++;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (b == null) {
|
||||
return value;
|
||||
}
|
||||
|
||||
if (count > 0) {
|
||||
b.Append(value, startIndex, count);
|
||||
}
|
||||
|
||||
return b.ToString();
|
||||
}
|
||||
|
||||
private static bool CharRequiresJavaScriptEncoding(char c) {
|
||||
return c < 0x20 // control chars always have to be encoded
|
||||
|| c == '\"' // chars which must be encoded per JSON spec
|
||||
|| c == '\\'
|
||||
|| c == '\'' // HTML-sensitive chars encoded for safety
|
||||
|| c == '<'
|
||||
|| c == '>'
|
||||
|| c == '&'
|
||||
|| c == '\u0085' // newline chars (see Unicode 6.2, Table 5-1 [http://www.unicode.org/versions/Unicode6.2.0/ch05.pdf]) have to be encoded (DevDiv #663531)
|
||||
|| c == '\u2028'
|
||||
|| c == '\u2029';
|
||||
}
|
||||
|
||||
private static void AppendCharAsUnicodeJavaScript(StringBuilder builder, char c) {
|
||||
builder.Append("\\u");
|
||||
builder.Append(((int)c).ToString("x4", CultureInfo.InvariantCulture));
|
||||
}
|
||||
}
|
||||
}
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 759855a435cd4b22ade51836cc8b00d6
|
||||
timeCreated: 1685434461
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
using System;
|
||||
using System.Text;
|
||||
|
||||
namespace UnityEditor.TestTools.TestRunner
|
||||
{
|
||||
internal class PlayerLauncherBuildOptions
|
||||
{
|
||||
public BuildPlayerOptions BuildPlayerOptions;
|
||||
public string PlayerDirectory;
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
var str = new StringBuilder();
|
||||
str.AppendLine("locationPathName = " + BuildPlayerOptions.locationPathName);
|
||||
str.AppendLine("target = " + BuildPlayerOptions.target);
|
||||
str.AppendLine("scenes = " + string.Join(", ", BuildPlayerOptions.scenes));
|
||||
str.AppendLine("assetBundleManifestPath = " + BuildPlayerOptions.assetBundleManifestPath);
|
||||
str.AppendLine("options.Development = " + ((BuildPlayerOptions.options & BuildOptions.Development) != 0));
|
||||
str.AppendLine("options.AutoRunPlayer = " + ((BuildPlayerOptions.options & BuildOptions.AutoRunPlayer) != 0));
|
||||
str.AppendLine("options.ForceEnableAssertions = " + ((BuildPlayerOptions.options & BuildOptions.ForceEnableAssertions) != 0));
|
||||
return str.ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 2a0bd678385f98e4d8eabdfc07d62b4f
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
using System;
|
||||
using UnityEngine.TestTools;
|
||||
|
||||
namespace UnityEditor.TestTools.TestRunner
|
||||
{
|
||||
internal class PostbuildCleanupAttributeFinder : AttributeFinderBase<IPostBuildCleanup, PostBuildCleanupAttribute>
|
||||
{
|
||||
public PostbuildCleanupAttributeFinder() : base(attribute => attribute.TargetClass) {}
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 2c2dfcbbb77359547bcaa7cdabd47ebb
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
using System;
|
||||
using UnityEngine.TestTools;
|
||||
|
||||
namespace UnityEditor.TestTools.TestRunner
|
||||
{
|
||||
internal class PrebuildSetupAttributeFinder : AttributeFinderBase<IPrebuildSetup, PrebuildSetupAttribute>
|
||||
{
|
||||
public PrebuildSetupAttributeFinder() : base(attribute => attribute.TargetClass) {}
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 3c4ccfb0896bcf44da13e152b267aa49
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user