Skip to content

Commit

Permalink
[tests] Add net6.0 TargetFramework. (#888)
Browse files Browse the repository at this point in the history
Today, even when building and testing on .NET 6, most of our test
suites are still building and running with `net472`.

Fix this by adding `net6.0` to `$(TargetFrameworks)` and updating CI
to run the `net6.0` version instead of the `net472` version.

Fixes were required to account for differences between `net472` and
`net6.0` behavior, in particular:

  * The semantics for the [Round-trip format specifier `R`][0]
    changed between .NET Framework and .NET 6.
    Update `ConstantPoolFloatItem.ToString()` and
    `ConstantPoolDoubleItem.ToString()` to use the G9 and G17
    format specifiers, respectively, so that behavior is consistent.

  * Types such as `System.String` moved from `mscorlib.dll` to
    `System.Private.CoreLib.dll`, which impacts the output of
    `JavaNativeTypeManager.GetPackageName()`, as the assembly name
    is taken into consideration.  Assertions around
    `JavaNativeTypeManager.GetPackageName()` need to be based on the
    target framework.

  * When using `CSharpCompilation.Create()`, `System.Runtime.dll`
    must now be referenced when building on net6+, otherwise certain
    types won't be resolved:

        error CS0012: The type 'Enum' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Runtime, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. (SourceFile([871..891)))
        error CS0012: The type 'ValueType' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Runtime, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. (SourceFile([944..962)))
        error CS0012: The type 'IDisposable' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Runtime, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. (SourceFile([312..321)))

  * `MarshalMemberBuilderTest.cs` must be disabled when building for
    .NET 6+, as `AssemblyBuilderAccess.Save` doesn't exist, nor does
    `AppDomain.DefineDynamicAssembly()`, nor
    `LambdaExpression.CompileToMethod()`.
    Related: #616

        error CS0117: 'AssemblyBuilderAccess' does not contain a definition for 'Save'
        error CS1061: 'AppDomain' does not contain a definition for 'DefineDynamicAssembly' and no accessible extension method 'DefineDynamicAssembly' accepting a first argument of type 'AppDomain' could be found (are you missing a using directive or an assembly reference?)
        error CS1061: 'LambdaExpression' does not contain a definition for 'CompileToMethod' and no accessible extension method 'CompileToMethod' accepting a first argument of type 'LambdaExpression' could be found (are you missing a using directive or an assembly reference?)

[0]: https://docs.microsoft.com/en-us/dotnet/standard/base-types/standard-numeric-format-strings#round-trip-format-specifier-r
  • Loading branch information
jpobst committed Oct 12, 2021
1 parent 403dd37 commit 41ba348
Show file tree
Hide file tree
Showing 18 changed files with 48 additions and 32 deletions.
20 changes: 10 additions & 10 deletions build-tools/automation/templates/core-tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,56 +7,56 @@ steps:
displayName: 'Tests: generator'
inputs:
command: test
arguments: bin/Test$(Build.Configuration)/generator-Tests.dll
arguments: bin/Test$(Build.Configuration)$(NetCoreTargetFrameworkPathSuffix)/generator-Tests.dll
continueOnError: true

- task: DotNetCoreCLI@2
displayName: 'Tests: JavaCallableWrappers'
inputs:
command: test
arguments: bin/Test$(Build.Configuration)/Java.Interop.Tools.JavaCallableWrappers-Tests.dll
arguments: bin/Test$(Build.Configuration)$(NetCoreTargetFrameworkPathSuffix)/Java.Interop.Tools.JavaCallableWrappers-Tests.dll
continueOnError: true

- task: DotNetCoreCLI@2
displayName: 'Tests: logcat-parse'
inputs:
command: test
arguments: bin/Test$(Build.Configuration)/logcat-parse-Tests.dll
arguments: bin/Test$(Build.Configuration)$(NetCoreTargetFrameworkPathSuffix)/logcat-parse-Tests.dll
continueOnError: true

- task: DotNetCoreCLI@2
displayName: 'Tests: ApiXmlAdjuster'
inputs:
command: test
arguments: bin/Test$(Build.Configuration)/Xamarin.Android.Tools.ApiXmlAdjuster-Tests.dll
arguments: bin/Test$(Build.Configuration)$(NetCoreTargetFrameworkPathSuffix)/Xamarin.Android.Tools.ApiXmlAdjuster-Tests.dll
continueOnError: true

- task: DotNetCoreCLI@2
displayName: 'Tests: Bytecode'
inputs:
command: test
arguments: bin/Test$(Build.Configuration)/Xamarin.Android.Tools.Bytecode-Tests.dll
arguments: bin/Test$(Build.Configuration)$(NetCoreTargetFrameworkPathSuffix)/Xamarin.Android.Tools.Bytecode-Tests.dll
continueOnError: true

- task: DotNetCoreCLI@2
displayName: 'Tests: Java.Interop.Tools.Generator'
inputs:
command: test
arguments: bin/Test$(Build.Configuration)/Java.Interop.Tools.Generator-Tests.dll
arguments: bin/Test$(Build.Configuration)$(NetCoreTargetFrameworkPathSuffix)/Java.Interop.Tools.Generator-Tests.dll
continueOnError: true

- task: DotNetCoreCLI@2
displayName: 'Tests: Java.Interop.Tools.JavaSource'
inputs:
command: test
arguments: bin/Test$(Build.Configuration)/Java.Interop.Tools.JavaSource-Tests.dll
arguments: bin/Test$(Build.Configuration)$(NetCoreTargetFrameworkPathSuffix)/Java.Interop.Tools.JavaSource-Tests.dll
continueOnError: true

- task: DotNetCoreCLI@2
displayName: 'Tests: Xamarin.SourceWriter'
inputs:
command: test
arguments: bin/Test$(Build.Configuration)/Xamarin.SourceWriter-Tests.dll
arguments: bin/Test$(Build.Configuration)$(NetCoreTargetFrameworkPathSuffix)/Xamarin.SourceWriter-Tests.dll
continueOnError: true

- task: DotNetCoreCLI@2
Expand All @@ -80,15 +80,15 @@ steps:
condition: eq('${{ parameters.runNativeTests }}', 'true')
inputs:
command: test
arguments: bin/Test$(Build.Configuration)/Java.Interop.Dynamic-Tests.dll
arguments: bin/Test$(Build.Configuration)$(NetCoreTargetFrameworkPathSuffix)/Java.Interop.Dynamic-Tests.dll
continueOnError: true

- task: DotNetCoreCLI@2
displayName: 'Tests: Java.Interop.Export'
condition: eq('${{ parameters.runNativeTests }}', 'true')
inputs:
command: test
arguments: bin/Test$(Build.Configuration)/Java.Interop.Export-Tests.dll
arguments: bin/Test$(Build.Configuration)$(NetCoreTargetFrameworkPathSuffix)/Java.Interop.Export-Tests.dll
continueOnError: true

- task: DotNetCoreCLI@2
Expand Down
4 changes: 2 additions & 2 deletions src/Xamarin.Android.Tools.Bytecode/ConstantPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ public float Value {

public override string ToString ()
{
return string.Format (CultureInfo.InvariantCulture, "Float({0})", Value);
return string.Format (CultureInfo.InvariantCulture, "Float({0:G9})", Value);
}
}

Expand Down Expand Up @@ -335,7 +335,7 @@ public double Value {

public override string ToString ()
{
return string.Format (CultureInfo.InvariantCulture, "Double({0})", Value);
return string.Format (CultureInfo.InvariantCulture, "Double({0:G17})", Value);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,7 @@ static XAttribute GetValue (FieldInfo field)
else if (Double.IsPositiveInfinity (doubleItem.Value))
value = "(1.0 / 0.0)";
else
value = doubleItem.Value.ToString ("R", CultureInfo.InvariantCulture);
value = doubleItem.Value.ToString ("G17", CultureInfo.InvariantCulture);
break;
case ConstantPoolItemType.Float:
var floatItem = (ConstantPoolFloatItem) constant;
Expand All @@ -540,7 +540,7 @@ static XAttribute GetValue (FieldInfo field)
else if (Double.IsPositiveInfinity (floatItem.Value))
value = "(1.0f / 0.0f)";
else
value = floatItem.Value.ToString ("R", CultureInfo.InvariantCulture);
value = floatItem.Value.ToString ("G9", CultureInfo.InvariantCulture);
break;
case ConstantPoolItemType.Long: value = ((ConstantPoolLongItem) constant).Value.ToString (); break;
case ConstantPoolItemType.Integer:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net472</TargetFramework>
<TargetFrameworks>net472;net6.0</TargetFrameworks>
<IsPackable>false</IsPackable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net472</TargetFramework>
<TargetFrameworks>net472;net6.0</TargetFrameworks>
<IsPackable>false</IsPackable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

namespace Java.InteropTests
{
#if !NET
[TestFixture]
class MarshalMemberBuilderTest : JavaVMFixture
{
Expand Down Expand Up @@ -555,4 +556,5 @@ public void CreateConstructActivationPeerExpression ()
}}");
}
}
#endif // !NET
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net472</TargetFramework>
<TargetFrameworks>net472;net6.0</TargetFrameworks>
<RootNamespace>Java.Interop.Tools.Common_Tests</RootNamespace>
<IsPackable>false</IsPackable>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net472</TargetFramework>
<TargetFrameworks>net472;net6.0</TargetFrameworks>
<IsPackable>false</IsPackable>
<DefineConstants>$(DefineConstants);HAVE_CECIL;JCW_ONLY_TYPE_NAMES</DefineConstants>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,12 @@ public void TearDown ()
public void Crc64 ()
{
JavaNativeTypeManager.PackageNamingPolicy = PackageNamingPolicy.LowercaseCrc64;
#if NET
// System.String moved assemblies in .NET
Assert.AreEqual ("crc64d04135c992393d83", JavaNativeTypeManager.GetPackageName (typeof (string)));
#else // !NET
Assert.AreEqual ("crc64b74743e9328eed0a", JavaNativeTypeManager.GetPackageName (typeof (string)));
#endif // !NET
}

[Test]
Expand All @@ -43,7 +48,12 @@ public void Lowercase ()
public void LowercaseWithAssemblyName ()
{
JavaNativeTypeManager.PackageNamingPolicy = PackageNamingPolicy.LowercaseWithAssemblyName;
#if NET
// System.String moved assemblies in .NET
Assert.AreEqual ("assembly_system_private_corelib.system", JavaNativeTypeManager.GetPackageName (typeof (string)));
#else // !NET
Assert.AreEqual ("assembly_mscorlib.system", JavaNativeTypeManager.GetPackageName (typeof (string)));
#endif // !NET
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net472</TargetFramework>
<TargetFrameworks>net472;net6.0</TargetFrameworks>
<IsPackable>False</IsPackable>
</PropertyGroup>
<PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net472</TargetFramework>
<TargetFrameworks>net472;net6.0</TargetFrameworks>
<IsPackable>false</IsPackable>
</PropertyGroup>

Expand Down
10 changes: 5 additions & 5 deletions tests/Xamarin.Android.Tools.Bytecode-Tests/JavaTypeTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Linq;

Expand Down Expand Up @@ -114,25 +114,25 @@ public void ClassFile_WithJavaType_class ()
Name = "STATIC_FINAL_SINGLE_MIN",
Descriptor = "F",
AccessFlags = FieldAccessFlags.Public | FieldAccessFlags.Static | FieldAccessFlags.Final,
ConstantValue = "Float(1.401298E-45)",
ConstantValue = "Float(1.40129846E-45)",
},
new ExpectedFieldDeclaration {
Name = "STATIC_FINAL_SINGLE_MAX",
Descriptor = "F",
AccessFlags = FieldAccessFlags.Public | FieldAccessFlags.Static | FieldAccessFlags.Final,
ConstantValue = "Float(3.402823E+38)",
ConstantValue = "Float(3.40282347E+38)",
},
new ExpectedFieldDeclaration {
Name = "STATIC_FINAL_DOUBLE_MIN",
Descriptor = "D",
AccessFlags = FieldAccessFlags.Public | FieldAccessFlags.Static | FieldAccessFlags.Final,
ConstantValue = "Double(4.94065645841247E-324)",
ConstantValue = "Double(4.9406564584124654E-324)",
},
new ExpectedFieldDeclaration {
Name = "STATIC_FINAL_DOUBLE_MAX",
Descriptor = "D",
AccessFlags = FieldAccessFlags.Public | FieldAccessFlags.Static | FieldAccessFlags.Final,
ConstantValue = "Double(1.79769313486232E+308)",
ConstantValue = "Double(1.7976931348623157E+308)",
},
new ExpectedFieldDeclaration {
Name = "STATIC_FINAL_STRING",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@
type="double"
type-generic-aware="double"
jni-signature="D"
value="4.94065645841247E-324"
value="4.9406564584124654E-324"
visibility="public"
volatile="false" />
<field
Expand Down Expand Up @@ -593,7 +593,7 @@
type="float"
type-generic-aware="float"
jni-signature="F"
value="1.401298E-45"
value="1.40129846E-45"
visibility="public"
volatile="false" />
<field
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net472</TargetFramework>
<TargetFrameworks>net472;net6.0</TargetFrameworks>
<IsPackable>false</IsPackable>
</PropertyGroup>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net472</TargetFramework>
<TargetFrameworks>net472;net6.0</TargetFrameworks>
<IsPackable>false</IsPackable>
</PropertyGroup>

Expand Down
6 changes: 5 additions & 1 deletion tests/generator-Tests/Integration-Tests/Compiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,13 @@ public static Assembly Compile (Xamarin.Android.Binder.CodeGeneratorOptions opti
MetadataReference.CreateFromFile (typeof(Enumerable).Assembly.Location),
MetadataReference.CreateFromFile (typeof(Uri).Assembly.Location),
MetadataReference.CreateFromFile (Path.Combine (binDir, "Java.Interop.dll")),
MetadataReference.CreateFromFile (Path.Combine (facDir, "netstandard.dll"))
MetadataReference.CreateFromFile (Path.Combine (facDir, "netstandard.dll")),
#if NET
MetadataReference.CreateFromFile (Path.Combine (facDir, "System.Runtime.dll")),
#endif // NET
};


// Compile!
var compilation = CSharpCompilation.Create (
Path.GetFileName (assemblyFileName),
Expand Down
2 changes: 1 addition & 1 deletion tests/generator-Tests/generator-Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net472</TargetFramework>
<TargetFrameworks>net472;net6.0</TargetFrameworks>
<IsPackable>false</IsPackable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
Expand Down
2 changes: 1 addition & 1 deletion tests/logcat-parse-Tests/logcat-parse-Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net472</TargetFramework>
<TargetFrameworks>net472;net6.0</TargetFrameworks>
<IsPackable>false</IsPackable>
</PropertyGroup>

Expand Down

0 comments on commit 41ba348

Please sign in to comment.