Debian Codegraph @

plumbing/object/file_test.go

raw
1 package object
2
3 import (
4 "io"
5
6 "github.com/go-git/go-git/v5/plumbing"
7 "github.com/go-git/go-git/v5/plumbing/cache"
8 "github.com/go-git/go-git/v5/plumbing/filemode"
9 "github.com/go-git/go-git/v5/plumbing/storer"
10 "github.com/go-git/go-git/v5/storage/filesystem"
11
12 fixtures "github.com/go-git/go-git-fixtures/v4"
13 . "gopkg.in/check.v1"
14 )
15
16 type FileSuite struct {
17 BaseObjectsSuite
18 }
19
20 var _ = Suite(&FileSuite{})
21
22 type fileIterExpectedEntry struct {
23 Name string
24 Hash string
25 }
26
27 var fileIterTests = []struct {
28 repo string // the repo name as in localRepos
29 commit string // the commit to search for the file
30 files []fileIterExpectedEntry
31 }{
32 {"https://github.com/git-fixtures/basic.git", "6ecf0ef2c2dffb796033e5a02219af86ec6584e5", []fileIterExpectedEntry{
33 {".gitignore", "32858aad3c383ed1ff0a0f9bdf231d54a00c9e88"},
34 {"CHANGELOG", "d3ff53e0564a9f87d8e84b6e28e5060e517008aa"},
35 {"LICENSE", "c192bd6a24ea1ab01d78686e417c8bdc7c3d197f"},
36 {"binary.jpg", "d5c0f4ab811897cadf03aec358ae60d21f91c50d"},
37 {"go/example.go", "880cd14280f4b9b6ed3986d6671f907d7cc2a198"},
38 {"json/long.json", "49c6bb89b17060d7b4deacb7b338fcc6ea2352a9"},
39 {"json/short.json", "c8f1d8c61f9da76f4cb49fd86322b6e685dba956"},
40 {"php/crappy.php", "9a48f23120e880dfbe41f7c9b7b708e9ee62a492"},
41 {"vendor/foo.go", "9dea2395f5403188298c1dabe8bdafe562c491e3"},
42 }},
43 }
44
45 func (s *FileSuite) TestIter(c *C) {
46 for i, t := range fileIterTests {
47 f := fixtures.ByURL(t.repo).One()
48 sto := filesystem.NewStorage(f.DotGit(), cache.NewObjectLRUDefault())
49
50 h := plumbing.NewHash(t.commit)
51 commit, err := GetCommit(sto, h)
52 c.Assert(err, IsNil, Commentf("subtest %d: %v (%s)", i, err, t.commit))
53
54 tree, err := commit.Tree()
55 c.Assert(err, IsNil)
56
57 iter := NewFileIter(sto, tree)
58 for k := 0; k < len(t.files); k++ {
59 exp := t.files[k]
60 file, err := iter.Next()
61 c.Assert(err, IsNil, Commentf("subtest %d, iter %d, err=%v", i, k, err))
62 c.Assert(file.Mode, Equals, filemode.Regular)
63 c.Assert(file.Hash.IsZero(), Equals, false)
64 c.Assert(file.Hash, Equals, file.ID())
65 c.Assert(file.Name, Equals, exp.Name, Commentf("subtest %d, iter %d, name=%s, expected=%s", i, k, file.Name, exp.Hash))
66 c.Assert(file.Hash.String(), Equals, exp.Hash, Commentf("subtest %d, iter %d, hash=%v, expected=%s", i, k, file.Hash.String(), exp.Hash))
67 }
68 _, err = iter.Next()
69 c.Assert(err, Equals, io.EOF)
70 }
71 }
72
73 var contentsTests = []struct {
74 repo string // the repo name as in localRepos
75 commit string // the commit to search for the file
76 path string // the path of the file to find
77 contents string // expected contents of the file
78 }{
79 {
80 "https://github.com/git-fixtures/basic.git",
81 "b029517f6300c2da0f4b651b8642506cd6aaf45d",
82 ".gitignore",
83 `*.class
84
85 # Mobile Tools for Java (J2ME)
86 .mtj.tmp/
87
88 # Package Files #
89 *.jar
90 *.war
91 *.ear
92
93 # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
94 hs_err_pid*
95 `,
96 },
97 {
98 "https://github.com/git-fixtures/basic.git",
99 "6ecf0ef2c2dffb796033e5a02219af86ec6584e5",
100 "CHANGELOG",
101 `Initial changelog
102 `,
103 },
104 }
105
106 func (s *FileSuite) TestContents(c *C) {
107 for i, t := range contentsTests {
108 f := fixtures.ByURL(t.repo).One()
109 sto := filesystem.NewStorage(f.DotGit(), cache.NewObjectLRUDefault())
110
111 h := plumbing.NewHash(t.commit)
112 commit, err := GetCommit(sto, h)
113 c.Assert(err, IsNil, Commentf("subtest %d: %v (%s)", i, err, t.commit))
114
115 file, err := commit.File(t.path)
116 c.Assert(err, IsNil)
117 content, err := file.Contents()
118 c.Assert(err, IsNil)
119 c.Assert(content, Equals, t.contents, Commentf(
120 "subtest %d: commit=%s, path=%s", i, t.commit, t.path))
121 }
122 }
123
124 var linesTests = []struct {
125 repo string // the repo name as in localRepos
126 commit string // the commit to search for the file
127 path string // the path of the file to find
128 lines []string // expected lines in the file
129 }{
130 {
131 "https://github.com/git-fixtures/basic.git",
132 "b029517f6300c2da0f4b651b8642506cd6aaf45d",
133 ".gitignore",
134 []string{
135 "*.class",
136 "",
137 "# Mobile Tools for Java (J2ME)",
138 ".mtj.tmp/",
139 "",
140 "# Package Files #",
141 "*.jar",
142 "*.war",
143 "*.ear",
144 "",
145 "# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml",
146 "hs_err_pid*",
147 },
148 },
149 {
150 "https://github.com/git-fixtures/basic.git",
151 "6ecf0ef2c2dffb796033e5a02219af86ec6584e5",
152 "CHANGELOG",
153 []string{
154 "Initial changelog",
155 },
156 },
157 }
158
159 func (s *FileSuite) TestLines(c *C) {
160 for i, t := range linesTests {
161 f := fixtures.ByURL(t.repo).One()
162 sto := filesystem.NewStorage(f.DotGit(), cache.NewObjectLRUDefault())
163
164 h := plumbing.NewHash(t.commit)
165 commit, err := GetCommit(sto, h)
166 c.Assert(err, IsNil, Commentf("subtest %d: %v (%s)", i, err, t.commit))
167
168 file, err := commit.File(t.path)
169 c.Assert(err, IsNil)
170 lines, err := file.Lines()
171 c.Assert(err, IsNil)
172 c.Assert(lines, DeepEquals, t.lines, Commentf(
173 "subtest %d: commit=%s, path=%s", i, t.commit, t.path))
174 }
175 }
176
177 var ignoreEmptyDirEntriesTests = []struct {
178 repo string // the repo name as in localRepos
179 commit string // the commit to search for the file
180 }{
181 {
182 "https://github.com/cpcs499/Final_Pres_P.git",
183 "70bade703ce556c2c7391a8065c45c943e8b6bc3",
184 // the Final dir in this commit is empty
185 },
186 }
187
188 // It is difficult to assert that we are ignoring an (empty) dir as even
189 // if we don't, no files will be found in it.
190 //
191 // At least this test has a high chance of panicking if
192 // we don't ignore empty dirs.
193 func (s *FileSuite) TestIgnoreEmptyDirEntries(c *C) {
194 for i, t := range ignoreEmptyDirEntriesTests {
195 f := fixtures.ByURL(t.repo).One()
196 sto := filesystem.NewStorage(f.DotGit(), cache.NewObjectLRUDefault())
197
198 h := plumbing.NewHash(t.commit)
199 commit, err := GetCommit(sto, h)
200 c.Assert(err, IsNil, Commentf("subtest %d: %v (%s)", i, err, t.commit))
201
202 tree, err := commit.Tree()
203 c.Assert(err, IsNil)
204
205 iter := tree.Files()
206 defer iter.Close()
207 for file, err := iter.Next(); err == nil; file, err = iter.Next() {
208 _, _ = file.Contents()
209 // this would probably panic if we are not ignoring empty dirs
210 }
211 }
212 }
213
214 func (s *FileSuite) TestFileIter(c *C) {
215 hash := plumbing.NewHash("1669dce138d9b841a518c64b10914d88f5e488ea")
216 commit, err := GetCommit(s.Storer, hash)
217 c.Assert(err, IsNil)
218
219 tree, err := commit.Tree()
220 c.Assert(err, IsNil)
221
222 expected := []string{
223 ".gitignore",
224 "CHANGELOG",
225 "LICENSE",
226 "binary.jpg",
227 }
228
229 var count int
230 i := tree.Files()
231 i.ForEach(func(f *File) error {
232 c.Assert(f.Name, Equals, expected[count])
233 count++
234 return nil
235 })
236
237 c.Assert(count, Equals, 4)
238
239 count = 0
240 i = tree.Files()
241 i.ForEach(func(f *File) error {
242 count++
243 return storer.ErrStop
244 })
245
246 c.Assert(count, Equals, 1)
247 }
248
249 func (s *FileSuite) TestFileIterSubmodule(c *C) {
250 dotgit := fixtures.ByURL("https://github.com/git-fixtures/submodule.git").One().DotGit()
251 st := filesystem.NewStorage(dotgit, cache.NewObjectLRUDefault())
252
253 hash := plumbing.NewHash("b685400c1f9316f350965a5993d350bc746b0bf4")
254 commit, err := GetCommit(st, hash)
255 c.Assert(err, IsNil)
256
257 tree, err := commit.Tree()
258 c.Assert(err, IsNil)
259
260 expected := []string{
261 ".gitmodules",
262 "README.md",
263 }
264
265 var count int
266 i := tree.Files()
267 i.ForEach(func(f *File) error {
268 c.Assert(f.Name, Equals, expected[count])
269 count++
270 return nil
271 })
272
273 c.Assert(count, Equals, 2)
274 }