Skip to content
This repository was archived by the owner on Feb 19, 2020. It is now read-only.

Commit d2ce47d

Browse files
committed
Added tests for handle-input to bring code coverage back up as far as possible (can't mock git CLI right now). Update other tests that use eventually.be.fulfilled to use the new eventually.be.fulfilled() to make sure they truly check async.
1 parent be6eb0a commit d2ce47d

File tree

8 files changed

+158
-25
lines changed

8 files changed

+158
-25
lines changed

.jscsrc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
"disallowSpacesInFunctionDeclaration": {
4040
"beforeOpeningRoundBrace": true
4141
},
42-
"requireMultipleVarDecl": true,
4342
"requireBlocksOnNewline": 1,
4443
"disallowEmptyBlocks": true,
4544
"disallowSpacesInsideArrayBrackets": true,

.jshintrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"strict": true,
1717
"eqnull": true,
1818
"node": true,
19+
"mocha": true,
1920
"predef": [
2021
"describe",
2122
"it",

bin/codacy-coverage.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
var opts = {
4545
endpoint: program.endpoint,
4646
token: program.token,
47-
commitId: program.commit,
47+
commit: program.commit,
4848
format: program.format,
4949
pathPrefix: program.prefix,
5050
verbose: program.verbose,

lib/handleInput.js

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,23 @@
33
module.exports = function (input, opts) {
44
opts = opts || {};
55

6-
var token = opts.token || process.env.CODACY_REPO_TOKEN,
7-
commitId = opts.commit,
8-
format = opts.format || 'lcov',
9-
pathPrefix = opts.prefix || '',
10-
loggerImpl;
6+
var token = opts.token || process.env.CODACY_REPO_TOKEN;
7+
var commit = opts.commit;
8+
var format = opts.format || 'lcov';
9+
var pathPrefix = opts.prefix || '';
10+
var loggerImpl;
1111

1212
loggerImpl = logger({
1313
verbose: opts.verbose,
1414
debug: opts.debug
1515
});
1616

1717
if (!token) {
18-
var err = new Error('Token is required');
19-
return Promise.reject(err);
18+
return Promise.reject(new Error('Token is required'));
2019
}
2120

2221
// Parse the coverage data for the given format and retrieve the commit id if we don't have it.
23-
return Promise.all([parser.getParser(format).parse(pathPrefix, input), getGitData.getCommitId(commitId)]).spread(function (parsedCoverage, commitId) {
22+
return Promise.all([parser.getParser(format).parse(pathPrefix, input), getGitData.getCommitId(commit)]).spread(function (parsedCoverage, commitId) {
2423
// Now that we've parse the coverage data to the correct format, send it to Codacy.
2524
loggerImpl.trace(parsedCoverage);
2625
loggerImpl.debug('Sending coverage');

test/getGitData.js

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,7 @@
66

77
describe('Get Git Data', function () {
88
beforeEach(function () {
9-
process.env.CODACY_GIT_COMMIT = '';
10-
process.env.TRAVIS_COMMIT = '';
11-
process.env.DRONE_COMMIT = '';
12-
process.env.GIT_COMMIT = '';
13-
process.env.CIRCLE_SHA1 = '';
14-
process.env.CI_COMMIT_ID = '';
15-
process.env.WERCKER_GIT_COMMIT = '';
9+
helper.clearEnvironmentVariables();
1610
});
1711
it('should be able to get the commit id when one is passed', function () {
1812
return expect(getGitData.getCommitId('1234')).to.eventually.equal('1234');
@@ -52,7 +46,7 @@
5246
if (actualTravisCommit && process.env.TRAVIS_PULL_REQUEST === 'false') {
5347
return expect(getGitData.getCommitId()).to.eventually.equal(actualTravisCommit);
5448
}
55-
return expect(getGitData.getCommitId()).to.eventually.be.fulfilled;
49+
return expect(getGitData.getCommitId()).to.eventually.be.fulfilled();
5650
});
5751
});
5852
}(require('util'), require('../lib/getGitData'), require('./helper')));

test/handleInput.js

Lines changed: 135 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,143 @@
1-
(function (handleInput, helper) {
1+
(function (handleInput, helper, Joi, request, fs, path) {
22
'use strict';
33

44
var expect = helper.chai.expect;
5+
var lcovData = fs.readFileSync(__dirname + '/mock/lcov.info').toString();
56

67
describe('Handle Input', function () {
7-
it('should return a promise', function () {
8-
return expect(handleInput()).to.eventually.be.rejected();
8+
beforeEach(function () {
9+
helper.clearEnvironmentVariables();
10+
});
11+
it('should be able to use the mock end-point', function () {
12+
var bodyValidator = Joi.object({
13+
total: Joi.number().valid(50),
14+
fileReports: Joi.array().items(Joi.object({
15+
filename: Joi.string().valid('filename'),
16+
total: Joi.number().valid(10),
17+
coverage: Joi.object({
18+
1: Joi.number().valid(1),
19+
2: Joi.number().valid(3)
20+
})
21+
}).required())
22+
});
23+
24+
var sampleCoverageData = {
25+
total: 50,
26+
fileReports: [
27+
{
28+
filename: 'filename',
29+
total: 10,
30+
coverage: {
31+
1: 1,
32+
2: 3
33+
}
34+
}
35+
]
36+
};
37+
38+
return helper.setupMockEndpoint('1234', '4321', bodyValidator)
39+
.then(function () {
40+
return expect(request({
41+
url: 'https://www.codacy.com/api/coverage/1234/4321',
42+
method: 'POST',
43+
json: sampleCoverageData,
44+
resolveWithFullResponse: true
45+
}).promise()).to.eventually.satisfy(function (res) {
46+
expect(res.statusCode).to.equal(200);
47+
return true;
48+
});
49+
});
50+
});
51+
it('should be able to parse lcov data', function () {
52+
var expectedCoverage = {
53+
total: 92,
54+
fileReports: Joi.array().items(Joi.compile({
55+
filename: path.normalize('lib/reporter.js'),
56+
coverage: {
57+
1: 1,
58+
25: 1,
59+
39: 1,
60+
40: 3,
61+
44: 3,
62+
48: 3,
63+
50: 3,
64+
52: 3,
65+
54: 3,
66+
55: 3,
67+
61: 3,
68+
63: 3,
69+
67: 3,
70+
73: 2,
71+
74: 1,
72+
75: 1,
73+
76: 1,
74+
77: 1,
75+
79: 2,
76+
81: 1,
77+
82: 1,
78+
83: 1,
79+
84: 1,
80+
87: 3
81+
},
82+
total: 92
83+
}))
84+
};
85+
86+
return helper.setupMockEndpoint('1234', '4321', Joi.compile(expectedCoverage))
87+
.then(function () {
88+
return expect(handleInput(lcovData, {
89+
token: '1234',
90+
commit: '4321'
91+
})).to.eventually.be.fulfilled();
92+
});
93+
});
94+
it('should be able to parse lcov data with path prefix', function () {
95+
var expectedCoverage = {
96+
total: 92,
97+
fileReports: Joi.array().items(Joi.compile({
98+
filename: path.normalize('my-project/lib/reporter.js'),
99+
coverage: {
100+
1: 1,
101+
25: 1,
102+
39: 1,
103+
40: 3,
104+
44: 3,
105+
48: 3,
106+
50: 3,
107+
52: 3,
108+
54: 3,
109+
55: 3,
110+
61: 3,
111+
63: 3,
112+
67: 3,
113+
73: 2,
114+
74: 1,
115+
75: 1,
116+
76: 1,
117+
77: 1,
118+
79: 2,
119+
81: 1,
120+
82: 1,
121+
83: 1,
122+
84: 1,
123+
87: 3
124+
},
125+
total: 92
126+
}))
127+
};
128+
129+
return helper.setupMockEndpoint('1234', '4321', Joi.compile(expectedCoverage))
130+
.then(function () {
131+
return expect(handleInput(lcovData, {
132+
token: '1234',
133+
commit: '4321',
134+
prefix: 'my-project/'
135+
})).to.eventually.be.fulfilled();
136+
});
137+
});
138+
it('shouldn\'t be able to send coverage with invalid input', function () {
139+
return expect(handleInput()).to.eventually.be.rejectedWith(Error, 'Token is required');
9140
});
10141
});
11142

12-
}(require('../lib/handleInput'), require('./helper')));
143+
}(require('../lib/handleInput'), require('./helper'), require('joi'), require('request-promise'), require('fs'), require('path')));

test/helper.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,15 @@
2727

2828
module.exports = {
2929
setupMockEndpoint: setupMockEndpoint,
30-
chai: chai
30+
chai: chai,
31+
clearEnvironmentVariables: function () {
32+
process.env.CODACY_GIT_COMMIT = '';
33+
process.env.TRAVIS_COMMIT = '';
34+
process.env.DRONE_COMMIT = '';
35+
process.env.GIT_COMMIT = '';
36+
process.env.CIRCLE_SHA1 = '';
37+
process.env.CI_COMMIT_ID = '';
38+
process.env.WERCKER_GIT_COMMIT = '';
39+
}
3140
};
3241
}(require('nock'), require('chai'), require('bluebird')));

test/reporter.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
.to.eventually.be.rejectedWith(Error, 'child "fileReports" fails because ["fileReports" does not contain 1 required value(s)]');
5858
});
5959
it('shouldn\'t be able to create a reporter with invalid options', function () {
60-
expect(function () {
60+
return expect(function () {
6161
reporter({endpoint: 1});
6262
}).to.throw(Error, 'child "endpoint" fails because ["endpoint" must be a string]');
6363
});
@@ -66,7 +66,7 @@
6666
.then(function () {
6767
return expect(reporter({})
6868
.sendCoverage('1234', '4321', sampleCoverageData))
69-
.to.eventually.be.fulfilled;
69+
.to.eventually.be.fulfilled();
7070
});
7171
});
7272
it('should receive error when non-200 status code', function () {

0 commit comments

Comments
 (0)