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 e0767cd
Show file tree
Hide file tree
Showing 3 changed files with 126 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
38 changes: 37 additions & 1 deletion lib/sandbox/postman-legacy-interface.js
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,37 @@ 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);
};
}
else if (typeof globalvars[key] === 'object') {
globalvars[key] = new Proxy(globalvars[key], {
set (target, prop, value) {
scope.__postman_legacy_api_used = true;

target[prop] = value;
},

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

return target[prop];
}
});
}
});

scope.__postman_legacy_api_used = false;

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


Expand Down Expand Up @@ -409,13 +440,18 @@ module.exports = {
*
* @param {Uniscope} scope -
* @param {Object} pmapi -
* @param {Object} console -
* @param {Function} 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 e0767cd

Please sign in to comment.