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

Commit a00d6b5

Browse files
committed
Fix tests to handle Travis providing the codacy token through an environment variable. Update code to use multiple var declarations instead of commas (easier to read, and remove/add)
1 parent d2ce47d commit a00d6b5

File tree

9 files changed

+49
-54
lines changed

9 files changed

+49
-54
lines changed

bin/codacy-coverage.js

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
process.stdin.resume();
55
process.stdin.setEncoding('utf8');
66

7-
var input = '',
8-
loggerImpl;
7+
var input = '';
8+
var loggerImpl;
99

1010
process.stdin.on('data', function (chunk) {
1111
input += chunk;
@@ -41,17 +41,7 @@
4141
return;
4242
}
4343

44-
var opts = {
45-
endpoint: program.endpoint,
46-
token: program.token,
47-
commit: program.commit,
48-
format: program.format,
49-
pathPrefix: program.prefix,
50-
verbose: program.verbose,
51-
debug: program.debug
52-
};
53-
54-
return lib.handleInput(input, opts).then(function () {
44+
return lib.handleInput(input, program).then(function () {
5545
loggerImpl.debug('Successfully sent coverage');
5646
}, function (err) {
5747
loggerImpl.error('Error sending coverage');

lib/coverageParser.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
(function (Joi, util, logger) {
22
'use strict';
33

4-
var validFormats = ['lcov'],
5-
formatValidation = Joi.string().valid(validFormats).required();
4+
var validFormats = ['lcov'];
5+
var formatValidation = Joi.string().valid(validFormats).required();
6+
67
module.exports = {
78
getParser: function getParser(coverageFormat) {
89
var validFormat = Joi.validate(coverageFormat, formatValidation);

lib/impl/lcov.js

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
(function (lcovParse, Promise, Joi, logger, path) {
22
'use strict';
3-
var lcovStringValidation = Joi.string().required(),
4-
optionsValidation = Joi.object().keys().optional();
3+
4+
var lcovStringValidation = Joi.string().required();
5+
var optionsValidation = Joi.object().keys().optional();
6+
57
module.exports = {
68
parse: function parseLcov(pathPrefix, lcovString, options) {
79
return new Promise(function (resolve, reject) {
810
logger.debug('Parsing Lcov Data');
9-
var validLcov = Joi.validate(lcovString, lcovStringValidation),
10-
validOptions = Joi.validate(options, optionsValidation, {
11+
var validLcov = Joi.validate(lcovString, lcovStringValidation);
12+
var validOptions = Joi.validate(options, optionsValidation, {
1113
stripUnknown: true
12-
}),
13-
validationError = validLcov.error || validOptions.error;
14+
});
15+
var validationError = validLcov.error || validOptions.error;
1416

1517
if (validationError) {
1618
logger.error(validationError);
@@ -28,9 +30,9 @@
2830
var result = {
2931
total: 0,
3032
fileReports: []
31-
},
32-
totalLines = 0,
33-
totalHits = 0;
33+
};
34+
var totalLines = 0;
35+
var totalHits = 0;
3436

3537
//TODO: Convert to reduce function
3638
data.forEach(function (stats) {

lib/reporter.js

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,18 @@
22
'use strict';
33

44
var optionsValidation = Joi.object({
5-
endpoint: Joi.string().min(1).optional().example('https://codacy.com/coverage/:token/:commitId')
6-
}),
7-
tokenValidation = Joi.string().required().min(1).example('1234567890'),//TODO: Revisit this validation to see if we can better validate the values
8-
commitIdValidation = Joi.string().required().min(1).example('1234567890'), //TODO: Revisit this validation to see if we can better validate the values
9-
coverageDataValidation = Joi.object({
5+
endpoint: Joi.string().min(1).optional().example('https://codacy.com/coverage/:token/:commitId')
6+
});
7+
var tokenValidation = Joi.string().required().min(1).example('1234567890');//TODO: Revisit this validation to see if we can better validate the values
8+
var commitIdValidation = Joi.string().required().min(1).example('1234567890'); //TODO: Revisit this validation to see if we can better validate the values
9+
var coverageDataValidation = Joi.object({
10+
total: Joi.number().integer().required().min(0).max(100),
11+
fileReports: Joi.array().required().items(Joi.object({
12+
filename: Joi.string().required().min(1),
1013
total: Joi.number().integer().required().min(0).max(100),
11-
fileReports: Joi.array().required().items(Joi.object({
12-
filename: Joi.string().required().min(1),
13-
total: Joi.number().integer().required().min(0).max(100),
14-
coverage: Joi.object().pattern(/\d/, Joi.number().integer().min(1))
15-
}).required())
16-
}).example({total: 50, fileReports: [{filename: 'filename', total: 10, coverage: {1: 1, 2: 3}}]});
14+
coverage: Joi.object().pattern(/\d/, Joi.number().integer().min(1))
15+
}).required())
16+
}).example({total: 50, fileReports: [{filename: 'filename', total: 10, coverage: {1: 1, 2: 3}}]});
1717

1818
module.exports = function (options) {
1919
logger.trace(util.format('Creating reporter for %j', options));
@@ -33,12 +33,12 @@
3333
sendCoverage: function sendCoverage(token, commitId, data) {
3434
return new Promise(function (resolve, reject) {
3535
logger.trace(util.format('Sending Coverage for token [%s] and commitId [%s]', token, commitId));
36-
var tokenValid = Joi.validate(token, tokenValidation),
37-
commitIdValid = Joi.validate(commitId, commitIdValidation),
38-
dataValid = Joi.validate(data, coverageDataValidation, {
39-
stripUnknown: true
40-
}),
41-
validationErr = tokenValid.error || commitIdValid.error || dataValid.error;
36+
var tokenValid = Joi.validate(token, tokenValidation);
37+
var commitIdValid = Joi.validate(commitId, commitIdValidation);
38+
var dataValid = Joi.validate(data, coverageDataValidation, {
39+
stripUnknown: true
40+
});
41+
var validationErr = tokenValid.error || commitIdValid.error || dataValid.error;
4242

4343
if (validationErr) {
4444
logger.error(validationErr);

test/coverageParser.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
(function (util, parser, helper) {
22
'use strict';
33

4-
var expect = helper.chai.expect,
5-
validFormats = ['lcov'];
4+
var expect = helper.chai.expect;
5+
var validFormats = ['lcov'];
66

77
describe('Coverage Parser', function () {
88
it('should receive an error when trying to use an unsupported format', function () {

test/getGitData.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
(function (util, getGitData, helper) {
22
'use strict';
33

4-
var expect = helper.chai.expect,
5-
actualTravisCommit = process.env.TRAVIS_COMMIT; // Store the commit id for the test, if we have it
4+
var expect = helper.chai.expect;
5+
var actualTravisCommit = process.env.TRAVIS_COMMIT; // Store the commit id for the test, if we have it
66

77
describe('Get Git Data', function () {
88
beforeEach(function () {

test/handleInput.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33

44
var expect = helper.chai.expect;
55
var lcovData = fs.readFileSync(__dirname + '/mock/lcov.info').toString();
6+
var originalCodacyToken = process.env.CODACY_REPO_TOKEN;
67

78
describe('Handle Input', function () {
89
beforeEach(function () {
910
helper.clearEnvironmentVariables();
11+
process.env.CODACY_REPO_TOKEN = originalCodacyToken;
1012
});
1113
it('should be able to use the mock end-point', function () {
1214
var bodyValidator = Joi.object({
@@ -136,6 +138,7 @@
136138
});
137139
});
138140
it('shouldn\'t be able to send coverage with invalid input', function () {
141+
process.env.CODACY_REPO_TOKEN = '';
139142
return expect(handleInput()).to.eventually.be.rejectedWith(Error, 'Token is required');
140143
});
141144
});

test/lcov.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
(function (fs, parser, helper) {
1+
(function (fs, parser, helper, path) {
22
'use strict';
33

4-
var path = require('path'),
5-
expect = helper.chai.expect,
6-
lcovData = fs.readFileSync(__dirname + '/mock/lcov.info').toString(),
7-
noStatsLcovData = fs.readFileSync(__dirname + '/mock/no-lines.info').toString(),
8-
nadaLcovData = fs.readFileSync(__dirname + '/mock/nada.info').toString();
4+
var expect = helper.chai.expect;
5+
var lcovData = fs.readFileSync(__dirname + '/mock/lcov.info').toString();
6+
var noStatsLcovData = fs.readFileSync(__dirname + '/mock/no-lines.info').toString();
7+
var nadaLcovData = fs.readFileSync(__dirname + '/mock/nada.info').toString();
98

109
describe('Lcov Parser', function () {
1110
it('should be able to parse lcov data', function () {
@@ -123,4 +122,4 @@
123122
});
124123
});
125124
});
126-
}(require('fs'), require('../lib/coverageParser'), require('./helper')));
125+
}(require('fs'), require('../lib/coverageParser'), require('./helper'), require('path')));

test/logger.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
(function (helper) {
22
'use strict';
33

4-
var expect = helper.chai.expect,
5-
logger;
4+
var expect = helper.chai.expect;
5+
var logger;
66

77
describe('Logger', function () {
88
beforeEach(function () {

0 commit comments

Comments
 (0)