Skip to content

Commit 26ab62b

Browse files
committed
Test for Index.conflictAdd
1 parent 53d38e9 commit 26ab62b

File tree

2 files changed

+154
-0
lines changed

2 files changed

+154
-0
lines changed

test/tests/index.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ var writeFile = promisify(function(filename, data, callback) {
99
});
1010

1111
describe("Index", function() {
12+
var IndexUtils = require("../utils/index_setup");
1213
var RepoUtils = require("../utils/repository_setup");
1314
var NodeGit = require("../../");
1415
var Repository = NodeGit.Repository;
@@ -307,4 +308,52 @@ describe("Index", function() {
307308
return Promise.all(promises);
308309
});
309310
});
311+
312+
it("can add a conflict to the index", function() {
313+
var repo;
314+
var repoPath = path.join(__dirname, "..", "repos", "index");
315+
var ourBranchName = "ours";
316+
var theirBranchName = "theirs";
317+
var fileName = "testFile.txt";
318+
var ancestorIndexEntry;
319+
var ourIndexEntry;
320+
var theirIndexEntry;
321+
322+
return RepoUtils.createRepository(repoPath)
323+
.then(function(_repo) {
324+
repo = _repo;
325+
return IndexUtils.createConflict(
326+
repo,
327+
ourBranchName,
328+
theirBranchName,
329+
fileName
330+
);
331+
})
332+
.then(function(indexEntries) {
333+
// Store all indexEntries for conflict
334+
ancestorIndexEntry = indexEntries.ancestor_out;
335+
ourIndexEntry = indexEntries.our_out;
336+
theirIndexEntry = indexEntries.their_out;
337+
338+
// Stage conflicted file
339+
return RepoUtils.addFileToIndex(repo, fileName);
340+
})
341+
.then(function() {
342+
return repo.openIndex();
343+
})
344+
.then(function(index) {
345+
assert.ok(!index.hasConflicts());
346+
return index.conflictAdd(
347+
ancestorIndexEntry,
348+
ourIndexEntry,
349+
theirIndexEntry
350+
);
351+
})
352+
.then(function() {
353+
return repo.openIndex();
354+
})
355+
.then(function(index) {
356+
assert(index.hasConflicts());
357+
});
358+
});
310359
});

test/utils/index_setup.js

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
var assert = require("assert");
2+
var NodeGit = require("../../");
3+
var path = require("path");
4+
var promisify = require("promisify-node");
5+
var fse = promisify(require("fs-extra"));
6+
var RepoUtils = require("../utils/repository_setup");
7+
8+
var IndexSetup = {
9+
createConflict: function createConflict(
10+
repository,
11+
_ourBranchName,
12+
_theirBranchName,
13+
_fileName
14+
) {
15+
var fileName = _fileName || "everyonesFile.txt";
16+
17+
var ourBranchName = _ourBranchName || "ours";
18+
var theirBranchName = _theirBranchName || "theirs";
19+
20+
var baseFileContent = "How do you feel about Toll Roads?\n";
21+
var ourFileContent = "I like Toll Roads. I have an EZ-Pass!\n";
22+
var theirFileContent = "I'm skeptical about Toll Roads\n";
23+
24+
var ourSignature = NodeGit.Signature.create
25+
("Ron Paul", "RonPaul@TollRoadsRBest.info", 123456789, 60);
26+
var theirSignature = NodeGit.Signature.create
27+
("Greg Abbott", "Gregggg@IllTollYourFace.us", 123456789, 60);
28+
29+
var ourCommit;
30+
var ourBranch;
31+
var theirBranch;
32+
33+
return fse.writeFile(
34+
path.join(repository.workdir(), fileName),
35+
baseFileContent
36+
)
37+
.then(function() {
38+
return RepoUtils.addFileToIndex(repository, fileName);
39+
})
40+
.then(function(oid) {
41+
return repository.createCommit("HEAD", ourSignature,
42+
ourSignature, "initial commit", oid, []);
43+
})
44+
.then(function(commitOid) {
45+
return repository.getCommit(commitOid).then(function(commit) {
46+
ourCommit = commit;
47+
}).then(function() {
48+
console.log("after creating base commit");
49+
return repository.createBranch(ourBranchName, commitOid)
50+
.then(function(branch) {
51+
console.log("after creating our branch");
52+
ourBranch = branch;
53+
return repository.createBranch(theirBranchName, commitOid);
54+
});
55+
});
56+
})
57+
.then(function(branch) {
58+
console.log("after creating their commit");
59+
60+
theirBranch = branch;
61+
return fse.writeFile(path.join(repository.workdir(), fileName),
62+
baseFileContent + theirFileContent);
63+
})
64+
.then(function() {
65+
return RepoUtils.addFileToIndex(repository, fileName);
66+
})
67+
.then(function(oid) {
68+
return repository.createCommit(theirBranch.name(), theirSignature,
69+
theirSignature, "they made a commit", oid, [ourCommit]);
70+
})
71+
.then(function(commitOid) {
72+
return fse.writeFile(path.join(repository.workdir(), fileName),
73+
baseFileContent + ourFileContent);
74+
})
75+
.then(function() {
76+
return RepoUtils.addFileToIndex(repository, fileName);
77+
})
78+
.then(function(oid) {
79+
return repository.createCommit(ourBranch.name(), ourSignature,
80+
ourSignature, "we made a commit", oid, [ourCommit]);
81+
})
82+
.then(function(commitOid) {
83+
var opts = {
84+
checkoutStrategy: NodeGit.Checkout.STRATEGY.FORCE
85+
};
86+
87+
return NodeGit.Checkout.head(repository, opts);
88+
})
89+
.then(function() {
90+
return repository.mergeBranches(ourBranchName, theirBranchName);
91+
})
92+
.then(function(commit) {
93+
assert.fail(commit, undefined,
94+
"The index should have been thrown due to merge conflicts");
95+
})
96+
.catch(function(index) {
97+
assert.ok(index);
98+
assert.ok(index.hasConflicts());
99+
100+
return index.conflictGet(fileName);
101+
});
102+
}
103+
};
104+
105+
module.exports = IndexSetup;

0 commit comments

Comments
 (0)