aboutsummaryrefslogtreecommitdiffstats
path: root/file_test.go
diff options
context:
space:
mode:
authorJoshua Sjoding <joshua.sjoding@scjalliance.com>2016-02-16 12:29:06 -0800
committerJoshua Sjoding <joshua.sjoding@scjalliance.com>2016-02-17 04:46:57 -0800
commit9df17e545a445f58c5c43a1ece49bf1ff09e3b02 (patch)
treef1e50178d47e6c4bd68df613a338036280c30c9b /file_test.go
parent6b0a5984ac0c69742e60a39ad9437fd981dbe31b (diff)
downloadgo-git-9df17e545a445f58c5c43a1ece49bf1ff09e3b02.tar.gz
New iteration behavior via FileIter and TreeWalker
Instead of returning a channel of files, Tree.Files() now returns a FileIter with these qualities: * It returns files in the original order of the repository (relying on a * new Tree.OrderedNames property) * It can return errors encountered when retrieving files and trees from * underlying storage * It can be Closed without having to drain the entire channel * It defers the heavy lifting to a new TreeWalker type * Its behavior is a little more consistent with other Iter types * It's a little less prone to memory leaks This update includes a new TreeWalker type that will iterate through all of the entries of a tree and its descendant subtrees. It does the dirty work that Tree.walkEntries() used to do, but with a public API. A new TreeIter type is also included that just walks through subtrees. This could be useful for performing a directory search while ignoring files/blobs altogether.
Diffstat (limited to 'file_test.go')
-rw-r--r--file_test.go48
1 files changed, 47 insertions, 1 deletions
diff --git a/file_test.go b/file_test.go
index 25b4cf8..e3b9b4b 100644
--- a/file_test.go
+++ b/file_test.go
@@ -1,6 +1,7 @@
package git
import (
+ "io"
"os"
"gopkg.in/src-d/go-git.v3/core"
@@ -41,6 +42,49 @@ func (s *SuiteFile) SetUpSuite(c *C) {
}
}
+type fileIterExpectedEntry struct {
+ Name string
+ Hash string
+}
+
+var fileIterTests = []struct {
+ repo string // the repo name as in localRepos
+ commit string // the commit to search for the file
+ files []fileIterExpectedEntry
+}{
+ // https://api.github.com/repos/tyba/git-fixture/git/trees/6ecf0ef2c2dffb796033e5a02219af86ec6584e5
+ {"https://github.com/tyba/git-fixture.git", "6ecf0ef2c2dffb796033e5a02219af86ec6584e5", []fileIterExpectedEntry{
+ {".gitignore", "32858aad3c383ed1ff0a0f9bdf231d54a00c9e88"},
+ {"CHANGELOG", "d3ff53e0564a9f87d8e84b6e28e5060e517008aa"},
+ {"LICENSE", "c192bd6a24ea1ab01d78686e417c8bdc7c3d197f"},
+ {"binary.jpg", "d5c0f4ab811897cadf03aec358ae60d21f91c50d"},
+ {"go/example.go", "880cd14280f4b9b6ed3986d6671f907d7cc2a198"},
+ {"json/long.json", "49c6bb89b17060d7b4deacb7b338fcc6ea2352a9"},
+ {"json/short.json", "c8f1d8c61f9da76f4cb49fd86322b6e685dba956"},
+ {"php/crappy.php", "9a48f23120e880dfbe41f7c9b7b708e9ee62a492"},
+ {"vendor/foo.go", "9dea2395f5403188298c1dabe8bdafe562c491e3"},
+ }},
+}
+
+func (s *SuiteFile) TestIter(c *C) {
+ for i, t := range fileIterTests {
+ r := s.repos[t.repo]
+ commit, err := r.Commit(core.NewHash(t.commit))
+ c.Assert(err, IsNil, Commentf("subtest %d: %v (%s)", i, err, t.commit))
+
+ iter := NewFileIter(r, commit.Tree())
+ for k := 0; k < len(t.files); k++ {
+ expected := t.files[k]
+ file, err := iter.Next()
+ c.Assert(err, IsNil, Commentf("subtest %d, iter %d, err=%v", i, k, err))
+ c.Assert(file.Name, Equals, expected.Name, Commentf("subtest %d, iter %d, name=%s, expected=%s", i, k, file.Name, expected.Hash))
+ c.Assert(file.Hash.String(), Equals, expected.Hash, Commentf("subtest %d, iter %d, hash=%v, expected=%s", i, k, file.Hash.String(), expected.Hash))
+ }
+ _, err = iter.Next()
+ c.Assert(err, Equals, io.EOF)
+ }
+}
+
var contentsTests = []struct {
repo string // the repo name as in localRepos
commit string // the commit to search for the file
@@ -154,7 +198,9 @@ func (s *SuiteFile) TestIgnoreEmptyDirEntries(c *C) {
commit, err := s.repos[t.repo].Commit(core.NewHash(t.commit))
c.Assert(err, IsNil, Commentf("subtest %d: %v (%s)", i, err, t.commit))
- for file := range commit.Tree().Files() {
+ iter := commit.Tree().Files()
+ defer iter.Close()
+ for file, err := iter.Next(); err == nil; file, err = iter.Next() {
_ = file.Contents()
// this would probably panic if we are not ignoring empty dirs
}