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

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

namespace LibGit2Sharp.Tests
{
    public class TreeFixture : BaseFixture
    {
        private const string sha = "581f9824ecaf824221bd36edf5430f2739a7c4f5";

        [Fact]
        public void CanCompareTwoTreeEntries()
        {
            using (var repo = new Repository(BareTestRepoPath))
            {
                var tree = repo.Lookup<Tree>(sha);
                TreeEntry treeEntry1 = tree["README"];
                TreeEntry treeEntry2 = tree["README"];
                Assert.Equal(treeEntry2, treeEntry1);
                Assert.True((treeEntry1 == treeEntry2));
            }
        }

        [Fact]
        public void CanConvertEntryToBlob()
        {
            using (var repo = new Repository(BareTestRepoPath))
            {
                var tree = repo.Lookup<Tree>(sha);
                TreeEntry treeEntry = tree["README"];

                var blob = treeEntry.Target as Blob;
                Assert.NotNull(blob);
            }
        }

        [Fact]
        public void CanConvertEntryToTree()
        {
            using (var repo = new Repository(BareTestRepoPath))
            {
                var tree = repo.Lookup<Tree>(sha);
                TreeEntry treeEntry = tree["1"];

                var subtree = treeEntry.Target as Tree;
                Assert.NotNull(subtree);
            }
        }

        [Fact]
        public void CanEnumerateBlobs()
        {
            using (var repo = new Repository(BareTestRepoPath))
            {
                var tree = repo.Lookup<Tree>(sha);

                IEnumerable<Blob> blobs = tree
                    .Where(e => e.TargetType == TreeEntryTargetType.Blob)
                    .Select(e => e.Target)
                    .Cast<Blob>();

                Assert.Equal(3, blobs.Count());
            }
        }

        [Fact]
        public void CanEnumerateSubTrees()
        {
            using (var repo = new Repository(BareTestRepoPath))
            {
                var tree = repo.Lookup<Tree>(sha);

                IEnumerable<Tree> subTrees = tree
                    .Where(e => e.TargetType == TreeEntryTargetType.Tree)
                    .Select(e => e.Target)
                    .Cast<Tree>();

                Assert.Equal(1, subTrees.Count());
            }
        }

        [Fact]
        public void CanEnumerateTreeEntries()
        {
            using (var repo = new Repository(BareTestRepoPath))
            {
                var tree = repo.Lookup<Tree>(sha);
                Assert.Equal(tree.Count, tree.Count());

                Assert.Equal(new[] { "1", "README", "branch_file.txt", "new.txt" }, tree.Select(te => te.Name).ToArray());
            }
        }

        [Fact]
        public void CanGetEntryByName()
        {
            using (var repo = new Repository(BareTestRepoPath))
            {
                var tree = repo.Lookup<Tree>(sha);
                TreeEntry treeEntry = tree["README"];
                Assert.Equal("a8233120f6ad708f843d861ce2b7228ec4e3dec6", treeEntry.Target.Sha);
                Assert.Equal("README", treeEntry.Name);
            }
        }

        [Fact]
        public void GettingAnUknownTreeEntryReturnsNull()
        {
            using (var repo = new Repository(BareTestRepoPath))
            {
                var tree = repo.Lookup<Tree>(sha);
                TreeEntry treeEntry = tree["I-do-not-exist"];
                Assert.Null(treeEntry);
            }
        }

        [Fact]
        public void CanGetEntryCountFromTree()
        {
            using (var repo = new Repository(BareTestRepoPath))
            {
                var tree = repo.Lookup<Tree>(sha);
                Assert.Equal(4, tree.Count);
            }
        }

        [Fact]
        public void CanReadEntryAttributes()
        {
            using (var repo = new Repository(BareTestRepoPath))
            {
                var tree = repo.Lookup<Tree>(sha);
                Assert.Equal(Mode.NonExecutableFile, tree["README"].Mode);
            }
        }

        [Fact]
        public void CanReadTheTreeData()
        {
            using (var repo = new Repository(BareTestRepoPath))
            {
                var tree = repo.Lookup<Tree>(sha);
                Assert.NotNull(tree);
            }
        }

        [Fact]
        public void TreeDataIsPresent()
        {
            using (var repo = new Repository(BareTestRepoPath))
            {
                GitObject tree = repo.Lookup(sha);
                Assert.NotNull(tree);
            }
        }

        [Fact]
        public void CanRetrieveTreeEntryPath()
        {
            using (var repo = new Repository(BareTestRepoPath))
            {
                /* From a commit tree */
                var commitTree = repo.Lookup<Commit>("4c062a6").Tree;

                TreeEntry treeTreeEntry = commitTree["1"];
                Assert.Equal("1", treeTreeEntry.Path);

                string completePath = Path.Combine("1", "branch_file.txt");

                TreeEntry blobTreeEntry = commitTree["1/branch_file.txt"];
                Assert.Equal(completePath, blobTreeEntry.Path);

                // A tree entry is now fetched through a relative path to the
                // tree but exposes a complete path through its Path property
                var subTree = treeTreeEntry.Target as Tree;
                Assert.NotNull(subTree);
                TreeEntry anInstance = subTree["branch_file.txt"];

                Assert.NotEqual("branch_file.txt", anInstance.Path);
                Assert.Equal(completePath, anInstance.Path);
                Assert.Equal(completePath, subTree.First().Path);

                /* From a random tree */
                var tree = repo.Lookup<Tree>(treeTreeEntry.Target.Id);
                TreeEntry anotherInstance = tree["branch_file.txt"];
                Assert.Equal("branch_file.txt", anotherInstance.Path);

                // From a rev-parse statement
                var revparseTree = repo.Lookup<Tree>("master:1");
                TreeEntry yetAnotherInstance = revparseTree["branch_file.txt"];
                Assert.Equal(completePath, yetAnotherInstance.Path);

                Assert.Equal(tree, subTree);
                Assert.Equal(revparseTree, tree);
                Assert.Equal(anotherInstance, anInstance);
                Assert.Equal(yetAnotherInstance, anotherInstance);
                Assert.NotEqual(anotherInstance.Path, anInstance.Path);
                Assert.NotSame(anotherInstance, anInstance);
            }
        }

        [Fact]
        public void CanParseSymlinkTreeEntries()
        {
            var path = CloneBareTestRepo();

            using (var repo = new Repository(path))
            {
                Blob linkContent = OdbHelper.CreateBlob(repo, "1/branch_file.txt");

                var td = TreeDefinition.From(repo.Head.Tip)
                    .Add("A symlink", linkContent, Mode.SymbolicLink);

                Tree t = repo.ObjectDatabase.CreateTree(td);

                var te = t["A symlink"];

                Assert.NotNull(te);

                Assert.Equal(Mode.SymbolicLink, te.Mode);
                Assert.Equal(linkContent, te.Target);
            }
        }
    }
}