|
| 1 | +"use strict"; |
| 2 | +const request = require("request"), |
| 3 | + config = require("../config"), |
| 4 | + utils = require("../utils"), |
| 5 | + logger = require("../logger").syncCliLogger, |
| 6 | + async = require('async'), |
| 7 | + Constants = require("../constants"), |
| 8 | + tableStream = require('table').createStream, |
| 9 | + chalk = require('chalk'); |
| 10 | + |
| 11 | +let whileLoop = true, whileTries = 40, options, timeout = 3000, n = 10, tableConfig, stream, startTime, endTime; |
| 12 | +let specSummary = { |
| 13 | + "specs": [], |
| 14 | + "duration": null |
| 15 | +} |
| 16 | + |
| 17 | + |
| 18 | +let getOptions = (auth, build_id) => { |
| 19 | + return { |
| 20 | + url: `${config.buildUrl}${build_id}`, |
| 21 | + auth: { |
| 22 | + user: auth.username, |
| 23 | + password: auth.access_key |
| 24 | + }, |
| 25 | + headers: { |
| 26 | + "Content-Type": "application/json", |
| 27 | + "User-Agent": utils.getUserAgent() |
| 28 | + } |
| 29 | + }; |
| 30 | +} |
| 31 | + |
| 32 | +let getTableConfig = () => { |
| 33 | + return { |
| 34 | + border: getBorderConfig(), |
| 35 | + singleLine: true, |
| 36 | + columns: { |
| 37 | + 0: { alignment: 'right' } |
| 38 | + }, |
| 39 | + columnDefault: { |
| 40 | + width: 50 |
| 41 | + }, |
| 42 | + columnCount: 2 |
| 43 | + }; |
| 44 | +} |
| 45 | + |
| 46 | +let getBorderConfig = () => { |
| 47 | + return { |
| 48 | + topBody: `-`, |
| 49 | + topJoin: ``, |
| 50 | + topLeft: ``, |
| 51 | + topRight: ``, |
| 52 | + |
| 53 | + bottomBody: `-`, |
| 54 | + bottomJoin: ``, |
| 55 | + bottomLeft: ``, |
| 56 | + bottomRight: ``, |
| 57 | + |
| 58 | + bodyLeft: ``, |
| 59 | + bodyRight: ``, |
| 60 | + bodyJoin: ``, |
| 61 | + |
| 62 | + joinBody: ``, |
| 63 | + joinLeft: ``, |
| 64 | + joinRight: ``, |
| 65 | + joinJoin: `` |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +let printSpecsStatus = (bsConfig, buildDetails) => { |
| 70 | + return new Promise((resolve, reject) => { |
| 71 | + options = getOptions(bsConfig.auth, buildDetails.build_id) |
| 72 | + tableConfig = getTableConfig(); |
| 73 | + stream = tableStream(tableConfig); |
| 74 | + |
| 75 | + async.whilst( |
| 76 | + function() { // condition for loop |
| 77 | + return whileLoop; |
| 78 | + }, |
| 79 | + function(callback) { // actual loop |
| 80 | + whileProcess(callback) |
| 81 | + }, |
| 82 | + function(err, result) { // when loop ends |
| 83 | + specSummary.duration = endTime - startTime |
| 84 | + logger.info(); |
| 85 | + resolve(specSummary) |
| 86 | + } |
| 87 | + ); |
| 88 | + }); |
| 89 | +}; |
| 90 | + |
| 91 | +let whileProcess = (whilstCallback) => { |
| 92 | + request.post(options, function(error, response, body) { |
| 93 | + if (error) { |
| 94 | + return whilstCallback(error); |
| 95 | + } |
| 96 | + switch (response.statusCode) { |
| 97 | + case 202: // get data here and print it |
| 98 | + n = 2 |
| 99 | + showSpecsStatus(body); |
| 100 | + return setTimeout(whilstCallback, timeout * n, null); |
| 101 | + case 204: // No data available, wait for some time and ask again |
| 102 | + n = 1 |
| 103 | + return setTimeout(whilstCallback, timeout * n, null); |
| 104 | + case 200: // Build is completed. |
| 105 | + whileLoop = false; |
| 106 | + endTime = Date.now(); |
| 107 | + showSpecsStatus(body); |
| 108 | + return whilstCallback(null, body); |
| 109 | + default: |
| 110 | + whileLoop = false; |
| 111 | + return whilstCallback({ status: response.statusCode, message: body }); |
| 112 | + } |
| 113 | + }); |
| 114 | +} |
| 115 | + |
| 116 | +let showSpecsStatus = (data) => { |
| 117 | + let specData = JSON.parse(data); |
| 118 | + specData.forEach(specDetails => { |
| 119 | + if (specDetails == "created") { |
| 120 | + printInitialLog(); |
| 121 | + } else { |
| 122 | + printSpecData(JSON.parse(specDetails)) |
| 123 | + } |
| 124 | + }); |
| 125 | +} |
| 126 | + |
| 127 | +let printInitialLog = () => { |
| 128 | + startTime = Date.now(); |
| 129 | + logger.info(Constants.syncCLI.LOGS.INIT_LOG) |
| 130 | + n = Constants.syncCLI.INITIAL_DELAY_MULTIPLIER |
| 131 | +} |
| 132 | + |
| 133 | +let printSpecData = (data) => { |
| 134 | + let combination = getCombinationName(data["spec"]); |
| 135 | + let status = getStatus(data["spec"]["status"]); |
| 136 | + writeToTable(combination, data["path"], status) |
| 137 | + addSpecToSummary(data["path"], data["spec"]["status"], combination, data["session_id"]) |
| 138 | +} |
| 139 | + |
| 140 | +let writeToTable = (combination, specName, status) => { |
| 141 | + stream.write([combination + ":", `${specName} ${status}`]); |
| 142 | +} |
| 143 | + |
| 144 | +let addSpecToSummary = (specName, status, combination, session_id) => { |
| 145 | + // Format for part 3: {specName: 'spec1.failed.js', status: 'Failed', combination: 'Win 10 / Chrome 78', sessionId: '3d3rdf3r...'}, |
| 146 | + specSummary["specs"].push({ |
| 147 | + "specName": specName, |
| 148 | + "status": status, |
| 149 | + "combination": combination, |
| 150 | + "sessionId": session_id |
| 151 | + }) |
| 152 | +} |
| 153 | + |
| 154 | +let getCombinationName = (spec) => { |
| 155 | + return `${spec["os"]} ${spec["osVersion"]} / ${spec["browser"]} ${spec["browserVersion"]}` |
| 156 | +} |
| 157 | + |
| 158 | +let getStatus = (status) => { |
| 159 | + switch(status) { |
| 160 | + case "passed": |
| 161 | + return chalk.green("✔"); |
| 162 | + case "failed": |
| 163 | + return chalk.red("✘"); |
| 164 | + default: |
| 165 | + return chalk.blue(`[${status}]`); |
| 166 | + } |
| 167 | +} |
| 168 | + |
| 169 | +exports.printSpecsStatus = printSpecsStatus; |
0 commit comments