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