Skip to content

Commit

Permalink
Fix lint
Browse files Browse the repository at this point in the history
  • Loading branch information
appurva21 committed Sep 25, 2024
1 parent 60e89ae commit 90b05f4
Show file tree
Hide file tree
Showing 6 changed files with 152 additions and 138 deletions.
1 change: 1 addition & 0 deletions examples/run-collections-in-directory.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
98 changes: 51 additions & 47 deletions lib/reporters/cli/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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([{
Expand All @@ -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;
},
Expand Down
101 changes: 52 additions & 49 deletions lib/run/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
16 changes: 10 additions & 6 deletions lib/run/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand Down
72 changes: 37 additions & 35 deletions lib/run/summary.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
}
});
},

Expand Down
2 changes: 1 addition & 1 deletion lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ 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));
});
Expand Down

0 comments on commit 90b05f4

Please sign in to comment.