mirror of
https://github.com/usebruno/bruno.git
synced 2024-11-21 23:43:15 +01:00
Fix/postman translation collection variables (#1894)
* fix(postman-translation-collectionVariables): auto translate pm.collectionVariables.set and get * fix(postman-translation-collectionVariables) : additional translations, simplify regex, add testing * fix(postman-translation-collectionVariables): update lock file --------- Co-authored-by: bpoulaindev <bpoulainpro@gmail.com>
This commit is contained in:
parent
8503752e09
commit
0b2b16abcc
838
package-lock.json
generated
838
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
134
packages/bruno-app/src/utils/importers/translators/index.spec.js
Normal file
134
packages/bruno-app/src/utils/importers/translators/index.spec.js
Normal file
@ -0,0 +1,134 @@
|
||||
const { postmanTranslation } = require('./postman_translation'); // Adjust path as needed
|
||||
|
||||
describe('postmanTranslation function', () => {
|
||||
test('should translate pm commands correctly', () => {
|
||||
const inputScript = `
|
||||
pm.environment.get('key');
|
||||
pm.environment.set('key', 'value');
|
||||
pm.variables.get('key');
|
||||
pm.variables.set('key', 'value');
|
||||
pm.collectionVariables.get('key');
|
||||
pm.collectionVariables.set('key', 'value');
|
||||
`;
|
||||
const expectedOutput = `
|
||||
bru.getEnvVar('key');
|
||||
bru.setEnvVar('key', 'value');
|
||||
bru.getVar('key');
|
||||
bru.setVar('key', 'value');
|
||||
bru.getVar('key');
|
||||
bru.setVar('key', 'value');
|
||||
`;
|
||||
expect(postmanTranslation(inputScript)).toBe(expectedOutput);
|
||||
});
|
||||
|
||||
test('should not translate non-pm commands', () => {
|
||||
const inputScript = `
|
||||
console.log('This script does not contain pm commands.');
|
||||
const data = pm.environment.get('key');
|
||||
pm.collectionVariables.set('key', data);
|
||||
`;
|
||||
const expectedOutput = `
|
||||
console.log('This script does not contain pm commands.');
|
||||
const data = bru.getEnvVar('key');
|
||||
bru.setVar('key', data);
|
||||
`;
|
||||
expect(postmanTranslation(inputScript)).toBe(expectedOutput);
|
||||
});
|
||||
|
||||
test('should comment non-translated pm commands', () => {
|
||||
const inputScript = "pm.test('random test', () => pm.response.json());";
|
||||
const expectedOutput = "// test('random test', () => pm.response.json());";
|
||||
expect(postmanTranslation(inputScript)).toBe(expectedOutput);
|
||||
});
|
||||
test('should handle multiple pm commands on the same line', () => {
|
||||
const inputScript = "pm.environment.get('key'); pm.environment.set('key', 'value');";
|
||||
const expectedOutput = "bru.getEnv(var); bru.setEnvVar(var, 'value');";
|
||||
});
|
||||
test('should handle comments and other JavaScript code', () => {
|
||||
const inputScript = `
|
||||
// This is a comment
|
||||
const value = 'test';
|
||||
pm.environment.set('key', value);
|
||||
/*
|
||||
Multi-line comment
|
||||
*/
|
||||
const result = pm.environment.get('key');
|
||||
console.log('Result:', result);
|
||||
`;
|
||||
const expectedOutput = `
|
||||
// This is a comment
|
||||
const value = 'test';
|
||||
bru.setEnvVar('key', value);
|
||||
/*
|
||||
Multi-line comment
|
||||
*/
|
||||
const result = bru.getEnvVar('key');
|
||||
console.log('Result:', result);
|
||||
`;
|
||||
expect(postmanTranslation(inputScript)).toBe(expectedOutput);
|
||||
});
|
||||
|
||||
test('should handle nested commands and edge cases', () => {
|
||||
const inputScript = `
|
||||
const sampleObjects = [
|
||||
{
|
||||
key: pm.environment.get('key'),
|
||||
value: pm.variables.get('value')
|
||||
},
|
||||
{
|
||||
key: pm.collectionVariables.get('key'),
|
||||
value: pm.collectionVariables.get('value')
|
||||
}
|
||||
];
|
||||
const dataTesting = Object.entries(sampleObjects || {}).reduce((acc, [key, value]) => {
|
||||
// this is a comment
|
||||
acc[key] = pm.collectionVariables.get(pm.environment.get(value));
|
||||
return acc; // Return the accumulator
|
||||
}, {});
|
||||
Object.values(dataTesting).forEach((data) => {
|
||||
pm.environment.set(data.key, pm.variables.get(data.value));
|
||||
});
|
||||
`;
|
||||
const expectedOutput = `
|
||||
const sampleObjects = [
|
||||
{
|
||||
key: bru.getEnvVar('key'),
|
||||
value: bru.getVar('value')
|
||||
},
|
||||
{
|
||||
key: bru.getVar('key'),
|
||||
value: bru.getVar('value')
|
||||
}
|
||||
];
|
||||
const dataTesting = Object.entries(sampleObjects || {}).reduce((acc, [key, value]) => {
|
||||
// this is a comment
|
||||
acc[key] = bru.getVar(bru.getEnvVar(value));
|
||||
return acc; // Return the accumulator
|
||||
}, {});
|
||||
Object.values(dataTesting).forEach((data) => {
|
||||
bru.setEnvVar(data.key, bru.getVar(data.value));
|
||||
});
|
||||
`;
|
||||
expect(postmanTranslation(inputScript)).toBe(expectedOutput);
|
||||
});
|
||||
|
||||
test('should handle test commands', () => {
|
||||
const inputScript = `
|
||||
pm.test('Status code is 200', () => {
|
||||
pm.response.to.have.status(200);
|
||||
});
|
||||
pm.test('this test will fail', () => {
|
||||
return false
|
||||
});
|
||||
`;
|
||||
const expectedOutput = `
|
||||
test('Status code is 200', () => {
|
||||
expect(res.getStatus()).to.equal(200);
|
||||
});
|
||||
test('this test will fail', () => {
|
||||
return false
|
||||
});
|
||||
`;
|
||||
expect(postmanTranslation(inputScript)).toBe(expectedOutput);
|
||||
});
|
||||
});
|
@ -1,27 +1,35 @@
|
||||
const replacements = {
|
||||
'pm\\.environment\\.get\\(([\'"])([^\'"]*)\\1\\)': 'bru.getEnvVar($1$2$1)',
|
||||
'pm\\.environment\\.set\\(([\'"])([^\'"]*)\\1, ([\'"])([^\'"]*)\\3\\)': 'bru.setEnvVar($1$2$1, $3$4$3)',
|
||||
'pm\\.variables\\.get\\(([\'"])([^\'"]*)\\1\\)': 'bru.getVar($1$2$1)',
|
||||
'pm\\.variables\\.set\\(([\'"])([^\'"]*)\\1, ([\'"])([^\'"]*)\\3\\)': 'bru.setVar($1$2$1, $3$4$3)'
|
||||
'pm\\.environment\\.get\\(': 'bru.getEnvVar(',
|
||||
'pm\\.environment\\.set\\(': 'bru.setEnvVar(',
|
||||
'pm\\.variables\\.get\\(': 'bru.getVar(',
|
||||
'pm\\.variables\\.set\\(': 'bru.setVar(',
|
||||
'pm\\.collectionVariables\\.get\\(': 'bru.getVar(',
|
||||
'pm\\.collectionVariables\\.set\\(': 'bru.setVar(',
|
||||
'pm\\.setNextRequest\\(': 'bru.setNextRequest(',
|
||||
'pm\\.test\\(': 'test(',
|
||||
'pm.response.to.have\\.status\\(': 'expect(res.getStatus()).to.equal('
|
||||
};
|
||||
|
||||
const compiledReplacements = Object.entries(replacements).map(([pattern, replacement]) => ({
|
||||
regex: new RegExp(pattern, 'g'),
|
||||
replacement
|
||||
}));
|
||||
|
||||
export const postmanTranslation = (script) => {
|
||||
try {
|
||||
const modifiedScript = Object.entries(replacements || {})
|
||||
.map(([pattern, replacement]) => {
|
||||
const regex = new RegExp(pattern, 'g');
|
||||
return script?.replace(regex, replacement);
|
||||
})
|
||||
.find((modified) => modified !== script);
|
||||
if (modifiedScript) {
|
||||
// translation successful
|
||||
return modifiedScript;
|
||||
} else {
|
||||
// non-translatable script
|
||||
return script?.includes('pm.') ? `// ${script}` : script;
|
||||
let modifiedScript = script;
|
||||
let modified = false;
|
||||
for (const { regex, replacement } of compiledReplacements) {
|
||||
if (regex.test(modifiedScript)) {
|
||||
modifiedScript = modifiedScript.replace(regex, replacement);
|
||||
modified = true;
|
||||
}
|
||||
}
|
||||
if (modified && modifiedScript.includes('pm.')) {
|
||||
modifiedScript = modifiedScript.replace(/^(.*pm\..*)$/gm, '// $1');
|
||||
}
|
||||
return modifiedScript;
|
||||
} catch (e) {
|
||||
// non-translatable script
|
||||
return script?.includes('pm.') ? `// ${script}` : script;
|
||||
return script;
|
||||
}
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user