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