diff options
author | Jan Melcher <info@yogularm.de> | 2013-12-03 02:41:07 +0400 |
---|---|---|
committer | Jan Melcher <info@yogularm.de> | 2013-12-03 02:41:07 +0400 |
commit | b51570e8b34e36902b409b8af164190559aae4ca (patch) | |
tree | 4ebb69cf4e08234272698a87e408e94d3f05537b /LibGit2Sharp.Tests | |
parent | 439ee14954e230d88a8950a30ae2958cccc1556f (diff) |
Extended RemoteUpdater to update refspecs
Push/Pull Ref Specs can be updated via Remotes.Update(). The collections
can either be manipulated or replaced completely. Any changes are saved
immediately.
Diffstat (limited to 'LibGit2Sharp.Tests')
-rw-r--r-- | LibGit2Sharp.Tests/RefSpecFixture.cs | 140 |
1 files changed, 140 insertions, 0 deletions
diff --git a/LibGit2Sharp.Tests/RefSpecFixture.cs b/LibGit2Sharp.Tests/RefSpecFixture.cs index b39d068b..673585e4 100644 --- a/LibGit2Sharp.Tests/RefSpecFixture.cs +++ b/LibGit2Sharp.Tests/RefSpecFixture.cs @@ -2,6 +2,7 @@ using System.Linq; using LibGit2Sharp.Tests.TestHelpers; using Xunit; +using Xunit.Extensions; namespace LibGit2Sharp.Tests { @@ -36,6 +37,20 @@ namespace LibGit2Sharp.Tests } [Fact] + public void FetchAndPushRefSpecsComposeRefSpecs() + { + var path = CloneStandardTestRepo(); + using (var repo = new Repository(path)) + { + var remote = repo.Network.Remotes["origin"]; + + var totalRefSpecs = remote.FetchRefSpecs.Concat(remote.PushRefSpecs); + var orderedRefSpecs = remote.RefSpecs.OrderBy(r => r.Direction == RefSpecDirection.Fetch ? 0 : 1); + Assert.Equal(orderedRefSpecs, totalRefSpecs); + } + } + + [Fact] public void CanReadRefSpecDetails() { var path = CloneStandardTestRepo(); @@ -51,5 +66,130 @@ namespace LibGit2Sharp.Tests Assert.Equal(true, refSpec.ForceUpdate); } } + + [Theory] + [InlineData(new string[] { "+refs/tags/*:refs/tags/*" }, new string[] { "refs/heads/*:refs/remotes/test/*", "+refs/abc:refs/def" })] + [InlineData(new string[] { "+refs/abc/x:refs/def/x", "refs/def:refs/ghi" }, new string[0])] + [InlineData(new string[0], new string[] { "refs/ghi:refs/jkl/mno" })] + public void CanReplaceRefSpecs(string[] newFetchRefSpecs, string[] newPushRefSpecs) + { + var path = CloneStandardTestRepo(); + using (var repo = new Repository(path)) + { + var remote = repo.Network.Remotes["origin"]; + var oldRefSpecs = remote.RefSpecs.ToList(); + + var newRemote = repo.Network.Remotes.Update(remote, + r => r.FetchRefSpecs = newFetchRefSpecs, r => r.PushRefSpecs = newPushRefSpecs); + + Assert.Equal(oldRefSpecs, remote.RefSpecs.ToList()); + + var actualNewFetchRefSpecs = newRemote.RefSpecs + .Where(s => s.Direction == RefSpecDirection.Fetch) + .Select(r => r.Specification) + .ToArray(); + Assert.Equal(newFetchRefSpecs, actualNewFetchRefSpecs); + + var actualNewPushRefSpecs = newRemote.RefSpecs + .Where(s => s.Direction == RefSpecDirection.Push) + .Select(r => r.Specification) + .ToArray(); + Assert.Equal(newPushRefSpecs, actualNewPushRefSpecs); + } + } + + [Fact] + public void RemoteUpdaterSavesRefSpecsPermanently() + { + var fetchRefSpecs = new string[] { "refs/their/heads/*:refs/my/heads/*", "+refs/their/tag:refs/my/tag" }; + + var path = CloneStandardTestRepo(); + using (var repo = new Repository(path)) + { + var remote = repo.Network.Remotes["origin"]; + repo.Network.Remotes.Update(remote, r => r.FetchRefSpecs = fetchRefSpecs); + } + + using (var repo = new Repository(path)) + { + var remote = repo.Network.Remotes["origin"]; + var actualRefSpecs = remote.RefSpecs + .Where(r => r.Direction == RefSpecDirection.Fetch) + .Select(r => r.Specification) + .ToArray(); + Assert.Equal(fetchRefSpecs, actualRefSpecs); + } + } + + [Fact] + public void CanAddAndRemoveRefSpecs() + { + string newRefSpec = "+refs/heads/test:refs/heads/other-test"; + + var path = CloneStandardTestRepo(); + using (var repo = new Repository(path)) + { + var remote = repo.Network.Remotes["origin"]; + + remote = repo.Network.Remotes.Update(remote, + r => r.FetchRefSpecs.Add(newRefSpec), + r => r.PushRefSpecs.Add(newRefSpec)); + + Assert.Contains(newRefSpec, remote.FetchRefSpecs.Select(r => r.Specification)); + Assert.Contains(newRefSpec, remote.PushRefSpecs.Select(r => r.Specification)); + + remote = repo.Network.Remotes.Update(remote, + r => r.FetchRefSpecs.Remove(newRefSpec), + r => r.PushRefSpecs.Remove(newRefSpec)); + + Assert.DoesNotContain(newRefSpec, remote.FetchRefSpecs.Select(r => r.Specification)); + Assert.DoesNotContain(newRefSpec, remote.PushRefSpecs.Select(r => r.Specification)); + } + } + + [Fact] + public void CanClearRefSpecs() + { + var path = CloneStandardTestRepo(); + using (var repo = new Repository(path)) + { + var remote = repo.Network.Remotes["origin"]; + + // Push refspec does not exist in cloned repository + remote = repo.Network.Remotes.Update(remote, r => r.PushRefSpecs.Add("+refs/test:refs/test")); + + remote = repo.Network.Remotes.Update(remote, + r => r.FetchRefSpecs.Clear(), + r => r.PushRefSpecs.Clear()); + + Assert.Empty(remote.FetchRefSpecs); + Assert.Empty(remote.PushRefSpecs); + Assert.Empty(remote.RefSpecs); + } + } + + [Theory] + [InlineData("refs/test:refs//double-slash")] + [InlineData("refs/trailing-slash/:refs/test")] + [InlineData("refs/.dotfile:refs/test")] + [InlineData("refs/.:refs/dotdir")] + [InlineData("refs/asterix:refs/not-matching/*")] + [InlineData("refs/double/*/asterix/*:refs/double/*asterix/*")] + [InlineData("refs/ whitespace:refs/test")] + public void SettingInvalidRefSpecsThrows(string refSpec) + { + var path = CloneStandardTestRepo(); + using (var repo = new Repository(path)) + { + var remote = repo.Network.Remotes["origin"]; + var oldRefSpecs = remote.RefSpecs.Select(r => r.Specification).ToList(); + + Assert.Throws<LibGit2SharpException>(() => + repo.Network.Remotes.Update(remote, r => r.FetchRefSpecs.Add(refSpec))); + + var newRemote = repo.Network.Remotes["origin"]; + Assert.Equal(oldRefSpecs, newRemote.RefSpecs.Select(r => r.Specification).ToList()); + } + } } } |