diff --git a/README.md b/README.md index 371adc0..bfa0bc3 100644 --- a/README.md +++ b/README.md @@ -31,7 +31,12 @@ var options = { outputFile: 'test-output.json' // default is 'coverage.json' }, lcovOptions: { - outputFile: 'lcov.dat' + outputFile: 'lcov.dat', + //provide a function to rename es6 modules to a file path + renamer: function(moduleName){ + var expression = /^APP_NAME/; + return moduleName.replace(expression, 'app') + '.js'; + } }, reporters: ['json'] } diff --git a/lib/reporters/lcov-reporter.js b/lib/reporters/lcov-reporter.js index a9a96e1..02ac89f 100644 --- a/lib/reporters/lcov-reporter.js +++ b/lib/reporters/lcov-reporter.js @@ -1,12 +1,16 @@ var Reporter = require('../reporter'); var lcovRecord = function(data) { - var str = "", lineHandled = 0, - lineFound = 0; + lineFound = 0, + fileName = data.fileName; + + if(this.options.lcovOptions && this.options.lcovOptions.renamer){ + fileName = this.options.lcovOptions.renamer(fileName); + } - str += 'SF:' + data.fileName + '\n'; + str += 'SF:' + fileName + '\n'; data.lines.forEach(function(value, num) { if (value !== null) { str += 'DA:' + num + ',' + value + '\n'; @@ -33,7 +37,7 @@ module.exports = Reporter.extend({ name: 'lcov', defaultOutput: 'lcov.dat', transform: function(coverageData) { - var data = coverageData.fileData.map(lcovRecord); + var data = coverageData.fileData.map(lcovRecord, this); return data.join('\n'); } }); diff --git a/tests/fixtures/lcov.dat b/tests/fixtures/lcov-output-no-renamer.dat similarity index 100% rename from tests/fixtures/lcov.dat rename to tests/fixtures/lcov-output-no-renamer.dat diff --git a/tests/fixtures/lcov-output-with-renamer.dat b/tests/fixtures/lcov-output-with-renamer.dat new file mode 100644 index 0000000..adead2f --- /dev/null +++ b/tests/fixtures/lcov-output-with-renamer.dat @@ -0,0 +1,167 @@ +SF:something-else/app +DA:1,1 +DA:5,1 +DA:7,1 +DA:9,1 +DA:15,1 +DA:17,1 +LF:6 +LH:6 +end_of_record +SF:something-else/config/environment +DA:1,1 +DA:4,0 +DA:7,0 +DA:8,0 +DA:9,0 +DA:10,0 +DA:12,0 +DA:15,0 +LF:8 +LH:1 +end_of_record +SF:something-else/initializers/app-version +DA:1,1 +DA:5,1 +DA:7,1 +DA:9,1 +DA:12,8 +DA:13,8 +LF:6 +LH:6 +end_of_record +SF:something-else/initializers/export-application-global +DA:1,1 +DA:5,1 +DA:7,1 +DA:9,1 +DA:10,8 +DA:12,8 +DA:13,1 +DA:19,1 +LF:8 +LH:8 +end_of_record +SF:something-else/router +DA:1,1 +DA:5,1 +DA:7,1 +DA:11,1 +DA:12,1 +DA:13,1 +DA:14,1 +DA:18,1 +LF:8 +LH:8 +end_of_record +SF:something-else/models/todo +DA:1,1 +DA:5,1 +DA:7,1 +DA:12,1 +LF:4 +LH:4 +end_of_record +SF:something-else/controllers/todo +DA:1,1 +DA:5,1 +DA:7,1 +DA:10,3 +DA:13,2 +DA:15,2 +DA:16,1 +DA:18,1 +DA:22,3 +DA:23,3 +DA:24,3 +DA:31,14 +DA:32,14 +DA:34,14 +DA:36,14 +DA:39,0 +DA:40,0 +DA:41,0 +LF:18 +LH:15 +end_of_record +SF:something-else/utils/util +DA:1,1 +DA:5,1 +DA:7,1 +DA:9,2 +DA:11,14 +DA:12,14 +DA:16,1 +LF:7 +LH:7 +end_of_record +SF:something-else/controllers/todos +DA:1,1 +DA:5,1 +DA:7,1 +DA:11,1 +DA:12,1 +DA:13,0 +DA:17,1 +DA:23,1 +DA:26,1 +DA:30,0 +DA:31,0 +DA:32,0 +DA:37,9 +DA:41,7 +DA:42,7 +DA:46,7 +DA:50,9 +DA:54,9 +DA:55,9 +DA:57,0 +DA:58,0 +DA:59,0 +LF:22 +LH:15 +end_of_record +SF:something-else/routes/todos +DA:1,1 +DA:5,1 +DA:7,1 +DA:9,4 +LF:4 +LH:4 +end_of_record +SF:something-else/routes/todos/active +DA:1,1 +DA:5,1 +DA:7,1 +DA:9,0 +DA:10,0 +DA:14,0 +LF:6 +LH:3 +end_of_record +SF:something-else/routes/todos/completed +DA:1,1 +DA:5,1 +DA:7,1 +DA:9,0 +DA:10,0 +DA:14,0 +LF:6 +LH:3 +end_of_record +SF:something-else/routes/todos/index +DA:1,1 +DA:5,1 +DA:7,1 +DA:9,4 +LF:4 +LH:4 +end_of_record +SF:something-else/adapters/application +DA:1,1 +DA:5,1 +DA:7,1 +DA:11,1 +LF:4 +LH:4 +end_of_record \ No newline at end of file diff --git a/tests/unit/reporters/lcov-reporter-test.js b/tests/unit/reporters/lcov-reporter-test.js index 74e050b..204a6c8 100644 --- a/tests/unit/reporters/lcov-reporter-test.js +++ b/tests/unit/reporters/lcov-reporter-test.js @@ -6,10 +6,21 @@ var LCOVReporter = require('../../../lib/reporters/lcov-reporter'); describe('LCOV Reporter', function() { it('should work without branches or modules', function () { - var expectedOutput = fs.readFileSync(path.join(__dirname, '../../fixtures/lcov.dat'), 'utf8'); + var expectedOutput = fs.readFileSync(path.join(__dirname, '../../fixtures/lcov-output-no-renamer.dat'), 'utf8'); var reporter = new LCOVReporter({}); var output = reporter.transform(fixture); expect(output).to.deep.equal(expectedOutput); }); + it('should replace modules names with file names when requested', function () { + var expectedOutput = fs.readFileSync(path.join(__dirname, '../../fixtures/lcov-output-with-renamer.dat'), 'utf8'); + var reporter = new LCOVReporter({ + lcovOptions: { + renamer: function(moduleName){ + return moduleName.replace(/^todomvc-ember-cli/, 'something-else'); + } + } + }); + var output = reporter.transform(fixture); + expect(output).to.deep.equal(expectedOutput); + }); }); -