Skip to content

Commit 4e0ee97

Browse files
author
John Haley
committed
Added a bunch of new test cases
1 parent 0865212 commit 4e0ee97

9 files changed

Lines changed: 249 additions & 50 deletions

File tree

lib/nodegit.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ require("./diff");
4848
require("./index");
4949
require("./object");
5050
require("./odb");
51+
require("./odb_object");
5152
require("./oid");
5253
require("./patch");
5354
require("./refs");

lib/odb.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,19 @@
11
var git = require("../");
2+
var normalizeOid = require("./util/normalize_oid");
23

34
var Odb = git.Odb;
5+
var read = Odb.prototype.read;
6+
7+
Odb.prototype.read = function(oid, callback) {
8+
oid = normalizeOid(oid);
9+
10+
return read.call(this, oid).then(function(odbObject) {
11+
if (typeof callback === "function") {
12+
callback(null, odbObject);
13+
}
14+
15+
return odbObject;
16+
}, callback);
17+
};
418

519
module.exports = Odb;

lib/odb_object.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
var NodeGit = require("../");
2+
3+
var OdbObject = NodeGit.OdbObject;
4+
5+
OdbObject.prototype.toString = function(size) {
6+
size = size || this.size();
7+
8+
return this.data().toBuffer(size).toString();
9+
};
10+
11+
module.exports = OdbObject;

lib/repository.js

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,15 @@ Repository.prototype.getReference = function(name, callback) {
6969
return reference.resolve(function (error, reference) {
7070
reference.repo = repository;
7171

72-
if (callback) {
72+
if (typeof callback === "function") {
7373
callback(null, reference);
7474
}
7575

7676
return reference;
7777
});
7878
} else {
7979
reference.repo = repository;
80-
if (callback) {
80+
if (typeof callback === "function") {
8181
callback(null, reference);
8282
}
8383
return reference;
@@ -115,7 +115,7 @@ Repository.getReferences = function(repo, type, refNamesOnly, callback) {
115115
});
116116

117117
return Promise.all(refFilterPromises).then(function() {
118-
if (callback) {
118+
if (typeof callback === "function") {
119119
callback(null, filteredRefs);
120120
}
121121
return filteredRefs;
@@ -222,6 +222,31 @@ Repository.prototype.getTag = function(oid, callback) {
222222
}, callback);
223223
};
224224

225+
/**
226+
* Retrieve the tag represented by the tag name.
227+
*
228+
* @param {String} Short or full tag name
229+
* @param {Function} callback
230+
* @return {Tag}
231+
*/
232+
Repository.prototype.getTagByName = function(name, callback) {
233+
var repo = this;
234+
235+
name = ~name.indexOf("refs/tags/") ? name : "refs/tags/" + name;
236+
237+
return Reference.nameToId(repo, name).then(function(oid) {
238+
return Tag.lookup(repo, oid).then(function(reference) {
239+
reference.repo = repo;
240+
241+
if (typeof callback === "function") {
242+
callback(null, reference);
243+
}
244+
245+
return reference;
246+
});
247+
}, callback);
248+
};
249+
225250
/**
226251
* Instantiate a new revision walker for browsing the Repository"s history.
227252
* See also `Commit.prototype.history()`

test/tests/index.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
var assert = require("assert");
2+
var path = require("path");
3+
4+
describe("Index", function() {
5+
var reposPath = path.resolve("test/repos/workdir/.git");
6+
7+
var Repository = require("../../lib/repository");
8+
9+
before(function() {
10+
var test = this;
11+
12+
return Repository.open(reposPath).then(function(repo) {
13+
test.repo = repo;
14+
15+
return repo.openIndex().then(function(index) {
16+
test.index = index;
17+
18+
return index;
19+
});
20+
});
21+
});
22+
23+
it("can get the index of a repo and examine entries", function() {
24+
var entries = this.index.entries();
25+
26+
assert.equal(entries[0].path(), ".gitignore");
27+
});
28+
});

test/tests/odb.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
var assert = require("assert");
2+
var path = require("path");
3+
4+
describe("Odb", function() {
5+
var reposPath = path.resolve("test/repos/workdir/.git");
6+
7+
var Repository = require("../../lib/repository");
8+
var Oid = require("../../lib/oid");
9+
var Obj = require("../../lib/object");
10+
11+
before(function() {
12+
var test = this;
13+
14+
return Repository.open(reposPath).then(function(repo) {
15+
test.repo = repo;
16+
17+
return repo;
18+
}).then(function(repo) {
19+
return repo.odb();
20+
}).then(function(odb) {
21+
test.odb = odb;
22+
23+
return odb;
24+
});
25+
});
26+
27+
it("can read raw objects directly from the odb using an OID", function() {
28+
var oid = Oid.fromString("32789a79e71fbc9e04d3eff7425e1771eb595150");
29+
30+
return this.odb.read(oid).then(function (object) {
31+
assert.equal(object.type(), Obj.Type.Commit);
32+
});
33+
});
34+
35+
it("can read objects directly from the odb using a string", function() {
36+
return this.odb.read("32789a79e71fbc9e04d3eff7425e1771eb595150")
37+
.then(function (object) {
38+
assert.equal(object.type(), Obj.Type.Commit);
39+
});
40+
});
41+
42+
it("can write raw objects to git", function() {
43+
var obj = "test data";
44+
var odb = this.odb;
45+
46+
return odb.write(obj, obj.length, Obj.Type.Blob).then(function(oid) {
47+
assert.ok(oid instanceof Oid);
48+
49+
return odb.read(oid);
50+
}).then(function(object) {
51+
assert.equal(object.type(), Obj.Type.Blob);
52+
assert.equal(object.toString(), obj);
53+
assert.equal(object.size(), obj.length);
54+
});
55+
});
56+
});

test/tests/oid.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ describe("Oid", function() {
1919
var string = this.oid.allocfmt();
2020

2121
assert.equal(string, oid);
22+
assert.equal(this.oid.toString(), oid);
2223
});
2324

2425
it("provides a custom inspect method to improve debugging", function() {

test/tests/revwalk.js

Lines changed: 47 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -40,44 +40,44 @@ describe("Revwalk", function() {
4040
var test = this;
4141

4242
return test.walker.next().then(function(commit) {
43-
return test.walker.next().then(function(commit) {
44-
return test.walker.next().then(function(commit) {
45-
return test.walker.next().then(function(commit) {
46-
assert.equal(commit.toString(),
47-
"b8a94aefb22d0534cc0e5acf533989c13d8725dc");
48-
test.walker = test.repository.createRevWalk();
49-
test.walker.push(test.branch.id());
50-
test.walker.hide(
51-
Oid.fromString("b8a94aefb22d0534cc0e5acf533989c13d8725dc"));
43+
return test.walker.next();
44+
}).then(function() {
45+
return test.walker.next();
46+
}).then(function() {
47+
return test.walker.next();
48+
}).then(function(commit) {
49+
assert.equal(commit.toString(),
50+
"b8a94aefb22d0534cc0e5acf533989c13d8725dc");
51+
test.walker = test.repository.createRevWalk();
52+
test.walker.push(test.branch.id());
53+
test.walker.hide(
54+
Oid.fromString("b8a94aefb22d0534cc0e5acf533989c13d8725dc"));
5255

53-
return test.walker.next().then(function(commit) {
54-
return test.walker.next().then(function(commit) {
55-
return test.walker.next().then(function(commit) {
56-
assert.equal(commit.toString(),
57-
"95f695136203a372751c19b6353aeb5ae32ea40e");
58-
return test.walker.next().then(function(commit) {
59-
assert.equal(commit, undefined);
60-
});
61-
});
62-
});
63-
});
64-
});
65-
});
66-
});
56+
return test.walker.next();
57+
}).then(function() {
58+
return test.walker.next();
59+
}).then(function() {
60+
return test.walker.next();
61+
}).then(function(commit) {
62+
assert.equal(commit.toString(),
63+
"95f695136203a372751c19b6353aeb5ae32ea40e");
64+
return test.walker.next();
65+
}).then(function(commit) {
66+
assert.equal(commit, undefined);
6767
});
6868
});
6969

7070
it("can simplify to first parent", function() {
7171
var test = this;
7272

7373
test.walker.simplifyFirstParent();
74-
return test.walker.next().then(function(commit) {
75-
return test.walker.next().then(function(commit) {
76-
return test.walker.next().then(function(commit) {
77-
assert.equal(commit.toString(),
78-
"b8a94aefb22d0534cc0e5acf533989c13d8725dc");
79-
});
80-
});
74+
return test.walker.next().then(function() {
75+
return test.walker.next();
76+
}).then(function() {
77+
return test.walker.next();
78+
}).then(function(commit) {
79+
assert.equal(commit.toString(),
80+
"b8a94aefb22d0534cc0e5acf533989c13d8725dc");
8181
});
8282
});
8383

@@ -86,26 +86,26 @@ describe("Revwalk", function() {
8686
var testGC = (global.gc ? it : it.skip);
8787

8888
testGC("doesnt segfault when accessing .author() twice", function(done) {
89-
this.timeout(10000);
90-
return Repository.open(reposPath).then(function(repository) {
91-
var walker = repository.createRevWalk();
92-
repository.getMaster().then(function(master) {
93-
var did = false;
94-
walker.walk(master, function(error, commit) {
95-
for (var i = 0; i < 1000; i++) {
96-
if (true) {
97-
commit.author().name();
98-
commit.author().email();
99-
}
100-
global.gc();
101-
}
102-
if (!did) {
103-
done();
104-
did = true;
89+
this.timeout(10000);
90+
return Repository.open(reposPath).then(function(repository) {
91+
var walker = repository.createRevWalk();
92+
return repository.getMaster().then(function(master) {
93+
var did = false;
94+
walker.walk(master, function(error, commit) {
95+
for (var i = 0; i < 1000; i++) {
96+
if (true) {
97+
commit.author().name();
98+
commit.author().email();
10599
}
106-
});
100+
global.gc();
101+
}
102+
if (!did) {
103+
done();
104+
did = true;
105+
}
107106
});
108107
});
109108
});
109+
});
110110

111111
});

test/tests/tag.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
var assert = require("assert");
2+
var path = require("path");
3+
4+
describe("Tag", function() {
5+
var reposPath = path.resolve("test/repos/workdir/.git");
6+
7+
var Repository = require("../../lib/repository");
8+
var Obj = require("../../lib/object");
9+
var Oid = require("../../lib/oid");
10+
11+
var tagName = "annotated-tag";
12+
var tagFullName = "refs/tags/" + tagName;
13+
var tagOid = "dc800017566123ff3c746b37284a24a66546667e";
14+
var commitPointedTo = "32789a79e71fbc9e04d3eff7425e1771eb595150";
15+
var tagMessage = "This is an annotated tag\n";
16+
17+
function testTag(tag) {
18+
assert.equal(tag.name(), tagName);
19+
assert.equal(tag.targetType(), Obj.Type.Commit);
20+
assert.equal(tag.message(), tagMessage);
21+
22+
var target = tag.target();
23+
24+
assert.ok(target.isCommit());
25+
assert.equal(target.id().toString(), commitPointedTo);
26+
}
27+
28+
before(function() {
29+
var test = this;
30+
31+
return Repository.open(reposPath).then(function(repo) {
32+
test.repo = repo;
33+
34+
return repo;
35+
});
36+
});
37+
38+
it("can get a tag from a repo via the tag name", function() {
39+
return this.repo.getTagByName(tagName).then(function(tag) {
40+
testTag(tag);
41+
});
42+
});
43+
44+
it("can get a tag from a repo via the long tag name", function() {
45+
return this.repo.getTagByName(tagFullName).then(function(tag) {
46+
testTag(tag);
47+
});
48+
});
49+
50+
it("can get a tag from a repo via the tag's OID as a string", function() {
51+
return this.repo.getTag(tagOid).then(function(tag) {
52+
testTag(tag);
53+
});
54+
});
55+
56+
it("can get a tag from a repo via the tag's OID object", function() {
57+
var oid = Oid.fromString(tagOid);
58+
59+
return this.repo.getTag(oid).then(function(tag) {
60+
testTag(tag);
61+
});
62+
});
63+
});

0 commit comments

Comments
 (0)