Set ReferenceAssemblyAttribute in reference assemblies (#11207)

* Set ReferenceAssemblyAttribute in reference assemblies

* Update ReferenceAssemblyInfo.cs

* Use src project for MVC analyzer tests
This commit is contained in:
Nate McMaster 2019-06-28 19:08:01 -04:00 committed by John Luo
parent 1480b99866
commit 7c809f6c22
5 changed files with 50 additions and 2 deletions

View File

@ -98,6 +98,10 @@
<BuildHelixPayload Condition="'$(BuildHelixPayload)' == '' AND '$(IsTestProject)' == 'true'">true</BuildHelixPayload>
</PropertyGroup>
<ItemGroup Condition="'$(Language)' == 'C#' AND '$(IsReferenceAssemblyProject)' == 'true'">
<Compile Include="$(SharedSourceRoot)ReferenceAssemblyInfo.cs" LinkBase="Properties" />
</ItemGroup>
<ItemGroup>
<KnownFrameworkReference Update="Microsoft.NETCore.App">
<!-- Always update the 'latest version', whether the repo is servicing or not. -->

View File

@ -5,6 +5,10 @@ using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Reflection.Metadata;
using System.Reflection.PortableExecutable;
using System.Runtime.CompilerServices;
using Newtonsoft.Json.Linq;
using Xunit;
using Xunit.Abstractions;
@ -24,6 +28,36 @@ namespace Microsoft.AspNetCore
_targetingPackRoot = Path.Combine(TestData.GetTestDataValue("TargetingPackLayoutRoot"), "packs", "Microsoft.AspNetCore.App.Ref", TestData.GetTestDataValue("TargetingPackVersion"));
}
[Fact]
public void AssembliesAreReferenceAssemblies()
{
IEnumerable<string> dlls = Directory.GetFiles(_targetingPackRoot, "*.dll", SearchOption.AllDirectories);
Assert.NotEmpty(dlls);
// Workaround https://github.com/aspnet/AspNetCore/issues/11206
dlls = dlls.Where(d => !d.Contains("System.IO.Pipelines"));
Assert.All(dlls, path =>
{
var assemblyName = AssemblyName.GetAssemblyName(path);
using var fileStream = File.OpenRead(path);
using var peReader = new PEReader(fileStream, PEStreamOptions.Default);
var reader = peReader.GetMetadataReader(MetadataReaderOptions.Default);
var assemblyDefinition = reader.GetAssemblyDefinition();
var hasRefAssemblyAttribute = assemblyDefinition.GetCustomAttributes().Any(attr =>
{
var attribute = reader.GetCustomAttribute(attr);
var attributeConstructor = reader.GetMemberReference((MemberReferenceHandle)attribute.Constructor);
var attributeType = reader.GetTypeReference((TypeReferenceHandle)attributeConstructor.Parent);
return reader.StringComparer.Equals(attributeType.Namespace, typeof(ReferenceAssemblyAttribute).Namespace)
&& reader.StringComparer.Equals(attributeType.Name, nameof(ReferenceAssemblyAttribute));
});
Assert.True(hasRefAssemblyAttribute, $"{path} should have {nameof(ReferenceAssemblyAttribute)}");
Assert.Equal(ProcessorArchitecture.None, assemblyName.ProcessorArchitecture);
});
}
[Fact]
public void PlatformManifestListsAllFiles()
{

View File

@ -22,7 +22,7 @@
<ItemGroup>
<ProjectReference Include="..\..\Mvc.Analyzers\src\Microsoft.AspNetCore.Mvc.Analyzers.csproj" />
<ProjectReference Include="..\..\Mvc\ref\Microsoft.AspNetCore.Mvc.csproj" />
<ProjectReference Include="..\..\Mvc\src\Microsoft.AspNetCore.Mvc.csproj" />
<Reference Include="Microsoft.AspNetCore.Analyzer.Testing" />
<Reference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" />
</ItemGroup>

View File

@ -20,7 +20,7 @@
<ItemGroup>
<ProjectReference Include="..\..\Mvc.Api.Analyzers\src\Microsoft.AspNetCore.Mvc.Api.Analyzers.csproj" />
<ProjectReference Include="..\..\Mvc\ref\Microsoft.AspNetCore.Mvc.csproj" />
<ProjectReference Include="..\..\Mvc\src\Microsoft.AspNetCore.Mvc.csproj" />
<Reference Include="Microsoft.AspNetCore.Analyzer.Testing" />
<Reference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" />
</ItemGroup>

View File

@ -0,0 +1,10 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
// Reference assemblies should have the ReferenceAssemblyAttribute.
[assembly:System.Runtime.CompilerServices.ReferenceAssembly]
// Reference assemblies should have the 0x70 flag which prevents them from loading.
// This flag sets AssemblyName.ProcessorArchitecture to None. There is no public API for this.
// Cref https://github.com/dotnet/coreclr/blob/64ca544ecf55490675e72b853e98ebc8cc75a4fe/src/System.Private.CoreLib/src/System/Reflection/AssemblyName.CoreCLR.cs#L74
[assembly:System.Reflection.AssemblyFlags((System.Reflection.AssemblyNameFlags)0x70)]