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

NoteFixture.cs « LibGit2Sharp.Tests - github.com/mono/libgit2sharp.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 7da99db07d94abf488f279fcbad1c545d45635df (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
using System;
using System.Collections.Generic;
using System.Linq;
using LibGit2Sharp.Core;
using LibGit2Sharp.Tests.TestHelpers;
using Xunit;

namespace LibGit2Sharp.Tests
{
    public class NoteFixture : BaseFixture
    {
        private static readonly Signature signatureNullToken = new Signature("nulltoken", "emeric.fermas@gmail.com", DateTimeOffset.UtcNow);
        private static readonly Signature signatureYorah = new Signature("yorah", "yoram.harmelin@gmail.com", Epoch.ToDateTimeOffset(1300557894, 60));

        [Fact]
        public void RetrievingNotesFromANonExistingGitObjectYieldsNoResult()
        {
            string path = SandboxBareTestRepo();
            using (var repo = new Repository(path))
            {
                var notes = repo.Notes[ObjectId.Zero];

                Assert.Equal(0, notes.Count());
            }
        }

        [Fact]
        public void RetrievingNotesFromAGitObjectWhichHasNoNoteYieldsNoResult()
        {
            string path = SandboxBareTestRepo();
            using (var repo = new Repository(path))
            {
                var notes = repo.Notes[new ObjectId("4c062a6361ae6959e06292c1fa5e2822d9c96345")];

                Assert.Equal(0, notes.Count());
            }
        }

        /*
         * $ git show 4a202 --show-notes=*
         * commit 4a202b346bb0fb0db7eff3cffeb3c70babbd2045
         * Author: Scott Chacon <schacon@gmail.com>
         * Date:   Mon May 24 10:19:04 2010 -0700
         *
         *     a third commit
         *
         * Notes:
         *     Just Note, don't you understand?
         *
         * Notes (answer):
         *     Nope
         *
         * Notes (answer2):
         *     Not Nope, Note!
         */
        [Fact]
        public void CanRetrieveNotesFromAGitObject()
        {
            var expectedMessages = new [] { "Just Note, don't you understand?\n", "Nope\n", "Not Nope, Note!\n" };

            string path = SandboxBareTestRepo();
            using (var repo = new Repository(path))
            {
                var notes = repo.Notes[new ObjectId("4a202b346bb0fb0db7eff3cffeb3c70babbd2045")];

                Assert.NotNull(notes);
                Assert.Equal(expectedMessages, SortedNotes(notes, n => n.Message));
            }
        }

        [Fact]
        public void CanRetrieveASpecificNoteFromAKnownNamespace()
        {
            string path = SandboxBareTestRepo();
            using (var repo = new Repository(path))
            {
                var singleNote = repo.Notes["answer", new ObjectId("4a202b346bb0fb0db7eff3cffeb3c70babbd2045")];
                Assert.Equal("Nope\n", singleNote.Message);
            }
        }

        [Fact]
        public void CanGetListOfNotesNamespaces()
        {
            var expectedNamespaces = new[] { "answer", "answer2", "commits", };

            string path = SandboxBareTestRepo();
            using (var repo = new Repository(path))
            {
                Assert.Equal(expectedNamespaces,
                             repo.Notes.Namespaces.OrderBy(n => n, StringComparer.Ordinal).ToArray());
                Assert.Equal(repo.Notes.DefaultNamespace, repo.Notes.Namespaces.First());
            }
        }

        /*
         * $ git show 4a202b346bb0fb0db7eff3cffeb3c70babbd2045 --show-notes=*
         * commit 4a202b346bb0fb0db7eff3cffeb3c70babbd2045
         * Author: Scott Chacon <schacon@gmail.com>
         * Date:   Mon May 24 10:19:04 2010 -0700
         *
         *     a third commit
         *
         * Notes:
         *     Just Note, don't you understand?
         *
         * Notes (answer):
         *     Nope
         *
         * Notes (answer2):
         *     Not Nope, Note!
         */
        [Fact]
        public void CanAccessNotesFromACommit()
        {
            var expectedNamespaces = new[] { "Just Note, don't you understand?\n", "Nope\n", "Not Nope, Note!\n" };

            string path = SandboxBareTestRepo();
            using (var repo = new Repository(path))
            {
                var commit = repo.Lookup<Commit>("4a202b346bb0fb0db7eff3cffeb3c70babbd2045");

                Assert.Equal(expectedNamespaces, SortedNotes(commit.Notes, n => n.Message));

                // Make sure that Commit.Notes is not refreshed automatically
                repo.Notes.Add(commit.Id, "I'm batman!\n", signatureNullToken, signatureYorah, "batmobile");

                Assert.Equal(expectedNamespaces, SortedNotes(commit.Notes, m => m.Message));
            }
        }

        [Fact]
        public void CanAddANoteOnAGitObject()
        {
            string path = SandboxBareTestRepo();
            using (var repo = new Repository(path))
            {
                var commit = repo.Lookup<Commit>("9fd738e8f7967c078dceed8190330fc8648ee56a");
                var note = repo.Notes.Add(commit.Id, "I'm batman!\n", signatureNullToken, signatureYorah, "batmobile");

                var newNote = commit.Notes.Single();
                Assert.Equal(note, newNote);

                Assert.Equal("I'm batman!\n", newNote.Message);
                Assert.Equal("batmobile", newNote.Namespace);
            }
        }

        [Fact]
        public void CreatingANoteWhichAlreadyExistsOverwritesThePreviousNote()
        {
            string path = SandboxBareTestRepo();
            using (var repo = new Repository(path))
            {
                var commit = repo.Lookup<Commit>("5b5b025afb0b4c913b4c338a42934a3863bf3644");
                Assert.NotNull(commit.Notes.FirstOrDefault(x => x.Namespace == "answer"));

                repo.Notes.Add(commit.Id, "I'm batman!\n", signatureNullToken, signatureYorah, "answer");
                var note = repo.Notes[new ObjectId("5b5b025afb0b4c913b4c338a42934a3863bf3644")].FirstOrDefault(x => x.Namespace == "answer");

                Assert.NotNull(note);

                Assert.Equal("I'm batman!\n", note.Message);
                Assert.Equal("answer", note.Namespace);
            }
        }

        [Fact]
        public void CanAddANoteWithSignatureFromConfig()
        {
            string configPath = CreateConfigurationWithDummyUser(Constants.Signature);
            var options = new RepositoryOptions { GlobalConfigurationLocation = configPath };
            string path = SandboxBareTestRepo();

            using (var repo = new Repository(path, options))
            {
                var commit = repo.Lookup<Commit>("9fd738e8f7967c078dceed8190330fc8648ee56a");
                var note = repo.Notes.Add(commit.Id, "I'm batman!\n", "batmobile");

                var newNote = commit.Notes.Single();
                Assert.Equal(note, newNote);

                Assert.Equal("I'm batman!\n", newNote.Message);
                Assert.Equal("batmobile", newNote.Namespace);

                AssertCommitSignaturesAre(repo.Lookup<Commit>("refs/notes/batmobile"), Constants.Signature);
            }
        }

        [Fact]
        public void CanCompareTwoUniqueNotes()
        {
            string path = SandboxBareTestRepo();
            using (var repo = new Repository(path))
            {
                var commit = repo.Lookup<Commit>("9fd738e8f7967c078dceed8190330fc8648ee56a");

                var firstNote = repo.Notes.Add(commit.Id, "I'm batman!\n", signatureNullToken, signatureYorah, "batmobile");
                var secondNote = repo.Notes.Add(commit.Id, "I'm batman!\n", signatureNullToken, signatureYorah, "batmobile");
                Assert.Equal(firstNote, secondNote);

                var firstNoteWithAnotherNamespace = repo.Notes.Add(commit.Id, "I'm batman!\n", signatureNullToken, signatureYorah, "batmobile2");
                Assert.NotEqual(firstNote, firstNoteWithAnotherNamespace);

                var firstNoteWithAnotherMessage = repo.Notes.Add(commit.Id, "I'm ironman!\n", signatureNullToken, signatureYorah, "batmobile");
                Assert.NotEqual(firstNote, firstNoteWithAnotherMessage);

                var anotherCommit = repo.Lookup<Commit>("c47800c7266a2be04c571c04d5a6614691ea99bd");
                var firstNoteOnAnotherCommit = repo.Notes.Add(anotherCommit.Id, "I'm batman!\n", signatureNullToken, signatureYorah, "batmobile");
                Assert.NotEqual(firstNote, firstNoteOnAnotherCommit);
            }
        }

        /*
         * $ git log 8496071c1b46c854b31185ea97743be6a8774479
         * commit 8496071c1b46c854b31185ea97743be6a8774479
         * Author: Scott Chacon <schacon@gmail.com>
         * Date:   Sat May 8 16:13:06 2010 -0700
         *
         *     testing
         *
         * Notes:
         *     Hi, I'm Note.
         */
        [Fact]
        public void CanRemoveANoteFromAGitObject()
        {
            string path = SandboxBareTestRepo();
            using (var repo = new Repository(path))
            {
                var commit = repo.Lookup<Commit>("8496071c1b46c854b31185ea97743be6a8774479");
                var notes = repo.Notes[commit.Id];

                Assert.NotEmpty(notes);

                repo.Notes.Remove(commit.Id, signatureNullToken, signatureYorah, repo.Notes.DefaultNamespace);

                Assert.Empty(notes);
            }
        }

        /*
         * $ git show 5b5b025afb0b4c913b4c338a42934a3863bf3644 --notes=answer
         * commit 5b5b025afb0b4c913b4c338a42934a3863bf3644
         * Author: Scott Chacon <schacon@gmail.com>
         * Date:   Tue May 11 13:38:42 2010 -0700
         *
         *     another commit
         *
         * Notes (answer):
         *     Not what?
         */
        [Fact]
        public void RemovingANonExistingNoteDoesntThrow()
        {
            string path = SandboxBareTestRepo();
            using (var repo = new Repository(path))
            {
                var commit = repo.Lookup<Commit>("5b5b025afb0b4c913b4c338a42934a3863bf3644");

                repo.Notes.Remove(commit.Id, signatureNullToken, signatureYorah, "answer2");
            }
        }

        [Fact]
        public void CanRemoveANoteWithSignatureFromConfig()
        {
            string configPath = CreateConfigurationWithDummyUser(Constants.Signature);
            RepositoryOptions options = new RepositoryOptions() { GlobalConfigurationLocation = configPath };
            string path = SandboxBareTestRepo();

            using (var repo = new Repository(path, options))
            {
                var commit = repo.Lookup<Commit>("8496071c1b46c854b31185ea97743be6a8774479");
                var notes = repo.Notes[commit.Id];

                Assert.NotEmpty(notes);

                repo.Notes.Remove(commit.Id, repo.Notes.DefaultNamespace);

                Assert.Empty(notes);

                AssertCommitSignaturesAre(repo.Lookup<Commit>("refs/notes/" + repo.Notes.DefaultNamespace), Constants.Signature);
            }
        }

        [Fact]
        public void CanRetrieveTheListOfNotesForAGivenNamespace()
        {
            var expectedNotes = new[]
            {
                new { Blob = "272a41cf2b22e57f2bc5bf6ef37b63568cd837e4", Target = "8496071c1b46c854b31185ea97743be6a8774479" },
                new { Blob = "1a550e416326cdb4a8e127a04dd69d7a01b11cf4", Target = "4a202b346bb0fb0db7eff3cffeb3c70babbd2045" },
            };

            string path = SandboxBareTestRepo();
            using (var repo = new Repository(path))
            {
                Assert.Equal(expectedNotes,
                             SortedNotes(repo.Notes["commits"], n => new { Blob = n.BlobId.Sha, Target = n.TargetObjectId.Sha }));

                Assert.Equal("commits", repo.Notes.DefaultNamespace);
                Assert.Equal(expectedNotes,
                             SortedNotes(repo.Notes, n => new { Blob = n.BlobId.Sha, Target = n.TargetObjectId.Sha }));
            }
        }

        private static T[] SortedNotes<T>(IEnumerable<Note> notes, Func<Note, T> selector)
        {
            return notes.OrderBy(n => n.Message, StringComparer.Ordinal).Select(selector).ToArray();
        }
    }
}