1
+ using System ;
2
+ using GitVersion ;
3
+ using LibGit2Sharp ;
4
+ using NUnit . Framework ;
5
+ using Shouldly ;
6
+
7
+ [ TestFixture ]
8
+ public class LastVersionOnMasterFinderTests
9
+ {
10
+ /*
11
+ * hotfix-1.2.1 -----------C--
12
+ * / \
13
+ * master A----------------F-----H-------N
14
+ * \ / \ /
15
+ * hotfix-1.3.1 \ / ----L
16
+ * \ / \
17
+ * release-1.3.0 \ -D----G--- \
18
+ * \ / \ \
19
+ * develop -----B----E-------I-----M--O--P
20
+ * \ /
21
+ * feature -------J-K-
22
+ *
23
+ *
24
+ * - A is tagged `1.2.0`
25
+ * - F is tagged `1.2.1`
26
+ * - H is tagged `1.3.0`
27
+ * - N is tagged `1.3.1`
28
+ */
29
+
30
+ [ Test ]
31
+ public void CanCorrectlyDetectCommitCountsAndReleaseDataWhenThatApplies ( )
32
+ {
33
+ using ( var f = new CommitCountingRepoFixture ( ) )
34
+ {
35
+ ResetToP ( f . Repository ) ;
36
+ EnsureMetaDataMatch ( f , "develop" ) ;
37
+
38
+ ResetToO ( f . Repository ) ;
39
+ EnsureMetaDataMatch ( f , "develop" ) ;
40
+
41
+ ResetToN ( f . Repository ) ;
42
+ EnsureMetaDataMatch ( f , "master" , r => ( Commit ) r . Tags [ "1.3.0" ] . Target ) ;
43
+
44
+ ResetToM ( f . Repository ) ;
45
+ EnsureMetaDataMatch ( f , "develop" ) ;
46
+
47
+ ResetToL ( f . Repository ) ;
48
+ EnsureMetaDataMatch ( f , "hotfix-1.3.1" , r => ( Commit ) r . Tags [ "1.3.0" ] . Target ) ;
49
+
50
+ ResetToK ( f . Repository ) ;
51
+ EnsureMetaDataMatch ( f , "feature" ) ;
52
+
53
+ ResetToJ ( f . Repository ) ;
54
+ EnsureMetaDataMatch ( f , "feature" ) ;
55
+
56
+ ResetToI ( f . Repository ) ;
57
+ EnsureMetaDataMatch ( f , "develop" ) ;
58
+
59
+ ResetToH ( f . Repository ) ;
60
+ EnsureMetaDataMatch ( f , "master" , r => ( Commit ) r . Tags [ "1.3.0" ] . Target ) ;
61
+
62
+ ResetToG ( f . Repository ) ;
63
+ EnsureMetaDataMatch ( f , "release-1.3.0" ) ;
64
+
65
+ ResetToF ( f . Repository ) ;
66
+ EnsureMetaDataMatch ( f , "master" , r => ( Commit ) r . Tags [ "1.2.0" ] . Target ) ;
67
+
68
+ ResetToE ( f . Repository ) ;
69
+ EnsureMetaDataMatch ( f , "develop" ) ;
70
+
71
+ ResetToD ( f . Repository ) ;
72
+ EnsureMetaDataMatch ( f , "release-1.3.0" ) ;
73
+
74
+ ResetToC ( f . Repository ) ;
75
+ EnsureMetaDataMatch ( f , "hotfix-1.2.1" , r => ( Commit ) r . Tags [ "1.2.0" ] . Target ) ;
76
+
77
+ ResetToB ( f . Repository ) ;
78
+ EnsureMetaDataMatch ( f , "develop" ) ;
79
+ }
80
+ }
81
+
82
+ static void EnsureMetaDataMatch ( CommitCountingRepoFixture fixture , string branchName , Func < IRepository , Commit > commitFinder = null )
83
+ {
84
+ var referenceCommitFinder = commitFinder ?? ( r => r . FindBranch ( branchName ) . Tip ) ;
85
+
86
+ var commit = referenceCommitFinder ( fixture . Repository ) ;
87
+ var releaseDate = LastVersionOnMasterFinder . Execute ( fixture . Repository , commit ) ;
88
+ releaseDate . OriginalCommitSha . ShouldBe ( commit . Sha ) ;
89
+ releaseDate . OriginalDate . ShouldBe ( commit . Committer . When ) ;
90
+ }
91
+
92
+ void DropTags ( IRepository repo , params string [ ] names )
93
+ {
94
+ foreach ( var name in names )
95
+ {
96
+ if ( repo . Tags [ name ] == null )
97
+ {
98
+ continue ;
99
+ }
100
+
101
+ repo . Tags . Remove ( name ) ;
102
+ }
103
+ }
104
+
105
+ void DropBranches ( IRepository repo , params string [ ] names )
106
+ {
107
+ foreach ( var name in names )
108
+ {
109
+ if ( repo . Branches [ name ] == null )
110
+ {
111
+ continue ;
112
+ }
113
+
114
+ repo . Branches . Remove ( name ) ;
115
+ }
116
+ }
117
+
118
+ void ResetBranch ( IRepository repo , string name , string committish )
119
+ {
120
+ var b = repo . Branches [ name ] ;
121
+ Assert . NotNull ( b ) ;
122
+ repo . Refs . UpdateTarget ( b . CanonicalName , committish ) ;
123
+ }
124
+
125
+ void ResetToP ( IRepository repo )
126
+ {
127
+ ResetBranch ( repo , "develop" , "4d65c519f88773854f9345eaf5dbb30cb49f6a74" ) ;
128
+ }
129
+
130
+ void ResetToO ( IRepository repo )
131
+ {
132
+ ResetBranch ( repo , "develop" , "7655537837096d925a4f974232f78ec589d86ebd" ) ;
133
+ }
134
+
135
+ void ResetToN ( IRepository repo )
136
+ {
137
+ ResetBranch ( repo , "develop" , "0b7a2482ab7d167cefa4ecfc106db001dc5c17ff" ) ;
138
+ repo . Refs . UpdateTarget ( "HEAD" , "refs/heads/master" ) ;
139
+ }
140
+
141
+ void ResetToM ( IRepository repo )
142
+ {
143
+ ResetBranch ( repo , "develop" , "0b7a2482ab7d167cefa4ecfc106db001dc5c17ff" ) ;
144
+ ResetBranch ( repo , "master" , "5b84136c848fd48f1f8b3fa4e1b767a1f6101279" ) ;
145
+ DropTags ( repo , "1.3.1" ) ;
146
+ repo . Refs . UpdateTarget ( "HEAD" , "refs/heads/develop" ) ;
147
+ }
148
+
149
+ void ResetToL ( IRepository repo )
150
+ {
151
+ ResetBranch ( repo , "develop" , "243f56dcdb543688fd0a99bd3e0e72dd9a786603" ) ;
152
+ repo . Refs . UpdateTarget ( "HEAD" , "refs/heads/hotfix-1.3.1" ) ;
153
+ }
154
+
155
+ void ResetToK ( IRepository repo )
156
+ {
157
+ repo . Refs . UpdateTarget ( "HEAD" , "refs/heads/feature" ) ;
158
+ DropBranches ( repo , "hotfix-1.3.1" ) ;
159
+ }
160
+
161
+ void ResetToJ ( IRepository repo )
162
+ {
163
+ ResetBranch ( repo , "feature" , "0491c5dac30d706f4e54c5cb26d082baad8228d1" ) ;
164
+ }
165
+
166
+ void ResetToI ( IRepository repo )
167
+ {
168
+ repo . Refs . UpdateTarget ( "HEAD" , "refs/heads/develop" ) ;
169
+ DropBranches ( repo , "feature" ) ;
170
+ }
171
+
172
+ void ResetToH ( IRepository repo )
173
+ {
174
+ ResetBranch ( repo , "develop" , "320f4b6820cf4b0853dc08ac153f04fbd4958200" ) ;
175
+ repo . Refs . UpdateTarget ( "HEAD" , "refs/heads/master" ) ;
176
+ }
177
+
178
+ void ResetToG ( IRepository repo )
179
+ {
180
+ ResetBranch ( repo , "master" , "576a28e321cd6dc764b52c5fface672fa076f37f" ) ;
181
+ repo . Refs . UpdateTarget ( "HEAD" , "refs/heads/release-1.3.0" ) ;
182
+ DropTags ( repo , "1.3.0" ) ;
183
+ }
184
+
185
+ void ResetToF ( IRepository repo )
186
+ {
187
+ ResetBranch ( repo , "release-1.3.0" , "b53054c614d36edc9d1bee8c35cd2ed575a43607" ) ;
188
+ repo . Refs . UpdateTarget ( "HEAD" , "refs/heads/master" ) ;
189
+ }
190
+
191
+ void ResetToE ( IRepository repo )
192
+ {
193
+ ResetBranch ( repo , "master" , "8c890487ed143d5a72d151e64be1c5ddb314c908" ) ;
194
+ repo . Refs . UpdateTarget ( "HEAD" , "refs/heads/develop" ) ;
195
+ DropTags ( repo , "1.2.1" ) ;
196
+ }
197
+
198
+ void ResetToD ( IRepository repo )
199
+ {
200
+ ResetBranch ( repo , "develop" , "fab69e28ee35dd912c0c95d5993dd84e4f2bcd92" ) ;
201
+ repo . Refs . UpdateTarget ( "HEAD" , "refs/heads/release-1.3.0" ) ;
202
+ }
203
+
204
+ void ResetToC ( IRepository repo )
205
+ {
206
+ repo . Refs . UpdateTarget ( "HEAD" , "refs/heads/hotfix-1.2.1" ) ;
207
+ DropBranches ( repo , "release-1.3.0" ) ;
208
+ }
209
+
210
+ void ResetToB ( IRepository repo )
211
+ {
212
+ repo . Refs . UpdateTarget ( "HEAD" , "refs/heads/develop" ) ;
213
+ DropBranches ( repo , "hotfix-1.2.1" ) ;
214
+ }
215
+ }
0 commit comments