Skip to content

Commit 6d77a5c

Browse files
committed
Greatly improved looking tests
1 parent 05efbc4 commit 6d77a5c

File tree

5 files changed

+55
-89
lines changed

5 files changed

+55
-89
lines changed

lib/repository.js

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,16 @@ util.makeSafe(Repo.prototype, 'getReference');
7777
* @return {Commit}
7878
*/
7979
Repo.prototype.getCommit = function(oid, callback) {
80-
var self = this;
81-
Commit.lookup(this, oid, function(error, commit) {
82-
if (error) return callback(error);
83-
commit.repo = self;
84-
callback(null, commit);
80+
var repository = this;
81+
82+
return Commit.lookup(repository, oid).then(function(commit) {
83+
commit.repo = repository;
84+
85+
if (callback) {
86+
callback(null, commit);
87+
}
88+
89+
return commit;
8590
});
8691
};
8792
util.normalizeOid(Repo.prototype, 'getCommit');
@@ -95,12 +100,17 @@ util.makeSafe(Repo.prototype, 'getCommit');
95100
* @return {Blob}
96101
*/
97102
Repo.prototype.getBlob = function(oid, callback) {
98-
var self = this;
99-
Blob.lookup(this, oid, function(error, blob) {
100-
if (error) return callback(error);
101-
blob.repo = self;
102-
callback(null, blob);
103-
});
103+
var repository= this;
104+
105+
return Blob.lookup(repository, oid).then(function(blob) {
106+
blob.repo = repository;
107+
108+
if (callback) {
109+
callback(null, blob);
110+
}
111+
112+
return blob;
113+
}, callback);
104114
};
105115
util.normalizeOid(Repo.prototype, 'getBlob');
106116
util.makeSafe(Repo.prototype, 'getBlob');

lib/util.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ exports.makeSafe = function(object, key) {
44
var oldFn = object[key];
55
object[key] = function() {
66
try {
7-
oldFn.apply(this, arguments);
7+
return oldFn.apply(this, arguments);
88
} catch (e) {
99
var callback = arguments[arguments.length - 1];
1010
callback(e);
@@ -16,6 +16,6 @@ exports.normalizeOid = function(object, key) {
1616
var oldFn = object[key];
1717
object[key] = function(oid) {
1818
if (typeof oid === 'string') oid = git.Oid.fromString(oid);
19-
oldFn.apply(this, arguments);
19+
return oldFn.apply(this, arguments);
2020
};
2121
};

test/tests/blob.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,19 @@ describe("Blob", function() {
99
var Oid = nodegit.Oid;
1010
var Repository = nodegit.Repository;
1111

12-
it("can fetch content from a commit", function(done) {
13-
var oid= Oid.fromstr("111dd657329797f6165f52f5085f61ac976dcf04");
12+
before(function() {
13+
var test = this;
1414

15-
Repository.open(reposPath, function(err, repository) {
16-
repository.getBlob(oid, function(err, blob) {
17-
var contents = blob.toString();
15+
return Repository.open(reposPath).then(function(repository) {
16+
test.repository = repository;
17+
});
18+
});
1819

19-
assert.equal(contents.slice(0, 7), "@import");
20+
it("can fetch content from a commit", function() {
21+
var oid= Oid.fromstr("111dd657329797f6165f52f5085f61ac976dcf04");
2022

21-
done();
22-
});
23+
return this.repository.getBlob(oid).then(function(blob) {
24+
assert.equal(blob.toString().slice(0, 7), "@import");
2325
});
2426
});
2527
});

test/tests/commit.js

Lines changed: 22 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -7,77 +7,40 @@ var nodegit = require("../../");
77
var Repository = nodegit.Repository;
88

99
describe("Commit", function() {
10-
var historyCountKnownSHA = "fce88902e66c72b5b93e75bdb5ae717038b221f6";
1110
var reposPath = path.resolve("test/repos/workdir/.git");
11+
var historyCountKnownSHA = "fce88902e66c72b5b93e75bdb5ae717038b221f6";
1212

1313
var Commit = require("./commit");
1414

15-
describe("when fetched", function() {
16-
17-
it("makes its message available", function(done) {
18-
Repository.open(reposPath, function(error, repository) {
19-
repository.getCommit(historyCountKnownSHA, function(error, commit) {
20-
var message = commit.message();
21-
22-
assert.equal(error, null);
23-
assert.equal(message, "Update README.md");
24-
25-
done();
26-
});
27-
});
28-
});
29-
30-
it("makes its sha available", function(done) {
31-
Repository.open(reposPath, function(error, repository) {
32-
repository.getCommit(historyCountKnownSHA, function(error, commit) {
33-
var sha = commit.sha();
34-
35-
assert.equal(error, null);
36-
assert.equal(sha, historyCountKnownSHA);
37-
38-
done();
39-
});
40-
});
41-
});
42-
43-
it("makes its time available", function(done) {
44-
Repository.open(reposPath, function(error, repository) {
45-
repository.getCommit(historyCountKnownSHA, function(error, commit) {
46-
var time = commit.timeMs();
15+
beforeEach(function() {
16+
var test = this;
4717

48-
assert.equal(error, null);
49-
assert.equal(time, 1362012884000);
18+
return Repository.open(reposPath).then(function(repository) {
19+
test.repository = repository;
5020

51-
done();
52-
});
21+
return repository.getCommit(historyCountKnownSHA).then(function(commit) {
22+
test.commit = commit;
5323
});
5424
});
25+
});
5526

56-
it("makes its date available", function(done) {
57-
Repository.open(reposPath, function(error, repository) {
58-
repository.getCommit(historyCountKnownSHA, function(error, commit) {
59-
var date = commit.date();
60-
61-
assert.equal(error, null);
62-
assert.equal(date.getTime(), 1362012884000);
63-
64-
done();
65-
});
66-
});
67-
});
27+
it("makes its message available", function() {
28+
assert.equal(this.commit.message(), "Update README.md");
29+
});
6830

69-
it("makes its offset available", function(done) {
70-
Repository.open(reposPath, function(error, repository) {
71-
repository.getCommit(historyCountKnownSHA, function(error, commit) {
72-
var offset = commit.offset();
31+
it("makes its sha available", function() {
32+
assert.equal(this.commit.sha(), historyCountKnownSHA);
33+
});
7334

74-
assert.equal(error, null);
75-
assert.equal(offset, 780);
35+
it("makes its time available", function() {
36+
assert.equal(this.commit.timeMs(), 1362012884000);
37+
});
7638

77-
done();
78-
});
79-
});
80-
});
39+
it("makes its date available", function() {
40+
assert.equal(this.commit.date().getTime(), 1362012884000);
41+
});
8142

43+
it("makes its offset available", function() {
44+
assert.equal(this.commit.offset(), 780);
8245
});
8346
});

test/tests/repository.js

Lines changed: 0 additions & 9 deletions
This file was deleted.

0 commit comments

Comments
 (0)