From c30bcfde4ed3c5e0fc2c6fdd01cbed00bab71b61 Mon Sep 17 00:00:00 2001 From: Appurva Murawat Date: Wed, 25 Sep 2024 22:09:49 +0530 Subject: [PATCH] Fix lint --- examples/run-collections-in-directory.js | 1 + lib/reporters/cli/index.js | 98 +++++++++++----------- lib/run/index.js | 101 ++++++++++++----------- lib/run/options.js | 16 ++-- lib/run/summary.js | 72 ++++++++-------- lib/util.js | 4 +- 6 files changed, 154 insertions(+), 138 deletions(-) diff --git a/examples/run-collections-in-directory.js b/examples/run-collections-in-directory.js index 22e24c32b..6b00656ef 100755 --- a/examples/run-collections-in-directory.js +++ b/examples/run-collections-in-directory.js @@ -19,6 +19,7 @@ fs.readdir('./examples', function (err, files) { newman.run({ // we load collection using require. for better validation and handling // JSON.parse could be used + // eslint-disable-next-line n/no-path-concat collection: require(`${__dirname}/${file}`) }, function (err) { // finally, when the collection executes, print the status diff --git a/lib/reporters/cli/index.js b/lib/reporters/cli/index.js index 2bccf8db5..cfe773697 100644 --- a/lib/reporters/cli/index.js +++ b/lib/reporters/cli/index.js @@ -443,38 +443,40 @@ _.assignIn(PostmanCLIReporter, { }); // add specific rows to show in summary - stats && _.forEach([{ - source: 'iterations', - label: 'iterations' - }, { - source: 'requests', - label: 'requests' - }, { - source: 'testScripts', - label: 'test-scripts' - }, { - source: 'prerequestScripts', - label: 'prerequest-scripts' - }, { - source: 'assertions', - label: 'assertions' - }], function (row) { - var metric = stats[row.source], - label = row.label; - - // colour the label based on the failure or pending count of the metric - label = metric.failed ? colors.red(label) : (metric.pending ? label : colors.green(label)); - - // push the statistics - summaryTable.push([ - label, - metric.total, - (metric.failed ? colors.red(metric.failed) : metric.failed) - // @todo - add information of pending scripts - // (metric.failed ? colors.red(metric.failed) : metric.failed) + - // (metric.pending ? format(' (%d pending)', metric.pending) : E) - ]); - }); + if (stats) { + _.forEach([{ + source: 'iterations', + label: 'iterations' + }, { + source: 'requests', + label: 'requests' + }, { + source: 'testScripts', + label: 'test-scripts' + }, { + source: 'prerequestScripts', + label: 'prerequest-scripts' + }, { + source: 'assertions', + label: 'assertions' + }], function (row) { + var metric = stats[row.source], + label = row.label; + + // colour the label based on the failure or pending count of the metric + label = metric.failed ? colors.red(label) : (metric.pending ? label : colors.green(label)); + + // push the statistics + summaryTable.push([ + label, + metric.total, + (metric.failed ? colors.red(metric.failed) : metric.failed) + // @todo - add information of pending scripts + // (metric.failed ? colors.red(metric.failed) : metric.failed) + + // (metric.pending ? format(' (%d pending)', metric.pending) : E) + ]); + }); + } // add the total execution time to summary timings && summaryTable.push([{ @@ -491,21 +493,23 @@ _.assignIn(PostmanCLIReporter, { }]); // add rows containing average time of different request phases - timings && _.forEach({ - response: 'average response time:', - dns: 'average DNS lookup time:', - firstByte: 'average first byte time:' - }, (value, key) => { - timings[`${key}Average`] && summaryTable.push([{ - colSpan: 3, - content: format(`${value} %s [min: %s, max: %s, s.d.: %s]`, - util.prettyms(timings[`${key}Average`]), - util.prettyms(timings[`${key}Min`]), - util.prettyms(timings[`${key}Max`]), - util.prettyms(timings[`${key}Sd`])), - hAlign: 'left' - }]); - }); + if (timings) { + _.forEach({ + response: 'average response time:', + dns: 'average DNS lookup time:', + firstByte: 'average first byte time:' + }, (value, key) => { + timings[`${key}Average`] && summaryTable.push([{ + colSpan: 3, + content: format(`${value} %s [min: %s, max: %s, s.d.: %s]`, + util.prettyms(timings[`${key}Average`]), + util.prettyms(timings[`${key}Min`]), + util.prettyms(timings[`${key}Max`]), + util.prettyms(timings[`${key}Sd`])), + hAlign: 'left' + }]); + }); + } return summaryTable; }, diff --git a/lib/run/index.js b/lib/run/index.js index b63409691..9898da6a0 100644 --- a/lib/run/index.js +++ b/lib/run/index.js @@ -360,64 +360,67 @@ module.exports = function (options, callback) { // initialise all the reporters !emitter.reporters && (emitter.reporters = {}); - _.isArray(reporters) && _.forEach(reporters, function (reporterName) { - // disallow duplicate reporter initialisation - if (_.has(emitter.reporters, reporterName)) { return; } - var Reporter; + if (_.isArray(reporters)) { + _.forEach(reporters, function (reporterName) { + // disallow duplicate reporter initialisation + if (_.has(emitter.reporters, reporterName)) { return; } - try { - // check if the reporter is an external reporter - Reporter = require((function (name) { // ensure scoped packages are loaded - var prefix = '', - scope = (name.charAt(0) === '@') && name.substr(0, name.indexOf('/') + 1); + var Reporter; - if (scope) { - prefix = scope; - name = name.substr(scope.length); - } + try { + // check if the reporter is an external reporter + Reporter = require((function (name) { // ensure scoped packages are loaded + var prefix = '', + scope = (name.charAt(0) === '@') && name.substr(0, name.indexOf('/') + 1); - return prefix + 'newman-reporter-' + name; - }(reporterName))); - } - // @todo - maybe have a debug mode and log error there - catch (error) { - if (!defaultReporters[reporterName]) { - // @todo: route this via print module to respect silent flags - console.warn(`newman: could not find "${reporterName}" reporter`); - console.warn(' ensure that the reporter is installed in the same directory as newman'); - - // print install instruction in case a known reporter is missing - if (knownReporterErrorMessages[reporterName]) { - console.warn(knownReporterErrorMessages[reporterName]); - } - else { - console.warn(' please install reporter using npm\n'); + if (scope) { + prefix = scope; + name = name.substr(scope.length); + } + + return prefix + 'newman-reporter-' + name; + }(reporterName))); + } + // @todo - maybe have a debug mode and log error there + catch (error) { + if (!defaultReporters[reporterName]) { + // @todo: route this via print module to respect silent flags + console.warn(`newman: could not find "${reporterName}" reporter`); + console.warn(' ensure that the reporter is installed in the same directory as newman'); + + // print install instruction in case a known reporter is missing + if (knownReporterErrorMessages[reporterName]) { + console.warn(knownReporterErrorMessages[reporterName]); + } + else { + console.warn(' please install reporter using npm\n'); + } } } - } - // load local reporter if its not an external reporter - !Reporter && (Reporter = defaultReporters[reporterName]); + // load local reporter if its not an external reporter + !Reporter && (Reporter = defaultReporters[reporterName]); - try { - // we could have checked _.isFunction(Reporter), here, but we do not do that so that the nature of - // reporter error can be bubbled up - Reporter && (emitter.reporters[reporterName] = new Reporter(emitter, - _.get(options, ['reporter', reporterName], {}), options)); - } - catch (error) { - // if the reporter errored out during initialisation, we should not stop the run simply log - // the error stack trace for debugging - console.warn(`newman: could not load "${reporterName}" reporter`); - - if (!defaultReporters[reporterName]) { - // @todo: route this via print module to respect silent flags - console.warn(` this seems to be a problem in the "${reporterName}" reporter.\n`); + try { + // we could have checked _.isFunction(Reporter), here, but we do + // not do that so that the nature of reporter error can be bubbled up + Reporter && (emitter.reporters[reporterName] = new Reporter(emitter, + _.get(options, ['reporter', reporterName], {}), options)); } - console.warn(error); - } - }); + catch (error) { + // if the reporter errored out during initialisation, we should not stop the run simply log + // the error stack trace for debugging + console.warn(`newman: could not load "${reporterName}" reporter`); + + if (!defaultReporters[reporterName]) { + // @todo: route this via print module to respect silent flags + console.warn(` this seems to be a problem in the "${reporterName}" reporter.\n`); + } + console.warn(error); + } + }); + } // raise warning when more than one dominant reporters are used (function (reporters) { diff --git a/lib/run/options.js b/lib/run/options.js index 19c876ce2..ddb31ea19 100644 --- a/lib/run/options.js +++ b/lib/run/options.js @@ -383,13 +383,17 @@ module.exports = function (options, callback) { config.get(options, { loaders: configLoaders, command: 'run' }, function (err, result) { if (err) { return callback(err); } - !_.isEmpty(options.globalVar) && _.forEach(options.globalVar, function (variable) { - variable && (result.globals.set(variable.key, variable.value)); - }); + if (!_.isEmpty(options.globalVar)) { + _.forEach(options.globalVar, function (variable) { + variable && (result.globals.set(variable.key, variable.value)); + }); + } - !_.isEmpty(options.envVar) && _.forEach(options.envVar, function (variable) { - variable && (result.environment.set(variable.key, variable.value)); - }); + if (!_.isEmpty(options.envVar)) { + _.forEach(options.envVar, function (variable) { + variable && (result.environment.set(variable.key, variable.value)); + }); + } callback(null, result); }); diff --git a/lib/run/summary.js b/lib/run/summary.js index 932dcf79b..78208aac6 100644 --- a/lib/run/summary.js +++ b/lib/run/summary.js @@ -319,43 +319,45 @@ _.assign(RunSummary, { timings = timings && timings.timings; timingPhases = timings && sdk.Response.timingPhases(timings); - (timingPhases || time) && _.forEach([ - 'dns', - 'firstByte', - 'response' - ], (value) => { - var currentValue = (value === 'response') ? time : (timingPhases && timingPhases[value]), - previousAverage = summary.run.timings[`${value}Average`], - previousVariance = summary.run.timings[`${value}Sd`] ** 2, - delta1 = currentValue - previousAverage, - delta2, - currentVariance; - - if (!currentValue) { return; } - - // compute average time for the given phase of request - summary.run.timings[`${value}Average`] = - (previousAverage * (requestCount - 1) + currentValue) / requestCount; - - // compute minimum time for the given phase of request - if (!summary.run.timings[`${value}Min`]) { - summary.run.timings[`${value}Min`] = currentValue; - } - else { - summary.run.timings[`${value}Min`] = - Math.min(summary.run.timings[`${value}Min`], currentValue); - } + if (timingPhases || time) { + _.forEach([ + 'dns', + 'firstByte', + 'response' + ], (value) => { + var currentValue = (value === 'response') ? time : (timingPhases && timingPhases[value]), + previousAverage = summary.run.timings[`${value}Average`], + previousVariance = summary.run.timings[`${value}Sd`] ** 2, + delta1 = currentValue - previousAverage, + delta2, + currentVariance; + + if (!currentValue) { return; } + + // compute average time for the given phase of request + summary.run.timings[`${value}Average`] = + (previousAverage * (requestCount - 1) + currentValue) / requestCount; + + // compute minimum time for the given phase of request + if (!summary.run.timings[`${value}Min`]) { + summary.run.timings[`${value}Min`] = currentValue; + } + else { + summary.run.timings[`${value}Min`] = + Math.min(summary.run.timings[`${value}Min`], currentValue); + } - // compute maximum time the given phase of request - summary.run.timings[`${value}Max`] = Math.max(summary.run.timings[`${value}Max`], currentValue); + // compute maximum time the given phase of request + summary.run.timings[`${value}Max`] = Math.max(summary.run.timings[`${value}Max`], currentValue); - // compute standard deviation for the given phase of request - // refer Welford's online algorithm from - // https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance - delta2 = currentValue - summary.run.timings[`${value}Average`]; - currentVariance = (previousVariance * (requestCount - 1) + (delta1 * delta2)) / requestCount; - summary.run.timings[`${value}Sd`] = Math.sqrt(currentVariance); - }); + // compute standard deviation for the given phase of request + // refer Welford's online algorithm from + // https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance + delta2 = currentValue - summary.run.timings[`${value}Average`]; + currentVariance = (previousVariance * (requestCount - 1) + (delta1 * delta2)) / requestCount; + summary.run.timings[`${value}Sd`] = Math.sqrt(currentVariance); + }); + } }); }, diff --git a/lib/util.js b/lib/util.js index 12119b5e6..5baac6653 100644 --- a/lib/util.js +++ b/lib/util.js @@ -95,10 +95,12 @@ util = { * @returns {Object} - {event1: time1, event2: time2, ...} (time in string with appropriate unit) */ beautifyTime: function (obj) { - return _.forEach(obj, (value, key) => { + _.forEach(obj, (value, key) => { // convert only non-zero values value && (obj[key] = this.prettyms(value)); }); + + return obj; }, /**