1
+ namespace GitTools . Tests . Git
2
+ {
3
+ using GitTools . Git ;
4
+ using LibGit2Sharp ;
5
+ using NUnit . Framework ;
6
+ using Shouldly ;
7
+ using Testing ;
8
+
9
+ public class GitHelperTests
10
+ {
11
+ [ Test ]
12
+ public void NormalisationOfPullRequestsWithFetch ( )
13
+ {
14
+ using ( var fixture = new EmptyRepositoryFixture ( ) )
15
+ {
16
+ fixture . Repository . MakeACommit ( ) ;
17
+
18
+ fixture . Repository . Checkout ( fixture . Repository . CreateBranch ( "feature/foo" ) ) ;
19
+ fixture . Repository . MakeACommit ( ) ;
20
+ var commit = fixture . Repository . CreatePullRequestRef ( "feature/foo" , "master" , prNumber : 3 ) ;
21
+ using ( var localFixture = fixture . CloneRepository ( ) )
22
+ {
23
+ localFixture . Checkout ( commit . Sha ) ;
24
+ GitHelper . NormalizeGitDirectory ( localFixture . RepositoryPath , new AuthenticationInfo ( ) , noFetch : false , currentBranch : string . Empty ) ;
25
+
26
+ var normalisedPullBranch = localFixture . Repository . FindBranch ( "pull/3/merge" ) ;
27
+ normalisedPullBranch . ShouldNotBe ( null ) ;
28
+ }
29
+ }
30
+ }
31
+
32
+ [ Test ]
33
+ public void NormalisationOfPullRequestsWithoutFetch ( )
34
+ {
35
+ using ( var fixture = new EmptyRepositoryFixture ( ) )
36
+ {
37
+ fixture . Repository . MakeACommit ( ) ;
38
+
39
+ fixture . Repository . Checkout ( fixture . Repository . CreateBranch ( "feature/foo" ) ) ;
40
+ fixture . Repository . MakeACommit ( ) ;
41
+ var commit = fixture . Repository . CreatePullRequestRef ( "feature/foo" , "master" , prNumber : 3 , allowFastFowardMerge : true ) ;
42
+ using ( var localFixture = fixture . CloneRepository ( ) )
43
+ {
44
+ localFixture . Checkout ( commit . Sha ) ;
45
+ GitHelper . NormalizeGitDirectory ( localFixture . RepositoryPath , new AuthenticationInfo ( ) , noFetch : true , currentBranch : "refs/pull/3/merge" ) ;
46
+
47
+ var normalisedPullBranch = localFixture . Repository . FindBranch ( "pull/3/merge" ) ;
48
+ normalisedPullBranch . ShouldNotBe ( null ) ;
49
+ }
50
+ }
51
+ }
52
+
53
+ [ Test ]
54
+ public void UpdatesLocalBranchesWhen ( )
55
+ {
56
+ using ( var fixture = new EmptyRepositoryFixture ( ) )
57
+ {
58
+ fixture . Repository . MakeACommit ( ) ;
59
+
60
+ fixture . Repository . Checkout ( fixture . Repository . CreateBranch ( "feature/foo" ) ) ;
61
+ fixture . Repository . MakeACommit ( ) ;
62
+ using ( var localFixture = fixture . CloneRepository ( ) )
63
+ {
64
+ localFixture . Checkout ( "feature/foo" ) ;
65
+ // Advance remote
66
+ var advancedCommit = fixture . Repository . MakeACommit ( ) ;
67
+ GitHelper . NormalizeGitDirectory ( localFixture . RepositoryPath , new AuthenticationInfo ( ) , noFetch : false , currentBranch : null ) ;
68
+
69
+ var normalisedBranch = localFixture . Repository . FindBranch ( "feature/foo" ) ;
70
+ normalisedBranch . ShouldNotBe ( null ) ;
71
+ normalisedBranch . Tip . Sha . ShouldBe ( advancedCommit . Sha ) ;
72
+ }
73
+ }
74
+ }
75
+
76
+ [ Test ]
77
+ public void UpdatesCurrentBranch ( )
78
+ {
79
+ using ( var fixture = new EmptyRepositoryFixture ( ) )
80
+ {
81
+ fixture . Repository . MakeACommit ( ) ;
82
+ fixture . Repository . Checkout ( fixture . Repository . CreateBranch ( "develop" ) ) ;
83
+ fixture . Repository . MakeACommit ( ) ;
84
+ fixture . Repository . Checkout ( "master" ) ;
85
+ using ( var localFixture = fixture . CloneRepository ( ) )
86
+ {
87
+ // Advance remote
88
+ fixture . Repository . Checkout ( "develop" ) ;
89
+ var advancedCommit = fixture . Repository . MakeACommit ( ) ;
90
+ localFixture . Repository . Network . Fetch ( localFixture . Repository . Network . Remotes [ "origin" ] ) ;
91
+ localFixture . Repository . Checkout ( advancedCommit . Sha ) ;
92
+ localFixture . Repository . DumpGraph ( ) ;
93
+ GitHelper . NormalizeGitDirectory ( localFixture . RepositoryPath , new AuthenticationInfo ( ) , noFetch : false , currentBranch : "ref/heads/develop" ) ;
94
+
95
+ var normalisedBranch = localFixture . Repository . FindBranch ( "develop" ) ;
96
+ normalisedBranch . ShouldNotBe ( null ) ;
97
+ fixture . Repository . DumpGraph ( ) ;
98
+ localFixture . Repository . DumpGraph ( ) ;
99
+ normalisedBranch . Tip . Sha . ShouldBe ( advancedCommit . Sha ) ;
100
+ localFixture . Repository . Head . Tip . Sha . ShouldBe ( advancedCommit . Sha ) ;
101
+ }
102
+ }
103
+ }
104
+
105
+ [ Test ]
106
+ public void ShouldNotChangeBranchWhenNormalizingTheDirectory ( )
107
+ {
108
+ using ( var fixture = new EmptyRepositoryFixture ( ) )
109
+ {
110
+ fixture . Repository . MakeATaggedCommit ( "v1.0.0" ) ;
111
+
112
+ fixture . Repository . Checkout ( fixture . Repository . CreateBranch ( "develop" ) ) ;
113
+ var lastCommitOnDevelop = fixture . Repository . MakeACommit ( ) ;
114
+
115
+ fixture . Repository . Checkout ( fixture . Repository . CreateBranch ( "feature/foo" ) ) ;
116
+ fixture . Repository . MakeACommit ( ) ;
117
+
118
+ using ( var localFixture = fixture . CloneRepository ( ) )
119
+ {
120
+ localFixture . Repository . Checkout ( "origin/develop" ) ;
121
+
122
+ // Another commit on feature/foo will force an update
123
+ fixture . Checkout ( "feature/foo" ) ;
124
+ fixture . Repository . MakeACommit ( ) ;
125
+
126
+ GitHelper . NormalizeGitDirectory ( localFixture . RepositoryPath , new AuthenticationInfo ( ) , noFetch : false , currentBranch : null ) ;
127
+
128
+ localFixture . Repository . DumpGraph ( ) ;
129
+ localFixture . Repository . Head . Tip . Sha . ShouldBe ( lastCommitOnDevelop . Sha ) ;
130
+ }
131
+ }
132
+ }
133
+ }
134
+ }
0 commit comments