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

TreeDefinitionFixture.cs « LibGit2Sharp.Tests - github.com/mono/libgit2sharp.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f6d9e3efb477b17167ea6e14be7021f4683b000d (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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
using System;
using LibGit2Sharp.Tests.TestHelpers;
using Xunit;
using Xunit.Extensions;

namespace LibGit2Sharp.Tests
{
    public class TreeDefinitionFixture : BaseFixture
    {
        /*
         * $ git ls-tree -r HEAD
         * 100755 blob 45b983be36b73c0788dc9cbcb76cbb80fc7bb057    1/branch_file.txt
         * 100644 blob a8233120f6ad708f843d861ce2b7228ec4e3dec6    README
         * 100644 blob 45b983be36b73c0788dc9cbcb76cbb80fc7bb057    branch_file.txt
         * 100644 blob a71586c1dfe8a71c6cbf6c129f404c5642ff31bd    new.txt
         */

        [Fact]
        public void CanBuildATreeDefinitionFromATree()
        {
            using (var repo = new Repository(BareTestRepoPath))
            {
                TreeDefinition td = TreeDefinition.From(repo.Head.Tip.Tree);
                Assert.NotNull(td);
            }
        }

        [Fact]
        public void BuildingATreeDefinitionWithBadParamsThrows()
        {
            Assert.Throws<ArgumentNullException>(() => TreeDefinition.From(null));
        }

        [Fact]
        public void RequestingANonExistingEntryReturnsNull()
        {
            using (var repo = new Repository(BareTestRepoPath))
            {
                TreeDefinition td = TreeDefinition.From(repo.Head.Tip.Tree);

                Assert.Null(td["nope"]);
                Assert.Null(td["not/here"]);
                Assert.Null(td["neither/in/here"]);
                Assert.Null(td["README/is/a-Blob/not-a-Tree"]);
            }
        }

        [Fact]
        public void RequestingAnEntryWithBadParamsThrows()
        {
            using (var repo = new Repository(BareTestRepoPath))
            {
                TreeDefinition td = TreeDefinition.From(repo.Head.Tip.Tree);

                Assert.Throws<ArgumentNullException>(() => td[null]);
                Assert.Throws<ArgumentException>(() => td[string.Empty]);
                Assert.Throws<ArgumentException>(() => td["/"]);
                Assert.Throws<ArgumentException>(() => td["/a"]);
                Assert.Throws<ArgumentException>(() => td["1//branch_file.txt"]);
                Assert.Throws<ArgumentException>(() => td["README/"]);
                Assert.Throws<ArgumentException>(() => td["1/"]);
            }
        }

        [Theory]
        [InlineData("1/branch_file.txt", "100755", TreeEntryTargetType.Blob, "45b983be36b73c0788dc9cbcb76cbb80fc7bb057")]
        [InlineData("README",            "100644", TreeEntryTargetType.Blob, "a8233120f6ad708f843d861ce2b7228ec4e3dec6")]
        [InlineData("branch_file.txt",   "100644", TreeEntryTargetType.Blob, "45b983be36b73c0788dc9cbcb76cbb80fc7bb057")]
        [InlineData("new.txt",           "100644", TreeEntryTargetType.Blob, "a71586c1dfe8a71c6cbf6c129f404c5642ff31bd")]
        [InlineData("1",                 "040000", TreeEntryTargetType.Tree, "7f76480d939dc401415927ea7ef25c676b8ddb8f")]
        public void CanRetrieveEntries(string path, string expectedAttributes, TreeEntryTargetType expectedType, string expectedSha)
        {
            using (var repo = new Repository(BareTestRepoPath))
            {
                TreeDefinition td = TreeDefinition.From(repo.Head.Tip.Tree);

                TreeEntryDefinition ted = td[path];

                Assert.Equal(ToMode(expectedAttributes), ted.Mode);
                Assert.Equal(expectedType, ted.TargetType);
                Assert.Equal(new ObjectId(expectedSha), ted.TargetId);
            }
        }

        //TODO: Convert Mode to a static class and add this helper method as 'FromString()'
        private static Mode ToMode(string expectedAttributes)
        {
            return (Mode)Convert.ToInt32(expectedAttributes, 8);
        }

        [Theory]
        [InlineData("README", "README_TOO")]
        [InlineData("README", "1/README")]
        [InlineData("1/branch_file.txt", "1/another_one.txt")]
        [InlineData("1/branch_file.txt", "another_one.txt")]
        [InlineData("1/branch_file.txt", "1/2/another_one.txt")]
        public void CanAddAnExistingTreeEntryDefinition(string sourcePath, string targetPath)
        {
            using (var repo = new Repository(BareTestRepoPath))
            {
                TreeDefinition td = TreeDefinition.From(repo.Head.Tip.Tree);
                Assert.Null(td[targetPath]);

                TreeEntryDefinition ted = td[sourcePath];
                td.Add(targetPath, ted);

                TreeEntryDefinition fetched = td[targetPath];
                Assert.NotNull(fetched);

                Assert.Equal(ted, fetched);
            }
        }

        [Fact]
        public void CanAddAnExistingGitLinkTreeEntryDefinition()
        {
            const string sourcePath = "sm_unchanged";
            const string targetPath = "sm_from_td";

            using (var repo = new Repository(SubmoduleTestRepoWorkingDirPath))
            {
                TreeDefinition td = TreeDefinition.From(repo.Head.Tip.Tree);
                Assert.Null(td[targetPath]);

                TreeEntryDefinition ted = td[sourcePath];
                td.Add(targetPath, ted);

                TreeEntryDefinition fetched = td[targetPath];
                Assert.NotNull(fetched);

                Assert.Equal(ted, fetched);
            }
        }

        [Theory]
        [InlineData("a8233120f6ad708f843d861ce2b7228ec4e3dec6", "README_TOO")]
        [InlineData("a8233120f6ad708f843d861ce2b7228ec4e3dec6", "1/README")]
        [InlineData("45b983be36b73c0788dc9cbcb76cbb80fc7bb057", "1/another_one.txt")]
        [InlineData("45b983be36b73c0788dc9cbcb76cbb80fc7bb057", "another_one.txt")]
        public void CanAddAnExistingBlob(string blobSha, string targetPath)
        {
            using (var repo = new Repository(BareTestRepoPath))
            {
                TreeDefinition td = TreeDefinition.From(repo.Head.Tip.Tree);
                Assert.Null(td[targetPath]);

                var objectId = new ObjectId(blobSha);
                var blob = repo.Lookup<Blob>(objectId);

                td.Add(targetPath, blob, Mode.NonExecutableFile);

                TreeEntryDefinition fetched = td[targetPath];
                Assert.NotNull(fetched);

                Assert.Equal(objectId, fetched.TargetId);
                Assert.Equal(Mode.NonExecutableFile, fetched.Mode);
            }
        }

        [Fact]
        public void CanAddAnExistingSubmodule()
        {
            const string submodulePath = "sm_unchanged";

            using (var repo = new Repository(SubmoduleTestRepoWorkingDirPath))
            {
                var submodule = repo.Submodules[submodulePath];
                Assert.NotNull(submodule);

                TreeDefinition td = TreeDefinition.From(repo.Head.Tip.Tree);
                Assert.NotNull(td[submodulePath]);

                td.Remove(submodulePath);
                Assert.Null(td[submodulePath]);

                td.Add(submodule);

                TreeEntryDefinition fetched = td[submodulePath];
                Assert.NotNull(fetched);

                Assert.Equal(submodule.HeadCommitId, fetched.TargetId);
                Assert.Equal(TreeEntryTargetType.GitLink, fetched.TargetType);
                Assert.Equal(Mode.GitLink, fetched.Mode);
            }
        }

        [Fact]
        public void CanAddAnExistingTree()
        {
            const string treeSha = "7f76480d939dc401415927ea7ef25c676b8ddb8f";
            const string targetPath = "1/2";

            using (var repo = new Repository(BareTestRepoPath))
            {
                TreeDefinition td = TreeDefinition.From(repo.Head.Tip.Tree);

                var objectId = new ObjectId(treeSha);
                var tree = repo.Lookup<Tree>(objectId);

                td.Add(targetPath, tree);

                TreeEntryDefinition fetched = td[targetPath];
                Assert.NotNull(fetched);

                Assert.Equal(objectId, fetched.TargetId);
                Assert.Equal(Mode.Directory, fetched.Mode);

                Assert.NotNull(td["1/2/branch_file.txt"]);
            }
        }

        [Fact]
        public void CanReplaceAnExistingTreeWithABlob()
        {
            const string blobSha = "a8233120f6ad708f843d861ce2b7228ec4e3dec6";
            const string targetPath = "1";

            using (var repo = new Repository(BareTestRepoPath))
            {
                TreeDefinition td = TreeDefinition.From(repo.Head.Tip.Tree);
                Assert.Equal(TreeEntryTargetType.Tree, td[targetPath].TargetType);

                var objectId = new ObjectId(blobSha);
                var blob = repo.Lookup<Blob>(objectId);

                Assert.NotNull(td["1/branch_file.txt"]);

                td.Add(targetPath, blob, Mode.NonExecutableFile);

                TreeEntryDefinition fetched = td[targetPath];
                Assert.NotNull(fetched);

                Assert.Equal(TreeEntryTargetType.Blob, td[targetPath].TargetType);
                Assert.Equal(objectId, fetched.TargetId);
                Assert.Equal(Mode.NonExecutableFile, fetched.Mode);

                Assert.Null(td["1/branch_file.txt"]);
            }
        }

        [Theory]
        [InlineData("README")]
        [InlineData("1/branch_file.txt")]
        public void CanReplaceAnExistingBlobWithATree(string targetPath)
        {
            const string treeSha = "7f76480d939dc401415927ea7ef25c676b8ddb8f";

            using (var repo = new Repository(BareTestRepoPath))
            {
                TreeDefinition td = TreeDefinition.From(repo.Head.Tip.Tree);
                Assert.NotNull(td[targetPath]);
                Assert.Equal(TreeEntryTargetType.Blob, td[targetPath].TargetType);

                var objectId = new ObjectId(treeSha);
                var tree = repo.Lookup<Tree>(objectId);

                td.Add(targetPath, tree);

                TreeEntryDefinition fetched = td[targetPath];
                Assert.NotNull(fetched);

                Assert.Equal(TreeEntryTargetType.Tree, td[targetPath].TargetType);
                Assert.Equal(objectId, fetched.TargetId);
                Assert.Equal(Mode.Directory, fetched.Mode);
            }
        }

        [Fact]
        public void CanReplaceAnExistingTreeWithAGitLink()
        {
            var commitId = (ObjectId)"480095882d281ed676fe5b863569520e54a7d5c0";
            const string targetPath = "just_a_dir";

            using (var repo = new Repository(SubmoduleTestRepoWorkingDirPath))
            {
                TreeDefinition td = TreeDefinition.From(repo.Head.Tip.Tree);
                Assert.Equal(TreeEntryTargetType.Tree, td[targetPath].TargetType);

                Assert.NotNull(td["just_a_dir/contents"]);

                td.AddGitLink(targetPath, commitId);

                TreeEntryDefinition fetched = td[targetPath];
                Assert.NotNull(fetched);

                Assert.Equal(commitId, fetched.TargetId);
                Assert.Equal(TreeEntryTargetType.GitLink, fetched.TargetType);
                Assert.Equal(Mode.GitLink, fetched.Mode);

                Assert.Null(td["just_a_dir/contents"]);
            }
        }

        [Fact]
        public void CanReplaceAnExistingGitLinkWithATree()
        {
            const string treeSha = "607d96653d4d0a4f733107f7890c2e67b55b620d";
            const string targetPath = "sm_unchanged";

            using (var repo = new Repository(SubmoduleTestRepoWorkingDirPath))
            {
                TreeDefinition td = TreeDefinition.From(repo.Head.Tip.Tree);
                Assert.NotNull(td[targetPath]);
                Assert.Equal(TreeEntryTargetType.GitLink, td[targetPath].TargetType);
                Assert.Equal(Mode.GitLink, td[targetPath].Mode);

                var objectId = new ObjectId(treeSha);
                var tree = repo.Lookup<Tree>(objectId);

                td.Add(targetPath, tree);

                TreeEntryDefinition fetched = td[targetPath];
                Assert.NotNull(fetched);

                Assert.Equal(objectId, fetched.TargetId);
                Assert.Equal(TreeEntryTargetType.Tree, fetched.TargetType);
                Assert.Equal(Mode.Directory, fetched.Mode);
            }
        }

        [Fact]
        public void CanReplaceAnExistingBlobWithAGitLink()
        {
            var commitId = (ObjectId)"480095882d281ed676fe5b863569520e54a7d5c0";
            const string targetPath = "just_a_file";

            using (var repo = new Repository(SubmoduleTestRepoWorkingDirPath))
            {
                TreeDefinition td = TreeDefinition.From(repo.Head.Tip.Tree);
                Assert.NotNull(td[targetPath]);
                Assert.Equal(TreeEntryTargetType.Blob, td[targetPath].TargetType);

                td.AddGitLink(targetPath, commitId);

                TreeEntryDefinition fetched = td[targetPath];
                Assert.NotNull(fetched);

                Assert.Equal(TreeEntryTargetType.GitLink, td[targetPath].TargetType);
                Assert.Equal(commitId, fetched.TargetId);
                Assert.Equal(Mode.GitLink, fetched.Mode);
            }
        }

        [Fact]
        public void CanReplaceAnExistingGitLinkWithABlob()
        {
            const string blobSha = "42cfb95cd01bf9225b659b5ee3edcc78e8eeb478";
            const string targetPath = "sm_unchanged";

            using (var repo = new Repository(SubmoduleTestRepoWorkingDirPath))
            {
                TreeDefinition td = TreeDefinition.From(repo.Head.Tip.Tree);
                Assert.NotNull(td[targetPath]);
                Assert.Equal(TreeEntryTargetType.GitLink, td[targetPath].TargetType);
                Assert.Equal(Mode.GitLink, td[targetPath].Mode);

                var objectId = new ObjectId(blobSha);
                var blob = repo.Lookup<Blob>(objectId);

                td.Add(targetPath, blob, Mode.NonExecutableFile);

                TreeEntryDefinition fetched = td[targetPath];
                Assert.NotNull(fetched);

                Assert.Equal(objectId, fetched.TargetId);
                Assert.Equal(TreeEntryTargetType.Blob, fetched.TargetType);
                Assert.Equal(Mode.NonExecutableFile, fetched.Mode);
            }
        }

        [Fact]
        public void CanNotReplaceAnExistingTreeWithATreeBeingAssembled()
        {
            using (var repo = new Repository(BareTestRepoPath))
            {
                TreeDefinition td = TreeDefinition.From(repo.Head.Tip.Tree);
                Assert.Equal(TreeEntryTargetType.Tree, td["1"].TargetType);

                td.Add("new/one", repo.Lookup<Blob>("a823312"), Mode.NonExecutableFile)
                    .Add("new/two", repo.Lookup<Blob>("a71586c"), Mode.NonExecutableFile)
                    .Add("new/tree", repo.Lookup<Tree>("7f76480"));

                Assert.Throws<InvalidOperationException>(() => td.Add("1", td["new"]));
            }
        }

        [Fact]
        public void ModifyingTheContentOfATreeSetsItsOidToNull()
        {
            const string blobSha = "a8233120f6ad708f843d861ce2b7228ec4e3dec6";
            const string targetPath = "1/another_branch_file.txt";

            using (var repo = new Repository(BareTestRepoPath))
            {
                TreeDefinition td = TreeDefinition.From(repo.Head.Tip.Tree);

                var objectId = new ObjectId(blobSha);
                var blob = repo.Lookup<Blob>(objectId);

                Assert.NotEqual(ObjectId.Zero, td["1"].TargetId);

                td.Add(targetPath, blob, Mode.NonExecutableFile);

                Assert.Equal(ObjectId.Zero, td["1"].TargetId);
            }
        }

        [Fact]
        public void CanAddAnExistingBlobEntryWithAnExistingTree()
        {
            using (var repo = new Repository(BareTestRepoPath))
            {
                TreeDefinition td = TreeDefinition.From(repo.Head.Tip.Tree);
                TreeEntryDefinition original = td["README"];

                td.Add("1/2/README", original);

                TreeEntryDefinition fetched = td["1/2/README"];
                Assert.NotNull(fetched);

                Assert.Equal(original.TargetId, fetched.TargetId);
                Assert.Equal(original.Mode, fetched.Mode);

                Assert.NotNull(td["1/branch_file.txt"]);
            }
        }
    }
}