Skip to content

Commit e0b76e2

Browse files
authored
FIX: Workaround for System.CodeDom test failures relating to Unity 2022 and .NET Standard (#1529)
* FIX: Improved error message detail for codedom compilation errors and added workaround for compiling on Unity 2022 or with netstandard API profile.
1 parent 22f79a0 commit e0b76e2

2 files changed

Lines changed: 19 additions & 14 deletions

File tree

Assets/Tests/InputSystem/CoreTests_Editor.cs

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2181,9 +2181,9 @@ public void Editor_ActionTree_CompositesAreShownWithNiceNames()
21812181
Assert.That(tree["map/action"].children[0].displayName, Is.EqualTo("1D Axis"));
21822182
}
21832183

2184-
#if UNITY_STANDALONE // CodeDom API not available in most players. We only build and run this in the editor but we're
2185-
// still affected by the current platform.
2186-
#if !TEMP_DISABLE_EDITOR_TESTS_ON_TRUNK // Temporary: Disables tests while net-profile passed from UTR to trunk is overridden to netstandard (missing CodeDom)
2184+
#if UNITY_STANDALONE // CodeDom API not available in most players. We only build and run this in the editor but we're
2185+
// still affected by the current platform.
2186+
#if !NET_STANDARD_2_0 // Not possible to run when using .NET standard at the moment.
21872187
[Test]
21882188
[Category("Editor")]
21892189
[TestCase("MyControls (2)", "MyNamespace", "", "MyNamespace.MyControls2")]
@@ -2223,9 +2223,8 @@ public void Editor_CanGenerateCodeWrapperForInputAsset(string assetName, string
22232223
Assert.That(set1map.ToJson(), Is.EqualTo(map1.ToJson()));
22242224
}
22252225

2226+
#endif // !NET_STANDARD_2_0
22262227
#endif
2227-
#endif
2228-
22292228
// Can take any given registered layout and generate a cross-platform C# struct for it
22302229
// that collects all the control values from both proper and optional controls (based on
22312230
// all derived layouts).
@@ -2881,7 +2880,7 @@ private void AssertAssetIsUnmodifiedAfterExitingPlayMode(Action<InputActionAsset
28812880
}
28822881

28832882
#if UNITY_STANDALONE // CodeDom API not available in most players.
2884-
#if !TEMP_DISABLE_EDITOR_TESTS_ON_TRUNK // Temporary: Disables tests while net-profile passed from UTR to trunk is overridden to netstandard (missing CodeDom)
2883+
#if !NET_STANDARD_2_0 // Not possible to run when using .NET standard at the moment.
28852884
[Test]
28862885
[Category("Editor")]
28872886
[TestCase("Mouse", typeof(Mouse))]
@@ -3061,6 +3060,12 @@ internal static Type Compile(string code, string typeName, string options = null
30613060
var cp = new CompilerParameters { CompilerOptions = options };
30623061
cp.ReferencedAssemblies.Add($"{EditorApplication.applicationContentsPath}/Managed/UnityEngine/UnityEngine.CoreModule.dll");
30633062
cp.ReferencedAssemblies.Add("Library/ScriptAssemblies/Unity.InputSystem.dll");
3063+
#if UNITY_2022
3064+
// Currently there is are cross-references to netstandard, e.g. System.IEquatable<UnityEngine.Vector2>, System.IFormattable
3065+
// causing compilation failure for 2022 versions. This is a workaround for running these tests.
3066+
var netstandard = Assembly.Load("netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51");
3067+
cp.ReferencedAssemblies.Add(netstandard.Location);
3068+
#endif
30643069
var cr = codeProvider.CompileAssemblyFromSource(cp, code);
30653070

30663071
var assembly = cr.CompiledAssembly;
@@ -3070,7 +3075,12 @@ internal static Type Compile(string code, string typeName, string options = null
30703075
if (cr.Errors.HasErrors)
30713076
{
30723077
if (!Encoding.UTF8.GetBytes(cr.Errors[0].ErrorText).SequenceEqual(Encoding.UTF8.GetPreamble()))
3073-
Assert.Fail($"Compilation failed: {cr.Errors}");
3078+
{
3079+
var sb = new StringBuilder("Compilation of generated code failed:");
3080+
for (var i = 0; i < cr.Errors.Count; ++i)
3081+
sb.Append("\n").Append(cr.Errors[i].ErrorText);
3082+
Assert.Fail(sb.ToString());
3083+
}
30743084

30753085
foreach (var tempFile in cr.TempFiles)
30763086
{
@@ -3088,8 +3098,8 @@ internal static Type Compile(string code, string typeName, string options = null
30883098
return type;
30893099
}
30903100

3091-
#endif
3092-
#endif
3101+
#endif // !NET_STANDARD_2_0
3102+
#endif // UNITY_STANDALONE
30933103

30943104
[Test]
30953105
[Category("Editor")]

Assets/Tests/InputSystem/Unity.InputSystem.Tests.asmdef

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,6 @@
3030
"expression": "1.0",
3131
"define": "HAVE_DOCTOOLS_INSTALLED"
3232
},
33-
{
34-
"name": "Unity",
35-
"expression": "[2022.2.0a1,2022.2.0a10)",
36-
"define": "TEMP_DISABLE_EDITOR_TESTS_ON_TRUNK"
37-
},
3833
{
3934
"name": "Unity",
4035
"expression": "[2021.2,2021.3)",

0 commit comments

Comments
 (0)