Uploading multiple LCOV and JUnit XMLs for the same source to Grunt-Karma-Sonar

posted by sacah on karma, grunt, sonar, software, programming, sacah,

I recently needed to upload 2 LCOV files, and 2 JUnit XML files up to Sonar. The problem was both LCOV and JUnit XMLs covered the same JS source files, so if I’d configured grunt-karma-sonar like normal, with the JS source path in the ‘path’ option, Sonar would throw an error about duplicate source files.

So I comment out the ‘path’ option for the second set, but then the LCOV file has the wrong path when it gets uploaded to Sonar, meaning Sonar doesn’t count those coverage results.

What I ended up doing, was adding the full path to the ‘prefix’ option, and commenting out ‘path’.

This means it merges both LCOV and JUnit XMLs and uploads with the 1 lot of JS source files.

Here is the config I used

karma_sonar: {
    options: {
        instance: {
            hostUrl: 'http://localhost:9000',
            jdbcUrl: 'jdbc:h2:tcp://localhost:9092/sonar',
            login: 'sonar',
            password: 'sonar'
        }
    },
    your_target: {
        project: {
            key: 'project-name',
            name: 'Full project name',
            version: grunt.file.readJSON('package.json').version
        },
        sources: [
            {
                path: 'web/src',
                prefix: '...',
                coverageReport: 'coverage/ui/PhantomJS 1.9 (Windows)/lcov.info',
                testReport: 'web/test/ui/test-output/test-ui.xml'
            },
            {
                prefix: 'C:/Development/project-name/web',
                coverageReport: 'coverage/js/PhantomJS 1.9 (Windows)/lcov.info',
                testReport: 'web/test/js/test-output/test-js.xml'
            }
        ],
        exclusions: []
    }
}