Skip to content

Commit

Permalink
Add warning when the legacy API is first used
Browse files Browse the repository at this point in the history
  • Loading branch information
coditva committed Apr 2, 2024
1 parent 598985a commit f3946ba
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 21 deletions.
4 changes: 2 additions & 2 deletions lib/sandbox/execute-context.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
43 changes: 24 additions & 19 deletions lib/sandbox/postman-legacy-interface.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 -
Expand Down Expand Up @@ -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}
Expand Down Expand Up @@ -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.
Expand All @@ -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;
},
Expand All @@ -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);
}
Expand Down Expand Up @@ -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);
},

Expand Down

0 comments on commit f3946ba

Please sign in to comment.