From 23c22a96bc32acf44713dd38b84bdfb5df033776 Mon Sep 17 00:00:00 2001 From: Pragadesh-45 <54320162+Pragadesh-45@users.noreply.github.com> Date: Wed, 20 Nov 2024 17:58:59 +0530 Subject: [PATCH] Feat/import translation for deprecated pm import (#3388) * feat: add translation for pm `tests[]` * feat: add bru.deleteEnvVar function and update translations --------- Co-authored-by: Pragadesh-45 --- .../bruno-app/src/components/CodeEditor/index.js | 1 + .../utils/importers/translators/index.spec.js | 16 ++++++++++++++++ .../importers/translators/postman_translation.js | 7 ++++++- packages/bruno-js/src/bru.js | 4 ++++ 4 files changed, 27 insertions(+), 1 deletion(-) diff --git a/packages/bruno-app/src/components/CodeEditor/index.js b/packages/bruno-app/src/components/CodeEditor/index.js index cd91c1b467..61ba430d70 100644 --- a/packages/bruno-app/src/components/CodeEditor/index.js +++ b/packages/bruno-app/src/components/CodeEditor/index.js @@ -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)', diff --git a/packages/bruno-app/src/utils/importers/translators/index.spec.js b/packages/bruno-app/src/utils/importers/translators/index.spec.js index b70691f21d..26b804b001 100644 --- a/packages/bruno-app/src/utils/importers/translators/index.spec.js +++ b/packages/bruno-app/src/utils/importers/translators/index.spec.js @@ -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'); @@ -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); }); @@ -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); +}); diff --git a/packages/bruno-app/src/utils/importers/translators/postman_translation.js b/packages/bruno-app/src/utils/importers/translators/postman_translation.js index ae5cd093d2..bd244673e1 100644 --- a/packages/bruno-app/src/utils/importers/translators/postman_translation.js +++ b/packages/bruno-app/src/utils/importers/translators/postman_translation.js @@ -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) => { diff --git a/packages/bruno-js/src/bru.js b/packages/bruno-js/src/bru.js index fc6f813784..5a4d69426d 100644 --- a/packages/bruno-js/src/bru.js +++ b/packages/bruno-js/src/bru.js @@ -65,6 +65,10 @@ class Bru { this.envVariables[key] = value; } + deleteEnvVar(key) { + delete this.envVariables[key]; + } + getGlobalEnvVar(key) { return this._interpolate(this.globalEnvironmentVariables[key]); }