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 <bpoulainpro@gmail.com>
This commit is contained in:
Baptiste Poulain 2024-06-19 15:11:39 +02:00 committed by GitHub
parent 9c11e27d1c
commit 1e2b07dead
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 44 additions and 19 deletions

View File

@ -65,20 +65,18 @@ const TranslationLog = ({ translationLog }) => {
</div> </div>
<div className="flex flex-col"> <div className="flex flex-col">
{value.script && ( {value.script && (
<div className="flex items-center text-xs font-light mb-1"> <div className="flex items-center text-xs font-light mb-1 flex-wrap">
<span className="mr-2"> <span className="mr-2">script :</span>
test :
{value.script.map((scriptValue, index) => ( {value.script.map((scriptValue, index) => (
<span className="flex items-center" key={`script_${name}_${index}`}> <span className="flex items-center" key={`script_${name}_${index}`}>
<span className="text-xs font-light">{scriptValue}</span> <span className="text-xs font-light">{scriptValue}</span>
{index < value.script.length - 1 && <> - </>} {index < value.script.length - 1 && <> - </>}
</span> </span>
))} ))}
</span>
</div> </div>
)} )}
{value.test && ( {value.test && (
<div className="flex items-center text-xs font-light mb-1"> <div className="flex items-center text-xs font-light mb-1 flex-wrap">
<span className="mr-2">test :</span> <span className="mr-2">test :</span>
{value.test.map((testValue, index) => ( {value.test.map((testValue, index) => (
<div className="flex items-center" key={`test_${name}_${index}`}> <div className="flex items-center" key={`test_${name}_${index}`}>

View File

@ -54,7 +54,7 @@ const convertV21Auth = (array) => {
}, {}); }, {});
}; };
const translationLog = {}; let translationLog = {};
const importPostmanV2CollectionItem = (brunoParent, item, parentAuth, options) => { const importPostmanV2CollectionItem = (brunoParent, item, parentAuth, options) => {
brunoParent.items = brunoParent.items || []; brunoParent.items = brunoParent.items || [];
@ -394,9 +394,13 @@ const importCollection = (options) => {
.then((collection) => resolve({ collection, translationLog })) .then((collection) => resolve({ collection, translationLog }))
.catch((err) => { .catch((err) => {
console.log(err); console.log(err);
translationLog = {};
reject(new BrunoError('Import collection failed')); reject(new BrunoError('Import collection failed'));
}) })
.then(() => logTranslationDetails(translationLog)); .then(() => {
logTranslationDetails(translationLog);
translationLog = {};
});
}); });
}; };

View File

@ -40,8 +40,8 @@ describe('postmanTranslation function', () => {
}); });
test('should comment non-translated pm commands', () => { test('should comment non-translated pm commands', () => {
const inputScript = "pm.test('random test', () => pm.variables.replaceIn('{{$guid}}'));"; const inputScript = "pm.test('random test', () => postman.variables.replaceIn('{{$guid}}'));";
const expectedOutput = "// test('random test', () => pm.variables.replaceIn('{{$guid}}'));"; const expectedOutput = "// test('random test', () => postman.variables.replaceIn('{{$guid}}'));";
expect(postmanTranslation(inputScript)).toBe(expectedOutput); expect(postmanTranslation(inputScript)).toBe(expectedOutput);
}); });
test('should handle multiple pm commands on the same line', () => { test('should handle multiple pm commands on the same line', () => {
@ -137,3 +137,17 @@ describe('postmanTranslation function', () => {
expect(postmanTranslation(inputScript)).toBe(expectedOutput); 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);
});

View File

@ -13,10 +13,19 @@ const replacements = {
'pm\\.expect\\(': 'expect(', 'pm\\.expect\\(': 'expect(',
'pm\\.environment\\.has\\(([^)]+)\\)': 'bru.getEnvVar($1) !== undefined && bru.getEnvVar($1) !== null', 'pm\\.environment\\.has\\(([^)]+)\\)': 'bru.getEnvVar($1) !== undefined && bru.getEnvVar($1) !== null',
'pm\\.response\\.code': 'res.getStatus()', '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'), regex: new RegExp(pattern, 'g'),
replacement replacement
})); }));
@ -31,8 +40,8 @@ export const postmanTranslation = (script, logCallback) => {
modified = true; modified = true;
} }
} }
if (modifiedScript.includes('pm.')) { if (modifiedScript.includes('pm.') || modifiedScript.includes('postman.')) {
modifiedScript = modifiedScript.replace(/^(.*pm\..*)$/gm, '// $1'); modifiedScript = modifiedScript.replace(/^(.*(pm\.|postman\.).*)$/gm, '// $1');
logCallback?.(); logCallback?.();
} }
return modifiedScript; return modifiedScript;