Skip to content

Commit

Permalink
Feat/import translation for deprecated pm import (#3388)
Browse files Browse the repository at this point in the history
* feat: add translation for pm `tests[]`
* feat: add bru.deleteEnvVar function and update translations

---------

Co-authored-by: Pragadesh-45 <[email protected]>
  • Loading branch information
Pragadesh-45 and Pragadesh-45 authored Nov 20, 2024
1 parent 77d3fa7 commit 23c22a9
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 1 deletion.
1 change: 1 addition & 0 deletions packages/bruno-app/src/components/CodeEditor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ if (!SERVER_RENDERED) {
'bru.getFolderVar(key)',
'bru.getCollectionVar(key)',
'bru.setEnvVar(key,value)',
'bru.deleteEnvVar(key)',
'bru.hasVar(key)',
'bru.getVar(key)',
'bru.setVar(key,value)',
Expand Down
16 changes: 16 additions & 0 deletions packages/bruno-app/src/utils/importers/translators/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ describe('postmanTranslation function', () => {
pm.collectionVariables.set('key', 'value');
const data = pm.response.json();
pm.expect(pm.environment.has('key')).to.be.true;
postman.setEnvironmentVariable('key', 'value');
postman.getEnvironmentVariable('key');
postman.clearEnvironmentVariable('key');
`;
const expectedOutput = `
bru.getEnvVar('key');
Expand All @@ -21,6 +24,9 @@ describe('postmanTranslation function', () => {
bru.setVar('key', 'value');
const data = res.getBody();
expect(bru.getEnvVar('key') !== undefined && bru.getEnvVar('key') !== null).to.be.true;
bru.setEnvVar('key', 'value');
bru.getEnvVar('key');
bru.deleteEnvVar('key');
`;
expect(postmanTranslation(inputScript)).toBe(expectedOutput);
});
Expand Down Expand Up @@ -151,3 +157,13 @@ test('should handle response commands', () => {
`;
expect(postmanTranslation(inputScript)).toBe(expectedOutput);
});

test('should handle tests object', () => {
const inputScript = `
tests['Status code is 200'] = responseCode.code === 200;
`;
const expectedOutput = `
test("Status code is 200", function() { expect(Boolean(responseCode.code === 200)).to.be.true; });
`;
expect(postmanTranslation(inputScript)).toBe(expectedOutput);
});
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@ const replacements = {
'pm\\.response\\.code': 'res.getStatus()',
'pm\\.response\\.text\\(': 'res.getBody()?.toString(',
'pm\\.expect\\.fail\\(': 'expect.fail(',
'pm\\.response\\.responseTime': 'res.getResponseTime()'
'pm\\.response\\.responseTime': 'res.getResponseTime()',
"tests\\['([^']+)'\\]\\s*=\\s*([^;]+);": 'test("$1", function() { expect(Boolean($2)).to.be.true; });',
// deprecated translations
'postman\\.setEnvironmentVariable\\(': 'bru.setEnvVar(',
'postman\\.getEnvironmentVariable\\(': 'bru.getEnvVar(',
'postman\\.clearEnvironmentVariable\\(': 'bru.deleteEnvVar(',
};

const extendedReplacements = Object.keys(replacements).reduce((acc, key) => {
Expand Down
4 changes: 4 additions & 0 deletions packages/bruno-js/src/bru.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ class Bru {
this.envVariables[key] = value;
}

deleteEnvVar(key) {
delete this.envVariables[key];
}

getGlobalEnvVar(key) {
return this._interpolate(this.globalEnvironmentVariables[key]);
}
Expand Down

0 comments on commit 23c22a9

Please sign in to comment.