Skip to content

Commit

Permalink
Add console warning when legacy API is used
Browse files Browse the repository at this point in the history
  • Loading branch information
coditva committed Mar 27, 2024
1 parent ba8bbf4 commit 68b19bb
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/sandbox/execute-context.js
Original file line number Diff line number Diff line change
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, onAssertion);
legacy.finish(scope, pmapi, console, onAssertion);

function complete () {
// if timers are running, we do not need to proceed with any logic of completing execution. instead we wait
Expand Down
42 changes: 41 additions & 1 deletion lib/sandbox/postman-legacy-interface.js
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,42 @@ module.exports = {
globalvars.postman = new (execution.target === TARGET_TEST ?
PostmanLegacyTestInterface : PostmanLegacyInterface)(execution, globalvars);

// wrap all globals to ensure we track their usage to show warnings
// on access.
LEGACY_GLOBS.forEach((key) => {
if (typeof globalvars[key] === 'function') {
const fn = globalvars[key];

globalvars[key] = function () {
scope.__postman_legacy_api_used = true;

return fn.apply(this, arguments);
}

Check failure on line 393 in lib/sandbox/postman-legacy-interface.js

View workflow job for this annotation

GitHub Actions / Lint

Missing semicolon

return;
}

if (typeof globalvars[key] === 'object') {
globalvars[key] = new Proxy(globalvars[key], {
set (target, prop, value) {
scope.__postman_legacy_api_used = true;

return target[prop] = value;

Check failure on line 403 in lib/sandbox/postman-legacy-interface.js

View workflow job for this annotation

GitHub Actions / Lint

Return statement should not contain assignment
},

get (target, prop) {
scope.__postman_legacy_api_used = true;

return target[prop];
}
});

return;

Check failure on line 413 in lib/sandbox/postman-legacy-interface.js

View workflow job for this annotation

GitHub Actions / Lint

Unnecessary return statement
}
});

scope.__postman_legacy_api_used = false;

// make a final pass to ensure that the global variables are present


Expand Down Expand Up @@ -411,11 +447,15 @@ module.exports = {
* @param {Object} pmapi -
* @param {Function} onAssertion -

Check failure on line 448 in lib/sandbox/postman-legacy-interface.js

View workflow job for this annotation

GitHub Actions / Lint

Expected @param names to be "scope, pmapi, console, onAssertion". Got "scope, pmapi, onAssertion"
*/
finish (scope, pmapi, onAssertion) {
finish (scope, pmapi, console, onAssertion) {
if (!scope.__postman_legacy_setup) {
return;
}

if (scope.__postman_legacy_api_used) {
console.warn('Using legacy globals is deprecated.');
}

raiseAssertionEvent(scope, pmapi, onAssertion);
},

Expand Down
88 changes: 88 additions & 0 deletions test/unit/sandbox-libraries/legacy.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
describe('sandbox library - legacy', function () {
this.timeout(1000 * 60);
var Sandbox = require('../../../'),
context;

beforeEach(function (done) {
Sandbox.createContext({}, function (err, ctx) {
context = ctx;
done(err);
});
});

afterEach(function () {
context.dispose();
context = null;
});

it('should not show a warning if no legacy vars used', function (done) {
const consoleSpy = sinon.spy();

context.on('console', consoleSpy);
context.execute(`
pm.iterationData.get('foo');
`, function (err) {
if (err) {
return done(err);
}

expect(consoleSpy).to.not.be.called;
done();
});
});

it('should show warning on using legacy vars', function (done) {
const consoleSpy = sinon.spy();

context.on('console', consoleSpy);
context.execute(`
data['foo'] = 'bar';
`, function (err) {
if (err) {
return done(err);
}

expect(consoleSpy).to.be.calledOnce;
expect(consoleSpy.firstCall.args[1]).to.equal('warn');
expect(consoleSpy.firstCall.args[2]).to.equal('Using legacy globals is deprecated.');
done();
});
});

it('should show a warning on using legacy functions', function (done) {
const consoleSpy = sinon.spy();

context.on('console', consoleSpy);
context.execute(`
atob('a');
`, function (err) {
if (err) {
return done(err);
}

expect(consoleSpy).to.be.calledOnce;
expect(consoleSpy.firstCall.args[1]).to.equal('warn');
expect(consoleSpy.firstCall.args[2]).to.equal('Using legacy globals is deprecated.');
done();
});
});

it('should show a single warning for one execution', function (done) {
const consoleSpy = sinon.spy();

context.on('console', consoleSpy);
context.execute(`
data['foo'] = 'bar';
environment['foo'] = 'bar';
`, function (err) {
if (err) {
return done(err);
}

expect(consoleSpy).to.be.calledOnce;
expect(consoleSpy.firstCall.args[1]).to.equal('warn');
expect(consoleSpy.firstCall.args[2]).to.equal('Using legacy globals is deprecated.');
done();
});
});
});

0 comments on commit 68b19bb

Please sign in to comment.