forked from libgit2/libgit2sharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FetchFixture.cs
293 lines (233 loc) · 12 KB
/
FetchFixture.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using LibGit2Sharp.Tests.TestHelpers;
using Xunit;
namespace LibGit2Sharp.Tests
{
public class FetchFixture : BaseFixture
{
private const string remoteName = "testRemote";
[Theory]
[InlineData("http://github.com/libgit2/TestGitRepository")]
[InlineData("https://github.com/libgit2/TestGitRepository")]
public void CanFetchIntoAnEmptyRepository(string url)
{
string path = InitNewRepository();
using (var repo = new Repository(path))
{
repo.Network.Remotes.Add(remoteName, url);
// Set up structures for the expected results
// and verifying the RemoteUpdateTips callback.
TestRemoteInfo expectedResults = TestRemoteInfo.TestRemoteInstance;
var expectedFetchState = new ExpectedFetchState(remoteName);
// Add expected branch objects
foreach (KeyValuePair<string, ObjectId> kvp in expectedResults.BranchTips)
{
expectedFetchState.AddExpectedBranch(kvp.Key, ObjectId.Zero, kvp.Value);
}
// Add the expected tags
string[] expectedTagNames = { "blob", "commit_tree", "annotated_tag" };
foreach (string tagName in expectedTagNames)
{
TestRemoteInfo.ExpectedTagInfo expectedTagInfo = expectedResults.Tags[tagName];
expectedFetchState.AddExpectedTag(tagName, ObjectId.Zero, expectedTagInfo);
}
// Perform the actual fetch
Commands.Fetch(repo, remoteName, new string[0], new FetchOptions { OnUpdateTips = expectedFetchState.RemoteUpdateTipsHandler }, null);
// Verify the expected
expectedFetchState.CheckUpdatedReferences(repo);
}
}
[SkippableFact]
public void CanFetchIntoAnEmptyRepositoryWithCredentials()
{
InconclusiveIf(() => string.IsNullOrEmpty(Constants.PrivateRepoUrl),
"Populate Constants.PrivateRepo* to run this test");
string path = InitNewRepository();
using (var repo = new Repository(path))
{
repo.Network.Remotes.Add(remoteName, Constants.PrivateRepoUrl);
// Perform the actual fetch
Commands.Fetch(repo, remoteName, new string[0], new FetchOptions
{
CredentialsProvider = Constants.PrivateRepoCredentials
}, null);
}
}
[Theory]
[InlineData("http://github.com/libgit2/TestGitRepository")]
[InlineData("https://github.com/libgit2/TestGitRepository")]
public void CanFetchAllTagsIntoAnEmptyRepository(string url)
{
string path = InitNewRepository();
using (var repo = new Repository(path))
{
repo.Network.Remotes.Add(remoteName, url);
// Set up structures for the expected results
// and verifying the RemoteUpdateTips callback.
TestRemoteInfo remoteInfo = TestRemoteInfo.TestRemoteInstance;
var expectedFetchState = new ExpectedFetchState(remoteName);
// Add expected tags
foreach (KeyValuePair<string, TestRemoteInfo.ExpectedTagInfo> kvp in remoteInfo.Tags)
{
expectedFetchState.AddExpectedTag(kvp.Key, ObjectId.Zero, kvp.Value);
}
// Add expected branch objects
foreach (KeyValuePair<string, ObjectId> kvp in remoteInfo.BranchTips)
{
expectedFetchState.AddExpectedBranch(kvp.Key, ObjectId.Zero, kvp.Value);
}
// Perform the actual fetch
Commands.Fetch(repo, remoteName, new string[0], new FetchOptions
{
TagFetchMode = TagFetchMode.All,
OnUpdateTips = expectedFetchState.RemoteUpdateTipsHandler
}, null);
// Verify the expected
expectedFetchState.CheckUpdatedReferences(repo);
// Verify the reflog entries
Assert.Single(repo.Refs.Log(string.Format("refs/remotes/{0}/master", remoteName))); // Branches are also retrieved
}
}
[Theory]
[InlineData("http://github.com/libgit2/TestGitRepository", "test-branch", "master")]
[InlineData("https://github.com/libgit2/TestGitRepository", "master", "master")]
public void CanFetchCustomRefSpecsIntoAnEmptyRepository(string url, string localBranchName, string remoteBranchName)
{
string path = InitNewRepository();
using (var repo = new Repository(path))
{
repo.Network.Remotes.Add(remoteName, url);
string refSpec = string.Format("refs/heads/{2}:refs/remotes/{0}/{1}", remoteName, localBranchName, remoteBranchName);
// Set up structures for the expected results
// and verifying the RemoteUpdateTips callback.
TestRemoteInfo remoteInfo = TestRemoteInfo.TestRemoteInstance;
var expectedFetchState = new ExpectedFetchState(remoteName);
expectedFetchState.AddExpectedBranch(localBranchName, ObjectId.Zero, remoteInfo.BranchTips[remoteBranchName]);
// Let's account for opportunistic updates during the Fetch() call
if (!string.Equals("master", localBranchName, StringComparison.OrdinalIgnoreCase))
{
expectedFetchState.AddExpectedBranch("master", ObjectId.Zero, remoteInfo.BranchTips["master"]);
}
if (string.Equals("master", localBranchName, StringComparison.OrdinalIgnoreCase)
&& !string.Equals("master", remoteBranchName, StringComparison.OrdinalIgnoreCase))
{
expectedFetchState.AddExpectedBranch(remoteBranchName, ObjectId.Zero, remoteInfo.BranchTips[remoteBranchName]);
}
// Perform the actual fetch
Commands.Fetch(repo, remoteName, new string[] { refSpec }, new FetchOptions
{
TagFetchMode = TagFetchMode.None,
OnUpdateTips = expectedFetchState.RemoteUpdateTipsHandler
}, null);
// Verify the expected
expectedFetchState.CheckUpdatedReferences(repo);
// Verify the reflog entries
var reflogEntry = repo.Refs.Log(string.Format("refs/remotes/{0}/{1}", remoteName, localBranchName)).Single();
Assert.StartsWith("fetch ", reflogEntry.Message);
}
}
[Theory]
[InlineData(TagFetchMode.All, 4)]
[InlineData(TagFetchMode.None, 0)]
[InlineData(TagFetchMode.Auto, 3)]
public void FetchRespectsConfiguredAutoTagSetting(TagFetchMode tagFetchMode, int expectedTagCount)
{
string url = "http://github.com/libgit2/TestGitRepository";
string path = InitNewRepository();
using (var repo = new Repository(path))
{
Remote remote = repo.Network.Remotes.Add(remoteName, url);
Assert.NotNull(remote);
// Update the configured autotag setting.
repo.Network.Remotes.Update(remoteName,
r => r.TagFetchMode = tagFetchMode);
// Perform the actual fetch.
Commands.Fetch(repo, remoteName, new string[0], null, null);
// Verify the number of fetched tags.
Assert.Equal(expectedTagCount, repo.Tags.Count());
}
}
[Fact]
public void CanFetchAllTagsAfterAnInitialClone()
{
var scd = BuildSelfCleaningDirectory();
const string url = "https://github.com/libgit2/TestGitRepository";
string clonedRepoPath = Repository.Clone(url, scd.DirectoryPath);
using (var repo = new Repository(clonedRepoPath))
{
Commands.Fetch(repo, "origin", new string[0], new FetchOptions { TagFetchMode = TagFetchMode.All }, null);
}
}
[Fact]
public void FetchHonorsTheFetchPruneConfigurationEntry()
{
var source = SandboxBareTestRepo();
var url = new Uri($"file://{Path.GetFullPath(source)}").AbsoluteUri;
var scd = BuildSelfCleaningDirectory();
string clonedRepoPath = Repository.Clone(url, scd.DirectoryPath);
using (var clonedRepo = new Repository(clonedRepoPath))
{
Assert.Equal(5, clonedRepo.Branches.Count(b => b.IsRemote && b.FriendlyName != "origin/HEAD"));
// Drop one of the branches in the remote repository
using (var sourceRepo = new Repository(source))
{
sourceRepo.Branches.Remove("packed-test");
}
// No pruning when the configuration entry isn't defined
Assert.Null(clonedRepo.Config.Get<bool>("fetch.prune"));
Commands.Fetch(clonedRepo, "origin", new string[0], null, null);
Assert.Equal(5, clonedRepo.Branches.Count(b => b.IsRemote && b.FriendlyName != "origin/HEAD"));
// No pruning when the configuration entry is set to false
clonedRepo.Config.Set<bool>("fetch.prune", false);
Commands.Fetch(clonedRepo, "origin", new string[0], null, null);
Assert.Equal(5, clonedRepo.Branches.Count(b => b.IsRemote && b.FriendlyName != "origin/HEAD"));
// Auto pruning when the configuration entry is set to true
clonedRepo.Config.Set<bool>("fetch.prune", true);
Commands.Fetch(clonedRepo, "origin", new string[0], null, null);
Assert.Equal(4, clonedRepo.Branches.Count(b => b.IsRemote && b.FriendlyName != "origin/HEAD"));
}
}
[Fact]
public void CannotFetchWithForbiddenCustomHeaders()
{
var scd = BuildSelfCleaningDirectory();
const string url = "https://github.com/libgit2/TestGitRepository";
string clonedRepoPath = Repository.Clone(url, scd.DirectoryPath);
const string knownHeader = "User-Agent: mygit-201";
var options = new FetchOptions { CustomHeaders = new String[] { knownHeader } };
using (var repo = new Repository(clonedRepoPath))
{
Assert.Throws<LibGit2SharpException>(() => Commands.Fetch(repo, "origin", new string[0], options, null));
}
}
[Fact]
public void CanFetchWithCustomHeaders()
{
var scd = BuildSelfCleaningDirectory();
const string url = "https://github.com/libgit2/TestGitRepository";
string clonedRepoPath = Repository.Clone(url, scd.DirectoryPath);
const string knownHeader = "X-Hello: mygit-201";
var options = new FetchOptions { CustomHeaders = new String[] { knownHeader } };
using (var repo = new Repository(clonedRepoPath))
{
Commands.Fetch(repo, "origin", new string[0], options, null);
}
}
[Fact]
public void CannotFetchWithMalformedCustomHeaders()
{
var scd = BuildSelfCleaningDirectory();
const string url = "https://github.com/libgit2/TestGitRepository";
string clonedRepoPath = Repository.Clone(url, scd.DirectoryPath);
const string knownHeader = "Hello world";
var options = new FetchOptions { CustomHeaders = new String[] { knownHeader } };
using (var repo = new Repository(clonedRepoPath))
{
Assert.Throws<LibGit2SharpException>(() => Commands.Fetch(repo, "origin", new string[0], options, null));
}
}
}
}