make tests work again

This commit is contained in:
vis2k 2021-08-24 23:14:38 +08:00
parent 55fb41ee0c
commit 1fa6830b69
4 changed files with 19 additions and 11 deletions

View File

@ -1,4 +1,3 @@
#if !UNITY_2020_1_OR_NEWER
using System;
using System.Collections.Generic;
using System.IO;
@ -28,6 +27,10 @@ public static class CompilationFinishedHook
// controls weather Weaver errors are reported direct to the Unity console (tests enable this)
public static bool UnityLogEnabled = true;
// for Unity 2020+ we use ILPostProcessor.
// only automatically invoke it for older versions.
// for newer versions, we still call CompilationFinished manually in tests.
#if !UNITY_2020_1_OR_NEWER
[InitializeOnLoadMethod]
public static void OnInitializeOnLoad()
{
@ -61,6 +64,7 @@ public static void WeaveExistingAssemblies()
UnityEditorInternal.InternalEditorUtility.RequestScriptReload();
#endif
}
#endif
static Assembly FindCompilationPipelineAssembly(string assemblyName) =>
CompilationPipeline.GetAssemblies().First(assembly => assembly.name == assemblyName);
@ -68,7 +72,7 @@ static Assembly FindCompilationPipelineAssembly(string assemblyName) =>
static bool CompilerMessagesContainError(CompilerMessage[] messages) =>
messages.Any(msg => msg.type == CompilerMessageType.Error);
static void OnCompilationFinished(string assemblyPath, CompilerMessage[] messages)
public static void OnCompilationFinished(string assemblyPath, CompilerMessage[] messages)
{
// Do nothing if there were compile errors on the target
if (CompilerMessagesContainError(messages))
@ -184,4 +188,3 @@ static bool WeaveFromFile(string assemblyPath, string[] dependencies)
}
}
}
#endif

View File

@ -1,6 +1,6 @@
// logger for compilation finished hook.
// where we need a callback and Debug.Log.
#if !UNITY_2020_1_OR_NEWER
//#if !UNITY_2020_1_OR_NEWER <- still need it for tests in 2020+
using Mono.CecilX;
using UnityEngine;
@ -27,4 +27,4 @@ public void Error(string message, MemberReference mr)
}
}
}
#endif
//#endif

View File

@ -113,6 +113,15 @@ public static void Build()
CompilerErrors = true;
}
}
#if UNITY_2020_1_OR_NEWER
// AssemblyBuilder doesn't invoke ILPostProcessor after build.
// we need to weave manually here in 2020+.
// -> we have the assemblyPath
// -> Weaver takes an AssemblyDefinition and resolver
// -> simply use the from-file Assembly + Resolver from before.
CompilationFinishedHook.OnCompilationFinished(assemblyPath, compilerMessages);
#endif
};
// Start build of assembly

View File

@ -45,23 +45,19 @@ protected void BuildAndWeaveTestAssembly(string className, string testName)
[OneTimeSetUp]
public void FixtureSetup()
{
// old weaver
#if !UNITY_2020_1_OR_NEWER
// we still use CompilationFinishedHook to run tests in 2020+
CompilationFinishedHook.UnityLogEnabled = false;
CompilationFinishedHook.OnWeaverError += HandleWeaverError;
CompilationFinishedHook.OnWeaverWarning += HandleWeaverWarning;
#endif
}
[OneTimeTearDown]
public void FixtureCleanup()
{
// old weaver
#if !UNITY_2020_1_OR_NEWER
// we still use CompilationFinishedHook to run tests in 2020+
CompilationFinishedHook.OnWeaverError -= HandleWeaverError;
CompilationFinishedHook.OnWeaverWarning -= HandleWeaverWarning;
CompilationFinishedHook.UnityLogEnabled = true;
#endif
}
[TearDown]