Skip to content

Commit 8b82d78

Browse files
asbjornuJakeGinnivan
authored andcommitted
Verify that the generated assembly info contains the GitVersionInformationAttribute and that it has the correct values.
1 parent 6730964 commit 8b82d78

File tree

2 files changed

+40
-5
lines changed

2 files changed

+40
-5
lines changed

src/GitVersionTask.Tests/AssemblyInfoBuilderTests.cs

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.IO;
33
using System.Linq;
4+
using System.Reflection;
45
using System.Runtime.CompilerServices;
56
using ApprovalTests;
67
using GitVersion;
@@ -29,12 +30,20 @@ public void VerifyCreatedCode()
2930
Approvals.Verify(assemblyInfoText);
3031

3132
var compilation = CSharpCompilation.Create("Fake.dll")
32-
.WithOptions(new CSharpCompilationOptions(OutputKind.NetModule))
33+
.WithOptions(new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary))
3334
.AddReferences(MetadataReference.CreateFromFile(typeof(object).Assembly.Location))
3435
.AddSyntaxTrees(CSharpSyntaxTree.ParseText(assemblyInfoText));
3536

36-
var emitResult = compilation.Emit(new MemoryStream());
37-
Assert.IsTrue(emitResult.Success, string.Join(Environment.NewLine, emitResult.Diagnostics.Select(x => x.Descriptor)));
37+
using (var stream = new MemoryStream())
38+
{
39+
var emitResult = compilation.Emit(stream);
40+
41+
Assert.IsTrue(emitResult.Success, string.Join(Environment.NewLine, emitResult.Diagnostics.Select(x => x.Descriptor)));
42+
43+
stream.Seek(0, SeekOrigin.Begin);
44+
var assembly = Assembly.Load(stream.ToArray());
45+
VerifyGitVersionInformationAttribute(assembly, versionVariables);
46+
}
3847
}
3948

4049
[Test]
@@ -89,4 +98,30 @@ static void VerifyAssemblyVersion(AssemblyVersioningScheme avs)
8998
var emitResult = compilation.Emit(new MemoryStream());
9099
Assert.IsTrue(emitResult.Success, string.Join(Environment.NewLine, emitResult.Diagnostics.Select(x => x.Descriptor)));
91100
}
101+
102+
static void VerifyGitVersionInformationAttribute(Assembly assembly, VersionVariables versionVariables)
103+
{
104+
var gitVersionInformationAttributeData = assembly.CustomAttributes
105+
.FirstOrDefault(a => a.AttributeType.Name == "GitVersionInformationAttribute");
106+
107+
Assert.IsNotNull(gitVersionInformationAttributeData);
108+
109+
var gitVersionInformationAttributeType = gitVersionInformationAttributeData.AttributeType;
110+
var gitVersionInformationAttribute = assembly
111+
.GetCustomAttributes(gitVersionInformationAttributeType)
112+
.FirstOrDefault();
113+
114+
Assert.IsNotNull(gitVersionInformationAttribute);
115+
116+
var properties = gitVersionInformationAttributeType.GetProperties(BindingFlags.Instance | BindingFlags.Public);
117+
118+
foreach (var variable in versionVariables)
119+
{
120+
var property = properties.FirstOrDefault(p => p.Name == variable.Key);
121+
Assert.IsNotNull(property);
122+
123+
var propertyValue = property.GetValue(gitVersionInformationAttribute, null);
124+
Assert.AreEqual(variable.Value, propertyValue, "{0} had an invalid value.", property.Name);
125+
}
126+
}
92127
}

src/GitVersionTask/AssemblyInfoBuilder/AssemblyInfoBuilder.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public string GetAssemblyInfoText(VersionVariables vars, string assemblyName)
2323
namespace {6}
2424
{{
2525
[System.Runtime.CompilerServices.CompilerGenerated]
26-
[AttributeUsage(AttributeTargets.Assembly)]
26+
[AttributeUsage(AttributeTargets.Assembly)]
2727
sealed class ReleaseDateAttribute : System.Attribute
2828
{{
2929
public string Date {{ get; private set; }}
@@ -41,7 +41,7 @@ static class GitVersionInformation
4141
}}
4242
4343
[System.Runtime.CompilerServices.CompilerGenerated]
44-
[AttributeUsage(AttributeTargets.Assembly)]
44+
[AttributeUsage(AttributeTargets.Assembly)]
4545
sealed class GitVersionInformationAttribute : System.Attribute
4646
{{
4747
{5}

0 commit comments

Comments
 (0)