Skip to content

Commit

Permalink
feat: add support to skip request execution from script.
Browse files Browse the repository at this point in the history
  • Loading branch information
vedkribhu committed Oct 31, 2023
1 parent b98d1e3 commit a9bb7c1
Show file tree
Hide file tree
Showing 4 changed files with 387 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
master:
new features:
- GH-1354 Added support for skipping execution through script

7.34.0:
date: 2023-10-19
new features:
Expand Down
35 changes: 32 additions & 3 deletions lib/runner/extensions/event.command.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ var _ = require('lodash'),
EXECUTION_ASSERTION_EVENT_BASE = 'execution.assertion.',
EXECUTION_ERROR_EVENT_BASE = 'execution.error.',
EXECUTION_COOKIES_EVENT_BASE = 'execution.cookies.',
EXECUTION_SKIP_EVENT_BASE = 'execution.skip.',

COOKIES_EVENT_STORE_ACTION = 'store',
COOKIE_STORE_PUT_METHOD = 'putCookie',
Expand Down Expand Up @@ -125,6 +126,11 @@ getCookieDomain = function (fnName, args) {
return domain;
};

const skippedExecutions = new Set(),
isExecutionSkipped = (executionId) => {
return skippedExecutions.has(executionId);
};

/**
* Script execution extension of the runner.
* This module exposes processors for executing scripts before and after requests. Essentially, the processors are
Expand Down Expand Up @@ -158,6 +164,21 @@ module.exports = {
// debug: true
}, run.options.host), function (err, context) {
if (err) { return done(err); }

const originalContextOnHandler = context.on;

// We are overriding the context.on method to ensure for executions
// that are skipped, we don't trigger any events.
context.on = function (eventName, handler) {
originalContextOnHandler.call(context, eventName, function () {
const cursor = arguments && arguments[0];

if (cursor && cursor.execution && isExecutionSkipped(cursor.execution)) { return; }

handler.apply(context, arguments);
});
};

// store the host in run object for future use and move on
run.host = context;

Expand Down Expand Up @@ -251,12 +272,14 @@ module.exports = {
// at this point, the one who queued this event, must ensure that the trigger for it is defined in its
// 'trigger' interface
this.triggers[_.camelCase('before-' + eventName)](null, cursor, events, item);
let shouldSkipExecution = false;

// with all the event listeners in place, we now iterate on them and execute its scripts. post execution,
// we accumulate the results in order to be passed on to the event callback trigger.
async.mapSeries(events, function (event, next) {
// in case the event has no script we bail out early
if (!event.script) {
// In case the event has no script or execution was skipped
// in some previous script we bail out early
if (!event.script || shouldSkipExecution) {
return next(null, { event });
}

Expand Down Expand Up @@ -425,6 +448,10 @@ module.exports = {
}.bind(this));
}.bind(this));

this.host.on(EXECUTION_SKIP_EVENT_BASE + executionId, function () {
skippedExecutions.add(executionId);
});

// finally execute the script
this.host.execute(event, {
id: executionId,
Expand Down Expand Up @@ -516,6 +543,8 @@ module.exports = {
// now that this script is done executing, we trigger the event and move to the next script
this.triggers.script(err || null, scriptCursor, result, script, event, item);

shouldSkipExecution = isExecutionSkipped(executionId);

// move to next script and pass on the results for accumulation
next(((stopOnScriptError || abortOnError || stopOnFailure) && err) ? err : null, _.assign({
event,
Expand All @@ -526,7 +555,7 @@ module.exports = {
}.bind(this), function (err, results) {
// trigger the event completion callback
this.triggers[eventName](null, cursor, results, item);
next((abortOnError && err) ? err : null, results, err);
next((abortOnError && err) ? err : null, results, err, shouldSkipExecution);
}.bind(this));
}
}
Expand Down
10 changes: 9 additions & 1 deletion lib/runner/extensions/item.command.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ module.exports = {
trackContext: ['globals', 'environment', 'collectionVariables'],
stopOnScriptError: stopOnError,
stopOnFailure: stopOnFailure
}).done(function (prereqExecutions, prereqExecutionError) {
}).done(function (prereqExecutions, prereqExecutionError, shouldSkipExecution) {
// if stop on error is marked and script executions had an error,
// do not proceed with more commands, instead we bail out
if ((stopOnError || stopOnFailure) && prereqExecutionError) {
Expand All @@ -163,6 +163,14 @@ module.exports = {
});
}

if (shouldSkipExecution) {
this.triggers.item(prereqExecutionError, coords, item);

return callback && callback.call(this, null, {
prerequest: prereqExecutions
});
}

// update allowed request mutation properties with the mutated context
// @note from this point forward, make sure this mutated
// request instance is used for upcoming commands.
Expand Down
Loading

0 comments on commit a9bb7c1

Please sign in to comment.