Code coverage report for preceptor-reporter/lib/reporter/junit.js

Statements: 97.67% (42 / 43)      Branches: 87.5% (21 / 24)      Functions: 100% (5 / 5)      Lines: 97.67% (42 / 43)      Ignored: none     

All files » preceptor-reporter/lib/reporter/ » junit.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156      1 1 1 1             1                 1   1 1   1                         2       2   2 2   2                         40     40     2                   2 2     38   8                         8 2   8     8 36     30   30         30 6         24 4         20 4         16 4         12 4             30 8   30         1  
// Copyright 2014, Yahoo! Inc.
// Copyrights licensed under the Mit License. See the accompanying LICENSE file for terms.
 
var AbstractReporter = require('../abstractReporter');
var builder = require('xmlbuilder');
var _ = require('underscore');
var os = require('os');
 
/**
 * @class JUnitReporter
 * @extends AbstractReporter
 * @constructor
 */
var JUnitReporter = AbstractReporter.extend(
 
	{
		/**
		 * Initializes the instance
		 *
		 * @method initialize
		 */
		initialize: function () {
			this.__super();
 
			Eif (this.getOptions().progress === undefined) {
				this.getOptions().progress = false;
			}
			Iif (this.getOptions().output === undefined) {
				this.getOptions().output = false;
			}
		},
 
 
		/**
		 * Gets the collected output
		 *
		 * @method getOutput
		 * @return {string}
		 */
		getOutput: function () {
			var root = {},
				output,
				build;
 
			this._createObjectTree(this.getContainer().getTree(), root);
 
			build = builder.create(root);
			output = build.end({ pretty: true}) + "\n";
 
			return output;
		},
 
		/**
		 * Create sub-tree for xml output
		 *
		 * @method _createObjectTree
		 * @param {object} inputTreeNode
		 * @param {object} outputTreeNode
		 * @private
		 */
		_createObjectTree: function (inputTreeNode, outputTreeNode) {
 
			var testOutcomes = this.getContainer().gatherTestOutcomes(inputTreeNode),
				newNode;
 
			if (inputTreeNode.type === 'root') {
 
				// Add attributes and plain elements
				outputTreeNode.testsuites = {
					'@name': 'Tests',
					'@time': inputTreeNode.duration / 1000,
					'@tests': testOutcomes.tests,
					'@failures': testOutcomes.failed + testOutcomes.undef,
					'@disabled': testOutcomes.incomplete + testOutcomes.skipped,
					'@errors': testOutcomes.error
				};
 
				// Add sub-trees
				_.each(inputTreeNode.children, function (child) {
					this._createObjectTree(child, outputTreeNode.testsuites);
				}, this);
 
			} else if (inputTreeNode.type === 'suite') {
 
				newNode = {
					'@name': inputTreeNode.name,
					'@time': inputTreeNode.duration / 1000,
					'@tests': testOutcomes.tests,
					'@failures': testOutcomes.failed + testOutcomes.undef,
					'@disabled': testOutcomes.incomplete,
					'@skipped': testOutcomes.skipped,
					'@errors': testOutcomes.error,
					'@hostname': os.hostname(),
					'@timestamp': (new Date(inputTreeNode.startTime)).toISOString()
				};
 
				// Add attributes and plain elements
				if (!outputTreeNode['#list']) {
					outputTreeNode['#list'] = [];
				}
				outputTreeNode['#list'].push({ testsuite: newNode });
 
				// Add sub-trees
				_.each(inputTreeNode.children, function (child) {
					this._createObjectTree(child, newNode);
				}, this);
 
			} else Eif (inputTreeNode.type === 'test') {
 
				newNode = {
					'@name': inputTreeNode.name,
					'@time': inputTreeNode.duration / 1000
				};
 
				if (inputTreeNode.outcome === 'skipped') {
					newNode.skipped = {
						'@type': inputTreeNode.reason,
						'@message': inputTreeNode.reason
					};
 
				} else if (inputTreeNode.outcome === 'incomplete') {
					newNode.skipped = {
						'@type': 'incomplete',
						'@message': 'incomplete'
					};
 
				} else if (inputTreeNode.outcome === 'undefined') {
					newNode.skipped = {
						'@type': 'undefined',
						'@message': 'undefined'
					};
 
				} else if (inputTreeNode.outcome === 'error') {
					newNode.error = {
						'@type': inputTreeNode.message,
						'@message': inputTreeNode.reason
					};
 
				} else if (inputTreeNode.outcome === 'failed') {
					newNode.failure = {
						'@type': inputTreeNode.message,
						'@message': inputTreeNode.reason
					};
				}
 
				// Add attributes and plain elements
				if (!outputTreeNode['#list']) {
					outputTreeNode['#list'] = [];
				}
				outputTreeNode['#list'].push({ testcase: newNode });
			}
		}
	});
 
module.exports = JUnitReporter;