Skip to content

Commit 4896d89

Browse files
committed
Construct test to provoke GH-864.
1 parent c6d8764 commit 4896d89

File tree

2 files changed

+203
-1
lines changed

2 files changed

+203
-1
lines changed
Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
using GitTools.Testing;
2+
using GitVersion.Logging;
3+
using GitVersion.Extensions;
4+
using GitVersionCore.Tests.Helpers;
5+
using LibGit2Sharp;
6+
using NUnit.Framework;
7+
using System.Linq;
8+
using NSubstitute;
9+
using System;
10+
using System.Collections.Generic;
11+
12+
namespace GitVersionCore.Tests
13+
{
14+
[TestFixture]
15+
public class RepositoryExtensionsTests : TestBase
16+
{
17+
[Test]
18+
public void EnsureLocalBranchExistsForCurrentBranch_CaseInsensitivelyMatchesBranches()
19+
{
20+
ILog log = Substitute.For<ILog>();
21+
var repository = Substitute.For<IRepository>();
22+
var remote = ConstructRemote(repository);
23+
24+
repository.EnsureLocalBranchExistsForCurrentBranch(log, remote, "refs/heads/featurE/feat-test");
25+
}
26+
27+
private Remote ConstructRemote(IRepository repository)
28+
{
29+
var branches = new TestableBranchCollection(repository);
30+
var tipId = new ObjectId("c6d8764d20ff16c0df14c73680e52b255b608926");
31+
var tip = new TestableCommit(repository, tipId);
32+
var head = branches.Add("refs/heads/feature/feat-test", tip);
33+
var remote = new TesatbleRemote("origin");
34+
var references = new TestableReferenceCollection();
35+
var reference = references.Add("develop", "refs/heads/develop");
36+
37+
repository.Refs.Returns(references);
38+
repository.Head.Returns(head);
39+
repository.Branches.Returns(branches);
40+
return remote;
41+
}
42+
43+
private class TestableBranchCollection : BranchCollection
44+
{
45+
private readonly IRepository repository;
46+
public TestableBranchCollection(IRepository repository)
47+
{
48+
}
49+
50+
IDictionary<string, Branch> branches = new Dictionary<string, Branch>();
51+
52+
public override Branch this[string name] =>
53+
this.branches.ContainsKey(name)
54+
? this.branches[name]
55+
: null;
56+
57+
public override Branch Add(string name, Commit commit)
58+
{
59+
var branch = new TestableBranch(name, commit);
60+
this.branches.Add(name, branch);
61+
return branch;
62+
}
63+
64+
public override Branch Add(string name, string committish)
65+
{
66+
var id = new ObjectId(committish);
67+
var commit = new TestableCommit(this.repository, id);
68+
return Add(name, commit);
69+
}
70+
71+
public override Branch Add(string name, Commit commit, bool allowOverwrite)
72+
{
73+
return Add(name, commit);
74+
}
75+
76+
public override Branch Add(string name, string committish, bool allowOverwrite)
77+
{
78+
return Add(name, committish);
79+
}
80+
81+
public override IEnumerator<Branch> GetEnumerator()
82+
{
83+
return this.branches.Values.GetEnumerator();
84+
}
85+
86+
public override void Remove(string name)
87+
{
88+
this.branches.Remove(name);
89+
}
90+
91+
public override void Remove(string name, bool isRemote)
92+
{
93+
this.branches.Remove(name);
94+
}
95+
96+
public override void Remove(Branch branch)
97+
{
98+
this.branches.Remove(branch.CanonicalName);
99+
}
100+
101+
public override Branch Update(Branch branch, params Action<BranchUpdater>[] actions)
102+
{
103+
return base.Update(branch, actions);
104+
}
105+
}
106+
107+
private class TestableBranch : Branch
108+
{
109+
private readonly string canonicalName;
110+
private readonly Commit tip;
111+
112+
public TestableBranch(string canonicalName, Commit tip)
113+
{
114+
this.tip = tip;
115+
this.canonicalName = canonicalName;
116+
}
117+
118+
public override string CanonicalName => this.canonicalName;
119+
public override Commit Tip => this.tip;
120+
}
121+
122+
private class TestableCommit : Commit, IBelongToARepository
123+
{
124+
private IRepository repository;
125+
private ObjectId id;
126+
127+
public TestableCommit(IRepository repository, ObjectId id)
128+
{
129+
this.repository = repository;
130+
this.id = id;
131+
}
132+
133+
public override ObjectId Id => this.id;
134+
public IRepository Repository => this.repository;
135+
}
136+
137+
private class TesatbleRemote : Remote
138+
{
139+
private string name;
140+
141+
public TesatbleRemote(string name)
142+
{
143+
this.name = name;
144+
}
145+
146+
public override string Name => this.name;
147+
}
148+
149+
private class TestableReferenceCollection : ReferenceCollection
150+
{
151+
Reference reference;
152+
153+
public override DirectReference Add(string name, ObjectId targetId)
154+
{
155+
throw new InvalidOperationException("Update should be invoked when case-insensitively comparing branches.");
156+
}
157+
158+
public override Reference Add(string name, string canonicalRefNameOrObjectish)
159+
{
160+
return this.reference = new TestableReference(canonicalRefNameOrObjectish);;
161+
}
162+
163+
public override Reference UpdateTarget(Reference directRef, ObjectId targetId)
164+
{
165+
return this.reference;
166+
}
167+
168+
public override Reference this[string name] => this.reference;
169+
}
170+
171+
private class TestableReference : Reference
172+
{
173+
private readonly string canonicalName;
174+
175+
public TestableReference(string canonicalName)
176+
{
177+
this.canonicalName = canonicalName;
178+
}
179+
180+
public override string CanonicalName => this.canonicalName;
181+
182+
public override DirectReference ResolveToDirectReference()
183+
{
184+
throw new NotImplementedException();
185+
}
186+
}
187+
}
188+
}

src/GitVersionCore/Extensions/RepositoryExtensions.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,25 @@ public static void DumpGraph(this IRepository repository, Action<string> writer
3636

3737
public static void EnsureLocalBranchExistsForCurrentBranch(this IRepository repo, ILog log, Remote remote, string currentBranch)
3838
{
39+
if (log is null)
40+
{
41+
throw new ArgumentNullException(nameof(log));
42+
}
43+
44+
if (remote is null)
45+
{
46+
throw new ArgumentNullException(nameof(remote));
47+
}
48+
3949
if (string.IsNullOrEmpty(currentBranch)) return;
4050

4151
var isRef = currentBranch.Contains("refs");
4252
var isBranch = currentBranch.Contains("refs/heads");
43-
var localCanonicalName = !isRef ? "refs/heads/" + currentBranch : isBranch ? currentBranch : currentBranch.Replace("refs/", "refs/heads/");
53+
var localCanonicalName = !isRef
54+
? "refs/heads/" + currentBranch
55+
: isBranch
56+
? currentBranch
57+
: currentBranch.Replace("refs/", "refs/heads/");
4458

4559
var repoTip = repo.Head.Tip;
4660

0 commit comments

Comments
 (0)