diff --git a/lib/sandbox/execute-context.js b/lib/sandbox/execute-context.js index 66874c69..07e2bca1 100644 --- a/lib/sandbox/execute-context.js +++ b/lib/sandbox/execute-context.js @@ -23,7 +23,7 @@ module.exports = function (scope, code, execution, console, timers, pmapi, onAss } else { // prepare legacy environment, which adds a tonne of global variables - legacy.setup(scope, execution); + legacy.setup(scope, execution, console); } // prepare the scope's environment variables @@ -54,7 +54,7 @@ module.exports = function (scope, code, execution, console, timers, pmapi, onAss execution.return.async = (timers.queueLength() > 0); // call this hook to perform any post script execution tasks - legacy.finish(scope, pmapi, console, onAssertion); + legacy.finish(scope, pmapi, onAssertion); function complete () { // if timers are running, we do not need to proceed with any logic of completing execution. instead we wait diff --git a/lib/sandbox/postman-legacy-interface.js b/lib/sandbox/postman-legacy-interface.js index 6a76bd8d..41fec233 100644 --- a/lib/sandbox/postman-legacy-interface.js +++ b/lib/sandbox/postman-legacy-interface.js @@ -175,6 +175,22 @@ function raiseAssertionEvent (scope, pmapi, onAssertion) { onAssertion(assertions); } +function logDeprecationWarning (key, legacyUsageSet, console) { + // we've already logged warning once. ignore all next usages + if (legacyUsageSet.has(key)) { + return; + } + + legacyUsageSet.add(key); + + if (LEGACY_GLOBS_ALTERNATIVES[key]) { + console.warn(`Using "${key}" is deprecated. Use "${LEGACY_GLOBS_ALTERNATIVES[key]}" instead.`); + } + else if (LEGACY_GLOBS.includes(key)) { + console.warn(`Using "${key}" is deprecated.`); + } +} + class PostmanLegacyInterface { /** * @param {Object} execution - @@ -279,10 +295,11 @@ module.exports = { * * @param {Uniscope} scope - * @param {Execution} execution - + * @param {Object} console - * * @note ensure that globalvars variables added here are added as part of the LEGACY_GLOBS array */ - setup (scope, execution) { + setup (scope, execution, console) { /** * @name SandboxGlobals * @type {Object} @@ -403,7 +420,7 @@ module.exports = { globalvars.postman = new (execution.target === TARGET_TEST ? PostmanLegacyTestInterface : PostmanLegacyInterface)(execution, globalvars); - scope.__postman_legacy_api_usage = new Set(); + const legacyAPIUsage = new Set(); // wrap all globals to ensure we track their usage to show warnings // on access. @@ -414,7 +431,7 @@ module.exports = { globalvars[key] = new Proxy(globalvars[key], { set (target, prop, value) { - scope.__postman_legacy_api_usage.add(key); + logDeprecationWarning(key, legacyAPIUsage, console); target[prop] = value; }, @@ -423,17 +440,17 @@ module.exports = { // special handling for postman because setNextRequest is // used a lot. if (key === 'postman') { - scope.__postman_legacy_api_usage.add('postman.setNextRequest'); + logDeprecationWarning('postman.setNextRequest', legacyAPIUsage, console); } else { - scope.__postman_legacy_api_usage.add(key); + logDeprecationWarning(key, legacyAPIUsage, console); } return target[prop]; }, apply (target, thisArg, args) { - scope.__postman_legacy_api_usage.add(key); + logDeprecationWarning(key, legacyAPIUsage, console); return target.apply(thisArg, args); } @@ -469,25 +486,13 @@ module.exports = { * * @param {Uniscope} scope - * @param {Object} pmapi - - * @param {Object} console - * @param {Function} onAssertion - */ - finish (scope, pmapi, console, onAssertion) { + finish (scope, pmapi, onAssertion) { if (!scope.__postman_legacy_setup) { return; } - if (scope.__postman_legacy_api_usage) { - scope.__postman_legacy_api_usage.forEach((key) => { - if (LEGACY_GLOBS_ALTERNATIVES[key]) { - console.warn(`Using "${key}" is deprecated. Use "${LEGACY_GLOBS_ALTERNATIVES[key]}" instead.`); - } - else if (LEGACY_GLOBS.includes(key)) { - console.warn(`Using "${key}" is deprecated.`); - } - }); - } - raiseAssertionEvent(scope, pmapi, onAssertion); },