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

RepositoryFixture.cs « LibGit2Sharp.Tests - github.com/mono/libgit2sharp.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b26491837dd4e38161cb71407d6778398a3731fa (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
using System;
using System.IO;
using System.Linq;
using LibGit2Sharp.Tests.TestHelpers;
using NUnit.Framework;

namespace LibGit2Sharp.Tests
{
    [TestFixture]
    public class RepositoryFixture : BaseFixture
    {
        private const string commitSha = "8496071c1b46c854b31185ea97743be6a8774479";

        [Test]
        public void CanCreateBareRepo()
        {
            using (var scd = new SelfCleaningDirectory())
            {
                var dir = Repository.Init(scd.DirectoryPath, true);
                Path.IsPathRooted(dir).ShouldBeTrue();
                Directory.Exists(dir).ShouldBeTrue();

                using (var repo = new Repository(dir))
                {
                    repo.Info.WorkingDirectory.ShouldBeNull();
                    repo.Info.Path.ShouldEqual(scd.RootedDirectoryPath + Path.DirectorySeparatorChar);
                    repo.Info.IsBare.ShouldBeTrue();

                    AssertInitializedRepository(repo);
                }
            }
        }

        [Test]
        public void CanCreateStandardRepo()
        {
            using (var scd = new SelfCleaningDirectory())
            {
                var dir = Repository.Init(scd.DirectoryPath);
                Path.IsPathRooted(dir).ShouldBeTrue();
                Directory.Exists(dir).ShouldBeTrue();

                using (var repo = new Repository(dir))
                {
                    repo.Info.WorkingDirectory.ShouldNotBeNull();
                    repo.Info.Path.ShouldEqual(Path.Combine(scd.RootedDirectoryPath, ".git" + Path.DirectorySeparatorChar));
                    repo.Info.IsBare.ShouldBeFalse();

                    AssertIsHidden(repo.Info.Path);

                    AssertInitializedRepository(repo);
                }
            }
        }

        private static void AssertIsHidden(string repoPath)
        {
            var attribs = File.GetAttributes(repoPath);
            
            (attribs & FileAttributes.Hidden).ShouldEqual(FileAttributes.Hidden);
        }

        [Test]
        public void CreatingRepoWithBadParamsThrows()
        {
            Assert.Throws<ArgumentException>(() => Repository.Init(string.Empty));
            Assert.Throws<ArgumentNullException>(() => Repository.Init(null));
        }

        private static void AssertInitializedRepository(Repository repo)
        {
            repo.Info.Path.ShouldNotBeNull();
            repo.Info.IsEmpty.ShouldBeTrue();
            repo.Info.IsHeadDetached.ShouldBeFalse();

            var headRef = repo.Refs["HEAD"];
            headRef.ShouldNotBeNull();
            headRef.TargetIdentifier.ShouldEqual("refs/heads/master");
            headRef.ResolveToDirectReference().ShouldBeNull();

            repo.Head.ShouldNotBeNull();
            repo.Head.CanonicalName.ShouldEqual(headRef.TargetIdentifier);
            repo.Head.Tip.ShouldBeNull();

            repo.Commits.Count().ShouldEqual(0);
            repo.Commits.QueryBy(new Filter { Since = repo.Head }).Count().ShouldEqual(0);
            repo.Commits.QueryBy(new Filter { Since = "HEAD" }).Count().ShouldEqual(0);
            repo.Commits.QueryBy(new Filter { Since = "refs/heads/master" }).Count().ShouldEqual(0);

            repo.Branches.Count().ShouldEqual(0);
            repo.Refs.Count().ShouldEqual(0);
            repo.Tags.Count().ShouldEqual(0);
        }


        [Test]
        public void CanOpenRepoWithFullPath()
        {
            var path = Path.GetFullPath(Constants.BareTestRepoPath);
            using (var repo = new Repository(path))
            {
                repo.ShouldNotBeNull();
            }
        }

        [Test]
        [Platform(Exclude = "Linux,Unix", Reason = "No need to test windows path separators on non-windows platforms")]
        // See http://www.nunit.org/index.php?p=platform&r=2.6 for other platforms that can be excluded/included.
        public void CanOpenRepoWithWindowsPathSeparators()
        {
            using (new Repository(@".\Resources\testrepo.git"))
            {
            }
        }

        [Test]
        public void CanOpenRepository()
        {
            using (var repo = new Repository(Constants.BareTestRepoPath))
            {
                repo.Info.Path.ShouldNotBeNull();
                repo.Info.WorkingDirectory.ShouldBeNull();
                repo.Info.IsBare.ShouldBeTrue();
                repo.Info.IsEmpty.ShouldBeFalse();
                repo.Info.IsHeadDetached.ShouldBeFalse();
            }
        }

        [Test]
        public void OpeningNonExistentRepoThrows()
        {
            Assert.Throws<ApplicationException>(() => { new Repository("a_bad_path"); });
        }

        [Test]
        public void OpeningRepositoryWithBadParamsThrows()
        {
            Assert.Throws<ArgumentException>(() => new Repository(string.Empty));
            Assert.Throws<ArgumentNullException>(() => new Repository(null));
        }

        [Test]
        public void CanLookupACommitByTheNameOfABranch()
        {
            using (var repo = new Repository(Constants.BareTestRepoPath))
            {
                var gitObject = repo.Lookup("refs/heads/master");
                gitObject.ShouldNotBeNull();
                Assert.IsInstanceOf<Commit>(gitObject);
            }
        }

        [Test]
        public void CanLookupACommitByTheNameOfALightweightTag()
        {
            using (var repo = new Repository(Constants.BareTestRepoPath))
            {
                var gitObject = repo.Lookup("refs/tags/lw");
                gitObject.ShouldNotBeNull();
                Assert.IsInstanceOf<Commit>(gitObject);
            }
        }

        [Test]
        public void CanLookupATagAnnotationByTheNameOfAnAnnotatedTag()
        {
            using (var repo = new Repository(Constants.BareTestRepoPath))
            {
                var gitObject = repo.Lookup("refs/tags/e90810b");
                gitObject.ShouldNotBeNull();
                Assert.IsInstanceOf<TagAnnotation>(gitObject);
            }
        }

        [Test]
        public void CanLookupObjects()
        {
            using (var repo = new Repository(Constants.BareTestRepoPath))
            {
                repo.Lookup(commitSha).ShouldNotBeNull();
                repo.Lookup<Commit>(commitSha).ShouldNotBeNull();
                repo.Lookup<GitObject>(commitSha).ShouldNotBeNull();
            }
        }

        [Test]
        public void CanLookupSameObjectTwiceAndTheyAreEqual()
        {
            using (var repo = new Repository(Constants.BareTestRepoPath))
            {
                var commit = repo.Lookup(commitSha);
                var commit2 = repo.Lookup(commitSha);
                commit.Equals(commit2).ShouldBeTrue();
                commit.GetHashCode().ShouldEqual(commit2.GetHashCode());
            }
        }

        [Test]
        public void LookupObjectByWrongShaReturnsNull()
        {
            using (var repo = new Repository(Constants.BareTestRepoPath))
            {
                repo.Lookup(Constants.UnknownSha).ShouldBeNull();
                repo.Lookup<GitObject>(Constants.UnknownSha).ShouldBeNull();
            }
        }

        [Test]
        public void LookupObjectByWrongTypeReturnsNull()
        {
            using (var repo = new Repository(Constants.BareTestRepoPath))
            {
                repo.Lookup(commitSha).ShouldNotBeNull();
                repo.Lookup<Commit>(commitSha).ShouldNotBeNull();
                repo.Lookup<TagAnnotation>(commitSha).ShouldBeNull();
            }
        }

        [Test]
        public void LookupObjectByUnknownReferenceNameReturnsNull()
        {
            using (var repo = new Repository(Constants.BareTestRepoPath))
            {
                repo.Lookup("refs/heads/chopped/off").ShouldBeNull();
                repo.Lookup<GitObject>(Constants.UnknownSha).ShouldBeNull();
            }
        }

        [Test]
        public void CanLookupWhithShortIdentifers()
        {
            const string expectedAbbrevSha = "edfecad";
            const string expectedSha = expectedAbbrevSha + "02d96c9dbf64f6e238c45ddcfa762eef0";

            using (var scd = new SelfCleaningDirectory())
            {
                var dir = Repository.Init(scd.DirectoryPath);

                using (var repo = new Repository(dir))
                {
                    string filePath = Path.Combine(repo.Info.WorkingDirectory, "new.txt");

                    File.WriteAllText(filePath, "one ");
                    repo.Index.Stage(filePath);

                    var author = Constants.Signature;
                    var commit = repo.Commit(author, author, "Initial commit");

                    commit.Sha.ShouldEqual(expectedSha);

                    var lookedUp1 = repo.Lookup(expectedSha);
                    lookedUp1.ShouldEqual(commit);

                    var lookedUp2 = repo.Lookup(expectedAbbrevSha);
                    lookedUp2.ShouldEqual(commit);
                }
            }
        }

        [Test]
        public void LookingUpWithBadParamsThrows()
        {
            using (var repo = new Repository(Constants.BareTestRepoPath))
            {
                Assert.Throws<ArgumentException>(() => repo.Lookup(string.Empty));
                Assert.Throws<ArgumentException>(() => repo.Lookup<GitObject>(string.Empty));
                Assert.Throws<ArgumentNullException>(() => repo.Lookup((string)null));
                Assert.Throws<ArgumentNullException>(() => repo.Lookup((ObjectId)null));
                Assert.Throws<ArgumentNullException>(() => repo.Lookup<Commit>((string)null));
                Assert.Throws<ArgumentNullException>(() => repo.Lookup<Commit>((ObjectId)null));
            }
        }

        [Test]
        public void CanCheckForObjectExistence()
        {
            using (var repo = new Repository(Constants.BareTestRepoPath))
            {
                repo.HasObject("8496071c1b46c854b31185ea97743be6a8774479").ShouldBeTrue();
                repo.HasObject("1385f264afb75a56a5bec74243be9b367ba4ca08").ShouldBeTrue();
                repo.HasObject("ce08fe4884650f067bd5703b6a59a8b3b3c99a09").ShouldBeFalse();
                repo.HasObject("8496071c1c46c854b31185ea97743be6a8774479").ShouldBeFalse();
            }
        }

        [Test]
        public void CheckingForObjectExistenceWithBadParamsThrows()
        {
            using (var repo = new Repository(Constants.BareTestRepoPath))
            {
                Assert.Throws<ArgumentException>(() => repo.HasObject(string.Empty));
                Assert.Throws<ArgumentNullException>(() => repo.HasObject(null));
            }
        }

        [Test]
        public void CanDiscoverABareRepoGivenTheRepoPath()
        {
            string path = Repository.Discover(Constants.BareTestRepoPath);
            path.ShouldEqual(Path.GetFullPath(Constants.BareTestRepoPath + Path.DirectorySeparatorChar));
        }

        [Test]
        public void CanDiscoverABareRepoGivenASubDirectoryOfTheRepoPath()
        {
            string path = Repository.Discover(Path.Combine(Constants.BareTestRepoPath, "objects/4a"));
            path.ShouldEqual(Path.GetFullPath(Constants.BareTestRepoPath + Path.DirectorySeparatorChar));
        }

        [Test]
        public void CanDiscoverAStandardRepoGivenTheRepoPath()
        {
            string path = Repository.Discover(Constants.StandardTestRepoPath);
            path.ShouldEqual(Path.GetFullPath(Constants.StandardTestRepoPath + Path.DirectorySeparatorChar));
        }

        [Test]
        public void CanDiscoverAStandardRepoGivenASubDirectoryOfTheRepoPath()
        {
            string path = Repository.Discover(Path.Combine(Constants.StandardTestRepoPath, "objects/4a"));
            path.ShouldEqual(Path.GetFullPath(Constants.StandardTestRepoPath + Path.DirectorySeparatorChar));
        }

        [Test]
        public void CanDiscoverAStandardRepoGivenTheWorkingDirPath()
        {
            string path = Repository.Discover(Constants.StandardTestRepoWorkingDirPath);
            path.ShouldEqual(Path.GetFullPath(Constants.StandardTestRepoPath + Path.DirectorySeparatorChar));
        }
    }
}