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

Commit 3ec69be

Browse files
committed
add option for prefixing filenames
1 parent d5cb05f commit 3ec69be

File tree

11 files changed

+74
-57
lines changed

11 files changed

+74
-57
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ node_modules
2929

3030
#IntelliJ
3131
.idea
32+
**/*.iml
3233

3334
# Mac
34-
.DS_Store
35+
.DS_Store

bin/codacy-coverage.js

100644100755
Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,13 @@
1515
});
1616

1717
program
18-
.version('0.1.0')
18+
.version('1.0.4')
1919
.usage('[options]')
2020
.option('-f, --format [value]', 'Coverage input format')
21-
.option('-t, --token [value]', 'Set Token')
22-
.option('-c, --commit [value]', 'Set Commit Id')
23-
.option('-e, --endpoint [value]', 'Set Endpoint')
21+
.option('-t, --token [value]', 'Codacy Project API Token')
22+
.option('-c, --commit [value]', 'Commit SHA hash')
23+
.option('-e, --endpoint [value]', 'Codacy API Endpoint')
24+
.option('-p, --prefix [value]', 'Project path prefix')
2425
.option('-v, --verbose', 'Display verbose output')
2526
.option('-d, --debug', 'Display debug output')
2627
.parse(process.argv);
@@ -30,7 +31,8 @@
3031
debug: program.debug
3132
});
3233

33-
loggerImpl.info(util.format('Started with: token [%j], commitId [%j], endpoint [%j], format [%j], verbose [%j], debug [%j]', program.token, program.commit, program.endpoint, program.format, program.verbose, program.debug));
34+
loggerImpl.info(util.format('Started with: token [%j], commitId [%j], endpoint [%j], format [%j], path prefix [%j], verbose [%j], debug [%j]',
35+
program.token, program.commit, program.endpoint, program.format, program.prefix, program.verbose, program.debug));
3436

3537
process.stdin.on('end', function () {
3638
loggerImpl.trace('Received file through stdin');
@@ -41,14 +43,15 @@
4143

4244
var token = program.token || process.env.CODACY_REPO_TOKEN,
4345
commitId = program.commit,
44-
format = program.format || 'lcov';
46+
format = program.format || 'lcov',
47+
pathPrefix = program.prefix || '';
4548

4649
if (!token) {
4750
return loggerImpl.error(new Error('Token is required'));
4851
}
4952

5053
// Parse the coverage data for the given format and retrieve the commit id if we don't have it.
51-
return Q.all([lib.getParser(format).parse(input), getGitData.getCommitId(commitId)]).spread(function (parsedCoverage, commitId) {
54+
return Q.all([lib.getParser(format).parse(pathPrefix, input), getGitData.getCommitId(commitId)]).spread(function (parsedCoverage, commitId) {
5255
// Now that we've parse the coverage data to the correct format, send it to Codacy.
5356
loggerImpl.trace(parsedCoverage);
5457
lib.reporter({

lib/coverageParser.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@
1616
return require('./impl/' + coverageFormat);
1717
}
1818
};
19-
}(require('joi'), require('util'), require('./logger')()));
19+
}(require('joi'), require('util'), require('./logger')()));

lib/getGitData.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,13 @@
99
return deferred.resolve(commitId);
1010
}
1111

12-
var gitCommit = process.env.CODACY_GIT_COMMIT || process.env.TRAVIS_COMMIT || process.env.DRONE_COMMIT || process.env.GIT_COMMIT || process.env.CIRCLE_SHA1 || process.env.CI_COMMIT_ID || process.env.WERCKER_GIT_COMMIT;
12+
var gitCommit = process.env.CODACY_GIT_COMMIT ||
13+
process.env.TRAVIS_COMMIT ||
14+
process.env.DRONE_COMMIT ||
15+
process.env.GIT_COMMIT ||
16+
process.env.CIRCLE_SHA1 ||
17+
process.env.CI_COMMIT_ID ||
18+
process.env.WERCKER_GIT_COMMIT;
1319

1420
if (gitCommit) {
1521
logger.debug('Received Commit Id: ' + gitCommit);
@@ -28,4 +34,4 @@
2834
return deferred.promise;
2935
}
3036
};
31-
}(require('./logger')(), require('child_process').exec, require('q')));
37+
}(require('./logger')(), require('child_process').exec, require('q')));

lib/impl/lcov.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
var lcovStringValidation = Joi.string().required(),
44
optionsValidation = Joi.object().keys().optional();
55
module.exports = {
6-
parse: function parseLcov(lcovString, options) {
6+
parse: function parseLcov(pathPrefix, lcovString, options) {
77
logger.debug('Parsing Lcov Data');
88
var deferred = Q.defer();
99
process.nextTick(function () {
@@ -37,7 +37,7 @@
3737
data.forEach(function (stats) {
3838
var fileStats = {
3939
// The API expects the filenames to be relative to the project, ex. lib/reporter.js
40-
filename: stats.file ? path.relative(process.cwd(), stats.file) : '',
40+
filename: stats.file ? pathPrefix + path.relative(process.cwd(), stats.file) : '',
4141
coverage: {}
4242
};
4343

lib/logger.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,4 @@
3737
return logLevel;
3838
}
3939

40-
}(require('log-driver')));
40+
}(require('log-driver')));

lib/reporter.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,4 @@
7373
}
7474
};
7575
};
76-
}(require('request-promise'), require('joi'), require('q'), require('util'), require('./logger')()));
76+
}(require('request-promise'), require('joi'), require('q'), require('util'), require('./logger')()));

test/cli.js

Lines changed: 1 addition & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -7,40 +7,6 @@
77
chai.config.includeStack = true;
88

99
describe('Command Line', function () {
10-
it('should be able to get help with `-h`', function (done) {
11-
exec('node ./bin/codacy-coverage.js -h', function (err, res) {
12-
expect(res).to.equal('\n' +
13-
' Usage: codacy-coverage [options]\n\n' +
14-
' Options:\n\n' +
15-
' -h, --help output usage information\n' +
16-
' -V, --version output the version number\n' +
17-
' -f, --format [value] Coverage input format\n' +
18-
' -t, --token [value] Set Token\n' +
19-
' -c, --commit [value] Set Commit Id\n' +
20-
' -e, --endpoint [value] Set Endpoint\n' +
21-
' -v, --verbose Display verbose output\n' +
22-
' -d, --debug Display debug output\n\n'
23-
);
24-
done();
25-
});
26-
});
27-
it('should be able to get help with `-help`', function (done) {
28-
exec('node ./bin/codacy-coverage.js -help', function (err, res) {
29-
expect(res).to.equal('\n' +
30-
' Usage: codacy-coverage [options]\n\n' +
31-
' Options:\n\n' +
32-
' -h, --help output usage information\n' +
33-
' -V, --version output the version number\n' +
34-
' -f, --format [value] Coverage input format\n' +
35-
' -t, --token [value] Set Token\n' +
36-
' -c, --commit [value] Set Commit Id\n' +
37-
' -e, --endpoint [value] Set Endpoint\n' +
38-
' -v, --verbose Display verbose output\n' +
39-
' -d, --debug Display debug output\n\n'
40-
);
41-
done();
42-
});
43-
});
4410
it('should be able to parse lcov data', function (done) {
4511
var bodyObject = {
4612
total: 92,
@@ -94,4 +60,4 @@
9460
});
9561
});
9662
});
97-
}(require('chai'), require('q'), require('child_process').exec, require('joi'), require('../'), require('./helper')));
63+
}(require('chai'), require('q'), require('child_process').exec, require('joi'), require('../'), require('./helper')));

test/coverageParser.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@
2121
}).to.not.throw();
2222
});
2323
it('shouldn\'t be able to parse a blank coverage file for ' + format, function () {
24-
return expect(parser.getParser(format).parse()).to.eventually.be.rejectedWith(Error, 'value is required');
24+
return expect(parser.getParser(format).parse('')).to.eventually.be.rejectedWith(Error, 'value is required');
2525
});
2626
it('shouldn\'t be able to parse invalid coverage for ' + format, function () {
27-
return expect(parser.getParser(format).parse('There is no Dana, only Zuul')).to.eventually.be.rejectedWith(Error, 'Failed to parse string');
27+
return expect(parser.getParser(format).parse('', 'There is no Dana, only Zuul')).to.eventually.be.rejectedWith(Error, 'Failed to parse string');
2828
});
2929
it('shouldn\'t be able to parse a non-existent coverage file for ' + format, function () {
30-
return expect(parser.getParser(format).parse('/no-exist/lcov')).to.eventually.be.rejectedWith(Error, 'Failed to parse string');
30+
return expect(parser.getParser(format).parse('', '/no-exist/lcov')).to.eventually.be.rejectedWith(Error, 'Failed to parse string');
3131
});
3232
});
3333
});

test/lcov.js

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
describe('Lcov Parser', function () {
1414
it('should be able to parse lcov data', function () {
15-
return expect(parser.getParser('lcov').parse(lcovData))
15+
return expect(parser.getParser('lcov').parse('', lcovData))
1616
.to.eventually.satisfy(function (data) {
1717
expect(data).to.deep.equal({
1818
total: 92,
@@ -52,8 +52,49 @@
5252
return true;
5353
});
5454
});
55+
it('should be able to parse lcov data with path prefix', function () {
56+
return expect(parser.getParser('lcov').parse('my-project/', lcovData))
57+
.to.eventually.satisfy(function (data) {
58+
expect(data).to.deep.equal({
59+
total: 92,
60+
fileReports: [
61+
{
62+
filename: 'my-project/lib/reporter.js',
63+
coverage: {
64+
1: 1,
65+
25: 1,
66+
39: 1,
67+
40: 3,
68+
44: 3,
69+
48: 3,
70+
50: 3,
71+
52: 3,
72+
54: 3,
73+
55: 3,
74+
61: 3,
75+
63: 3,
76+
67: 3,
77+
73: 2,
78+
74: 1,
79+
75: 1,
80+
76: 1,
81+
77: 1,
82+
79: 2,
83+
81: 1,
84+
82: 1,
85+
83: 1,
86+
84: 1,
87+
87: 3
88+
},
89+
total: 92
90+
}
91+
]
92+
});
93+
return true;
94+
});
95+
});
5596
it('should be able to parse lcov data without lines', function () {
56-
return expect(parser.getParser('lcov').parse(noStatsLcovData))
97+
return expect(parser.getParser('lcov').parse('', noStatsLcovData))
5798
.to.eventually.satisfy(function (data) {
5899
expect(JSON.stringify(data)).to.equal(JSON.stringify({
59100
total: null,
@@ -69,7 +110,7 @@
69110
});
70111
});
71112
it('should be able to parse lcov data without anything', function () {
72-
return expect(parser.getParser('lcov').parse(nadaLcovData))
113+
return expect(parser.getParser('lcov').parse('', nadaLcovData))
73114
.to.eventually.satisfy(function (data) {
74115
expect(JSON.stringify(data)).to.equal(JSON.stringify({
75116
total: null,

0 commit comments

Comments
 (0)