Skip to content

Commit ca5a8b8

Browse files
committed
Fixed test repo, created commit tests
1 parent 0daef54 commit ca5a8b8

9 files changed

Lines changed: 147 additions & 51 deletions

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,11 @@ Release information
8787

8888
### v0.0.2: ###
8989
* Write capabilities
90+
* GitHub landing page
9091

9192
### v0.0.3: ###
9293
* Custom odb backend
94+
* API coverage in GitHub Wiki
9395

9496
Getting involved
9597
----------------

example/convenience-repo.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ var git = require( 'nodegit2' );
33
git.repo( '.git', function( err, path ) {
44
if(err === 0) {
55
console.log( 'Successfully loaded repository.' );
6-
this.commit( '5f2aa9407f7b3aeb531c621c3358953841ccfc98' );
6+
this.commit( '5f2aa9407f7b3aeb531c621c3358953841ccfc98', function( err ) {
7+
console.log( err );
8+
});
79
}
810
});

lib/repo.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@ var Repo = function( path, callback ) {
1010
var oid = new git2.Oid(),
1111
commit = new git2.Commit();
1212

13-
console.log( oid.mkstr( sha ) );
13+
oid.mkstr( sha );
1414

15-
callback && callback.appy( self, arguments );
15+
commit.lookup( self.repo, oid, function() {
16+
callback && callback.apply( self, arguments );
17+
});
1618
};
1719

1820
self.init = function( path, is_bare, callback ) {

src/commit.cc

Lines changed: 56 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,17 +44,67 @@ Handle<Value> Commit::New(const Arguments& args) {
4444

4545
Handle<Value> Commit::Lookup(const Arguments& args) {
4646
Commit *commit = ObjectWrap::Unwrap<Commit>(args.This());
47+
Local<Function> callback;
4748

4849
HandleScope scope;
4950

50-
//if(args.Length() == 0 || !args[0]->IsString()) {
51-
// return ThrowException(Exception::Error(String::New("Object id is required and must be a hex formatted String.")));
52-
//}
51+
if(args.Length() == 0 || !args[0]->IsObject()) {
52+
return ThrowException(Exception::Error(String::New("Repo is required and must be an Object.")));
53+
}
5354

54-
Repo *repo = ObjectWrap::Unwrap<Repo>(args[0]->ToObject());
55-
Oid *oid = ObjectWrap::Unwrap<Oid>(args[1]->ToObject());
55+
if(args.Length() == 1 || !args[1]->IsObject()) {
56+
return ThrowException(Exception::Error(String::New("Oid is required and must be an Object.")));
57+
}
5658

57-
return Local<Value>::New( Integer::New(commit->Lookup(repo, oid)) );
59+
if(args.Length() == 2 || !args[2]->IsFunction()) {
60+
return ThrowException(Exception::Error(String::New("Callback is required and must be a Function.")));
61+
}
62+
63+
lookup_request *ar = new lookup_request();
64+
callback = Local<Function>::Cast(args[2]);
65+
ar->commit = commit;
66+
ar->repo = ObjectWrap::Unwrap<Repo>(args[0]->ToObject());
67+
ar->oid = ObjectWrap::Unwrap<Oid>(args[1]->ToObject());
68+
ar->callback = Persistent<Function>::New(callback);
69+
70+
commit->Ref();
71+
72+
eio_custom(EIO_Lookup, EIO_PRI_DEFAULT, EIO_AfterLookup, ar);
73+
ev_ref(EV_DEFAULT_UC);
74+
75+
return Undefined();
5876
}
5977

78+
int Commit::EIO_Lookup(eio_req *req) {
79+
lookup_request *ar = static_cast<lookup_request *>(req->data);
80+
81+
ar->err = Persistent<Value>::New(Integer::New(ar->commit->Lookup(ar->repo, ar->oid)));
82+
83+
return 0;
84+
}
85+
86+
int Commit::EIO_AfterLookup(eio_req *req) {
87+
HandleScope scope;
88+
89+
lookup_request *ar = static_cast<lookup_request *>(req->data);
90+
ev_unref(EV_DEFAULT_UC);
91+
ar->commit->Unref();
92+
93+
Local<Value> argv[1];
94+
argv[0] = Number::Cast(*ar->err);
95+
96+
TryCatch try_catch;
97+
98+
ar->callback->Call(Context::GetCurrent()->Global(), 1, argv);
99+
100+
if(try_catch.HasCaught())
101+
FatalException(try_catch);
102+
103+
ar->err.Dispose();
104+
ar->callback.Dispose();
105+
106+
delete ar;
107+
108+
return 0;
109+
}
60110
Persistent<FunctionTemplate> Commit::constructor_template;

src/commit.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,21 @@ class Commit : public EventEmitter {
2727
Commit() {}
2828
~Commit() {}
2929
static Handle<Value> New(const Arguments& args);
30+
3031
static Handle<Value> Lookup(const Arguments& args);
32+
static int EIO_Lookup(eio_req *req);
33+
static int EIO_AfterLookup(eio_req *req);
3134

3235
private:
3336
git_commit *commit;
3437
};
3538

39+
struct lookup_request {
40+
Commit *commit;
41+
Repo *repo;
42+
Oid *oid;
43+
Persistent<Value> err;
44+
Persistent<Function> callback;
45+
};
46+
3647
#endif

src/repo.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,14 @@ Handle<Value> Repo::Open(const Arguments& args) {
7272
}
7373

7474
if(args.Length() == 1 || !args[1]->IsFunction()) {
75-
return ThrowException(Exception::Error(String::New("Callback must be a Function.")));
75+
return ThrowException(Exception::Error(String::New("Callback is required and must be a Function.")));
7676
}
7777

7878
callback = Local<Function>::Cast(args[1]);
7979

8080
open_request *ar = new open_request();
8181
ar->repo = repo;
82-
ar->path = Persistent<Value>::New( args[0] );
82+
ar->path = Persistent<Value>::New(args[0]);
8383
ar->callback = Persistent<Function>::New(callback);
8484

8585
repo->Ref();
@@ -151,7 +151,7 @@ Handle<Value> Repo::Init(const Arguments& args) {
151151
}
152152

153153
if(args.Length() == 2 || !args[2]->IsFunction()) {
154-
return ThrowException(Exception::Error(String::New("Callback must be a Function.")));
154+
return ThrowException(Exception::Error(String::New("Callback is required and must be a Function.")));
155155
}
156156

157157
callback = Local<Function>::Cast(args[2]);
@@ -175,7 +175,7 @@ int Repo::EIO_Init(eio_req *req) {
175175

176176
String::Utf8Value path(ar->path);
177177
Local<Boolean> is_bare = ar->is_bare->ToBoolean();
178-
ar->err = Persistent<Value>::New( Integer::New(ar->repo->Init(*path, *is_bare)) );
178+
ar->err = Persistent<Value>::New(Integer::New(ar->repo->Init(*path, *is_bare)));
179179

180180
return 0;
181181
}

test/dummyrepo

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Subproject commit 978feacee2432e67051f2714ec7d28ad80e16908

test/index.js

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,39 @@
33
require.paths.unshift( '../vendor' );
44

55
try {
6-
var reporter = require( '../vendor/nodeunit' ).reporters.default;
6+
var reporter = require( '../vendor/nodeunit' ).reporters.default;
77
}
88
catch(e) {
9-
var sys = require( 'sys' );
10-
sys.puts( 'Cannot find nodeunit module.' );
11-
sys.puts( 'You can download submodules for this project by doing:' );
12-
sys.puts( '' );
13-
sys.puts( ' git submodule init vendor/nodeunit' );
14-
sys.puts( ' git submodule update vendor/nodeunit' );
15-
sys.puts( '' );
16-
process.exit();
9+
var sys = require( 'sys' );
10+
sys.puts( 'Cannot find nodeunit module.' );
11+
sys.puts( 'You can download submodules for this project by doing:' );
12+
sys.puts( '' );
13+
sys.puts( ' git submodule init vendor/nodeunit' );
14+
sys.puts( ' git submodule update vendor/nodeunit' );
15+
sys.puts( '' );
16+
process.exit();
1717
}
1818

1919
try {
20-
var rimraf = require( '../vendor/rimraf' );
20+
var rimraf = require( '../vendor/rimraf' );
2121
}
2222
catch(e) {
23-
var sys = require( 'sys' );
24-
sys.puts( 'Cannot find rimraf module.' );
25-
sys.puts( 'You can download submodules for this project by doing:' );
26-
sys.puts( '' );
27-
sys.puts( ' git submodule init vendor/rimraf' );
28-
sys.puts( ' git submodule update vendor/rimraf' );
29-
sys.puts( '' );
30-
process.exit();
23+
var sys = require( 'sys' );
24+
sys.puts( 'Cannot find rimraf module.' );
25+
sys.puts( 'You can download submodules for this project by doing:' );
26+
sys.puts( '' );
27+
sys.puts( ' git submodule init vendor/rimraf' );
28+
sys.puts( ' git submodule update vendor/rimraf' );
29+
sys.puts( '' );
30+
process.exit();
3131
}
3232

33-
process.chdir( './' );
34-
reporter.run( ['test'] );
33+
process.chdir( './test' );
34+
reporter.run(
35+
[
36+
'raw-repo.js',
37+
'raw-oid.js',
38+
'raw-commit.js',
39+
'convenience-repo.js'
40+
]
41+
);

test/raw-commit.js

Lines changed: 39 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
var git = require( 'nodegit2' ).git2,
22
rimraf = require( '../vendor/rimraf');
33

4+
var testRepo = new git.Repo();
5+
46
// Helper functions
57
var helper = {
68
// Test if obj is a true function
@@ -37,28 +39,47 @@ exports.constructor = function( test ){
3739

3840
// Oid::Mkstr
3941
exports.lookup = function( test ) {
40-
var testCommit = new git.Commit();
42+
var testOid = new git.Oid(),
43+
testCommit = new git.Commit();
4144

42-
test.expect( 2 );
45+
test.expect( 8 );
4346

4447
// Test for function
4548
helper.testFunction( test.equals, testCommit.lookup, 'Commit::Lookup' );
4649

47-
// Test path argument existence
48-
// helper.testException( test.ok, function() {
49-
// testOid.mkstr();
50-
// }, 'Throw an exception if no hex String' );
51-
//
52-
// // Test that both arguments result correctly
53-
// helper.testException( test.ifError, function() {
54-
// testOid.mkstr( "somestr" );
55-
// }, 'No exception is thrown with proper arguments' );
56-
//
57-
// // Test invalid hex id string
58-
// test.equals( -2, testOid.mkstr( '1392DLFJIOS' ), 'Invalid hex id String' );
59-
//
60-
// // Test valid hex id string
61-
// test.equals( 0, testOid.mkstr( '1810DFF58D8A660512D4832E740F692884338CCD' ), 'Valid hex id String' );
50+
// Test repo argument existence
51+
helper.testException( test.ok, function() {
52+
testCommit.lookup();
53+
}, 'Throw an exception if no repo' );
54+
55+
// Test oid argument existence
56+
helper.testException( test.ok, function() {
57+
testCommit.lookup( testRepo );
58+
}, 'Throw an exception if no oid' );
6259

63-
test.done();
60+
// Test callback argument existence
61+
helper.testException( test.ok, function() {
62+
testCommit.lookup( testRepo, testOid );
63+
}, 'Throw an exception if no callback' );
64+
65+
// Test that both arguments result correctly
66+
helper.testException( test.ifError, function() {
67+
testCommit.lookup( testRepo, testOid, function() {} );
68+
}, 'No exception is thrown with proper arguments' );
69+
70+
testRepo.open( './dummyrepo/.git', function( err, path ) {
71+
// Test invalid commit
72+
testOid.mkstr( '100644' );
73+
testCommit.lookup( testRepo, testOid, function( err ) {
74+
test.notEqual( 0, err, 'Not a valid commit');
75+
76+
// Test valid commit
77+
testOid.mkstr( '978feacee2432e67051f2714ec7d28ad80e16908' );
78+
testCommit.lookup( testRepo, testOid, function( err ) {
79+
test.equals( 0, err, 'Valid commit');
80+
81+
test.done();
82+
});
83+
});
84+
});
6485
};

0 commit comments

Comments
 (0)