1
1
using System ;
2
2
using System . IO ;
3
3
using System . Linq ;
4
+ using System . Reflection ;
4
5
using System . Runtime . CompilerServices ;
5
6
using ApprovalTests ;
6
7
using GitVersion ;
@@ -29,12 +30,20 @@ public void VerifyCreatedCode()
29
30
Approvals . Verify ( assemblyInfoText ) ;
30
31
31
32
var compilation = CSharpCompilation . Create ( "Fake.dll" )
32
- . WithOptions ( new CSharpCompilationOptions ( OutputKind . NetModule ) )
33
+ . WithOptions ( new CSharpCompilationOptions ( OutputKind . DynamicallyLinkedLibrary ) )
33
34
. AddReferences ( MetadataReference . CreateFromFile ( typeof ( object ) . Assembly . Location ) )
34
35
. AddSyntaxTrees ( CSharpSyntaxTree . ParseText ( assemblyInfoText ) ) ;
35
36
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
+ }
38
47
}
39
48
40
49
[ Test ]
@@ -89,4 +98,30 @@ static void VerifyAssemblyVersion(AssemblyVersioningScheme avs)
89
98
var emitResult = compilation . Emit ( new MemoryStream ( ) ) ;
90
99
Assert . IsTrue ( emitResult . Success , string . Join ( Environment . NewLine , emitResult . Diagnostics . Select ( x => x . Descriptor ) ) ) ;
91
100
}
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
+ }
92
127
}
0 commit comments