1
+ using System ;
2
+ using System . IO ;
3
+ using GitVersion ;
4
+ using GitVersionTask . Tests . Mocks ;
5
+ using Microsoft . Build . Framework ;
6
+ using NUnit . Framework ;
7
+
8
+ [ TestFixture ]
9
+ public class InvalidFileCheckerTests
10
+ {
11
+ string projectDirectory ;
12
+ string projectFile ;
13
+
14
+ [ SetUp ]
15
+ public void CreateTemporaryProject ( )
16
+ {
17
+ projectDirectory = Path . Combine ( Path . GetTempPath ( ) , Guid . NewGuid ( ) . ToString ( ) ) ;
18
+ projectFile = Path . Combine ( projectDirectory , "Fake.csproj" ) ;
19
+
20
+ Directory . CreateDirectory ( projectDirectory ) ;
21
+
22
+ File . Create ( projectFile ) . Close ( ) ;
23
+ }
24
+
25
+ [ TearDown ]
26
+ public void Cleanup ( )
27
+ {
28
+ Directory . Delete ( projectDirectory , true ) ;
29
+ }
30
+
31
+ [ Test ]
32
+ public void VerifyIgnoreNonAssemblyInfoFile ( )
33
+ {
34
+ using ( var writer = File . CreateText ( Path . Combine ( projectDirectory , "SomeOtherFile.cs" ) ) )
35
+ {
36
+ writer . Write ( @"
37
+ using System;
38
+ using System.Reflection;
39
+
40
+ [assembly: AssemblyVersion(""1.0.0.0"")]
41
+ " ) ;
42
+ }
43
+
44
+ InvalidFileChecker . CheckForInvalidFiles ( new ITaskItem [ ] { new MockTaskItem { ItemSpec = "SomeOtherFile.cs" } } , projectFile ) ;
45
+ }
46
+
47
+ [ Test ]
48
+ public void VerifyAttributeFound ( [ Values ( "AssemblyVersion" , "AssemblyFileVersion" , "AssemblyInformationalVersion" , "System.Reflection.AssemblyVersion" ) ] string attribute )
49
+ {
50
+ using ( var writer = File . CreateText ( Path . Combine ( projectDirectory , "AssemblyInfo.cs" ) ) )
51
+ {
52
+ writer . Write ( @"
53
+ using System;
54
+ using System.Reflection;
55
+
56
+ [assembly:{0}(""1.0.0.0"")]
57
+ " , attribute ) ;
58
+ }
59
+
60
+ var ex = Assert . Throws < WarningException > ( ( ) => InvalidFileChecker . CheckForInvalidFiles ( new ITaskItem [ ] { new MockTaskItem { ItemSpec = "AssemblyInfo.cs" } } , projectFile ) , attribute ) ;
61
+ Assert . That ( ex . Message , Is . EqualTo ( "File contains assembly version attributes with conflict with the attributes generated by GitVersion AssemblyInfo.cs" ) ) ;
62
+ }
63
+
64
+ [ Test ]
65
+ public void VerifyUnformattedAttributeFound ( [ Values ( "AssemblyVersion" , "AssemblyFileVersion" , "AssemblyInformationalVersion" , "System . Reflection . AssemblyVersion" ) ] string attribute )
66
+ {
67
+ using ( var writer = File . CreateText ( Path . Combine ( projectDirectory , "AssemblyInfo.cs" ) ) )
68
+ {
69
+ writer . Write ( @"
70
+ using System;
71
+ using System.Reflection;
72
+
73
+ [ assembly :
74
+ {0} ( ""1.0.0.0"")]
75
+ " , attribute ) ;
76
+ }
77
+
78
+ var ex = Assert . Throws < WarningException > ( ( ) => InvalidFileChecker . CheckForInvalidFiles ( new ITaskItem [ ] { new MockTaskItem { ItemSpec = "AssemblyInfo.cs" } } , projectFile ) , attribute ) ;
79
+ Assert . That ( ex . Message , Is . EqualTo ( "File contains assembly version attributes with conflict with the attributes generated by GitVersion AssemblyInfo.cs" ) ) ;
80
+ }
81
+
82
+ [ Test ]
83
+ public void VerifyCommentWorks ( [ Values ( "AssemblyVersion" , "AssemblyFileVersion" , "AssemblyInformationalVersion" ) ] string attribute )
84
+ {
85
+ using ( var writer = File . CreateText ( Path . Combine ( projectDirectory , "AssemblyInfo.cs" ) ) )
86
+ {
87
+ writer . Write ( @"
88
+ using System;
89
+ using System.Reflection;
90
+
91
+ //[assembly: {0}(""1.0.0.0"")]
92
+ " , attribute ) ;
93
+ }
94
+
95
+ InvalidFileChecker . CheckForInvalidFiles ( new ITaskItem [ ] { new MockTaskItem { ItemSpec = "AssemblyInfo.cs" } } , projectFile ) ;
96
+ }
97
+
98
+ [ Test ]
99
+ public void VerifyStringWorks ( [ Values ( "AssemblyVersion" , "AssemblyFileVersion" , "AssemblyInformationalVersion" ) ] string attribute )
100
+ {
101
+ using ( var writer = File . CreateText ( Path . Combine ( projectDirectory , "AssemblyInfo.cs" ) ) )
102
+ {
103
+ writer . Write ( @"
104
+ using System;
105
+ using System.Reflection;
106
+
107
+ public class Temp
108
+ {{
109
+ static const string Foo = ""[assembly: {0}(""""1.0.0.0"""")]"";
110
+ }}
111
+ " , attribute ) ;
112
+ }
113
+
114
+ InvalidFileChecker . CheckForInvalidFiles ( new ITaskItem [ ] { new MockTaskItem { ItemSpec = "AssemblyInfo.cs" } } , projectFile ) ;
115
+ }
116
+
117
+ [ Test ]
118
+ public void VerifyIdentifierWorks ( [ Values ( "AssemblyVersion" , "AssemblyFileVersion" , "AssemblyInformationalVersion" ) ] string attribute )
119
+ {
120
+ using ( var writer = File . CreateText ( Path . Combine ( projectDirectory , "AssemblyInfo.cs" ) ) )
121
+ {
122
+ writer . Write ( @"
123
+ using System;
124
+ using System.Reflection;
125
+
126
+ public class {0}
127
+ {{
128
+ }}
129
+ " , attribute ) ;
130
+ }
131
+
132
+ InvalidFileChecker . CheckForInvalidFiles ( new ITaskItem [ ] { new MockTaskItem { ItemSpec = "AssemblyInfo.cs" } } , projectFile ) ;
133
+ }
134
+ }
0 commit comments