forked from libgit2/libgit2sharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPushFixture.cs
81 lines (70 loc) · 3.57 KB
/
PushFixture.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
using System;
using System.Linq;
using LibGit2Sharp.Tests.TestHelpers;
using Xunit;
namespace LibGit2Sharp.Tests
{
public class PushFixture : BaseFixture
{
private void OnPushStatusError(PushStatusError pushStatusErrors)
{
Assert.True(false, string.Format("Failed to update reference '{0}': {1}",
pushStatusErrors.Reference, pushStatusErrors.Message));
}
private void AssertPush(Action<Repository> push)
{
var scd = BuildSelfCleaningDirectory();
string originalRepoPath = CloneBareTestRepo();
string clonedRepoPath = Repository.Clone(originalRepoPath, scd.DirectoryPath);
using (var originalRepo = new Repository(originalRepoPath))
using (var clonedRepo = new Repository(clonedRepoPath))
{
Remote remote = clonedRepo.Network.Remotes["origin"];
// Compare before
Assert.Equal(originalRepo.Refs["HEAD"].ResolveToDirectReference().TargetIdentifier,
clonedRepo.Refs["HEAD"].ResolveToDirectReference().TargetIdentifier);
Assert.Equal(
clonedRepo.Network.ListReferences(remote).Single(r => r.CanonicalName == "refs/heads/master"),
clonedRepo.Refs.Head.ResolveToDirectReference());
// Change local state (commit)
const string relativeFilepath = "new_file.txt";
Touch(clonedRepo.Info.WorkingDirectory, relativeFilepath, "__content__");
clonedRepo.Index.Stage(relativeFilepath);
clonedRepo.Commit("__commit_message__", Constants.Signature, Constants.Signature);
// Assert local state has changed
Assert.NotEqual(originalRepo.Refs["HEAD"].ResolveToDirectReference().TargetIdentifier,
clonedRepo.Refs["HEAD"].ResolveToDirectReference().TargetIdentifier);
Assert.NotEqual(
clonedRepo.Network.ListReferences(remote).Single(r => r.CanonicalName == "refs/heads/master"),
clonedRepo.Refs.Head.ResolveToDirectReference());
// Push the change upstream (remote state is supposed to change)
push(clonedRepo);
// Assert that both local and remote repos are in sync
Assert.Equal(originalRepo.Refs["HEAD"].ResolveToDirectReference().TargetIdentifier,
clonedRepo.Refs["HEAD"].ResolveToDirectReference().TargetIdentifier);
Assert.Equal(
clonedRepo.Network.ListReferences(remote).Single(r => r.CanonicalName == "refs/heads/master"),
clonedRepo.Refs.Head.ResolveToDirectReference());
}
}
[Fact]
public void CanPushABranchTrackingAnUpstreamBranch()
{
AssertPush(repo => repo.Network.Push(repo.Head));
AssertPush(repo => repo.Network.Push(repo.Branches["master"]));
AssertPush(repo => repo.Network.Push(repo.Network.Remotes["origin"], "HEAD", @"refs/heads/master", OnPushStatusError));
}
[Fact]
public void PushingABranchThatDoesNotTrackAnUpstreamBranchThrows()
{
Assert.Throws<LibGit2SharpException>(
() =>
AssertPush(repo =>
{
Branch branch = repo.Branches["master"];
repo.Branches.Update(branch, b => b.TrackedBranch = null);
repo.Network.Push(branch);
}));
}
}
}