Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

karma-sonarqube-reporter

Package Overview
Dependencies
Maintainers
1
Versions
17
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

karma-sonarqube-reporter - npm Package Compare versions

Comparing version 1.0.4 to 1.1.0

107

index.js

@@ -5,3 +5,2 @@ var fs = require('fs');

var js2xmlparser = require('js2xmlparser');
var pathfinder = require('./lib/path-finder');

@@ -11,13 +10,17 @@ var reportbuilder = require('./lib/report-builder');

const BASE_PATH = 'src/app/';
const OUTPUT_FOLDER = '/reports';
const OUTPUT_FOLDER = 'reports';
const FILE_PATTERN = '**/*spec.ts';
const ENCODING = 'utf-8';
var sonarqubeReporter = function(baseReporterDecorator, config,
logger, helper, formatError) {
const REPORT_NAME = (metadata) => {
return metadata.concat('xml').join('.');
}
var sonarqubeReporter = function(baseReporterDecorator, config,
logger, helper, formatError) {
var sonarqubeConfig = config.sonarqubeReporter || {};
var pattern = path.join(
sonarqubeConfig.basePath || BASE_PATH,
sonarqubeConfig.basePath || BASE_PATH,
sonarqubeConfig.filePattern || FILE_PATTERN);

@@ -27,6 +30,7 @@

var encoding = sonarqubeConfig.encoding || ENCODING;
var reportName = sonarqubeConfig.reportName || REPORT_NAME;
var paths = pathfinder.parseTestFiles(
pattern, encoding);
var reports = {};

@@ -40,3 +44,3 @@

if (result.success) {
this.specSuccess(browser, result);
this.specSuccess(browser, result);
}

@@ -47,3 +51,3 @@ else if (result.skipped) {

else {
this.specFailure(browser, result);
this.specFailure(browser, result);
}

@@ -53,5 +57,6 @@ }

this.specSuccess = function(browser, result) {
var report = browserReport(reports, browser);
var path = pathfinder.testFile(paths,
result.suite[0], result.description);
var report = browserReport(reports,
reportName(metadata(browser.name)));
var path = pathfinder.testFile(paths,
result.suite[0], result.description);
reportFile(report, path).testCase.push(

@@ -62,4 +67,5 @@ reportbuilder.createReportTestCaseSuccess(result));

this.specSkipped = function(browser, result) {
var report = browserReport(reports, browser);
var path = pathfinder.testFile(paths,
var report = browserReport(reports,
reportName(metadata(browser.name)));
var path = pathfinder.testFile(paths,
result.suite[0], result.description);

@@ -71,7 +77,8 @@ reportFile(report, path).testCase.push(

this.specFailure = function(browser, result) {
var report = browserReport(reports, browser);
var path = pathfinder.testFile(paths,
var report = browserReport(reports,
reportName(metadata(browser.name)));
var path = pathfinder.testFile(paths,
result.suite[0], result.description);
reportFile(report, path).testCase.push(
reportbuilder.createReportTestCaseFailure(result,
reportbuilder.createReportTestCaseFailure(result,
stacktrace(result, formatError)));

@@ -81,19 +88,19 @@ };

this.onRunComplete = function(browsersCollection, results) {
saveReports(reports, outputFolder);
saveReports(outputFolder, reports);
};
};
function browserReport(reports, browser) {
var reportKey = Object.keys(reports).find((currentKey) => {
return currentKey == browser.name }
function browserReport(reports, reportKey) {
var foundKey = Object.keys(reports).find((key) => {
return key == reportKey }
);
if (reportKey == undefined) {
if (foundKey == undefined) {
report = reportbuilder.createReport();
reports[browser.name] = report;
reports[reportKey] = report;
}
return reports[browser.name];
return reports[reportKey];
}
function reportFile(report, path) {
var reportFile = report.file.find((currentFile) => {
function reportFile(report, path) {
var reportFile = report.file.find((currentFile) => {
return currentFile['@'].path == path

@@ -109,30 +116,32 @@ });

function stacktrace(result, formatError) {
return result.log.reduce((previousLog, currentLog)=> {
return previousLog + formatError(currentLog);
});
return result.log.map(formatError).reduce(
(errors, value)=> { return errors.concat(value) });
}
function saveReports(reports, outputFolder) {
createOutputFolder(outputFolder);
Object.keys(reports).forEach((browserName)=> {
fs.writeFileSync(reportFileName(outputFolder, browserName),
toXml(reports[browserName]));
function saveReports(folder, reports) {
Object.keys(reports).forEach((report) => {
saveReport(path.join(folder, report),
reports[report]);
});
}
function createOutputFolder(outputFolder) {
mkdirp.sync(outputFolder, (error) => {
if (error) throw error;
});
function saveReport(filePath, data) {
createFolder(filePath);
createFile(filePath, data);
}
function reportFileName(outputFolder, browserName) {
return path.join(outputFolder, formatReportFileName(browserName));
function createFolder(filePath) {
mkdirp.sync(path.dirname(filePath),
(error) => { if (error) throw error; }
);
}
function formatReportFileName(reportFileName) {
return reportFileName.replace(/\s/g, '.').replace(/\(|\)/g,'')
.toLowerCase() + '.xml';
function createFile(filePath, data) {
fs.writeFileSync(filePath, toXml(data));
}
function metadata(report) {
return report.replace(/\(|\)/gm,'').split(" ");
}
function toXml(report) {

@@ -143,6 +152,6 @@ return js2xmlparser.parse('testExecutions', report);

sonarqubeReporter.$inject = [
'baseReporterDecorator',
'config',
'logger',
'helper',
'baseReporterDecorator',
'config',
'logger',
'helper',
'formatError'

@@ -152,3 +161,3 @@ ];

module.exports = {
'reporter:sonarqube': ['type', sonarqubeReporter]
'reporter:sonarqube': ['type', sonarqubeReporter]
};

@@ -26,3 +26,6 @@ // Example of a typical configuration

filePattern: '**/*spec.ts',
encoding: 'utf-8'
encoding: 'utf-8',
reportName: (metadata) => {
return metadata.concat('xml').join('.');
}
},

@@ -29,0 +32,0 @@ angularCli: {

@@ -54,3 +54,3 @@ // report-builder.ts

function testCaseName(result) {
return result.suite[0] + ':' + result.description;
return result.fullName;
}

@@ -57,0 +57,0 @@

{
"name": "karma-sonarqube-reporter",
"version": "1.0.4",
"version": "1.1.0",
"description": "A karma reporter plugin for generating Sonarqube generic test reports",

@@ -33,6 +33,7 @@ "main": "index.js",

"jasmine-spec-reporter": "^4.2.1",
"lodash": "^4.17.10",
"mock-require": "^3.0.2",
"nyc": "^11.6.0",
"lodash": "^4.17.10"
"rimraf": "^2.6.2"
}
}

@@ -13,3 +13,3 @@ # karma-sonarqube-reporter

Adjust your `karma.conf.js` file:
Adjust your `karma.conf.js` file:

@@ -26,8 +26,19 @@ **Create a new plugin entry**

```typescript
```javascript
// Configuration example
sonarqubeReporter: {
basePath: 'src/app', // test folder
filePattern: '**/*spec.ts', // test file pattern
outputFolder: 'reports', // reports destination
encoding: 'utf-8' // file format
basePath: 'src/app', // test files folder
filePattern: '**/*spec.ts', // test files glob pattern
outputFolder: 'reports', // report destination
encoding: 'utf-8', // report encoding
reportName: (metadata) => { // report name callback
/* Report metadata content:
* - metadata[0] = browser name
* - metadata[1] = browser version
* - metadata[2] = plataform name
* - metadata[3] = plataform version
* e.g. firefox.54.0.0.linux.0.0.0.xml
* e.g. chrome.65.0.3325.linux.0.0.0.xml
*/
return metadata.concat('xml').join('.');
}

@@ -51,18 +62,17 @@ ```

$ ls reports
firefox.54.0.0.linux.0.0.0.xml
chrome.65.0.3325.linux.0.0.0.xml
firefox.54.0.0.linux.0.0.0.xml
```
The report files' schema is defined on the [SonarQube Generic Test Data][5] page.
Add the following property to your `sonar-project.properties`:
Add the following property to your `sonar-project.properties`:
```
sonar.testExecutionReportPaths= \
reports/chrome.65.0.3325.linux.0.0.0.xml, \
reports/firefox.54.0.0.linux.0.0.0.xml
reports/firefox.54.0.0.linux.0.0.0.xml, \
reports/chrome.65.0.3325.linux.0.0.0.xml
```
Finally, start [SonarQube Scanner][6] on your project folder.
That's all!

@@ -69,0 +79,0 @@

var fs = require('fs');
var path = require('path');
var rimraf = require('rimraf');
var mock = require('mock-require');
const config = {
const config1 = {
sonarqubeReporter: {
basePath: 'src/app', // test folder
filePattern: '**/*spec.ts', // test file pattern
outputFolder: 'tmp/reports', // reports destination
encoding: 'utf-8' // file format
outputFolder: 'reports/config1',
encoding: 'utf-8',
reportName: (metadata) => {
return metadata[0].concat('.xml');
}
}
}
};
const config2 = {
sonarqubeReporter: {
outputFolder: 'reports/config2',
encoding: 'iso-8859-1',
reportName: (metadata) => {
return metadata[0].concat(
'/result.xml');
}
}
};
describe('Sonarqube reporter tests', function() {
var reporter;
beforeAll(function() {
var reporterDefault;
var reporterConfig1;
var reporterConfig2;
const firefox = 'firefox 1.0.0 (linux 1.0.0)';
const chrome = 'chrome 1.0.0 (linux 1.0.0)';
const ie = 'ie 1.0.0 (linux 1.0.0)'
beforeAll(function() {
mock('../lib/path-finder', {

@@ -27,11 +44,5 @@ parseTestFiles: function(pattern, encoding) {

testFile: function(paths, describe, it) {
if (describe == 's1' && it == 'd1') {
testFilePath = 'test/file1/path';
}
if (describe == 's2' && it == 'd2') {
testFilePath = 'test/file1/path';
}
if (describe == 's3' && it == 'd3') {
testFilePath = 'test/file3/path';
}
if (describe == 's1' && it == 'd1') { testFilePath = 'test/file1/path'; }
if (describe == 's2' && it == 'd2') { testFilePath = 'test/file1/path'; }
if (describe == 's3' && it == 'd3') { testFilePath = 'test/file3/path'; }
return testFilePath;

@@ -45,11 +56,15 @@ }

helper = jasmine.createSpy('helper');
logger = jasmine.createSpy('logger');
formatError = jasmine.createSpy('formatError');
reporter = new sonarqubeReporter(
baseReporterDecorator,
config, helper, logger,
formatError);
spyOn(reporter, "specSuccess").and.callThrough();
spyOn(reporter, "specSkipped").and.callThrough();
spyOn(reporter, "specFailure").and.callThrough();
logger = jasmine.createSpy('logger');
formatError = (error) => { return error };
reporterDefault = initReporter({});
reporterConfig1 = initReporter(config1);
reporterConfig2 = initReporter(config2);
function initReporter(config) {
var reporter = new sonarqubeReporter(
baseReporterDecorator, config, helper, logger, formatError);
spyOn(reporter, "specSuccess").and.callThrough();
spyOn(reporter, "specSkipped").and.callThrough();
spyOn(reporter, "specFailure").and.callThrough();
return reporter;
};
});

@@ -59,102 +74,127 @@ afterAll(function() {

});
it('Sonarqube reporter is defined', function() {
expect(reporter).toBeDefined();
afterEach(function() {
clearReports('reports');
});
describe('Single browsers', function() {
it('Report test case success (firefox)', function() {
reporter.onSpecComplete({name: 'firefox'}, { success: true, suite: ['s1'],
description: 'd1', time: '1'});
reporter.onSpecComplete({name: 'firefox'}, { success: true, suite: ['s2'],
description: 'd2', time: '1'});
reporter.onSpecComplete({name: 'firefox'}, { success: true, suite: ['s3'],
description: 'd3', time: '1'});
// TODO: assert specSuccess arguments
expect(reporter.specSuccess.calls.count()).toEqual(3);
reporter.onRunComplete({}, {});
// TODO: assert report file content
expect(reportFileCreated('firefox.xml')).toBe(true);
describe('Reporter using default configuration', function() {
it('Sonarqube reporter is defined', function() {
expect(reporterDefault).toBeDefined();
});
it('Report test case skipped (chrome)', function() {
reporter.onSpecComplete({name: 'chrome'}, { skipped: true, suite: ['s1'],
description: 'd1', time: '2'});
reporter.onSpecComplete({name: 'chrome'}, { skipped: true, suite: ['s2'],
description: 'd2', time: '2'});
reporter.onSpecComplete({name: 'chrome'}, { skipped: true, suite: ['s3'],
description: 'd3', time: '2'});
// TODO: assert specSkipped arguments
expect(reporter.specSkipped.calls.count()).toEqual(3);
reporter.onRunComplete({}, {});
// TODO: assert report file content
expect(reportFileCreated('chrome.xml')).toBe(true);
});
describe('Reporter using custom configuration 1', function() {
it('Sonarqube reporter is defined', function() {
expect(reporterConfig1).toBeDefined();
});
it('Report rest case failure (ie)', function() {
reporter.onSpecComplete({name: 'ie'}, { suite: ['s1'],
description: 'd1', time: '3', log: ['e1']});
reporter.onSpecComplete({name: 'ie'}, { suite: ['s2'],
description: 'd2', time: '3', log: ['e2']});
reporter.onSpecComplete({name: 'ie'}, { suite: ['s3'],
description: 'd3', time: '3', log: ['e3']});
// TODO: assert specFailure arguments
expect(reporter.specFailure).toHaveBeenCalled();
reporter.onRunComplete({}, {});
// TODO: assert report file content
expect(reportFileCreated('ie.xml')).toBe(true);
});
describe('Reporter using custom configuration 2', function() {
it('Sonarqube reporter is defined', function() {
expect(reporterConfig2).toBeDefined();
});
});
describe('Multiple browsers (firefox, chrome, ie)', function() {
it('Report test case success', function() {
reporter.onSpecComplete({name: 'firefox'}, { success: true, suite: ['s1'],
description: 'd1', time: '1'});
reporter.onSpecComplete({name: 'chrome'}, { success: true, suite: ['s1'],
description: 'd1', time: '1'});
reporter.onSpecComplete({name: 'ie'}, { success: true, suite: ['s1'],
description: 'd1', time: '1'});
// TODO: assert specSuccess arguments
expect(reporter.specSuccess.calls.count()).toEqual(3);
reporter.onRunComplete({}, {});
// TODO: assert report file content
expect(reportFileCreated('firefox.xml')).toBe(true);
expect(reportFileCreated('chrome.xml')).toBe(true);
expect(reportFileCreated('ie.xml')).toBe(true);
describe('Karma runner configured to single browser', function() {
describe('Reporter using default configuration', function() {
it('All test cases passed (firefox)', function() {
reporterDefault.onSpecComplete({name: firefox}, { fullName: 's1 d1',
success: true, suite: ['s1'], description: 'd1', time: '1'});
reporterDefault.onSpecComplete({name: firefox}, { fullName: 's2 d2',
success: true, suite: ['s2'], description: 'd2', time: '1'});
reporterDefault.onSpecComplete({name: firefox}, { fullName: 's3 d3',
success: true, suite: ['s3'], description: 'd3', time: '1'});
reporterDefault.onRunComplete({}, {});
// TODO: assert specSuccess arguments
// TODO: assert report file content
expect(reporterDefault.specSuccess.calls.count()).toEqual(3);
expect(isReportFileCreated('reports/firefox.1.0.0.linux.1.0.0.xml')).toBe(true);
});
});
it('Report test case skipped', function() {
reporter.onSpecComplete({name: 'firefox'}, { skipped: true, suite: ['s2'],
description: 'd2', time: '2'});
reporter.onSpecComplete({name: 'chrome'}, { skipped: true, suite: ['s2'],
description: 'd2', time: '2'});
reporter.onSpecComplete({name: 'ie'}, { skipped: true, suite: ['s2'],
description: 'd2', time: '2'});
// TODO: assert specSkipped arguments
expect(reporter.specSkipped.calls.count()).toEqual(3);
reporter.onRunComplete({}, {});
// TODO: assert report file content
expect(reportFileCreated('firefox.xml')).toBe(true);
expect(reportFileCreated('chrome.xml')).toBe(true);
expect(reportFileCreated('ie.xml')).toBe(true);
describe('Reporter using custom configuration 1', function() {
it('All test cases skipped (chrome)', function() {
reporterConfig1.onSpecComplete({name: chrome}, { fullName: 's1 d1',
skipped: true, suite: ['s1'], description: 'd1', time: '2'});
reporterConfig1.onSpecComplete({name: chrome}, { fullName: 's2 d2',
skipped: true, suite: ['s2'], description: 'd2', time: '2'});
reporterConfig1.onSpecComplete({name: chrome}, { fullName: 's3 d3',
skipped: true, suite: ['s3'], description: 'd3', time: '2'});
reporterConfig1.onRunComplete({}, {});
// TODO: assert specSuccess arguments
// TODO: assert report file content
expect(reporterConfig1.specSkipped.calls.count()).toEqual(3);
expect(isReportFileCreated('reports/config1/chrome.xml')).toBe(true);
});
});
it('Report rest case failure', function() {
reporter.onSpecComplete({name: 'firefox'}, { suite: ['s3'],
description: 'd3', time: '3', log: ['e3']});
reporter.onSpecComplete({name: 'chrome'}, { suite: ['s3'],
description: 'd3', time: '3', log: ['e3']});
reporter.onSpecComplete({name: 'ie'}, { suite: ['s3'],
description: 'd3', time: '3', log: ['e3.1', 'e3.2']});
// TODO: assert specFailure arguments
expect(reporter.specFailure.calls.count()).toEqual(3);
reporter.onRunComplete({}, {});
// TODO: assert report file content
expect(reportFileCreated('firefox.xml')).toBe(true);
expect(reportFileCreated('chrome.xml')).toBe(true);
expect(reportFileCreated('ie.xml')).toBe(true);
describe('Reporter using custom configuration 2', function() {
it('All test cases failed (ie)', function() {
reporterConfig2.onSpecComplete({name: ie}, { fullName: 's1 d1',
suite: ['s1'], description: 'd1', time: '3', log: ['e1']});
reporterConfig2.onSpecComplete({name: ie}, { fullName: 's2 d2',
suite: ['s2'], description: 'd2', time: '3', log: ['e2']});
reporterConfig2.onSpecComplete({name: ie}, { fullName: 's3 d3',
suite: ['s3'], description: 'd3', time: '3', log: ['e3']});
reporterConfig2.onRunComplete({}, {});
// TODO: assert specSuccess arguments
// TODO: assert report file content
expect(reporterConfig2.specFailure.calls.count()).toEqual(3);
expect(isReportFileCreated('reports/config2/ie/result.xml')).toBe(true);
});
});
});
function reportFileCreated(browserName) {
return fs.existsSync(filePath(browserName));
describe('Karma runner configured to multiple browsers', function() {
describe('Reporter using default configuration', function() {
it('All test cases passed', function() {
reporterDefault.onSpecComplete({name: firefox}, { fullName: 's1 d1',
success: true, suite: ['s1'], description: 'd1', time: '1'});
reporterDefault.onSpecComplete({name: chrome}, { fullName: 's1 d1',
success: true, suite: ['s1'], description: 'd1', time: '1'});
reporterDefault.onSpecComplete({name: ie}, { fullName: 's1 d1',
success: true, suite: ['s1'], description: 'd1', time: '1'});
reporterDefault.onRunComplete({}, {});
// TODO: assert specSuccess arguments
// TODO: assert report file content
expect(reporterDefault.specSuccess.calls.count()).toEqual(3);
expect(isReportFileCreated('reports/firefox.1.0.0.linux.1.0.0.xml')).toBe(true);
expect(isReportFileCreated('reports/chrome.1.0.0.linux.1.0.0.xml')).toBe(true);
expect(isReportFileCreated('reports/ie.1.0.0.linux.1.0.0.xml')).toBe(true);
});
});
describe('Reporter using custom configuration 1', function() {
it('All test cases skipped', function() {
reporterConfig1.onSpecComplete({name: firefox}, { fullName: 's2 d2',
skipped: true, suite: ['s2'], description: 'd2', time: '2'});
reporterConfig1.onSpecComplete({name: chrome}, { fullName: 's2 d2',
skipped: true, suite: ['s2'], description: 'd2', time: '2'});
reporterConfig1.onSpecComplete({name: ie}, { fullName: 's2 d2',
skipped: true, suite: ['s2'], description: 'd2', time: '2'});
reporterConfig1.onRunComplete({}, {});
// TODO: assert specSuccess arguments
// TODO: assert report file content
expect(reporterConfig1.specSkipped.calls.count()).toEqual(3);
expect(isReportFileCreated('reports/config1/firefox.xml')).toBe(true);
expect(isReportFileCreated('reports/config1/chrome.xml')).toBe(true);
expect(isReportFileCreated('reports/config1/ie.xml')).toBe(true);
});
});
describe('Reporter using custom configuration 2', function() {
it('All test cases failed', function() {
reporterConfig2.onSpecComplete({name: firefox}, { fullName: 's3 d3',
suite: ['s3'], description: 'd3', time: '3', log: ['e3']});
reporterConfig2.onSpecComplete({name: chrome}, { fullName: 's3 d3',
suite: ['s3'], description: 'd3', time: '3', log: ['e3']});
reporterConfig2.onSpecComplete({name: ie}, { fullName: 's3 d3',
suite: ['s3'], description: 'd3', time: '3',
log: ['e3.1', 'e3.2']});
reporterConfig2.onRunComplete({}, {});
// TODO: assert specSuccess arguments
// TODO: assert report file content
expect(reporterConfig2.specFailure.calls.count()).toEqual(3);
expect(isReportFileCreated('reports/config2/firefox/result.xml')).toBe(true);
expect(isReportFileCreated('reports/config2/chrome/result.xml')).toBe(true);
expect(isReportFileCreated('reports/config2/ie/result.xml')).toBe(true);
});
});
});
function isReportFileCreated(filePath) {
return fs.existsSync(filePath);
}
function filePath(file) {
return path.join(configFolder(), file);
function clearReports(folder) {
rimraf.sync(folder);
}
function configFolder() {
return config.sonarqubeReporter.outputFolder;
}
});
});
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc