From 1e2b07dead2d0fc463597f3017bef2acba2d2034 Mon Sep 17 00:00:00 2001 From: Baptiste Poulain <64689165+bpoulaindev@users.noreply.github.com> Date: Wed, 19 Jun 2024 15:11:39 +0200 Subject: [PATCH] Fix postman translation : support postman. ; additional translations (#2453) * fix(postman-translation): cleanup logs after each import, pm.expect.fail(), remove duplicated _test_ key, flex wrap * fix(postman-translation): support for postman. - update jest file --------- Co-authored-by: bpoulaindev --- .../Sidebar/ImportCollectionLocation/index.js | 20 +++++++++---------- .../src/utils/importers/postman-collection.js | 8 ++++++-- .../utils/importers/translators/index.spec.js | 18 +++++++++++++++-- .../translators/postman_translation.js | 17 ++++++++++++---- 4 files changed, 44 insertions(+), 19 deletions(-) diff --git a/packages/bruno-app/src/components/Sidebar/ImportCollectionLocation/index.js b/packages/bruno-app/src/components/Sidebar/ImportCollectionLocation/index.js index 96fade0db..4211e8ff1 100644 --- a/packages/bruno-app/src/components/Sidebar/ImportCollectionLocation/index.js +++ b/packages/bruno-app/src/components/Sidebar/ImportCollectionLocation/index.js @@ -65,20 +65,18 @@ const TranslationLog = ({ translationLog }) => {
{value.script && ( -
- - test : - {value.script.map((scriptValue, index) => ( - - {scriptValue} - {index < value.script.length - 1 && <> - } - - ))} - +
+ script : + {value.script.map((scriptValue, index) => ( + + {scriptValue} + {index < value.script.length - 1 && <> - } + + ))}
)} {value.test && ( -
+
test : {value.test.map((testValue, index) => (
diff --git a/packages/bruno-app/src/utils/importers/postman-collection.js b/packages/bruno-app/src/utils/importers/postman-collection.js index dc291dbdb..0683a3bbc 100644 --- a/packages/bruno-app/src/utils/importers/postman-collection.js +++ b/packages/bruno-app/src/utils/importers/postman-collection.js @@ -54,7 +54,7 @@ const convertV21Auth = (array) => { }, {}); }; -const translationLog = {}; +let translationLog = {}; const importPostmanV2CollectionItem = (brunoParent, item, parentAuth, options) => { brunoParent.items = brunoParent.items || []; @@ -394,9 +394,13 @@ const importCollection = (options) => { .then((collection) => resolve({ collection, translationLog })) .catch((err) => { console.log(err); + translationLog = {}; reject(new BrunoError('Import collection failed')); }) - .then(() => logTranslationDetails(translationLog)); + .then(() => { + logTranslationDetails(translationLog); + translationLog = {}; + }); }); }; 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 6f9d2fb1a..b70691f21 100644 --- a/packages/bruno-app/src/utils/importers/translators/index.spec.js +++ b/packages/bruno-app/src/utils/importers/translators/index.spec.js @@ -40,8 +40,8 @@ describe('postmanTranslation function', () => { }); test('should comment non-translated pm commands', () => { - const inputScript = "pm.test('random test', () => pm.variables.replaceIn('{{$guid}}'));"; - const expectedOutput = "// test('random test', () => pm.variables.replaceIn('{{$guid}}'));"; + const inputScript = "pm.test('random test', () => postman.variables.replaceIn('{{$guid}}'));"; + const expectedOutput = "// test('random test', () => postman.variables.replaceIn('{{$guid}}'));"; expect(postmanTranslation(inputScript)).toBe(expectedOutput); }); test('should handle multiple pm commands on the same line', () => { @@ -137,3 +137,17 @@ describe('postmanTranslation function', () => { expect(postmanTranslation(inputScript)).toBe(expectedOutput); }); }); + +test('should handle response commands', () => { + const inputScript = ` + const responseTime = pm.response.responseTime; + const responseCode = pm.response.code; + const responseText = pm.response.text(); + `; + const expectedOutput = ` + const responseTime = res.getResponseTime(); + const responseCode = res.getStatus(); + const responseText = res.getBody()?.toString(); + `; + 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 7844f91f8..bd60fbcac 100644 --- a/packages/bruno-app/src/utils/importers/translators/postman_translation.js +++ b/packages/bruno-app/src/utils/importers/translators/postman_translation.js @@ -13,10 +13,19 @@ const replacements = { 'pm\\.expect\\(': 'expect(', 'pm\\.environment\\.has\\(([^)]+)\\)': 'bru.getEnvVar($1) !== undefined && bru.getEnvVar($1) !== null', 'pm\\.response\\.code': 'res.getStatus()', - 'pm\\.response\\.text\\(': 'res.getBody()?.toString(' + 'pm\\.response\\.text\\(': 'res.getBody()?.toString(', + 'pm\\.expect\\.fail\\(': 'expect.fail(', + 'pm\\.response\\.responseTime': 'res.getResponseTime()' }; -const compiledReplacements = Object.entries(replacements).map(([pattern, replacement]) => ({ +const extendedReplacements = Object.keys(replacements).reduce((acc, key) => { + const newKey = key.replace(/^pm\\\./, 'postman\\.'); + acc[key] = replacements[key]; + acc[newKey] = replacements[key]; + return acc; +}, {}); + +const compiledReplacements = Object.entries(extendedReplacements).map(([pattern, replacement]) => ({ regex: new RegExp(pattern, 'g'), replacement })); @@ -31,8 +40,8 @@ export const postmanTranslation = (script, logCallback) => { modified = true; } } - if (modifiedScript.includes('pm.')) { - modifiedScript = modifiedScript.replace(/^(.*pm\..*)$/gm, '// $1'); + if (modifiedScript.includes('pm.') || modifiedScript.includes('postman.')) { + modifiedScript = modifiedScript.replace(/^(.*(pm\.|postman\.).*)$/gm, '// $1'); logCallback?.(); } return modifiedScript;