From a42eb1f111a46c0051f7e49e5f538ce2a94934d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Strehovsk=C3=BD?= Date: Tue, 5 Apr 2022 11:08:07 +0900 Subject: [PATCH] Allow running System.Reflection tests on NativeAOT Baselines/annotates anything that needs baselining/annotating. --- .../tests/AssemblyNameTests.cs | 9 +- .../System.Reflection/tests/AssemblyTests.cs | 86 ++++++++++++------- .../tests/ConstructorInfoTests.cs | 6 +- .../tests/MemberInfoTests.cs | 1 + .../tests/MethodInfoTests.cs | 1 + .../tests/ParameterInfoTests.cs | 1 + .../tests/System.Reflection.Tests.csproj | 3 + .../System.Reflection/tests/TypeInfoTests.cs | 2 + .../System.Reflection/tests/default.rd.xml | 51 +++++++++++ .../System.Runtime/tests/System/Attributes.cs | 2 +- 10 files changed, 125 insertions(+), 37 deletions(-) create mode 100644 src/libraries/System.Reflection/tests/default.rd.xml diff --git a/src/libraries/System.Reflection/tests/AssemblyNameTests.cs b/src/libraries/System.Reflection/tests/AssemblyNameTests.cs index 90aa1a62f63277..612ca620ee7a01 100644 --- a/src/libraries/System.Reflection/tests/AssemblyNameTests.cs +++ b/src/libraries/System.Reflection/tests/AssemblyNameTests.cs @@ -308,8 +308,11 @@ public static void GetAssemblyName() Assert.Throws(() => AssemblyName.GetAssemblyName(tempFile.Path)); } - Assembly a = typeof(AssemblyNameTests).Assembly; - Assert.Equal(new AssemblyName(a.FullName).ToString(), AssemblyName.GetAssemblyName(AssemblyPathHelper.GetAssemblyLocation(a)).ToString()); + if (!PlatformDetection.IsNativeAot) + { + Assembly a = typeof(AssemblyNameTests).Assembly; + Assert.Equal(new AssemblyName(a.FullName).ToString(), AssemblyName.GetAssemblyName(AssemblyPathHelper.GetAssemblyLocation(a)).ToString()); + } } [Fact] @@ -371,7 +374,7 @@ public void FullName_CurrentlyExecutingAssembly() Assert.Equal(assemblyName.Name.Length, assemblyName.FullName.IndexOf(',')); } - [Fact] + [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsAssemblyLoadingSupported))] public void EmptyFusionLog() { FileNotFoundException fnfe = Assert.Throws(() => Assembly.LoadFrom(@"\non\existent\file.dll")); diff --git a/src/libraries/System.Reflection/tests/AssemblyTests.cs b/src/libraries/System.Reflection/tests/AssemblyTests.cs index 2ce8843eb3373d..3dcd9623ce3df5 100644 --- a/src/libraries/System.Reflection/tests/AssemblyTests.cs +++ b/src/libraries/System.Reflection/tests/AssemblyTests.cs @@ -38,12 +38,15 @@ public class AssemblyTests : FileCleanupTestBase public AssemblyTests() { - // Assembly.Location does not return the file path for single-file deployment targets. - DestTestAssemblyPath = Path.Combine(base.TestDirectory, s_sourceTestAssemblyName); - LoadFromTestPath = Path.Combine(base.TestDirectory, "System.Reflection.Tests.dll"); - File.Copy(SourceTestAssemblyPath, DestTestAssemblyPath); - string currAssemblyPath = Path.Combine(Environment.CurrentDirectory, "System.Reflection.Tests.dll"); - File.Copy(currAssemblyPath, LoadFromTestPath, true); + if (PlatformDetection.IsAssemblyLoadingSupported) + { + // Assembly.Location does not return the file path for single-file deployment targets. + DestTestAssemblyPath = Path.Combine(base.TestDirectory, s_sourceTestAssemblyName); + LoadFromTestPath = Path.Combine(base.TestDirectory, "System.Reflection.Tests.dll"); + File.Copy(SourceTestAssemblyPath, DestTestAssemblyPath); + string currAssemblyPath = Path.Combine(Environment.CurrentDirectory, "System.Reflection.Tests.dll"); + File.Copy(currAssemblyPath, LoadFromTestPath, true); + } } [Theory] @@ -150,7 +153,11 @@ public void GetEntryAssembly() { Assert.NotNull(Assembly.GetEntryAssembly()); string assembly = Assembly.GetEntryAssembly().ToString(); - bool correct = assembly.IndexOf("xunit.console", StringComparison.OrdinalIgnoreCase) != -1; + + // The single file test runner is not xunit.console + string expectedAssembly = PlatformDetection.IsNativeAot ? "System.Reflection.Tests" : "xunit.console"; + + bool correct = assembly.IndexOf(expectedAssembly, StringComparison.OrdinalIgnoreCase) != -1; Assert.True(correct, $"Unexpected assembly name {assembly}"); } @@ -174,7 +181,7 @@ public void GetFile() } } - [Fact] + [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsAssemblyLoadingSupported))] public void GetFile_InMemory() { var inMemBlob = File.ReadAllBytes(SourceTestAssemblyPath); @@ -186,7 +193,7 @@ public void GetFile_InMemory() Assert.Throws(() => asm.GetFiles(getResourceModules: false)); } - [Fact] + [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsAssemblyLoadingSupported))] public void CodeBaseInMemory() { var inMemBlob = File.ReadAllBytes(SourceTestAssemblyPath); @@ -337,7 +344,7 @@ public void Load_Invalid() Assert.Throws(() => Assembly.Load(new AssemblyName("no such assembly"))); // No such assembly } - [Fact] + [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsAssemblyLoadingSupported))] public void LoadFile() { Assembly currentAssembly = typeof(AssemblyTests).Assembly; @@ -361,20 +368,20 @@ public void LoadFile() Assert.Equal(loadedAssembly1, loadedAssembly2); } - [Fact] + [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsAssemblyLoadingSupported))] public void LoadFile_NullPath_ThrowsArgumentNullException() { AssertExtensions.Throws("path", () => Assembly.LoadFile(null)); } - [Fact] + [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsAssemblyLoadingSupported))] public void LoadFile_NoSuchPath_ThrowsFileNotFoundException() { string rootedPath = Path.GetFullPath(Guid.NewGuid().ToString("N")); AssertExtensions.ThrowsContains(() => Assembly.LoadFile(rootedPath), rootedPath); } - [Fact] + [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsAssemblyLoadingSupported))] public void LoadFile_PartiallyQualifiedPath_ThrowsArgumentException() { string path = "System.Runtime.Tests.dll"; @@ -384,7 +391,7 @@ public void LoadFile_PartiallyQualifiedPath_ThrowsArgumentException() // This test should apply equally to Unix, but this reliably hits a particular one of the // myriad ways that assembly load can fail - [Fact] + [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsAssemblyLoadingSupported))] [PlatformSpecific(TestPlatforms.Windows)] public void LoadFile_ValidPEBadIL_ThrowsBadImageFormatExceptionWithPath() { @@ -395,7 +402,7 @@ public void LoadFile_ValidPEBadIL_ThrowsBadImageFormatExceptionWithPath() AssertExtensions.ThrowsContains(() => Assembly.LoadFile(path), path); } - [Theory] + [ConditionalTheory(typeof(PlatformDetection), nameof(PlatformDetection.IsAssemblyLoadingSupported))] [InlineData(0)] [InlineData(5)] [InlineData(50)] @@ -415,13 +422,13 @@ public void LoadFile_ValidPEBadIL_ThrowsBadImageFormatExceptionWithPath_ByInitia AssertExtensions.ThrowsContains(() => Assembly.LoadFile(path), path); } - [Fact] + [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsAssemblyLoadingSupported))] public void LoadFromUsingHashValue() { Assert.Throws(() => Assembly.LoadFrom("abc", null, System.Configuration.Assemblies.AssemblyHashAlgorithm.SHA1)); } - [Fact] + [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsAssemblyLoadingSupported))] public void LoadFrom_SamePath_ReturnsEqualAssemblies() { Assembly assembly1 = Assembly.LoadFrom(DestTestAssemblyPath); @@ -429,7 +436,7 @@ public void LoadFrom_SamePath_ReturnsEqualAssemblies() Assert.Equal(assembly1, assembly2); } - [Fact] + [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsAssemblyLoadingSupported))] public void LoadFrom_SameIdentityAsAssemblyWithDifferentPath_ReturnsEqualAssemblies() { Assembly assembly1 = Assembly.LoadFrom(AssemblyPathHelper.GetAssemblyLocation(typeof(AssemblyTests).Assembly)); @@ -440,28 +447,28 @@ public void LoadFrom_SameIdentityAsAssemblyWithDifferentPath_ReturnsEqualAssembl Assert.Equal(assembly1, assembly2); } - [Fact] + [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsAssemblyLoadingSupported))] public void LoadFrom_NullAssemblyFile_ThrowsArgumentNullException() { AssertExtensions.Throws("assemblyFile", () => Assembly.LoadFrom(null)); AssertExtensions.Throws("assemblyFile", () => Assembly.UnsafeLoadFrom(null)); } - [Fact] + [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsAssemblyLoadingSupported))] public void LoadFrom_EmptyAssemblyFile_ThrowsArgumentException() { AssertExtensions.Throws("path", null, (() => Assembly.LoadFrom(""))); AssertExtensions.Throws("path", null, (() => Assembly.UnsafeLoadFrom(""))); } - [Fact] + [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsAssemblyLoadingSupported))] public void LoadFrom_NoSuchFile_ThrowsFileNotFoundException() { Assert.Throws(() => Assembly.LoadFrom("NoSuchPath")); Assert.Throws(() => Assembly.UnsafeLoadFrom("NoSuchPath")); } - [Fact] + [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsAssemblyLoadingSupported))] public void UnsafeLoadFrom_SamePath_ReturnsEqualAssemblies() { Assembly assembly1 = Assembly.UnsafeLoadFrom(DestTestAssemblyPath); @@ -469,13 +476,13 @@ public void UnsafeLoadFrom_SamePath_ReturnsEqualAssemblies() Assert.Equal(assembly1, assembly2); } - [Fact] + [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsAssemblyLoadingSupported))] public void LoadFrom_WithHashValue_ThrowsNotSupportedException() { Assert.Throws(() => Assembly.LoadFrom(DestTestAssemblyPath, new byte[0], Configuration.Assemblies.AssemblyHashAlgorithm.None)); } - [Fact] + [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsAssemblyLoadingSupported))] public void LoadModule() { Assembly assembly = typeof(AssemblyTests).Assembly; @@ -513,7 +520,14 @@ public void Location_ExecutingAssembly_IsNotNull() [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotBrowser))] // single file public void CodeBase() { - Assert.NotEmpty(Helpers.ExecutingAssembly.CodeBase); + if (PlatformDetection.IsNativeAot) + { + Assert.Throws(() => _ = Helpers.ExecutingAssembly.CodeBase); + } + else + { + Assert.NotEmpty(Helpers.ExecutingAssembly.CodeBase); + } } #pragma warning restore SYSLIB0012 @@ -589,9 +603,16 @@ public void CreateQualifiedName() [Fact] public void GetReferencedAssemblies() { - // It is too brittle to depend on the assembly references so we just call the method and check that it does not throw. - AssemblyName[] assemblies = Helpers.ExecutingAssembly.GetReferencedAssemblies(); - Assert.NotEmpty(assemblies); + if (PlatformDetection.IsNativeAot) + { + Assert.Throws(() => Helpers.ExecutingAssembly.GetReferencedAssemblies()); + } + else + { + // It is too brittle to depend on the assembly references so we just call the method and check that it does not throw. + AssemblyName[] assemblies = Helpers.ExecutingAssembly.GetReferencedAssemblies(); + Assert.NotEmpty(assemblies); + } } public static IEnumerable Modules_TestData() @@ -654,6 +675,7 @@ public static IEnumerable GetCallingAssembly_TestData() [Theory] [ActiveIssue("https://github.com/dotnet/runtime/issues/51673", typeof(PlatformDetection), nameof(PlatformDetection.IsBrowser), nameof(PlatformDetection.IsMonoAOT))] + [ActiveIssue("https://github.com/dotnet/runtimelab/issues/155", typeof(PlatformDetection), nameof(PlatformDetection.IsNativeAot))] [MemberData(nameof(GetCallingAssembly_TestData))] public void GetCallingAssembly(Assembly assembly1, Assembly assembly2, bool expected) { @@ -667,6 +689,7 @@ public void GetExecutingAssembly() } [Fact] + [ActiveIssue("https://github.com/dotnet/runtime/issues/67569", typeof(PlatformDetection), nameof(PlatformDetection.IsNativeAot))] public void GetSatelliteAssemblyNeg() { Assert.Throws(() => (typeof(AssemblyTests).Assembly.GetSatelliteAssembly(null))); @@ -701,7 +724,7 @@ public void AssemblyLoadFromStringNeg() Assert.Throws(() => Assembly.Load("no such assembly")); // No such assembly } - [Fact] + [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsAssemblyLoadingSupported))] public void AssemblyLoadFromBytes() { Assembly assembly = typeof(AssemblyTests).Assembly; @@ -725,7 +748,7 @@ public void AssemblyLoadFromBytesNeg() Assert.Throws(() => Assembly.Load(new byte[0])); } - [Fact] + [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsAssemblyLoadingSupported))] [ActiveIssue("https://github.com/dotnet/runtime/issues/36892", TestPlatforms.iOS | TestPlatforms.tvOS | TestPlatforms.MacCatalyst)] public void AssemblyLoadFromBytesWithSymbols() { @@ -746,7 +769,7 @@ public void AssemblyReflectionOnlyLoadFromString() Assert.Throws(() => Assembly.ReflectionOnlyLoad(an.FullName)); } - [Fact] + [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsAssemblyLoadingSupported))] public void AssemblyReflectionOnlyLoadFromBytes() { Assembly assembly = typeof(AssemblyTests).Assembly; @@ -826,6 +849,7 @@ public static void AssemblyGetForwardedTypes() } [Fact] + [ActiveIssue("https://github.com/dotnet/runtimelab/issues/155", typeof(PlatformDetection), nameof(PlatformDetection.IsNativeAot))] public static void AssemblyGetForwardedTypesLoadFailure() { Assembly a = typeof(TypeInForwardedAssembly).Assembly; diff --git a/src/libraries/System.Reflection/tests/ConstructorInfoTests.cs b/src/libraries/System.Reflection/tests/ConstructorInfoTests.cs index ea861aa1dcdeb8..eee43395bc3e68 100644 --- a/src/libraries/System.Reflection/tests/ConstructorInfoTests.cs +++ b/src/libraries/System.Reflection/tests/ConstructorInfoTests.cs @@ -59,7 +59,7 @@ public void Invoke() Assert.NotNull(obj); } - [Fact] + [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsInvokingStaticConstructorsSupported))] public void Invoke_StaticConstructor_NullObject_NullParameters() { ConstructorInfo[] constructors = GetConstructors(typeof(ClassWithStaticConstructor)); @@ -68,7 +68,7 @@ public void Invoke_StaticConstructor_NullObject_NullParameters() Assert.Null(obj); } - [Fact] + [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsInvokingStaticConstructorsSupported))] [ActiveIssue("https://github.com/dotnet/runtime/issues/40351", TestRuntimes.Mono)] public void Invoke_StaticConstructorMultipleTimes() { @@ -127,6 +127,7 @@ public void Invoke_OneDimensionalArray_NegativeLengths_ThrowsOverflowException() } [Fact] + [ActiveIssue("https://github.com/dotnet/runtime/issues/67531", typeof(PlatformDetection), nameof(PlatformDetection.IsNativeAot))] public void Invoke_TwoDimensionalArray_CustomBinder_IncorrectTypeArguments() { var ctor = typeof(int[,]).GetConstructor(new[] { typeof(int), typeof(int) }); @@ -155,6 +156,7 @@ public void Invoke_TwoParameters() } [Fact] + [ActiveIssue("https://github.com/dotnet/runtime/issues/67531", typeof(PlatformDetection), nameof(PlatformDetection.IsNativeAot))] public void Invoke_TwoParameters_CustomBinder_IncorrectTypeArgument() { ConstructorInfo[] constructors = GetConstructors(typeof(ClassWith3Constructors)); diff --git a/src/libraries/System.Reflection/tests/MemberInfoTests.cs b/src/libraries/System.Reflection/tests/MemberInfoTests.cs index a6cee62dfb0887..56ce01e5b18295 100644 --- a/src/libraries/System.Reflection/tests/MemberInfoTests.cs +++ b/src/libraries/System.Reflection/tests/MemberInfoTests.cs @@ -166,6 +166,7 @@ public void MethodInfoReflectedTypeDoesNotSurviveRuntimeHandles() } [Fact] + [ActiveIssue("https://github.com/dotnet/runtimelab/issues/830", typeof(PlatformDetection), nameof(PlatformDetection.IsNativeAot))] public void GetCustomAttributesData() { MemberInfo[] m = typeof(MemberInfoTests).GetMember("SampleClass"); diff --git a/src/libraries/System.Reflection/tests/MethodInfoTests.cs b/src/libraries/System.Reflection/tests/MethodInfoTests.cs index a538fc9f3a9c9f..61c958769efb31 100644 --- a/src/libraries/System.Reflection/tests/MethodInfoTests.cs +++ b/src/libraries/System.Reflection/tests/MethodInfoTests.cs @@ -382,6 +382,7 @@ public static void Invoke_OptionalParameterUnassingableFromMissing_WithMissingVa } [Fact] + [ActiveIssue("https://github.com/dotnet/runtime/issues/67531", typeof(PlatformDetection), nameof(PlatformDetection.IsNativeAot))] public void Invoke_TwoParameters_CustomBinder_IncorrectTypeArguments() { MethodInfo method = GetMethod(typeof(MI_SubClass), nameof(MI_SubClass.StaticIntIntMethodReturningInt)); diff --git a/src/libraries/System.Reflection/tests/ParameterInfoTests.cs b/src/libraries/System.Reflection/tests/ParameterInfoTests.cs index 26d2d65867f690..65cb8a7624e303 100644 --- a/src/libraries/System.Reflection/tests/ParameterInfoTests.cs +++ b/src/libraries/System.Reflection/tests/ParameterInfoTests.cs @@ -212,6 +212,7 @@ public void Attributes(Type type, string name, int index, ParameterAttributes ex [Theory] [MemberData(nameof(s_CustomAttributesTestData))] + [ActiveIssue("https://github.com/dotnet/runtimelab/issues/830", typeof(PlatformDetection), nameof(PlatformDetection.IsNativeAot))] public void CustomAttributesTest(Type attrType) { ParameterInfo parameterInfo = GetParameterInfo(typeof(ParameterInfoMetadata), "MethodWithOptionalDefaultOutInMarshalParam", 0); diff --git a/src/libraries/System.Reflection/tests/System.Reflection.Tests.csproj b/src/libraries/System.Reflection/tests/System.Reflection.Tests.csproj index a31fb65f6ce48e..d423ed1b8d26ff 100644 --- a/src/libraries/System.Reflection/tests/System.Reflection.Tests.csproj +++ b/src/libraries/System.Reflection/tests/System.Reflection.Tests.csproj @@ -12,6 +12,9 @@ true + + + diff --git a/src/libraries/System.Reflection/tests/TypeInfoTests.cs b/src/libraries/System.Reflection/tests/TypeInfoTests.cs index e7d2977ec56b50..fb0bccb86c4708 100644 --- a/src/libraries/System.Reflection/tests/TypeInfoTests.cs +++ b/src/libraries/System.Reflection/tests/TypeInfoTests.cs @@ -593,6 +593,7 @@ class G where T : U static volatile object s_boxedInt32; [Fact] + [ActiveIssue("https://github.com/dotnet/runtime/issues/67568", typeof(PlatformDetection), nameof(PlatformDetection.IsNativeAot))] public void IsAssignableNullable() { Type nubInt = typeof(Nullable); @@ -1655,6 +1656,7 @@ private static (Type, Type) CreateGeneratedTypes() } [Theory, MemberData(nameof(GetMemberWithSameMetadataDefinitionAsData))] + [ActiveIssue("https://github.com/dotnet/runtime/issues/67533", typeof(PlatformDetection), nameof(PlatformDetection.IsNativeAot))] public void GetMemberWithSameMetadataDefinitionAs(Type openGenericType, Type closedGenericType, bool checkDeclaringType) { BindingFlags all = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance | BindingFlags.DeclaredOnly; diff --git a/src/libraries/System.Reflection/tests/default.rd.xml b/src/libraries/System.Reflection/tests/default.rd.xml new file mode 100644 index 00000000000000..de87619639313b --- /dev/null +++ b/src/libraries/System.Reflection/tests/default.rd.xml @@ -0,0 +1,51 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/libraries/System.Runtime/tests/System/Attributes.cs b/src/libraries/System.Runtime/tests/System/Attributes.cs index ed213cddcefe27..8178c8f2781d75 100644 --- a/src/libraries/System.Runtime/tests/System/Attributes.cs +++ b/src/libraries/System.Runtime/tests/System/Attributes.cs @@ -221,7 +221,7 @@ public static void GetCustomAttributes_Interface() Assert.True(typeof(ExampleWithAttribute).GetCustomAttributes(typeof(INameable), inherit: false)[0] is NameableAttribute); } - [Fact] + [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsReflectionEmitSupported))] [SkipOnMono("Mono does not support getting DynamicMethod attributes via Attribute.GetCustomAttributes()")] public static void GetCustomAttributes_DynamicMethod() {