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

RemoteFixture.cs « LibGit2Sharp.Tests - github.com/mono/libgit2sharp.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 8b8c81133ba5c1a7f2a4f7830a4ed6f9b83ba191 (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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
using System.Linq;
using LibGit2Sharp.Tests.TestHelpers;
using Xunit;
using Xunit.Extensions;

namespace LibGit2Sharp.Tests
{
    public class RemoteFixture : BaseFixture
    {
        [Fact]
        public void CanGetRemoteOrigin()
        {
            var path = SandboxStandardTestRepoGitDir();
            using (var repo = new Repository(path))
            {
                Remote origin = repo.Network.Remotes["origin"];
                Assert.NotNull(origin);
                AssertBelongsToARepository(repo, origin);
                Assert.Equal("origin", origin.Name);
                Assert.Equal("c:/GitHub/libgit2sharp/Resources/testrepo.git", origin.Url);
            }
        }

        [Fact]
        public void GettingRemoteThatDoesntExistReturnsNull()
        {
            var path = SandboxStandardTestRepoGitDir();
            using (var repo = new Repository(path))
            {
                Assert.Null(repo.Network.Remotes["test"]);
            }
        }

        [Fact]
        public void CanEnumerateTheRemotes()
        {
            var path = SandboxStandardTestRepoGitDir();
            using (var repo = new Repository(path))
            {
                int count = 0;

                foreach (Remote remote in repo.Network.Remotes)
                {
                    Assert.NotNull(remote);
                    AssertBelongsToARepository(repo, remote);
                    count++;
                }

                Assert.Equal(2, count);
            }
        }

        [Theory]
        [InlineData(TagFetchMode.All)]
        [InlineData(TagFetchMode.Auto)]
        [InlineData(TagFetchMode.None)]
        public void CanSetTagFetchMode(TagFetchMode tagFetchMode)
        {
            string path = SandboxBareTestRepo();
            using (var repo = new Repository(path))
            {
                const string name = "upstream";
                const string url = "https://github.com/libgit2/libgit2sharp.git";

                repo.Network.Remotes.Add(name, url);
                Remote remote = repo.Network.Remotes[name];
                Assert.NotNull(remote);

                Remote updatedremote = repo.Network.Remotes.Update(remote,
                    r => r.TagFetchMode = tagFetchMode);

                Assert.Equal(tagFetchMode, updatedremote.TagFetchMode);
            }
        }

        [Fact]
        public void CanSetRemoteUrl()
        {
            string path = SandboxBareTestRepo();
            using (var repo = new Repository(path))
            {
                const string name = "upstream";
                const string url = "https://github.com/libgit2/libgit2sharp.git";
                const string newUrl = "https://github.com/libgit2/libgit2.git";

                repo.Network.Remotes.Add(name, url);
                Remote remote = repo.Network.Remotes[name];
                Assert.NotNull(remote);

                Remote updatedremote = repo.Network.Remotes.Update(remote,
                    r => r.Url = newUrl);

                Assert.Equal(newUrl, updatedremote.Url);
                // with no push url set, PushUrl defaults to the fetch url
                Assert.Equal(newUrl, updatedremote.PushUrl);
            }
        }

        [Fact]
        public void CanSetRemotePushUrl()
        {
            string path = SandboxBareTestRepo();
            using (var repo = new Repository(path))
            {
                const string name = "upstream";
                const string url = "https://github.com/libgit2/libgit2sharp.git";
                const string pushurl = "https://github.com/libgit2/libgit2.git";

                repo.Network.Remotes.Add(name, url);
                Remote remote = repo.Network.Remotes[name];
                Assert.NotNull(remote);

                // before setting push, both push and fetch urls should match
                Assert.Equal(url, remote.Url);
                Assert.Equal(url, remote.PushUrl);

                Remote updatedremote = repo.Network.Remotes.Update(remote,
                    r => r.PushUrl = pushurl);

                // url should not change, push url should be set to new value
                Assert.Equal(url, updatedremote.Url);
                Assert.Equal(pushurl, updatedremote.PushUrl);
            }
        }

        [Fact]
        public void CanCheckEqualityOfRemote()
        {
            string path = SandboxStandardTestRepo();
            using (var repo = new Repository(path))
            {
                Remote oneOrigin = repo.Network.Remotes["origin"];
                Assert.NotNull(oneOrigin);

                Remote otherOrigin = repo.Network.Remotes["origin"];
                Assert.Equal(oneOrigin, otherOrigin);

                Remote createdRemote = repo.Network.Remotes.Add("origin2", oneOrigin.Url);

                Remote loadedRemote = repo.Network.Remotes["origin2"];
                Assert.NotNull(loadedRemote);
                Assert.Equal(createdRemote, loadedRemote);

                Assert.NotEqual(oneOrigin, loadedRemote);
            }
        }

        [Fact]
        public void CreatingANewRemoteAddsADefaultRefSpec()
        {
            string path = SandboxStandardTestRepo();
            using (var repo = new Repository(path))
            {
                const string name = "upstream";
                const string url = "https://github.com/libgit2/libgit2sharp.git";

                repo.Network.Remotes.Add(name, url);
                Remote remote = repo.Network.Remotes[name];
                Assert.NotNull(remote);

                Assert.Equal(name, remote.Name);
                Assert.Equal(url, remote.Url);

                var refSpec = repo.Config.Get<string>("remote", remote.Name, "fetch");
                Assert.NotNull(refSpec);

                Assert.Equal("+refs/heads/*:refs/remotes/upstream/*", refSpec.Value);
            }
        }

        [Fact]
        public void CanAddANewRemoteWithAFetchRefSpec()
        {
            string path = SandboxStandardTestRepo();
            using (var repo = new Repository(path))
            {
                const string name = "pull-requests";
                const string url = "https://github.com/libgit2/libgit2sharp.git";
                const string fetchRefSpec = "+refs/pull/*:refs/remotes/pull-requests/*";

                repo.Network.Remotes.Add(name, url, fetchRefSpec);

                var refSpec = repo.Config.Get<string>("remote", name, "fetch");
                Assert.NotNull(refSpec);

                Assert.Equal(fetchRefSpec, refSpec.Value);
            }
        }

        [Theory]
        [InlineData("sher.lock")]
        [InlineData("/")]
        public void AddingARemoteWithAnInvalidNameThrows(string name)
        {
            string path = SandboxBareTestRepo();
            using (var repo = new Repository(path))
            {
                const string url = "https://github.com/libgit2/libgit2sharp.git";

                Assert.Throws<InvalidSpecificationException>(() => repo.Network.Remotes.Add(name, url));
            }
        }

        [Theory]
        [InlineData("valid/remote", true)]
        [InlineData("sher.lock", false)]
        [InlineData("/", false)]
        public void CanTellIfARemoteNameIsValid(string refname, bool expectedResult)
        {
            Assert.Equal(expectedResult, Remote.IsValidName(refname));
        }

        [Fact]
        public void DoesNotThrowWhenARemoteHasNoUrlSet()
        {
            var path = SandboxStandardTestRepoGitDir();
            using (var repo = new Repository(path))
            {
                var noUrlRemote = repo.Network.Remotes["no_url"];
                Assert.NotNull(noUrlRemote);
                Assert.Equal(null, noUrlRemote.Url);

                var remotes = repo.Network.Remotes.ToList();
                Assert.Equal(1, remotes.Count(r => r.Name == "no_url"));
            }
        }

        [Fact]
        public void CreatingARemoteAddsADefaultFetchRefSpec()
        {
            var path = SandboxStandardTestRepo();
            using (var repo = new Repository(path))
            {
                var remote = repo.Network.Remotes.Add("one", "http://github.com/up/stream");
                Assert.Equal("+refs/heads/*:refs/remotes/one/*", remote.RefSpecs.Single().Specification);
            }
        }

        [Fact]
        public void CanCreateARemoteWithASpecifiedFetchRefSpec()
        {
            var path = SandboxStandardTestRepo();
            using (var repo = new Repository(path))
            {
                var remote = repo.Network.Remotes.Add("two", "http://github.com/up/stream", "+refs/heads/*:refs/remotes/grmpf/*");
                Assert.Equal("+refs/heads/*:refs/remotes/grmpf/*", remote.RefSpecs.Single().Specification);
            }
        }

        [Fact]
        public void CanDeleteExistingRemote()
        {
            var path = SandboxStandardTestRepo();
            using (var repo = new Repository(path))
            {
                Assert.NotNull(repo.Network.Remotes["origin"]);
                Assert.NotEmpty(repo.Refs.FromGlob("refs/remotes/origin/*"));

                repo.Network.Remotes.Remove("origin");
                Assert.Null(repo.Network.Remotes["origin"]);
                Assert.Empty(repo.Refs.FromGlob("refs/remotes/origin/*"));
            }
        }

        [Fact]
        public void CanDeleteNonExistingRemote()
        {
            var path = SandboxStandardTestRepoGitDir();
            using (var repo = new Repository(path))
            {
                Assert.Null(repo.Network.Remotes["i_dont_exist"]);
                repo.Network.Remotes.Remove("i_dont_exist");
            }
        }

        [Fact]
        public void CanRenameExistingRemote()
        {
            var path = SandboxStandardTestRepo();
            using (var repo = new Repository(path))
            {
                Assert.NotNull(repo.Network.Remotes["origin"]);
                Assert.Null(repo.Network.Remotes["renamed"]);
                Assert.NotEmpty(repo.Refs.FromGlob("refs/remotes/origin/*"));
                Assert.Empty(repo.Refs.FromGlob("refs/remotes/renamed/*"));

                repo.Network.Remotes.Rename("origin", "renamed", problem => Assert.True(false));
                Assert.Null(repo.Network.Remotes["origin"]);
                Assert.Empty(repo.Refs.FromGlob("refs/remotes/origin/*"));

                Assert.NotNull(repo.Network.Remotes["renamed"]);
                Assert.NotEmpty(repo.Refs.FromGlob("refs/remotes/renamed/*"));
            }
        }

        [Fact]
        public void RenamingNonExistingRemoteThrows()
        {
            var path = SandboxStandardTestRepoGitDir();
            using (var repo = new Repository(path))
            {
                Assert.Throws<NotFoundException>(() =>
                {
                    repo.Network.Remotes.Rename("i_dont_exist", "i_dont_either");
                });
            }
        }

        [Fact]
        public void ReportsRemotesWithNonDefaultRefSpecs()
        {
            var path = SandboxStandardTestRepo();
            using (var repo = new Repository(path))
            {
                Assert.NotNull(repo.Network.Remotes["origin"]);

                repo.Network.Remotes.Update(
                    repo.Network.Remotes["origin"],
                    r => r.FetchRefSpecs = new[] { "+refs/heads/*:refs/remotes/upstream/*" });

                repo.Network.Remotes.Rename("origin", "nondefault", problem => Assert.Equal("+refs/heads/*:refs/remotes/upstream/*", problem));

                Assert.NotEmpty(repo.Refs.FromGlob("refs/remotes/nondefault/*"));
                Assert.Empty(repo.Refs.FromGlob("refs/remotes/upstream/*"));

                Assert.Null(repo.Network.Remotes["origin"]);
                Assert.NotNull(repo.Network.Remotes["nondefault"]);
            }
        }

        [Fact]
        public void DoesNotReportRemotesWithAlreadyExistingRefSpec()
        {
            var path = SandboxStandardTestRepo();
            using (var repo = new Repository(path))
            {
                Assert.NotNull(repo.Network.Remotes["origin"]);

                repo.Refs.Add("refs/remotes/renamed/master", "32eab9cb1f450b5fe7ab663462b77d7f4b703344");

                repo.Network.Remotes.Rename("origin", "renamed", problem => Assert.True(false));

                Assert.NotEmpty(repo.Refs.FromGlob("refs/remotes/renamed/*"));
                Assert.Empty(repo.Refs.FromGlob("refs/remotes/origin/*"));

                Assert.Null(repo.Network.Remotes["origin"]);
                Assert.NotNull(repo.Network.Remotes["renamed"]);
            }
        }

        [Fact]
        public void CanNotRenameWhenRemoteWithSameNameExists()
        {
            const string name = "upstream";
            const string url = "https://github.com/libgit2/libgit2sharp.git";

            var path = SandboxStandardTestRepo();
            using (var repo = new Repository(path))
            {
                Assert.NotNull(repo.Network.Remotes["origin"]);
                repo.Network.Remotes.Add(name, url);

                Assert.Throws<NameConflictException>(() => repo.Network.Remotes.Rename("origin", "upstream"));
            }
        }

        [Theory]
        [InlineData(null, null, false)]
        [InlineData(null, false, false)]
        [InlineData(null, true, true)]
        [InlineData(false, null, false)]
        [InlineData(false, false, false)]
        [InlineData(false, true, true)]
        [InlineData(true, null, true)]
        [InlineData(true, false, false)]
        [InlineData(true, true, true)]
        public void ShoudlPruneOnFetchReflectsTheConfiguredSetting(bool? fetchPrune, bool? remotePrune, bool expectedFetchPrune)
        {
            var path = SandboxStandardTestRepo();
            var scd = BuildSelfCleaningDirectory();

            using (var repo = new Repository(path, BuildFakeConfigs(scd)))
            {
                Assert.Null(repo.Config.Get<bool>("fetch.prune"));
                Assert.Null(repo.Config.Get<bool>("remote.origin.prune"));

                SetIfNotNull(repo, "fetch.prune", fetchPrune);
                SetIfNotNull(repo, "remote.origin.prune", remotePrune);

                var remote = repo.Network.Remotes["origin"];
                Assert.Equal(expectedFetchPrune, remote.AutomaticallyPruneOnFetch);
            }
        }

        private void SetIfNotNull(IRepository repo, string configName, bool? value)
        {
            if (!value.HasValue)
            {
                return;
            }

            repo.Config.Set(configName, value.Value);
        }
    }
}