Skip to content

Commit eb00cd2

Browse files
kmctownmaxkorp
authored andcommitted
Diff test: use nodegit to add diff file to index
1 parent d55a6a3 commit eb00cd2

1 file changed

Lines changed: 37 additions & 11 deletions

File tree

test/tests/diff.js

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
11
var assert = require("assert");
22
var path = require("path");
3-
var fs = require("fs");
4-
var exec = require("child_process").exec;
3+
var promisify = require("promisify-node");
4+
var fse = promisify(require("fs-extra"));
55
var Diff = require("../../lib/diff");
66

77
describe("Diff", function() {
8+
var Repository = require("../../lib/repository");
89
var reposPath = path.resolve("test/repos/workdir/.git");
910
var oid = "fce88902e66c72b5b93e75bdb5ae717038b221f6";
10-
11-
var Repository = require("../../lib/repository");
11+
var diffFilename = "wddiff.txt";
12+
var diffFilepath = path.join(
13+
path.resolve("test/repos/workdir"),
14+
diffFilename
15+
);
1216

1317
before(function(done) {
1418
var test = this;
15-
var newFilePath = path.join(path.resolve("test/repos/workdir"), "wddiff.txt");
1619

1720
return Repository.open(reposPath).then(function(repository) {
1821
test.repository = repository;
@@ -27,13 +30,33 @@ describe("Diff", function() {
2730

2831
return commit.getDiff().then(function(diff) {
2932
test.diff = diff;
30-
fs.writeFileSync(newFilePath, "1 line\n2 line\n3 line\n\n4");
3133

32-
exec("git add wddiff.txt", {cwd: path.resolve("test/repos/workdir")}, function() {
33-
Diff.treeToWorkdirWithIndex(test.repository, test.masterCommitTree, null)
34+
fse.writeFile(diffFilepath, "1 line\n2 line\n3 line\n\n4")
35+
.then(function() {
36+
return test.repository.openIndex();
37+
})
38+
.then(function(indexResult) {
39+
test.index = indexResult;
40+
return test.index.read(1);
41+
})
42+
.then(function() {
43+
return test.index.addByPath(diffFilename);
44+
})
45+
.then(function() {
46+
return test.index.write();
47+
})
48+
.then(function() {
49+
return test.index.writeTree();
50+
})
51+
.then(function() {
52+
Diff.treeToWorkdirWithIndex(
53+
test.repository,
54+
test.masterCommitTree,
55+
null
56+
)
3457
.then(function(workdirDiff) {
35-
test.workdirDiff = workdirDiff;
36-
done();
58+
test.workdirDiff = workdirDiff;
59+
done();
3760
});
3861
});
3962
});
@@ -81,6 +104,9 @@ describe("Diff", function() {
81104
assert.equal(hunks.length, 1);
82105

83106
var lines = hunks[0].lines();
84-
assert.equal(lines[0].content().substr(0, lines[0].contentLen()), "1 line\n");
107+
assert.equal(
108+
lines[0].content().substr(0, lines[0].contentLen()),
109+
"1 line\n"
110+
);
85111
});
86112
});

0 commit comments

Comments
 (0)