junit-report-builder
Advanced tools
Comparing version 3.1.0 to 3.2.0
{ | ||
"name": "junit-report-builder", | ||
"version": "3.1.0", | ||
"version": "3.2.0", | ||
"description": "Aimed at making it easier to build Jenkins compatible JUnit XML reports in plugins for testing frameworks", | ||
@@ -33,3 +33,2 @@ "main": "src/index.js", | ||
"devDependencies": { | ||
"coffeescript": "^2.7.0", | ||
"grunt": "^1.6.1", | ||
@@ -36,0 +35,0 @@ "grunt-contrib-jshint": "^3.2.0", |
@@ -65,2 +65,7 @@ # junit-report-builder | ||
### 3.2.0 | ||
- Support name and test count attributes for the root test suites element. Thanks to [Simeon Cheeseman](https://github.com/SimeonC). | ||
- Describe parameter types and return types with JSDoc. Thanks to [Simeon Cheeseman](https://github.com/SimeonC). | ||
### 3.1.0 | ||
@@ -67,0 +72,0 @@ |
@@ -1,43 +0,64 @@ | ||
var _ = require('lodash'); | ||
var xmlBuilder = require('xmlbuilder'); | ||
// @ts-check | ||
var path = require('path'); | ||
var makeDir = require('make-dir'); | ||
var fs = require('fs'); | ||
var TestSuite = require('./test_suite'); | ||
var TestCase = require('./test_case'); | ||
var { TestSuites } = require('./test_suites'); | ||
function JUnitReportBuilder(factory) { | ||
this._factory = factory; | ||
this._testSuitesAndCases = []; | ||
} | ||
class JUnitReportBuilder { | ||
/** | ||
* @param {import('./factory').Factory} factory | ||
*/ | ||
constructor(factory) { | ||
this._factory = factory; | ||
this._rootTestSuites = new TestSuites(factory); | ||
} | ||
JUnitReportBuilder.prototype.writeTo = function (reportPath) { | ||
makeDir.sync(path.dirname(reportPath)); | ||
fs.writeFileSync(reportPath, this.build(), 'utf8'); | ||
}; | ||
/** | ||
* @param {string} reportPath | ||
*/ | ||
writeTo(reportPath) { | ||
makeDir.sync(path.dirname(reportPath)); | ||
fs.writeFileSync(reportPath, this.build(), 'utf8'); | ||
} | ||
JUnitReportBuilder.prototype.build = function () { | ||
var xmlTree = xmlBuilder.create('testsuites', { encoding: 'UTF-8', invalidCharReplacement: '' }); | ||
_.forEach(this._testSuitesAndCases, function (suiteOrCase) { | ||
suiteOrCase.build(xmlTree); | ||
}); | ||
return xmlTree.end({ pretty: true }); | ||
}; | ||
/** | ||
* @returns {string} | ||
*/ | ||
build() { | ||
var xmlTree = this._rootTestSuites.build(); | ||
return xmlTree.end({ pretty: true }); | ||
} | ||
JUnitReportBuilder.prototype.testSuite = function () { | ||
var suite = this._factory.newTestSuite(); | ||
this._testSuitesAndCases.push(suite); | ||
return suite; | ||
}; | ||
/** | ||
* @param {string} name | ||
* @returns {JUnitReportBuilder} | ||
* @chainable | ||
*/ | ||
name(name) { | ||
this._rootTestSuites.name(name); | ||
return this; | ||
} | ||
JUnitReportBuilder.prototype.testCase = function () { | ||
var testCase = this._factory.newTestCase(); | ||
this._testSuitesAndCases.push(testCase); | ||
return testCase; | ||
}; | ||
/** | ||
* @returns {import('./test_suite').TestSuite} | ||
*/ | ||
testSuite() { | ||
return this._rootTestSuites.testSuite(); | ||
} | ||
JUnitReportBuilder.prototype.newBuilder = function () { | ||
return this._factory.newBuilder(); | ||
}; | ||
/** | ||
* @returns {import('./test_case').TestCase} | ||
*/ | ||
testCase() { | ||
return this._rootTestSuites.testCase(); | ||
} | ||
module.exports = JUnitReportBuilder; | ||
/** | ||
* @returns {JUnitReportBuilder} | ||
*/ | ||
newBuilder() { | ||
return this._factory.newBuilder(); | ||
} | ||
} | ||
module.exports = { Builder: JUnitReportBuilder }; |
@@ -1,19 +0,36 @@ | ||
var Builder = require('./builder'); | ||
var TestSuite = require('./test_suite'); | ||
var TestCase = require('./test_case'); | ||
var { Builder } = require('./builder'); | ||
var { TestSuites } = require('./test_suites'); | ||
var { TestSuite } = require('./test_suite'); | ||
var { TestCase } = require('./test_case'); | ||
function Factory() {} | ||
class Factory { | ||
/** | ||
* @returns {import('./builder').Builder} | ||
*/ | ||
newBuilder() { | ||
return new Builder(this); | ||
} | ||
Factory.prototype.newBuilder = function () { | ||
return new Builder(this); | ||
}; | ||
/** | ||
* @returns {import('./test_suite').TestSuite} | ||
*/ | ||
newTestSuite() { | ||
return new TestSuite(this); | ||
} | ||
Factory.prototype.newTestSuite = function () { | ||
return new TestSuite(this); | ||
}; | ||
/** | ||
* @returns {import('./test_case').TestCase} | ||
*/ | ||
newTestCase() { | ||
return new TestCase(this); | ||
} | ||
Factory.prototype.newTestCase = function () { | ||
return new TestCase(this); | ||
}; | ||
/** | ||
* @returns {import('./test_suites').TestSuites} | ||
*/ | ||
newTestSuites() { | ||
return new TestSuites(this); | ||
} | ||
} | ||
module.exports = Factory; | ||
module.exports = { Factory: Factory }; |
@@ -1,3 +0,3 @@ | ||
var Factory = require('./factory'); | ||
var { Factory } = require('./factory'); | ||
module.exports = new Factory().newBuilder(); |
@@ -0,144 +1,195 @@ | ||
// @ts-check | ||
var _ = require('lodash'); | ||
function TestCase() { | ||
this._error = false; | ||
this._failure = false; | ||
this._skipped = false; | ||
this._standardOutput = undefined; | ||
this._standardError = undefined; | ||
this._stacktrace = undefined; | ||
this._attributes = {}; | ||
this._errorAttributes = {}; | ||
this._failureAttributes = {}; | ||
this._errorAttachment = undefined; | ||
this._errorContent = undefined; | ||
this._properties = []; | ||
} | ||
var { TestNode } = require('./test_node'); | ||
TestCase.prototype.className = function (className) { | ||
this._attributes.classname = className; | ||
return this; | ||
}; | ||
class TestCase extends TestNode { | ||
/** | ||
* @param {import('./factory').Factory} factory | ||
*/ | ||
constructor(factory) { | ||
super(factory, 'testcase'); | ||
this._error = false; | ||
this._failure = false; | ||
this._skipped = false; | ||
this._standardOutput = undefined; | ||
this._standardError = undefined; | ||
this._stacktrace = undefined; | ||
this._errorAttributes = {}; | ||
this._failureAttributes = {}; | ||
this._errorAttachment = undefined; | ||
this._errorContent = undefined; | ||
} | ||
TestCase.prototype.name = function (name) { | ||
this._attributes.name = name; | ||
return this; | ||
}; | ||
/** | ||
* @param {string} className | ||
* @returns {TestCase} | ||
* @chainable | ||
*/ | ||
className(className) { | ||
this._attributes.classname = className; | ||
return this; | ||
} | ||
TestCase.prototype.time = function (timeInSeconds) { | ||
this._attributes.time = timeInSeconds; | ||
return this; | ||
}; | ||
/** | ||
* @param {string} filepath | ||
* @returns {TestCase} | ||
* @chainable | ||
*/ | ||
file(filepath) { | ||
this._attributes.file = filepath; | ||
return this; | ||
} | ||
TestCase.prototype.file = function (filepath) { | ||
this._attributes.file = filepath; | ||
return this; | ||
}; | ||
/** | ||
* @param {string} [message] | ||
* @param {string} [type] | ||
* @returns {TestCase} | ||
* @chainable | ||
*/ | ||
failure(message, type) { | ||
this._failure = true; | ||
if (message) { | ||
this._failureAttributes.message = message; | ||
} | ||
if (type) { | ||
this._failureAttributes.type = type; | ||
} | ||
return this; | ||
} | ||
TestCase.prototype.failure = function (message, type) { | ||
this._failure = true; | ||
if (message) { | ||
this._failureAttributes.message = message; | ||
/** | ||
* @param {string} [message] | ||
* @param {string} [type] | ||
* @param {string} [content] | ||
* @returns {TestCase} | ||
* @chainable | ||
*/ | ||
error(message, type, content) { | ||
this._error = true; | ||
if (message) { | ||
this._errorAttributes.message = message; | ||
} | ||
if (type) { | ||
this._errorAttributes.type = type; | ||
} | ||
if (content) { | ||
this._errorContent = content; | ||
} | ||
return this; | ||
} | ||
if (type) { | ||
this._failureAttributes.type = type; | ||
/** | ||
* @param {string} [stacktrace] | ||
* @returns {TestCase} | ||
* @chainable | ||
*/ | ||
stacktrace(stacktrace) { | ||
this._failure = true; | ||
this._stacktrace = stacktrace; | ||
return this; | ||
} | ||
return this; | ||
}; | ||
TestCase.prototype.error = function (message, type, content) { | ||
this._error = true; | ||
if (message) { | ||
this._errorAttributes.message = message; | ||
/** | ||
* @returns {TestCase} | ||
* @chainable | ||
*/ | ||
skipped() { | ||
this._skipped = true; | ||
return this; | ||
} | ||
if (type) { | ||
this._errorAttributes.type = type; | ||
/** | ||
* @param {string} [log] | ||
* @returns {TestCase} | ||
* @chainable | ||
*/ | ||
standardOutput(log) { | ||
this._standardOutput = log; | ||
return this; | ||
} | ||
if (content) { | ||
this._errorContent = content; | ||
/** | ||
* @param {string} [log] | ||
* @returns {TestCase} | ||
* @chainable | ||
*/ | ||
standardError(log) { | ||
this._standardError = log; | ||
return this; | ||
} | ||
return this; | ||
}; | ||
TestCase.prototype.stacktrace = function (stacktrace) { | ||
this._failure = true; | ||
this._stacktrace = stacktrace; | ||
return this; | ||
}; | ||
/** | ||
* @returns {number} | ||
*/ | ||
getTestCaseCount() { | ||
return 1; | ||
} | ||
TestCase.prototype.skipped = function () { | ||
this._skipped = true; | ||
return this; | ||
}; | ||
/** | ||
* @returns {number} | ||
*/ | ||
getFailureCount() { | ||
return Number(this._failure); | ||
} | ||
TestCase.prototype.standardOutput = function (log) { | ||
this._standardOutput = log; | ||
return this; | ||
}; | ||
/** | ||
* @returns {number} | ||
*/ | ||
getErrorCount() { | ||
return Number(this._error); | ||
} | ||
TestCase.prototype.standardError = function (log) { | ||
this._standardError = log; | ||
return this; | ||
}; | ||
/** | ||
* @returns {number} | ||
*/ | ||
getSkippedCount() { | ||
return Number(this._skipped); | ||
} | ||
TestCase.prototype.getFailureCount = function () { | ||
return Number(this._failure); | ||
}; | ||
/** | ||
* | ||
* @param {string} path | ||
* @returns {TestCase} | ||
* @chainable | ||
*/ | ||
errorAttachment(path) { | ||
this._errorAttachment = path; | ||
return this; | ||
} | ||
TestCase.prototype.getErrorCount = function () { | ||
return Number(this._error); | ||
}; | ||
TestCase.prototype.getSkippedCount = function () { | ||
return Number(this._skipped); | ||
}; | ||
TestCase.prototype.errorAttachment = function (path) { | ||
this._errorAttachment = path; | ||
return this; | ||
}; | ||
TestCase.prototype.property = function (name, value) { | ||
this._properties.push({ name: name, value: value }); | ||
return this; | ||
}; | ||
TestCase.prototype.build = function (parentElement) { | ||
var testCaseElement = parentElement.ele('testcase', this._attributes); | ||
if (this._properties.length) { | ||
var propertiesElement = testCaseElement.ele('properties'); | ||
_.forEach(this._properties, function (property) { | ||
propertiesElement.ele('property', { | ||
name: property.name, | ||
value: property.value, | ||
}); | ||
}); | ||
} | ||
if (this._failure) { | ||
var failureElement = testCaseElement.ele('failure', this._failureAttributes); | ||
if (this._stacktrace) { | ||
failureElement.cdata(this._stacktrace); | ||
/** | ||
* @param {import('xmlbuilder').XMLElement} parentElement | ||
*/ | ||
build(parentElement) { | ||
const testCaseElement = this.buildNode(this.createElement(parentElement)); | ||
if (this._failure) { | ||
var failureElement = testCaseElement.ele('failure', this._failureAttributes); | ||
if (this._stacktrace) { | ||
failureElement.cdata(this._stacktrace); | ||
} | ||
} | ||
} | ||
if (this._error) { | ||
var errorElement = testCaseElement.ele('error', this._errorAttributes); | ||
if (this._errorContent) { | ||
errorElement.cdata(this._errorContent); | ||
if (this._error) { | ||
var errorElement = testCaseElement.ele('error', this._errorAttributes); | ||
if (this._errorContent) { | ||
errorElement.cdata(this._errorContent); | ||
} | ||
} | ||
} | ||
if (this._skipped) { | ||
testCaseElement.ele('skipped'); | ||
} | ||
if (this._standardOutput) { | ||
testCaseElement.ele('system-out').cdata(this._standardOutput); | ||
} | ||
var systemError; | ||
if (this._standardError) { | ||
systemError = testCaseElement.ele('system-err').cdata(this._standardError); | ||
if (this._skipped) { | ||
testCaseElement.ele('skipped'); | ||
} | ||
if (this._standardOutput) { | ||
testCaseElement.ele('system-out').cdata(this._standardOutput); | ||
} | ||
var systemError; | ||
if (this._standardError) { | ||
systemError = testCaseElement.ele('system-err').cdata(this._standardError); | ||
if (this._errorAttachment) { | ||
systemError.txt('[[ATTACHMENT|' + this._errorAttachment + ']]'); | ||
if (this._errorAttachment) { | ||
systemError.txt('[[ATTACHMENT|' + this._errorAttachment + ']]'); | ||
} | ||
} | ||
return testCaseElement; | ||
} | ||
}; | ||
} | ||
module.exports = TestCase; | ||
module.exports = { TestCase: TestCase }; |
@@ -0,86 +1,14 @@ | ||
// @ts-check | ||
var _ = require('lodash'); | ||
var formatDate = require('date-format').asString; | ||
var { TestGroup } = require('./test_group'); | ||
function TestSuite(factory) { | ||
this._factory = factory; | ||
this._attributes = {}; | ||
this._testCases = []; | ||
this._properties = []; | ||
class TestSuite extends TestGroup { | ||
/** | ||
* @param {import('./factory').Factory} factory | ||
*/ | ||
constructor(factory) { | ||
super(factory, 'testsuite'); | ||
} | ||
} | ||
TestSuite.prototype.name = function (name) { | ||
this._attributes.name = name; | ||
return this; | ||
}; | ||
TestSuite.prototype.time = function (timeInSeconds) { | ||
this._attributes.time = timeInSeconds; | ||
return this; | ||
}; | ||
TestSuite.prototype.timestamp = function (timestamp) { | ||
if (_.isDate(timestamp)) { | ||
this._attributes.timestamp = formatDate('yyyy-MM-ddThh:mm:ss', timestamp); | ||
} else { | ||
this._attributes.timestamp = timestamp; | ||
} | ||
return this; | ||
}; | ||
TestSuite.prototype.property = function (name, value) { | ||
this._properties.push({ name: name, value: value }); | ||
return this; | ||
}; | ||
TestSuite.prototype.testCase = function () { | ||
var testCase = this._factory.newTestCase(); | ||
this._testCases.push(testCase); | ||
return testCase; | ||
}; | ||
TestSuite.prototype.getFailureCount = function () { | ||
return this._sumTestCaseCounts(function (testCase) { | ||
return testCase.getFailureCount(); | ||
}); | ||
}; | ||
TestSuite.prototype.getErrorCount = function () { | ||
return this._sumTestCaseCounts(function (testCase) { | ||
return testCase.getErrorCount(); | ||
}); | ||
}; | ||
TestSuite.prototype.getSkippedCount = function () { | ||
return this._sumTestCaseCounts(function (testCase) { | ||
return testCase.getSkippedCount(); | ||
}); | ||
}; | ||
TestSuite.prototype._sumTestCaseCounts = function (counterFunction) { | ||
var counts = _.map(this._testCases, counterFunction); | ||
return _.sum(counts); | ||
}; | ||
TestSuite.prototype.build = function (parentElement) { | ||
this._attributes.tests = this._testCases.length; | ||
this._attributes.failures = this.getFailureCount(); | ||
this._attributes.errors = this.getErrorCount(); | ||
this._attributes.skipped = this.getSkippedCount(); | ||
var suiteElement = parentElement.ele('testsuite', this._attributes); | ||
if (this._properties.length) { | ||
var propertiesElement = suiteElement.ele('properties'); | ||
_.forEach(this._properties, function (property) { | ||
propertiesElement.ele('property', { | ||
name: property.name, | ||
value: property.value, | ||
}); | ||
}); | ||
} | ||
_.forEach(this._testCases, function (testCase) { | ||
testCase.build(suiteElement); | ||
}); | ||
}; | ||
module.exports = TestSuite; | ||
module.exports = { TestSuite: TestSuite }; |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
19033
9
14
511
138
1