Skip to content

Commit 0cff089

Browse files
authored
Merge pull request #1181 from rcjsuen/branch-tests
Added tests for Branch
2 parents ff71e77 + b88d332 commit 0cff089

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

generate/input/descriptor.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@
183183
}
184184
},
185185
"git_branch_create_from_annotated": {
186+
"isAsync": true,
186187
"args": {
187188
"ref_out": {
188189
"isReturn": true
@@ -193,6 +194,9 @@
193194
"force": {
194195
"isOptional": true
195196
}
197+
},
198+
"return": {
199+
"isErrorCode": true
196200
}
197201
},
198202
"git_branch_next": {

test/tests/branch.js

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,14 @@ describe("Branch", function() {
66
var NodeGit = require("../../");
77
var Repository = NodeGit.Repository;
88
var Branch = NodeGit.Branch;
9+
var AnnotatedCommit = NodeGit.AnnotatedCommit;
910
var branchName = "test-branch";
11+
var branchName2 = "test-branch2";
1012
var fullBranchName = "refs/heads/" + branchName;
13+
var fullBranchName2 = "refs/heads/" + branchName2;
1114
var upstreamName = "origin/master";
15+
var fullUpstreamName = "refs/remotes/origin/master";
16+
var nonHeadCommit = "c82fb078a192ea221c9f1093c64321c60d64aa0d";
1217

1318
var reposPath = local("../repos/workdir");
1419

@@ -27,6 +32,8 @@ describe("Branch", function() {
2732
})
2833
.then(function(branch) {
2934
test.branch = branch;
35+
return test.repository.createBranch(
36+
branchName2, test.masterCommit, true);
3037
});
3138
});
3239

@@ -77,4 +84,63 @@ describe("Branch", function() {
7784
assert.equal(branchNameToTest, branchName);
7885
});
7986
});
87+
88+
it("can rename a branch", function() {
89+
var branch = this.branch;
90+
91+
// don't force the move
92+
return Branch.move(branch, branchName2, 0)
93+
.then(function(branch) {
94+
return Promise.reject(new Error(
95+
"should not be able to rename the branch"));
96+
}, function(error) {
97+
return Promise.resolve()
98+
.then(function() {
99+
// force the move
100+
return Branch.move(branch, branchName2, 1);
101+
})
102+
.then(function(branch) {
103+
assert.equal(branch.name(), fullBranchName2);
104+
});
105+
});
106+
});
107+
108+
it("can lookup a branch", function() {
109+
var repo = this.repository;
110+
111+
return Branch.lookup(repo, branchName, Branch.BRANCH.LOCAL)
112+
.then(function(branch) {
113+
assert.equal(branch.name(), fullBranchName);
114+
return Branch.lookup(repo, upstreamName, Branch.BRANCH.REMOTE);
115+
})
116+
.then(function(branch) {
117+
assert.equal(branch.name(), fullUpstreamName);
118+
});
119+
});
120+
121+
it("can create branch from annotated commit", function() {
122+
var repo = this.repository;
123+
var annotatedCommit = null;
124+
125+
return AnnotatedCommit.fromRevspec(repo, nonHeadCommit)
126+
.then(function(theAnnotatedCommit) {
127+
annotatedCommit = theAnnotatedCommit;
128+
return Branch.createFromAnnotated(
129+
repo, branchName, annotatedCommit, 0);
130+
})
131+
.then(function(branch) {
132+
return Promise.reject(new Error(
133+
"should not be able to create the branch"));
134+
}, function(error) {
135+
return Promise.resolve()
136+
.then(function() {
137+
// force the branch creation
138+
return Branch.createFromAnnotated(
139+
repo, branchName, annotatedCommit, 1);
140+
})
141+
.then(function(branch) {
142+
assert.equal(branch.name(), fullBranchName);
143+
});
144+
});
145+
});
80146
});

0 commit comments

Comments
 (0)