Skip to content

Commit 7fa0554

Browse files
committed
Tidy up tests
1 parent 01a711e commit 7fa0554

File tree

8 files changed

+160
-104
lines changed

8 files changed

+160
-104
lines changed

test/index.js

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
1+
var fork = require("child_process").fork;
2+
var path = require("path");
3+
var local = path.join.bind(path, __dirname);
4+
15
var args = [
2-
"cover",
3-
process.platform != "win32" ? "_mocha" : "../node_modules/mocha/bin/_mocha",
4-
"--",
5-
"runner",
6-
"tests",
7-
"--report=lcov",
8-
"--expose-gc"
9-
];
6+
"cover",
7+
process.platform != "win32" ?
8+
"_mocha" :
9+
local("../node_modules/mocha/bin/_mocha"),
10+
"--",
11+
"runner",
12+
"tests",
13+
"--report=lcov",
14+
"--expose-gc"
15+
];
1016

11-
require("child_process").fork("../node_modules/istanbul/lib/cli.js", args, {
17+
fork(local("../node_modules/istanbul/lib/cli.js"), args, {
1218
cwd: __dirname
1319
}).on("close", function(code) {
1420
process.exit(code);

test/runner.js

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,40 @@ var exec = promisify(function(command, opts, callback) {
88
return require("child_process").exec(command, opts, callback);
99
});
1010

11+
var workdirPath = local("repos/workdir");
12+
1113
before(function() {
1214
this.timeout(350000);
1315

1416
var url = "https://github.com/nodegit/test";
15-
fse.removeSync(local("repos"));
16-
17-
return fse.mkdir(local("repos"))
17+
return fse.remove(local("repos"))
18+
.then(function() {
19+
fse.mkdir(local("repos"));
20+
})
1821
.then(function() {
1922
return exec("git init " + local("repos", "empty"));
2023
})
2124
.then(function() {
22-
return exec("git clone " + url + " " + local("repos", "workdir"));
23-
}).then(function() {
24-
return exec("git checkout rev-walk", {cwd: local("repos", "workdir")})
25-
}).then(function() {
26-
return exec("git checkout master", {cwd: local("repos", "workdir")})
27-
}).then(function() {
25+
return exec("git clone " + url + " " + workdirPath);
26+
})
27+
.then(function() {
28+
return exec("git checkout rev-walk", {cwd: workdirPath});
29+
})
30+
.then(function() {
31+
return exec("git checkout master", {cwd: workdirPath});
32+
})
33+
.then(function() {
2834
return fse.mkdir(local("repos", "nonrepo"));
29-
}).then(function() {
35+
})
36+
.then(function() {
3037
return fse.writeFile(local("repos", "nonrepo", "file.txt"),
3138
"This is a bogus file");
3239
});
3340
});
41+
42+
beforeEach(function() {
43+
return exec("git clean -xdf", {cwd: workdirPath})
44+
.then(function() {
45+
return exec("git reset --hard", {cwd: workdirPath});
46+
});
47+
});

test/tests/clone.js

Lines changed: 17 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
var assert = require("assert");
21
var path = require("path");
3-
var Promise = require("nodegit-promise");
2+
var assert = require("assert");
43
var promisify = require("promisify-node");
54
var fse = promisify(require("fs-extra"));
65
var local = path.join.bind(path, __dirname);
7-
var fixAppveyor = process.env.APPVEYOR ? describe.skip : describe;
86

9-
fixAppveyor("Clone", function() {
7+
describe("Clone", function() {
108
var http = local("../repos/http");
119
var https = local("../repos/https");
1210
var ssh = local("../repos/ssh");
@@ -24,32 +22,28 @@ fixAppveyor("Clone", function() {
2422
// Set a reasonable timeout here now that our repository has grown.
2523
this.timeout(15000);
2624

27-
before(function() {
28-
return Promise.all([
29-
fse.remove(http),
30-
fse.remove(https),
31-
fse.remove(ssh),
32-
fse.remove(git),
33-
fse.remove(file),
34-
]);
35-
});
25+
function prepTestAndClean(url, location, opts) {
26+
return fse.remove(location)
27+
.then(function() {
28+
return Clone.clone(url, location, opts);
29+
})
30+
.then(function(repo) {
31+
assert.ok(repo instanceof Repository);
32+
});
33+
}
3634

3735
it("can clone with http", function() {
3836
var url = "http://github.com/nodegit/test.git";
3937
var opts = { ignoreCertErrors: 1 };
4038

41-
return Clone.clone(url, http, opts).then(function(repository) {
42-
assert.ok(repository instanceof Repository);
43-
});
39+
return prepTestAndClean(url, http, opts);
4440
});
4541

4642
it("can clone with https", function() {
4743
var url = "https://github.com/nodegit/test.git";
4844
var opts = { ignoreCertErrors: 1 };
4945

50-
return Clone.clone(url, https, opts).then(function(repository) {
51-
assert.ok(repository instanceof Repository);
52-
});
46+
return prepTestAndClean(url, https, opts);
5347
});
5448

5549
it("can clone with ssh", function() {
@@ -63,9 +57,7 @@ fixAppveyor("Clone", function() {
6357
}
6458
};
6559

66-
return Clone.clone(url, ssh, opts).then(function(repository) {
67-
assert.ok(repository instanceof Repository);
68-
});
60+
return prepTestAndClean(url, ssh, opts);
6961
});
7062

7163
it("can clone with ssh while manually loading a key", function() {
@@ -83,26 +75,20 @@ fixAppveyor("Clone", function() {
8375
}
8476
};
8577

86-
return Clone.clone(url, sshManual, opts).then(function(repository) {
87-
assert.ok(repository instanceof Repository);
88-
});
78+
return prepTestAndClean(url, sshManual, opts);
8979
});
9080

9181
it("can clone with git", function() {
9282
var url = "git://github.com/nodegit/test.git";
9383
var opts = { ignoreCertErrors: 1 };
9484

95-
return Clone.clone(url, git, opts).then(function(repository) {
96-
assert.ok(repository instanceof Repository);
97-
});
85+
return prepTestAndClean(url, git, opts);
9886
});
9987

10088
it("can clone with filesystem", function() {
10189
var prefix = process.platform === "win32" ? "" : "file://";
10290
var url = prefix + local("../repos/empty");
10391

104-
return Clone.clone(url, file).then(function(repository) {
105-
assert.ok(repository instanceof Repository);
106-
});
92+
return prepTestAndClean(url, file);
10793
});
10894
});

test/tests/commit.js

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,20 @@ describe("Commit", function() {
1212
var reposPath = local("../repos/workdir/.git");
1313
var oid = "fce88902e66c72b5b93e75bdb5ae717038b221f6";
1414

15-
beforeEach(function() {
16-
var test = this;
17-
18-
return Repository.open(reposPath).then(function(repository) {
19-
test.repository = repository;
20-
21-
return repository.getCommit(oid).then(function(commit) {
15+
function reinitialize(test) {
16+
return Repository.open(reposPath)
17+
.then(function(repository) {
18+
test.repository = repository;
19+
20+
return repository.getCommit(oid);
21+
})
22+
.then(function(commit) {
2223
test.commit = commit;
2324
});
24-
});
25+
}
26+
27+
before(function() {
28+
return reinitialize(this);
2529
});
2630

2731
it("will fail with an invalid sha", function() {
@@ -64,6 +68,7 @@ describe("Commit", function() {
6468
});
6569

6670
it("can create a commit", function() {
71+
var test = this;
6772
var expectedCommitId = "315e77328ef596f3bc065d8ac6dd2c72c09de8a5";
6873
var fileName = "newfile.txt";
6974
var fileContent = "hello world";
@@ -123,8 +128,12 @@ describe("Commit", function() {
123128
})
124129
.then(function(commitId) {
125130
assert.equal(expectedCommitId, commitId);
126-
}, function(rejected) {
127-
assert.fail();
131+
return reinitialize(test);
132+
}, function(reason) {
133+
return reinitialize(test)
134+
.then(function() {
135+
return Promise.reject();
136+
});
128137
});
129138
});
130139

@@ -148,23 +157,23 @@ describe("Commit", function() {
148157

149158
describe("committer", function() {
150159
before(function() {
151-
this.author = this.commit.committer();
160+
this.committer = this.commit.committer();
152161
});
153162

154163
it("is available", function() {
155-
assert.ok(this.author instanceof NodeGit.Signature);
164+
assert.ok(this.committer instanceof NodeGit.Signature);
156165
});
157166

158167
it("has a name", function() {
159-
assert.equal(this.author.name(), "Michael Robinson");
168+
assert.equal(this.committer.name(), "Michael Robinson");
160169
});
161170

162171
it("has an email", function() {
163-
assert.equal(this.author.email(), "mike@panmedia.co.nz");
172+
assert.equal(this.committer.email(), "mike@panmedia.co.nz");
164173
});
165174
});
166175

167-
it("has owner", function() {
176+
it("has an owner", function() {
168177
var owner = this.commit.owner();
169178
assert.ok(owner instanceof Repository);
170179
});

test/tests/diff.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
var assert = require("assert");
22
var path = require("path");
33
var promisify = require("promisify-node");
4+
var Promise = require("nodegit-promise");
45
var fse = promisify(require("fs-extra"));
56
var local = path.join.bind(path, __dirname);
67

@@ -35,7 +36,8 @@ describe("Diff", function() {
3536
test.commit = commit;
3637

3738
return commit.getDiff();
38-
}).then(function(diff) {
39+
})
40+
.then(function(diff) {
3941
test.diff = diff;
4042

4143
return fse.writeFile(diffFilepath, "1 line\n2 line\n3 line\n\n4");
@@ -51,13 +53,18 @@ describe("Diff", function() {
5153
})
5254
.then(function(workdirDiff) {
5355
test.workdirDiff = workdirDiff;
56+
})
57+
.then(function() {
58+
return fse.remove(diffFilepath);
59+
})
60+
.catch(function(e) {
61+
return fse.remove(diffFilepath)
62+
.then(function() {
63+
return Promise.reject(e);
64+
});
5465
});
5566
});
5667

57-
after(function() {
58-
return fse.unlink(diffFilepath);
59-
});
60-
6168
it("can walk a DiffList", function() {
6269
var patch = this.diff[0].patches()[0];
6370

test/tests/index.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,18 @@ describe("Index", function() {
1010
before(function() {
1111
var test = this;
1212

13-
return Repository.open(reposPath).then(function(repo) {
14-
test.repo = repo;
15-
16-
return repo.openIndex().then(function(index) {
13+
return Repository.open(reposPath)
14+
.then(function(repo) {
15+
test.repo = repo;
16+
return repo.openIndex();
17+
})
18+
.then(function(index) {
1719
test.index = index;
18-
19-
return index;
2020
});
21-
});
21+
});
22+
23+
after(function() {
24+
this.index.clear();
2225
});
2326

2427
it("can get the index of a repo and examine entries", function() {

test/tests/repository.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
var assert = require("assert");
22
var path = require("path");
33
var promisify = require("promisify-node");
4+
var Promise = require("nodegit-promise");
45
var fse = promisify(require("fs-extra"));
56
var local = path.join.bind(path, __dirname);
67

@@ -78,8 +79,8 @@ describe("Repository", function() {
7879
});
7980

8081
it("gets statuses with StatusFile", function() {
81-
var fileName = "my-new-file-that-shouldnt-exist";
82-
var fileContent = "new file";
82+
var fileName = "my-new-file-that-shouldnt-exist.file";
83+
var fileContent = "new file from repository test";
8384
var repo = this.repository;
8485
var filePath = path.join(repo.workdir(), fileName);
8586

@@ -89,8 +90,15 @@ describe("Repository", function() {
8990
assert.equal(statuses.length, 1);
9091
assert.equal(statuses[0].path(), fileName);
9192
assert.ok(statuses[0].isNew());
92-
return fse.unlink(filePath);
9393
});
94+
})
95+
.then(function() {
96+
return fse.remove(filePath);
97+
}, function (e) {
98+
return fse.remove(filePath)
99+
.then(function() {
100+
return Promise.reject(e);
101+
});
94102
});
95103
});
96104
});

0 commit comments

Comments
 (0)