Permalink
Browse files

Ensure incremental build works on AssemblyAttribute items with multip…

…le parameters
  • Loading branch information...
natemcmaster committed Sep 12, 2017
1 parent 8127c13 commit 73fb22a07091585380ead0bcbcdc38839a0266e8
@@ -5,9 +5,13 @@
<ProjectGuid>{BB691360-49BC-46EB-8BA9-6A69A54CD4B0}</ProjectGuid>
</PropertyGroup>
<ItemGroup>
<AssemblyAttribute Include="System.Reflection.AssemblyMetadataAttribute" Condition="'$(BuildNumber)' != ''">
<_Parameter1>BuildNumber</_Parameter1>
<_Parameter2>$(BuildNumber)</_Parameter2>
</AssemblyAttribute>
<Content Include="CopyToOutputFromProjectReference.txt" CopyToOutputDirectory="Always" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="9.0.1" />
</ItemGroup>
</Project>
</Project>
@@ -95,7 +95,8 @@ Copyright (c) .NET Foundation. All rights reserved.
<GeneratedAssemblyInfoInputsCacheFile>$(IntermediateOutputPath)$(MSBuildProjectName).AssemblyInfoInputs.cache</GeneratedAssemblyInfoInputsCacheFile>
</PropertyGroup>
<Hash ItemsToHash="@(AssemblyAttribute->'%(Identity)%(_Parameter1)')">
<!-- We only use up to _Parameter1 for most attributes, but other targets may add additional assembly attributes with multiple parameters. -->
<Hash ItemsToHash="@(AssemblyAttribute->'%(Identity)%(_Parameter1)%(_Parameter2)%(_Parameter3)%(_Parameter4)%(_Parameter5)%(_Parameter6)%(_Parameter7)%(_Parameter8)')">
<Output TaskParameter="HashResult" PropertyName="_AssemblyAttributesHash" />
</Hash>
@@ -153,5 +153,45 @@ BuildCommand BuildProject(string versionPrefix)
return command;
}
}
[Fact]
public void It_respects_custom_assembly_atrribute_items_on_incremental_build()
{
var targetFramework = "netstandard1.5";
var testAsset = _testAssetsManager
.CopyTestAsset("KitchenSink", identifier: targetFramework)
.WithSource()
.Restore(Log, "TestLibrary");
var firstBuildCommand = BuildProject(buildNumber: "1");
var assemblyPath = Path.Combine(firstBuildCommand.GetOutputDirectory(targetFramework).FullName, "TestLibrary.dll");
AssemblyInfo.Get(assemblyPath)["AssemblyMetadataAttribute"].Should().Be("BuildNumber:1");
var firstWriteTime = File.GetLastWriteTimeUtc(assemblyPath);
// When rebuilding with the same value
BuildProject(buildNumber: "1");
// the build should no-op.
File.GetLastWriteTimeUtc(assemblyPath).Should().Be(firstWriteTime);
// When the same project is built again using a different build number
BuildProject(buildNumber: "2");
// the file should change
File.GetLastWriteTimeUtc(assemblyPath).Should().NotBe(firstWriteTime);
// and the custom assembly should be generated with the updated value.
AssemblyInfo.Get(assemblyPath)["AssemblyMetadataAttribute"].Should().Be("BuildNumber:2");
BuildCommand BuildProject(string buildNumber)
{
var command = new BuildCommand(Log, Path.Combine(testAsset.TestRoot, "TestLibrary"));
command.Execute($"/p:BuildNumber={buildNumber}")
.Should()
.Pass();
return command;
}
}
}
}
@@ -6,6 +6,7 @@
using System.IO;
using System.Reflection.PortableExecutable;
using System.Reflection.Metadata;
using System.Text;
namespace Microsoft.NET.TestFramework
{
@@ -41,19 +42,44 @@ public static class AssemblyInfo
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();
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
var paramCount = signature.ReadCompressedInteger();
if (paramCount <= 0 || // must have at least 1 parameter
signature.ReadSignatureTypeCode() != SignatureTypeCode.Void) // return type must be void
{
dictionary.Add(name, value.ReadSerializedString());
continue;
}
var sb = new StringBuilder();
while (paramCount > 0 && sb != null)
{
switch (signature.ReadSignatureTypeCode())
{
case SignatureTypeCode.String:
sb.Append(value.ReadSerializedString());
break;
default:
sb = null;
break;
}
paramCount--;
if (paramCount != 0)
{
sb?.Append(':');
}
}
if (sb != null)
{
dictionary.Add(name, sb.ToString());
}
}
}
return dictionary;
}
}
}
}

0 comments on commit 73fb22a

Please sign in to comment.