Skip to content

Commit 3d60252

Browse files
committed
fix tree.walk() and add test
1 parent d33bd76 commit 3d60252

File tree

3 files changed

+93
-5
lines changed

3 files changed

+93
-5
lines changed

lib/tree.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ Tree.prototype.walk = function(blobsOnly) {
127127
var event = new events.EventEmitter();
128128

129129
var total = 1;
130+
var entries = [];
130131

131132
// This looks like a DFS, but it is a BFS because of implicit queueing in
132133
// the recursive call to `entry.getTree(bfs)`
@@ -136,8 +137,8 @@ Tree.prototype.walk = function(blobsOnly) {
136137
if (error) {
137138
return event.emit("error", error);
138139
}
139-
var entries = tree.entries();
140-
entries.forEach(function (entry, entryIndex) {
140+
141+
tree.entries().forEach(function (entry, entryIndex) {
141142
if (!blobsOnly || entry.isFile()) {
142143
event.emit("entry", entry);
143144
entries.push(entry);

test/tests/tree.js

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
var assert = require("assert");
2+
var path = require("path");
3+
var local = path.join.bind(path, __dirname);
4+
var promisify = require("promisify-node");
5+
var fse = promisify(require("fs-extra"));
6+
7+
describe("Tree", function() {
8+
var RepoUtils = require("../utils/repository_setup");
9+
10+
var repoPath = local("../repos/tree");
11+
12+
beforeEach(function() {
13+
var test = this;
14+
return RepoUtils.createRepository(repoPath)
15+
.then(function(repo) {
16+
test.repository = repo;
17+
});
18+
});
19+
20+
after(function() {
21+
return fse.remove(repoPath);
22+
});
23+
24+
it("walks its entries and returns the same entries on both progress and end",
25+
function() {
26+
var repo = this.repository;
27+
var progressEntries = [];
28+
var endEntries;
29+
30+
return RepoUtils.commitFileToRepo(repo, "test.txt", "")
31+
.then(function(commit) {
32+
return RepoUtils.commitFileToRepo(repo, "foo/bar.txt", "", commit);
33+
})
34+
.then(function(commit) {
35+
return commit.getTree();
36+
})
37+
.then(function(tree) {
38+
assert(tree);
39+
40+
return new Promise(function (resolve, reject) {
41+
var walker = tree.walk();
42+
43+
walker.on("entry", function(entry) {
44+
progressEntries.push(entry);
45+
});
46+
walker.on("end", function(entries) {
47+
endEntries = entries;
48+
resolve();
49+
});
50+
walker.on("error", reject);
51+
52+
walker.start();
53+
});
54+
})
55+
.then(function() {
56+
assert(progressEntries.length);
57+
assert(endEntries && endEntries.length);
58+
59+
assert.equal(
60+
progressEntries.length, endEntries.length,
61+
"Different number of progress entries and end entries"
62+
);
63+
64+
function getEntryPath(entry) {
65+
return entry.path();
66+
}
67+
68+
var progressFilePaths = progressEntries.map(getEntryPath).sort();
69+
var endFilePaths = endEntries.map(getEntryPath);
70+
71+
assert.deepEqual(
72+
progressFilePaths.sort(), endFilePaths.sort(),
73+
"progress entries do not match end entries"
74+
);
75+
});
76+
});
77+
});

test/utils/repository_setup.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,28 @@ var RepositorySetup = {
1818
},
1919

2020
commitFileToRepo:
21-
function commitFileToRepo(repository, fileName, fileContent) {
21+
function commitFileToRepo(repository, fileName, fileContent, parentCommit) {
2222
var repoWorkDir = repository.workdir();
2323
var signature = NodeGit.Signature.create("Foo bar",
2424
"foo@bar.com", 123456789, 60);
2525

26-
return fse.writeFile(path.join(repoWorkDir, fileName), fileContent)
26+
var filePath = path.join(repoWorkDir, fileName);
27+
var parents = [];
28+
if (parentCommit) {
29+
parents.push(parentCommit);
30+
}
31+
32+
// fse.ensure allows us to write files inside new folders
33+
return fse.ensureFile(filePath)
34+
.then(function() {
35+
return fse.writeFile(filePath, fileContent);
36+
})
2737
.then(function() {
2838
return RepositorySetup.addFileToIndex(repository, fileName);
2939
})
3040
.then(function(oid) {
3141
return repository.createCommit("HEAD", signature, signature,
32-
"initial commit", oid, []);
42+
"initial commit", oid, parents);
3343
})
3444
.then(function(commitOid) {
3545
return repository.getCommit(commitOid);

0 commit comments

Comments
 (0)