Skip to content
This repository has been archived by the owner on Dec 18, 2017. It is now read-only.

Commit

Permalink
Use System.Reflection.Metadata in functional test.
Browse files Browse the repository at this point in the history
  • Loading branch information
Cesar Blum Silveira committed Oct 14, 2015
1 parent c294701 commit 6649e9c
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 9 deletions.
61 changes: 53 additions & 8 deletions test/Microsoft.Dnx.Tooling.FunctionalTests/DnuPackTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
using System.IO;
using System.Linq;
using System.Reflection;
using System.Reflection.Metadata;
using System.Reflection.PortableExecutable;
using Microsoft.AspNet.Testing.xunit;
using Microsoft.Dnx.Testing.Framework;
using Newtonsoft.Json.Linq;
Expand Down Expand Up @@ -131,16 +133,59 @@ public void AssemblyInfo(DnxSdk sdk)
#else
var assemblyPath = Path.Combine(result.RootPath, result.Configuration, "dnxcore50", "Test.dll");
#endif
var assembly = Assembly.LoadFrom(assemblyPath);
var attributes = assembly.GetCustomAttributes(true);
Assert.Equal(project.Title, attributes.OfType<AssemblyTitleAttribute>().Single().Title);
Assert.Equal(project.Description, attributes.OfType<AssemblyDescriptionAttribute>().Single().Description);
Assert.Equal(project.Copyright, attributes.OfType<AssemblyCopyrightAttribute>().Single().Copyright);
Assert.Equal(project.AssemblyFileVersion.ToString(), attributes.OfType<AssemblyFileVersionAttribute>().Single().Version);
Assert.Equal(project.Version.ToString(), attributes.OfType<AssemblyInformationalVersionAttribute>().Single().InformationalVersion);
Assert.Equal(project.Version.Version, assembly.GetName().Version);
using (var stream = File.OpenRead(assemblyPath))
{
using (var peReader = new PEReader(stream))
{
var metadataReader = peReader.GetMetadataReader();
var assemblyDefinition = metadataReader.GetAssemblyDefinition();
var attributes = assemblyDefinition.GetCustomAttributes()
.Select(handle => metadataReader.GetCustomAttribute(handle))
.ToDictionary(attribute => GetAttributeName(attribute, metadataReader), attribute => GetAttributeArgument(attribute, metadataReader));

Assert.Equal(project.Title, attributes[typeof(AssemblyTitleAttribute).Name]);
Assert.Equal(project.Description, attributes[typeof(AssemblyDescriptionAttribute).Name]);
Assert.Equal(project.Copyright, attributes[typeof(AssemblyCopyrightAttribute).Name]);
Assert.Equal(project.AssemblyFileVersion.ToString(), attributes[typeof(AssemblyFileVersionAttribute).Name]);
Assert.Equal(project.Version.ToString(), attributes[typeof(AssemblyInformationalVersionAttribute).Name]);
Assert.Equal(project.Version.Version, assemblyDefinition.Version);
}
}

TestUtils.CleanUpTestDir<DnuPackTests>(sdk);
}

private string GetAttributeName(CustomAttribute attribute, MetadataReader metadataReader)
{
var container = metadataReader.GetMemberReference((MemberReferenceHandle)attribute.Constructor).Parent;
var name = metadataReader.GetTypeReference((TypeReferenceHandle)container).Name;
return metadataReader.GetString(name);
}

private string GetAttributeArgument(CustomAttribute attribute, MetadataReader metadataReader)
{
var signature = metadataReader.GetMemberReference((MemberReferenceHandle)attribute.Constructor).Signature;
var signatureReader = metadataReader.GetBlobReader(signature);
var valueReader = metadataReader.GetBlobReader(attribute.Value);

valueReader.ReadUInt16(); // Skip prolog
signatureReader.ReadSignatureHeader(); // Skip header

int parameterCount;
signatureReader.TryReadCompressedInteger(out parameterCount);

signatureReader.ReadSignatureTypeCode(); // Skip return type

for (int i = 0; i < parameterCount; i++)
{
var signatureTypeCode = signatureReader.ReadSignatureTypeCode();
if (signatureTypeCode == SignatureTypeCode.String)
{
return valueReader.ReadSerializedString();
}
}

return string.Empty;
}
}
}
2 changes: 1 addition & 1 deletion test/Microsoft.Dnx.Tooling.FunctionalTests/project.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"dependencies": {
"System.Reflection.Metadata": "1.0.0-*",
"System.Reflection.Metadata": "1.0.21-*",
"Microsoft.AspNet.Testing": "1.0.0-*",
"Microsoft.Dnx.CommonTestUtils": "1.0.0-*",
"Microsoft.Dnx.Runtime.Sources": "1.0.0-*",
Expand Down

0 comments on commit 6649e9c

Please sign in to comment.