1
1
using System ;
2
+ using System . Collections . Generic ;
2
3
using System . Diagnostics ;
4
+ using System . Text ;
3
5
using GitVersion ;
4
6
using LibGit2Sharp ;
5
7
using Shouldly ;
6
8
7
9
public abstract class RepositoryFixtureBase : IDisposable
8
10
{
11
+ Dictionary < string , string > participants = new Dictionary < string , string > ( ) ;
9
12
public string RepositoryPath ;
10
13
public IRepository Repository ;
11
- private Config configuration ;
14
+ Config configuration ;
15
+ StringBuilder diagramBuilder ;
12
16
13
17
protected RepositoryFixtureBase ( Func < string , IRepository > repoBuilder , Config configuration )
14
18
{
19
+ diagramBuilder = new StringBuilder ( ) ;
20
+ diagramBuilder . AppendLine ( "@startuml" ) ;
15
21
this . configuration = configuration ;
16
22
RepositoryPath = PathHelper . GetTempPath ( ) ;
17
23
Repository = repoBuilder ( RepositoryPath ) ;
@@ -22,15 +28,118 @@ protected RepositoryFixtureBase(Func<string, IRepository> repoBuilder, Config co
22
28
23
29
public bool IsForTrackedBranchOnly { private get ; set ; }
24
30
31
+ public void Checkout ( string branch )
32
+ {
33
+ Repository . Checkout ( branch ) ;
34
+ }
35
+
36
+ public void Activate ( string branch )
37
+ {
38
+ diagramBuilder . AppendLineFormat ( "activate {0}" , GetParticipant ( branch ) ) ;
39
+ }
40
+
41
+ public void Destroy ( string branch )
42
+ {
43
+ diagramBuilder . AppendLineFormat ( "destroy {0}" , GetParticipant ( branch ) ) ;
44
+ }
45
+
46
+ public void Participant ( string participant , string @as = null )
47
+ {
48
+ participants . Add ( participant , @as ?? participant ) ;
49
+ if ( @as == null )
50
+ diagramBuilder . AppendLineFormat ( "participant {0}" , participant ) ;
51
+ else
52
+ diagramBuilder . AppendLineFormat ( "participant \" {0}\" as {1}" , participant , @as ) ;
53
+ }
54
+
55
+ public void Divider ( string text )
56
+ {
57
+ diagramBuilder . AppendLineFormat ( "== {0} ==" , text ) ;
58
+ }
59
+
60
+ public void NoteOver ( string noteText , string startNode , string endNode = null , string prefix = null )
61
+ {
62
+ diagramBuilder . AppendLineFormat (
63
+ prefix + @"note over {0}{1}
64
+ {2}
65
+ end note" ,
66
+ GetParticipant ( startNode ) ,
67
+ endNode == null ? null : ", " + GetParticipant ( endNode ) ,
68
+ noteText . Replace ( "\n " , "\n " ) ) ;
69
+ }
70
+
71
+ public void MakeATaggedCommit ( string tag )
72
+ {
73
+ MakeACommit ( ) ;
74
+ ApplyTag ( tag ) ;
75
+ }
76
+
77
+ public void ApplyTag ( string tag )
78
+ {
79
+ diagramBuilder . AppendLineFormat ( "{0} -> {0}: tag {1}" , GetParticipant ( Repository . Head . Name ) , tag ) ;
80
+ Repository . ApplyTag ( tag ) ;
81
+ }
82
+
83
+ public void BranchTo ( string branchName , string @as = null )
84
+ {
85
+ if ( ! participants . ContainsKey ( branchName ) )
86
+ {
87
+ diagramBuilder . Append ( "create " ) ;
88
+ Participant ( branchName , @as ) ;
89
+ }
90
+ else
91
+ {
92
+ NoteOver ( branchName , branchName , prefix : "h" ) ;
93
+ }
94
+
95
+ var branch = Repository . Head . Name ;
96
+ diagramBuilder . AppendLineFormat ( "{0} -> {1}: branch from {2}" , GetParticipant ( branch ) , GetParticipant ( branchName ) , branch ) ;
97
+ Repository . CreateBranch ( branchName ) . Checkout ( ) ;
98
+ }
99
+
100
+ public void BranchToFromTag ( string branchName , string fromTag , string onBranch , string @as = null )
101
+ {
102
+ if ( ! participants . ContainsKey ( branchName ) )
103
+ {
104
+ diagramBuilder . Append ( "create " ) ;
105
+ Participant ( branchName , @as ) ;
106
+ }
107
+
108
+ diagramBuilder . AppendLineFormat ( "{0} -> {1}: branch from tag ({2})" , GetParticipant ( onBranch ) , GetParticipant ( branchName ) , fromTag ) ;
109
+ Repository . CreateBranch ( branchName ) . Checkout ( ) ;
110
+ }
111
+
112
+ public void MakeACommit ( )
113
+ {
114
+ diagramBuilder . AppendLineFormat ( "{0} -> {0}: commit" , GetParticipant ( Repository . Head . Name ) ) ;
115
+ Repository . MakeACommit ( ) ;
116
+ }
117
+
118
+ public void MergeNoFF ( string mergeTarget )
119
+ {
120
+ diagramBuilder . AppendLineFormat ( "{0} -> {1}: merge" , GetParticipant ( mergeTarget ) , GetParticipant ( Repository . Head . Name ) ) ;
121
+ Repository . MergeNoFF ( mergeTarget , Constants . SignatureNow ( ) ) ;
122
+ }
123
+
25
124
public void AssertFullSemver ( string fullSemver , IRepository repository = null , string commitId = null )
26
125
{
27
126
Trace . WriteLine ( "---------" ) ;
28
127
29
128
var variables = GetVersion ( repository , commitId ) ;
30
129
variables . FullSemVer . ShouldBe ( fullSemver ) ;
130
+ if ( commitId == null )
131
+ diagramBuilder . AppendLineFormat ( "note over {0} #D3D3D3: {1}" , GetParticipant ( Repository . Head . Name ) , fullSemver ) ;
132
+ }
133
+
134
+ string GetParticipant ( string branch )
135
+ {
136
+ if ( participants . ContainsKey ( branch ) )
137
+ return participants [ branch ] ;
138
+
139
+ return branch ;
31
140
}
32
141
33
- public VersionVariables GetVersion ( IRepository repository = null , string commitId = null )
142
+ VersionVariables GetVersion ( IRepository repository = null , string commitId = null )
34
143
{
35
144
var gitVersionContext = new GitVersionContext ( repository ?? Repository , configuration , IsForTrackedBranchOnly , commitId ) ;
36
145
var executeGitVersion = ExecuteGitVersion ( gitVersionContext ) ;
@@ -51,7 +160,7 @@ public VersionVariables GetVersion(IRepository repository = null, string commitI
51
160
}
52
161
}
53
162
54
- private SemanticVersion ExecuteGitVersion ( GitVersionContext context )
163
+ SemanticVersion ExecuteGitVersion ( GitVersionContext context )
55
164
{
56
165
var vf = new GitVersionFinder ( ) ;
57
166
return vf . FindVersion ( context ) ;
@@ -67,7 +176,12 @@ public virtual void Dispose()
67
176
}
68
177
catch ( Exception e )
69
178
{
70
- Console . WriteLine ( "Failed to clean up repository path at {0}. Received exception: {1}" , RepositoryPath , e . Message ) ;
179
+ Trace . WriteLine ( string . Format ( "Failed to clean up repository path at {0}. Received exception: {1}" , RepositoryPath , e . Message ) ) ;
71
180
}
181
+
182
+ diagramBuilder . AppendLine ( "@enduml" ) ;
183
+ Trace . WriteLine ( "**Visualisation of test:**" ) ;
184
+ Trace . WriteLine ( string . Empty ) ;
185
+ Trace . WriteLine ( diagramBuilder . ToString ( ) ) ;
72
186
}
73
187
}
0 commit comments