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