Welcome to mirror list, hosted at ThFree Co, Russian Federation.

RefSpecFixture.cs « LibGit2Sharp.Tests - github.com/mono/libgit2sharp.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ea78f4eb23bf01875777623f2ee8da2732fa3441 (plain)
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
using System.Linq;
using LibGit2Sharp.Tests.TestHelpers;
using Xunit;
using Xunit.Extensions;

namespace LibGit2Sharp.Tests
{
    public class RefSpecFixture : BaseFixture
    {
        [Fact]
        public void CanCountRefSpecs()
        {
            var path = SandboxStandardTestRepo();
            using (var repo = InitIsolatedRepository(path))
            {
                var remote = repo.Network.Remotes["origin"];
                Assert.Equal(1, remote.RefSpecs.Count());
            }
        }

        [Fact]
        public void CanIterateOverRefSpecs()
        {
            var path = SandboxStandardTestRepo();
            using (var repo = InitIsolatedRepository(path))
            {
                var remote = repo.Network.Remotes["origin"];
                int count = 0;
                foreach (RefSpec refSpec in remote.RefSpecs)
                {
                    Assert.NotNull(refSpec);
                    count++;
                }
                Assert.Equal(1, count);
            }
        }

        [Fact]
        public void FetchAndPushRefSpecsComposeRefSpecs()
        {
            var path = SandboxStandardTestRepo();
            using (var repo = InitIsolatedRepository(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 = SandboxStandardTestRepo();
            using (var repo = InitIsolatedRepository(path))
            {
                var remote = repo.Network.Remotes["origin"];

                RefSpec refSpec = remote.RefSpecs.First();
                Assert.NotNull(refSpec);

                Assert.Equal("refs/heads/*", refSpec.Source);
                Assert.Equal("refs/remotes/origin/*", refSpec.Destination);
                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 = SandboxStandardTestRepo();
            using (var repo = InitIsolatedRepository(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 = SandboxStandardTestRepo();
            using (var repo = InitIsolatedRepository(path))
            {
                var remote = repo.Network.Remotes["origin"];
                repo.Network.Remotes.Update(remote, r => r.FetchRefSpecs = fetchRefSpecs);
            }

            using (var repo = InitIsolatedRepository(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 = SandboxStandardTestRepo();
            using (var repo = InitIsolatedRepository(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 = SandboxStandardTestRepo();
            using (var repo = InitIsolatedRepository(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 = SandboxStandardTestRepo();
            using (var repo = InitIsolatedRepository(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());
            }
        }
    }
}