-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow granular opt out of assembly info generation
- Loading branch information
Showing
5 changed files
with
176 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
test/Microsoft.NET.Build.Tests/GivenThatWeWantToControlGeneratedAssemblyInfo.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
// Copyright (c) .NET Foundation and contributors. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System.Collections.Generic; | ||
using System.IO; | ||
using Microsoft.NET.TestFramework; | ||
using Microsoft.NET.TestFramework.Assertions; | ||
using Microsoft.NET.TestFramework.Commands; | ||
using Xunit; | ||
using FluentAssertions; | ||
using static Microsoft.NET.TestFramework.Commands.MSBuildTest; | ||
|
||
namespace Microsoft.NET.Build.Tests | ||
{ | ||
public class GivenThatWeWantToControlGeneratedAssemblyInfo | ||
{ | ||
private TestAssetsManager _testAssetsManager = TestAssetsManager.TestProjectsAssetsManager; | ||
|
||
[Theory] | ||
[InlineData("AssemblyInformationVersionAttribute")] | ||
[InlineData("AssemblyFileVersionAttribute")] | ||
[InlineData("AssemblyVersionAttribute")] | ||
[InlineData("AssemblyCompanyAttribute")] | ||
[InlineData("AssemblyConfigurationAttribute")] | ||
[InlineData("AssemblyCopyrightAttribute")] | ||
[InlineData("AssemblyDescriptionAttribute")] | ||
[InlineData("AssemblyTitleAttribute")] | ||
[InlineData("NeutralResourcesLanguageAttribute")] | ||
[InlineData("All")] | ||
public void It_respects_opt_outs(string attributeToOptOut) | ||
{ | ||
var testAsset = _testAssetsManager | ||
.CopyTestAsset("HelloWorld", identifier: Path.DirectorySeparatorChar + attributeToOptOut) | ||
.WithSource() | ||
.Restore(); | ||
|
||
var buildCommand = new BuildCommand(Stage0MSBuild, testAsset.TestRoot); | ||
buildCommand | ||
.Execute( | ||
"/p:Version=1.2.3-beta", | ||
"/p:FileVersion=4.5.6.7", | ||
"/p:AssemblyVersion=8.9.10.11", | ||
"/p:Company=TestCompany", | ||
"/p:Configuration=Release", | ||
"/p:Copyright=TestCopyright", | ||
"/p:Description=TestDescription", | ||
"/p:Product=TestProduct", | ||
"/p:AssemblyTitle=TestTitle", | ||
"/p:NeutralLanguage=fr", | ||
attributeToOptOut == "All" ? | ||
"/p:GenerateAssemblyInfo=false" : | ||
$"/p:Generate{attributeToOptOut}=false" | ||
) | ||
.Should() | ||
.Pass(); | ||
|
||
var expectedInfo = new SortedDictionary<string, string> | ||
{ | ||
{ "AssemblyInformationalVersionAttribute", "1.2.3-beta" }, | ||
{ "AssemblyFileVersionAttribute", "4.5.6.7" }, | ||
{ "AssemblyVersionAttribute", "8.9.10.11" }, | ||
{ "AssemblyCompanyAttribute", "TestCompany" }, | ||
{ "AssemblyConfigurationAttribute", "Release" }, | ||
{ "AssemblyCopyrightAttribute", "TestCopyright" }, | ||
{ "AssemblyDescriptionAttribute", "TestDescription" }, | ||
{ "AssemblyProductAttribute", "TestProduct" }, | ||
{ "AssemblyTitleAttribute", "TestTitle" }, | ||
{ "NeutralResourcesLanguageAttribute", "fr" }, | ||
}; | ||
|
||
if (attributeToOptOut == "All") | ||
{ | ||
expectedInfo.Clear(); | ||
} | ||
else | ||
{ | ||
expectedInfo.Remove(attributeToOptOut); | ||
} | ||
|
||
expectedInfo.Add("TargetFrameworkAttribute", ".NETCoreApp,Version=v1.0"); | ||
|
||
var assemblyPath = Path.Combine(buildCommand.GetOutputDirectory("netcoreapp1.0", "Release").FullName, "HelloWorld.dll"); | ||
var actualInfo = AssemblyInfo.Get(assemblyPath); | ||
|
||
actualInfo.Should().Equal(expectedInfo); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// Copyright (c) .NET Foundation and contributors. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Reflection.PortableExecutable; | ||
using System.Reflection.Metadata; | ||
|
||
namespace Microsoft.NET.TestFramework | ||
{ | ||
public static class AssemblyInfo | ||
{ | ||
public static IDictionary<string, string> Get(string assemblyPath) | ||
{ | ||
var dictionary = new SortedDictionary<string, string>(); | ||
|
||
using (var stream = File.OpenRead(assemblyPath)) | ||
using (var peReader = new PEReader(stream)) | ||
{ | ||
var metadataReader = peReader.GetMetadataReader(); | ||
var assemblyDefinition = metadataReader.GetAssemblyDefinition(); | ||
|
||
// AssemblyVersion is not actually a custom attribute | ||
if (assemblyDefinition.Version != new Version(0, 0, 0, 0)) | ||
{ | ||
dictionary.Add("AssemblyVersionAttribute", assemblyDefinition.Version.ToString()); | ||
} | ||
|
||
foreach (var handle in assemblyDefinition.GetCustomAttributes()) | ||
{ | ||
var attribute = metadataReader.GetCustomAttribute(handle); | ||
var constructor = metadataReader.GetMemberReference((MemberReferenceHandle)attribute.Constructor); | ||
var type = metadataReader.GetTypeReference((TypeReferenceHandle)constructor.Parent); | ||
var name = metadataReader.GetString(type.Name); | ||
|
||
var signature = metadataReader.GetBlobReader(constructor.Signature); | ||
var value = metadataReader.GetBlobReader(attribute.Value); | ||
var header = signature.ReadSignatureHeader(); | ||
|
||
const ushort prolog = 1; // two-byte "prolog" defined by ECMA-335 (II.23.3) to be at the beginning of attribute value blobs | ||
if (value.ReadUInt16() != prolog || header.Kind != SignatureKind.Method || header.IsGeneric) | ||
{ | ||
throw new BadImageFormatException(); | ||
} | ||
|
||
if (signature.ReadCompressedInteger() == 1 && // must have 1 parameter | ||
signature.ReadSignatureTypeCode() == SignatureTypeCode.Void && // return type must be void | ||
signature.ReadSignatureTypeCode() == SignatureTypeCode.String) // first parameter must be string | ||
{ | ||
dictionary.Add(name, value.ReadSerializedString()); | ||
} | ||
} | ||
} | ||
|
||
return dictionary; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters