未验证 提交 ab92a77b 编写于 作者: M Mike McLaughlin 提交者: GitHub

Add test case for deleting custom attributes using ApplyUpdate (#56644)

* Add test case for deleting custom attributes using ApplyUpdate

Issue: https://github.com/dotnet/runtime/issues/54284

* Fix mono failures
上级 3bd0acf3
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
namespace System.Reflection.Metadata.ApplyUpdate.Test
{
[AttributeUsage (AttributeTargets.Method, AllowMultiple=true)]
public class MyDeleteAttribute : Attribute
{
public MyDeleteAttribute (string stringValue) { StringValue = stringValue; }
public MyDeleteAttribute (Type typeValue) { TypeValue = typeValue; }
public MyDeleteAttribute (int x) { IntValue = x; }
public string StringValue { get; set; }
public Type TypeValue {get; set; }
public int IntValue {get; set; }
}
public class ClassWithCustomAttributeDelete
{
[MyDeleteAttribute ("abcd")]
public static string Method1 () => null;
[MyDeleteAttribute (typeof(Exception))]
public static string Method2 () => null;
[MyDeleteAttribute (42, StringValue = "hijkl", TypeValue = typeof(Type))]
[MyDeleteAttribute (17, StringValue = "", TypeValue = typeof(object))]
public static string Method3 () => null;
}
}
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
namespace System.Reflection.Metadata.ApplyUpdate.Test
{
[AttributeUsage (AttributeTargets.Method, AllowMultiple=true)]
public class MyDeleteAttribute : Attribute
{
public MyDeleteAttribute (string stringValue) { StringValue = stringValue; }
public MyDeleteAttribute (Type typeValue) { TypeValue = typeValue; }
public MyDeleteAttribute (int x) { IntValue = x; }
public string StringValue { get; set; }
public Type TypeValue {get; set; }
public int IntValue {get; set; }
}
public class ClassWithCustomAttributeDelete
{
public static string Method1 () => null;
public static string Method2 () => null;
[MyDeleteAttribute (17, StringValue = "Not Deleted", TypeValue = typeof(object))]
public static string Method3 () => null;
}
}
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<RootNamespace>System.Runtime.Loader.Tests</RootNamespace>
<TargetFrameworks>$(NetCoreAppCurrent)</TargetFrameworks>
<TestRuntime>true</TestRuntime>
<DeltaScript>deltascript.json</DeltaScript>
</PropertyGroup>
<ItemGroup>
<Compile Include="CustomAttributeDelete.cs" />
</ItemGroup>
</Project>
{
"changes": [
{"document": "CustomAttributeDelete.cs", "update": "CustomAttributeDelete_v1.cs"},
]
}
......@@ -112,6 +112,46 @@ public void CustomAttributeUpdates()
});
}
[ActiveIssue("https://github.com/dotnet/runtime/issues/52993", TestRuntimes.Mono)]
[ConditionalFact(typeof(ApplyUpdateUtil), nameof (ApplyUpdateUtil.IsSupported))]
public void CustomAttributeDelete()
{
// Test that deleting custom attribute on constructor/property works as expected.
ApplyUpdateUtil.TestCase(static () =>
{
var assm = typeof(System.Reflection.Metadata.ApplyUpdate.Test.ClassWithCustomAttributeDelete).Assembly;
ApplyUpdateUtil.ApplyUpdate(assm);
ApplyUpdateUtil.ClearAllReflectionCaches();
// Just check the updated value on one method
Type attrType = typeof(System.Reflection.Metadata.ApplyUpdate.Test.MyDeleteAttribute);
Type ty = assm.GetType("System.Reflection.Metadata.ApplyUpdate.Test.ClassWithCustomAttributeDelete");
Assert.NotNull(ty);
MethodInfo mi1 = ty.GetMethod(nameof(System.Reflection.Metadata.ApplyUpdate.Test.ClassWithCustomAttributeDelete.Method1), BindingFlags.Public | BindingFlags.Static);
Assert.NotNull(mi1);
Attribute[] cattrs = Attribute.GetCustomAttributes(mi1, attrType);
Assert.NotNull(cattrs);
Assert.Equal(0, cattrs.Length);
MethodInfo mi2 = ty.GetMethod(nameof(System.Reflection.Metadata.ApplyUpdate.Test.ClassWithCustomAttributeDelete.Method2), BindingFlags.Public | BindingFlags.Static);
Assert.NotNull(mi2);
cattrs = Attribute.GetCustomAttributes(mi2, attrType);
Assert.NotNull(cattrs);
Assert.Equal(0, cattrs.Length);
MethodInfo mi3 = ty.GetMethod(nameof(System.Reflection.Metadata.ApplyUpdate.Test.ClassWithCustomAttributeDelete.Method3), BindingFlags.Public | BindingFlags.Static);
Assert.NotNull(mi3);
cattrs = Attribute.GetCustomAttributes(mi3, attrType);
Assert.NotNull(cattrs);
Assert.Equal(1, cattrs.Length);
string p = (cattrs[0] as System.Reflection.Metadata.ApplyUpdate.Test.MyDeleteAttribute).StringValue;
Assert.Equal("Not Deleted", p);
});
}
class NonRuntimeAssembly : Assembly
{
}
......
......@@ -23,15 +23,10 @@
<EmbeddedResource Include="MainStrings*.resx" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="System.Runtime.Loader.Test.Assembly\System.Runtime.Loader.Test.Assembly.csproj"
ReferenceOutputAssembly="false"
OutputItemType="EmbeddedResource" />
<ProjectReference Include="System.Runtime.Loader.Test.Assembly2\System.Runtime.Loader.Test.Assembly2.csproj"
ReferenceOutputAssembly="false"
OutputItemType="EmbeddedResource" />
<ProjectReference Include="System.Runtime.Loader.Test.AssemblyNotSupported\System.Runtime.Loader.Test.AssemblyNotSupported.csproj"
ReferenceOutputAssembly="false"
OutputItemType="EmbeddedResource" />
<ProjectReference Include="ApplyUpdate\System.Reflection.Metadata.ApplyUpdate.Test.CustomAttributeDelete\System.Reflection.Metadata.ApplyUpdate.Test.CustomAttributeDelete.csproj" />
<ProjectReference Include="System.Runtime.Loader.Test.Assembly\System.Runtime.Loader.Test.Assembly.csproj" ReferenceOutputAssembly="false" OutputItemType="EmbeddedResource" />
<ProjectReference Include="System.Runtime.Loader.Test.Assembly2\System.Runtime.Loader.Test.Assembly2.csproj" ReferenceOutputAssembly="false" OutputItemType="EmbeddedResource" />
<ProjectReference Include="System.Runtime.Loader.Test.AssemblyNotSupported\System.Runtime.Loader.Test.AssemblyNotSupported.csproj" ReferenceOutputAssembly="false" OutputItemType="EmbeddedResource" />
<ProjectReference Include="ContextualReflectionDependency\System.Runtime.Loader.Test.ContextualReflectionDependency.csproj" />
<ProjectReference Include="ReferencedClassLib\ReferencedClassLib.csproj" />
<ProjectReference Include="ReferencedClassLibNeutralIsSatellite\ReferencedClassLibNeutralIsSatellite.csproj" />
......@@ -48,35 +43,22 @@
<TrimmerRootDescriptor Include="$(MSBuildThisFileDirectory)ILLink.Descriptors.xml" />
</ItemGroup>
<Target Name="PreserveEnCAssembliesFromLinking"
Condition="'$(TargetOS)' == 'Browser' and '$(EnableAggressiveTrimming)' == 'true'"
BeforeTargets="ConfigureTrimming">
<Target Name="PreserveEnCAssembliesFromLinking" Condition="'$(TargetOS)' == 'Browser' and '$(EnableAggressiveTrimming)' == 'true'" BeforeTargets="ConfigureTrimming">
<ItemGroup>
<!-- Don't modify EnC test assemblies -->
<TrimmerRootAssembly
Condition="$([System.String]::Copy('%(ResolvedFileToPublish.FileName)%(ResolvedFileToPublish.Extension)').EndsWith('System.Reflection.Metadata.ApplyUpdate.Test.MethodBody1.dll'))"
Include="%(ResolvedFileToPublish.FullPath)" />
<TrimmerRootAssembly
Condition="$([System.String]::Copy('%(ResolvedFileToPublish.FileName)%(ResolvedFileToPublish.Extension)').EndsWith('System.Reflection.Metadata.ApplyUpdate.Test.ClassWithCustomAttributes.dll'))"
Include="%(ResolvedFileToPublish.FullPath)" />
<TrimmerRootAssembly
Condition="$([System.String]::Copy('%(ResolvedFileToPublish.FileName)%(ResolvedFileToPublish.Extension)').EndsWith('System.Reflection.Metadata.ApplyUpdate.Test.CustomAttributeUpdate.dll'))"
Include="%(ResolvedFileToPublish.FullPath)" />
<TrimmerRootAssembly Condition="$([System.String]::Copy('%(ResolvedFileToPublish.FileName)%(ResolvedFileToPublish.Extension)').EndsWith('System.Reflection.Metadata.ApplyUpdate.Test.MethodBody1.dll'))" Include="%(ResolvedFileToPublish.FullPath)" />
<TrimmerRootAssembly Condition="$([System.String]::Copy('%(ResolvedFileToPublish.FileName)%(ResolvedFileToPublish.Extension)').EndsWith('System.Reflection.Metadata.ApplyUpdate.Test.ClassWithCustomAttributes.dll'))" Include="%(ResolvedFileToPublish.FullPath)" />
<TrimmerRootAssembly Condition="$([System.String]::Copy('%(ResolvedFileToPublish.FileName)%(ResolvedFileToPublish.Extension)').EndsWith('System.Reflection.Metadata.ApplyUpdate.Test.CustomAttributeUpdate.dll'))" Include="%(ResolvedFileToPublish.FullPath)" />
</ItemGroup>
</Target>
<Target Name="IncludeDeltasInWasmBundle"
BeforeTargets="PrepareForWasmBuildApp"
Condition="'$(TargetOS)' == 'Browser'">
<Target Name="IncludeDeltasInWasmBundle" BeforeTargets="PrepareForWasmBuildApp" Condition="'$(TargetOS)' == 'Browser'">
<ItemGroup>
<!-- FIXME: this belongs in eng/testing/tests.wasm.targets -->
<!-- FIXME: Can we do something on the Content items in the referenced projects themselves to get this for free? -->
<WasmFilesToIncludeInFileSystem Include="@(PublishItemsOutputGroupOutputs)"
Condition="$([System.String]::new('%(PublishItemsOutputGroupOutputs.Identity)').EndsWith('.dmeta'))" />
<WasmFilesToIncludeInFileSystem Include="@(PublishItemsOutputGroupOutputs)"
Condition="$([System.String]::new('%(PublishItemsOutputGroupOutputs.Identity)').EndsWith('.dil'))" />
<WasmFilesToIncludeInFileSystem Include="@(PublishItemsOutputGroupOutputs)"
Condition="$([System.String]::new('%(PublishItemsOutputGroupOutputs.Identity)').EndsWith('.dpdb'))" />
<WasmFilesToIncludeInFileSystem Include="@(PublishItemsOutputGroupOutputs)" Condition="$([System.String]::new('%(PublishItemsOutputGroupOutputs.Identity)').EndsWith('.dmeta'))" />
<WasmFilesToIncludeInFileSystem Include="@(PublishItemsOutputGroupOutputs)" Condition="$([System.String]::new('%(PublishItemsOutputGroupOutputs.Identity)').EndsWith('.dil'))" />
<WasmFilesToIncludeInFileSystem Include="@(PublishItemsOutputGroupOutputs)" Condition="$([System.String]::new('%(PublishItemsOutputGroupOutputs.Identity)').EndsWith('.dpdb'))" />
</ItemGroup>
</Target>
</Project>
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册