Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Added a bunch of new test cases
  • Loading branch information
John Haley committed Nov 19, 2014
commit 4e0ee978919f58b23e122eae5f59651f2d607464
1 change: 1 addition & 0 deletions lib/nodegit.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ require("./diff");
require("./index");
require("./object");
require("./odb");
require("./odb_object");
require("./oid");
require("./patch");
require("./refs");
Expand Down
14 changes: 14 additions & 0 deletions lib/odb.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
var git = require("../");
var normalizeOid = require("./util/normalize_oid");

var Odb = git.Odb;
var read = Odb.prototype.read;

Odb.prototype.read = function(oid, callback) {
oid = normalizeOid(oid);

return read.call(this, oid).then(function(odbObject) {
if (typeof callback === "function") {
callback(null, odbObject);
}

return odbObject;
}, callback);
};

module.exports = Odb;
11 changes: 11 additions & 0 deletions lib/odb_object.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
var NodeGit = require("../");

var OdbObject = NodeGit.OdbObject;

OdbObject.prototype.toString = function(size) {
size = size || this.size();

return this.data().toBuffer(size).toString();
};

module.exports = OdbObject;
31 changes: 28 additions & 3 deletions lib/repository.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,15 @@ Repository.prototype.getReference = function(name, callback) {
return reference.resolve(function (error, reference) {
reference.repo = repository;

if (callback) {
if (typeof callback === "function") {
callback(null, reference);
}

return reference;
});
} else {
reference.repo = repository;
if (callback) {
if (typeof callback === "function") {
callback(null, reference);
}
return reference;
Expand Down Expand Up @@ -115,7 +115,7 @@ Repository.getReferences = function(repo, type, refNamesOnly, callback) {
});

return Promise.all(refFilterPromises).then(function() {
if (callback) {
if (typeof callback === "function") {
callback(null, filteredRefs);
}
return filteredRefs;
Expand Down Expand Up @@ -222,6 +222,31 @@ Repository.prototype.getTag = function(oid, callback) {
}, callback);
};

/**
* Retrieve the tag represented by the tag name.
*
* @param {String} Short or full tag name
* @param {Function} callback
* @return {Tag}
*/
Repository.prototype.getTagByName = function(name, callback) {
var repo = this;

name = ~name.indexOf("refs/tags/") ? name : "refs/tags/" + name;

return Reference.nameToId(repo, name).then(function(oid) {
return Tag.lookup(repo, oid).then(function(reference) {
reference.repo = repo;

if (typeof callback === "function") {
callback(null, reference);
}

return reference;
});
}, callback);
};

/**
* Instantiate a new revision walker for browsing the Repository"s history.
* See also `Commit.prototype.history()`
Expand Down
28 changes: 28 additions & 0 deletions test/tests/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
var assert = require("assert");
var path = require("path");

describe("Index", function() {
var reposPath = path.resolve("test/repos/workdir/.git");

var Repository = require("../../lib/repository");

before(function() {
var test = this;

return Repository.open(reposPath).then(function(repo) {
test.repo = repo;

return repo.openIndex().then(function(index) {
test.index = index;

return index;
});
});
});

it("can get the index of a repo and examine entries", function() {
var entries = this.index.entries();

assert.equal(entries[0].path(), ".gitignore");
});
});
56 changes: 56 additions & 0 deletions test/tests/odb.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
var assert = require("assert");
var path = require("path");

describe("Odb", function() {
var reposPath = path.resolve("test/repos/workdir/.git");

var Repository = require("../../lib/repository");
var Oid = require("../../lib/oid");
var Obj = require("../../lib/object");

before(function() {
var test = this;

return Repository.open(reposPath).then(function(repo) {
test.repo = repo;

return repo;
}).then(function(repo) {
return repo.odb();
}).then(function(odb) {
test.odb = odb;

return odb;
});
});

it("can read raw objects directly from the odb using an OID", function() {
var oid = Oid.fromString("32789a79e71fbc9e04d3eff7425e1771eb595150");

return this.odb.read(oid).then(function (object) {
assert.equal(object.type(), Obj.Type.Commit);
});
});

it("can read objects directly from the odb using a string", function() {
return this.odb.read("32789a79e71fbc9e04d3eff7425e1771eb595150")
.then(function (object) {
assert.equal(object.type(), Obj.Type.Commit);
});
});

it("can write raw objects to git", function() {
var obj = "test data";
var odb = this.odb;

return odb.write(obj, obj.length, Obj.Type.Blob).then(function(oid) {
assert.ok(oid instanceof Oid);

return odb.read(oid);
}).then(function(object) {
assert.equal(object.type(), Obj.Type.Blob);
assert.equal(object.toString(), obj);
assert.equal(object.size(), obj.length);
});
});
});
1 change: 1 addition & 0 deletions test/tests/oid.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ describe("Oid", function() {
var string = this.oid.allocfmt();

assert.equal(string, oid);
assert.equal(this.oid.toString(), oid);
});

it("provides a custom inspect method to improve debugging", function() {
Expand Down
94 changes: 47 additions & 47 deletions test/tests/revwalk.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,44 +40,44 @@ describe("Revwalk", function() {
var test = this;

return test.walker.next().then(function(commit) {
return test.walker.next().then(function(commit) {
return test.walker.next().then(function(commit) {
return test.walker.next().then(function(commit) {
assert.equal(commit.toString(),
"b8a94aefb22d0534cc0e5acf533989c13d8725dc");
test.walker = test.repository.createRevWalk();
test.walker.push(test.branch.id());
test.walker.hide(
Oid.fromString("b8a94aefb22d0534cc0e5acf533989c13d8725dc"));
return test.walker.next();
}).then(function() {
return test.walker.next();
}).then(function() {
return test.walker.next();
}).then(function(commit) {
assert.equal(commit.toString(),
"b8a94aefb22d0534cc0e5acf533989c13d8725dc");
test.walker = test.repository.createRevWalk();
test.walker.push(test.branch.id());
test.walker.hide(
Oid.fromString("b8a94aefb22d0534cc0e5acf533989c13d8725dc"));

return test.walker.next().then(function(commit) {
return test.walker.next().then(function(commit) {
return test.walker.next().then(function(commit) {
assert.equal(commit.toString(),
"95f695136203a372751c19b6353aeb5ae32ea40e");
return test.walker.next().then(function(commit) {
assert.equal(commit, undefined);
});
});
});
});
});
});
});
return test.walker.next();
}).then(function() {
return test.walker.next();
}).then(function() {
return test.walker.next();
}).then(function(commit) {
assert.equal(commit.toString(),
"95f695136203a372751c19b6353aeb5ae32ea40e");
return test.walker.next();
}).then(function(commit) {
assert.equal(commit, undefined);
});
});

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

test.walker.simplifyFirstParent();
return test.walker.next().then(function(commit) {
return test.walker.next().then(function(commit) {
return test.walker.next().then(function(commit) {
assert.equal(commit.toString(),
"b8a94aefb22d0534cc0e5acf533989c13d8725dc");
});
});
return test.walker.next().then(function() {
return test.walker.next();
}).then(function() {
return test.walker.next();
}).then(function(commit) {
assert.equal(commit.toString(),
"b8a94aefb22d0534cc0e5acf533989c13d8725dc");
});
});

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

testGC("doesnt segfault when accessing .author() twice", function(done) {
this.timeout(10000);
return Repository.open(reposPath).then(function(repository) {
var walker = repository.createRevWalk();
repository.getMaster().then(function(master) {
var did = false;
walker.walk(master, function(error, commit) {
for (var i = 0; i < 1000; i++) {
if (true) {
commit.author().name();
commit.author().email();
}
global.gc();
}
if (!did) {
done();
did = true;
this.timeout(10000);
return Repository.open(reposPath).then(function(repository) {
var walker = repository.createRevWalk();
return repository.getMaster().then(function(master) {
var did = false;
walker.walk(master, function(error, commit) {
for (var i = 0; i < 1000; i++) {
if (true) {
commit.author().name();
commit.author().email();
}
});
global.gc();
}
if (!did) {
done();
did = true;
}
});
});
});
});

});
63 changes: 63 additions & 0 deletions test/tests/tag.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
var assert = require("assert");
var path = require("path");

describe("Tag", function() {
var reposPath = path.resolve("test/repos/workdir/.git");

var Repository = require("../../lib/repository");
var Obj = require("../../lib/object");
var Oid = require("../../lib/oid");

var tagName = "annotated-tag";
var tagFullName = "refs/tags/" + tagName;
var tagOid = "dc800017566123ff3c746b37284a24a66546667e";
var commitPointedTo = "32789a79e71fbc9e04d3eff7425e1771eb595150";
var tagMessage = "This is an annotated tag\n";

function testTag(tag) {
assert.equal(tag.name(), tagName);
assert.equal(tag.targetType(), Obj.Type.Commit);
assert.equal(tag.message(), tagMessage);

var target = tag.target();

assert.ok(target.isCommit());
assert.equal(target.id().toString(), commitPointedTo);
}

before(function() {
var test = this;

return Repository.open(reposPath).then(function(repo) {
test.repo = repo;

return repo;
});
});

it("can get a tag from a repo via the tag name", function() {
return this.repo.getTagByName(tagName).then(function(tag) {
testTag(tag);
});
});

it("can get a tag from a repo via the long tag name", function() {
return this.repo.getTagByName(tagFullName).then(function(tag) {
testTag(tag);
});
});

it("can get a tag from a repo via the tag's OID as a string", function() {
return this.repo.getTag(tagOid).then(function(tag) {
testTag(tag);
});
});

it("can get a tag from a repo via the tag's OID object", function() {
var oid = Oid.fromString(tagOid);

return this.repo.getTag(oid).then(function(tag) {
testTag(tag);
});
});
});