1
1
namespace GitVersionExe . Tests
2
2
{
3
3
using System ;
4
- using System . CodeDom . Compiler ;
4
+ using System . IO ;
5
5
using System . Reflection ;
6
6
using GitVersion ;
7
+ using Mono . Cecil ;
7
8
using NUnit . Framework ;
8
9
9
10
[ TestFixture ]
10
11
public class VersionWriterTests
11
12
{
12
- [ Category ( "NoMono" ) ]
13
- [ Description ( "Won't run on Mono because generating an assembly at runtime does not work on mono" ) ]
14
13
[ Test ]
15
14
public void WriteVersion_ShouldWriteFileVersion_WithNoPrereleaseTag ( )
16
15
{
@@ -23,8 +22,6 @@ public void WriteVersion_ShouldWriteFileVersion_WithNoPrereleaseTag()
23
22
Assert . AreEqual ( "1.0.0" , version ) ;
24
23
}
25
24
26
- [ Category ( "NoMono" ) ]
27
- [ Description ( "Won't run on Mono because generating an assembly at runtime does not work on mono" ) ]
28
25
[ Test ]
29
26
public void WriteVersion_ShouldWriteFileVersion_WithPrereleaseTag ( )
30
27
{
@@ -39,30 +36,21 @@ public void WriteVersion_ShouldWriteFileVersion_WithPrereleaseTag()
39
36
40
37
private Assembly GenerateAssembly ( Version fileVersion , string prereleaseInfo )
41
38
{
42
- var source = string . Format ( @"
43
- using System.Reflection;
44
- using System.Runtime.CompilerServices;
45
- [assembly: AssemblyTitle(""GitVersion.DynamicUnitTests"")]
46
- [assembly: AssemblyProduct(""GitVersion"")]
47
- [assembly: AssemblyVersion(""{0}"")]
48
- [assembly: AssemblyFileVersion(""{0}"")]
49
- [assembly: AssemblyInformationalVersion(""{0}{1}"")]
39
+ var definition = new AssemblyNameDefinition ( "test-asm" , fileVersion ) ;
50
40
51
- public class B
52
- {{
53
- public static int k=7;
54
- }}
55
- " , fileVersion , prereleaseInfo ) ;
41
+ var asmDef = AssemblyDefinition . CreateAssembly ( definition , "test-asm" , ModuleKind . Dll ) ;
42
+ var constructor = typeof ( AssemblyInformationalVersionAttribute ) . GetConstructor ( new [ ] { typeof ( string ) } ) ;
43
+ var methodReference = asmDef . MainModule . Import ( constructor ) ;
44
+ var customAttribute = new CustomAttribute ( methodReference ) ;
45
+ customAttribute . ConstructorArguments . Add ( new CustomAttributeArgument ( asmDef . MainModule . TypeSystem . String , fileVersion + prereleaseInfo ) ) ;
46
+ asmDef . CustomAttributes . Add ( customAttribute ) ;
56
47
57
- CompilerParameters parameters = new CompilerParameters
48
+ using ( var memoryStream = new MemoryStream ( ) )
58
49
{
59
- GenerateInMemory = true ,
60
- GenerateExecutable = false ,
61
- OutputAssembly = "GitVersion.DynamicUnitTests.dll"
62
- } ;
50
+ asmDef . Write ( memoryStream ) ;
63
51
64
- var r = CodeDomProvider . CreateProvider ( "CSharp" ) . CompileAssemblyFromSource ( parameters , source ) ;
65
- return r . CompiledAssembly ;
52
+ return Assembly . Load ( memoryStream . ToArray ( ) ) ;
53
+ }
66
54
}
67
55
}
68
56
}
0 commit comments