Code coverage report for preceptor-reporter/lib/listener/teamCity.js

Statements: 93.33% (56 / 60)      Branches: 80.77% (21 / 26)      Functions: 100% (9 / 9)      Lines: 93.33% (56 / 60)      Ignored: none     

All files » preceptor-reporter/lib/listener/ » teamCity.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 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258      1 1             1                   1                         85                                 1                                                                           1                                         1                         1   50   50 49 49     49   49   49     49 4     49   49   49 19 19 19 19 19   30   15 4 4 4 4   11     15 15 15 15           38         1                           49     49 81 81   81     49                           87   87 85   2                             81   81       81 15 66     66         1        
// Copyright 2014, Yahoo! Inc.
// Copyrights licensed under the Mit License. See the accompanying LICENSE file for terms.
 
var AbstractListener = require('../abstractListener');
var _ = require('underscore');
 
/**
 * @class TeamCity listener
 * @extends AbstractListener
 * @constructor
 */
var TeamCityListener = AbstractListener.extend(
 
	{
		/**
		 * Gets the parent-id of the team-city root messages
		 *
		 * @method getParentId
		 * @return {string}
		 */
		getParentId: function () {
			return this.getConfiguration().parentId;
		},
 
 
		/**
		 * Unescapes teamcity messages
		 *
		 * @method _unescape
		 * @param {string} msg
		 * @return {string}
		 * @private
		 */
		_unescape: function (msg) {
			return (msg + '').
				replace(/\|\|/g, "|").
				replace(/\|'/g, "'").
				replace(/\|n/g, "\n").
				replace(/\|r/g, "\r").
				replace(/\|\[/g, "[").
				replace(/\|\]/g, "]");
		},
 
 
		/**
		 * Gets a list of relayed messages
		 *
		 * @method getRelayedMessages
		 * @return {object}
		 */
		getRelayedMessages: function () {
			return {
				'testSuiteStarted': [
					{ name: 'name', defaultValue: 'Undeclared', type: 'string' }
				],
				'testSuiteFinished': [
					{ name: 'name', defaultValue: 'Undeclared', type: 'string' }
				],
				'testStarted': [
					{ name: 'name', defaultValue: 'Undeclared', type: 'string' }
				],
				'testFinished': [
					{ name: 'name', defaultValue: 'Undeclared', type: 'string' },
					{ name: 'duration', defaultValue: '0', type: 'int' }
				],
				'testFailed': [
					{ name: 'name', defaultValue: 'Undeclared', type: 'string' },
					{ name: 'message', defaultValue: '-', type: 'string' },
					{ name: 'details', defaultValue: '-', type: 'string' }
				],
				'testError': [
					{ name: 'name', defaultValue: 'Undeclared', type: 'string' },
					{ name: 'message', defaultValue: '-', type: 'string' },
					{ name: 'details', defaultValue: '-', type: 'string' }
				],
				'testIgnored': [
					{ name: 'name', defaultValue: 'Undeclared', type: 'string' },
					{ name: 'message', defaultValue: '-', type: 'string' }
				]
			};
		},
 
		/**
		 * Gets the mapping from TeamCity into Preceptor format
		 *
		 * @method getMessageMapping
		 * @return {object[]}
		 */
		getMessageMapping: function () {
			return {
				testSuiteStarted: 'suiteStart',
				testSuiteFinished: 'suiteEnd',
				testStarted: 'testStart',
				testFinished: 'testFinished', // Will not be used - possibly relayed to testPassed
				testFailed: 'testFailed',
				testError: 'testError',
				testIgnored: 'testSkipped'
			};
		},
 
		/**
		 * Parses a string and extracts message information
		 *
		 * @method parse
		 * @param {string} text
		 * @param {object} [placeholder]
		 * @return {string}
		 */
		parse: function (text, placeholder) {
 
			var messageType, data,
				localText = text,
				relayedMessaged = this.getRelayedMessages(),
				messageTypes = _.keys(relayedMessaged),
				messageMapping = this.getMessageMapping(),
				match = true,
				args,
				currentId,
				idCounter = 0,
				id = +(new Date()),
				idStack = [this.getParentId()],
				idList = {};
 
			while(match) {
 
				match = localText.match(/^##teamcity\[(\w*?)\s(.*?)]$/m);
 
				if (match) {
					messageType = match[1];
					data = match[2];
 
					// Remove data from stream
					localText = localText.replace(match[0] + "\n", "");
 
					Eif (messageTypes.indexOf(messageType) != -1) {
 
						data = this.processPlaceholder(data, placeholder);
 
						// Convert a failed to an error if parameter given
						if ((messageType == 'testFailed') && this._parseAttribute(data, 'error', false)) {
							messageType = 'testError';
						}
 
						args = this._parseAttributes(data, relayedMessaged[messageType]);
 
						messageType = messageMapping[messageType];
 
						if (['suiteStart', 'testStart'].indexOf(messageType) !== -1) {
							currentId = id + '-' + (++idCounter);
							idList[args[0]] = currentId;
							args.unshift(idList[idStack[0]]);
							args.unshift(currentId);
							idStack.unshift(args[2]);
 
						} else if (['testFinished'].indexOf(messageType) !== -1) {
 
							if (args[0] === idStack[0]) {
								messageType = 'testPassed';
								args.shift();
								args.unshift(idList[idStack[0]]);
								idStack.shift();
							} else {
								continue;
							}
 
						} else Eif (args[0] === idStack[0]) {
							args.shift();
							args.unshift(idList[idStack[0]]);
							idStack.shift();
 
						} else {
							throw new Error('An error was encountered during parsing team-city code: ' + data);
						}
 
						this.triggerMessage(messageType, args);
					}
				}
			}
 
			return localText;
		},
 
 
		/**
		 * Parses all attributes and returns a list of attributes in the correct order
		 *
		 * @method _parseAttributes
		 * @param {string} attributes
		 * @param {object[]} parameters
		 * @return {*[]}
		 * @private
		 */
		_parseAttributes: function (attributes, parameters) {
			var attributeValue,
				args = [];
 
			_.each(parameters, function (parameter) {
				attributeValue = this._parseAttribute(attributes, parameter.name, parameter.defaultValue);
				attributeValue = this._castAttribute(attributeValue, parameter.type);
 
				args.push(attributeValue);
			}, this);
 
			return args;
		},
 
		/**
		 * Parses a specific attribute
		 *
		 * @method _parseAttribute
		 * @param {string} attributes
		 * @param {string} attributeName
		 * @param {*} defaultValue
		 * @return {*}
		 * @private
		 */
		_parseAttribute: function (attributes, attributeName, defaultValue) {
			var match = attributes.match(new RegExp(attributeName + "='(.*?)'\s?"));
 
			if (match) {
				return this._unescape(match[1]);
			} else {
				return defaultValue;
			}
		},
 
		/**
		 * Casts the attribute value to the correct data-type
		 *
		 * @method _castAttribute
		 * @param {string} value
		 * @param {string} type
		 * @return {*}
		 * @private
		 */
		_castAttribute: function (value, type) {
 
			Iif (value === 'null') {
				return null;
			} else Iif (value == 'undefined') {
				return undefined;
			}
 
			if (type == 'int') {
				return value * 1;
			} else Iif (type == 'bool') {
				return !!value;
			} else {
				return value;
			}
		}
	});
 
module.exports = TeamCityListener;