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:
Baptiste Poulain 2024-03-22 14:07:30 +01:00 committed by GitHub
parent 8503752e09
commit 0b2b16abcc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 566 additions and 450 deletions

838
package-lock.json generated

File diff suppressed because it is too large Load Diff

View 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);
});
});

View File

@ -1,27 +1,35 @@
const replacements = { const replacements = {
'pm\\.environment\\.get\\(([\'"])([^\'"]*)\\1\\)': 'bru.getEnvVar($1$2$1)', 'pm\\.environment\\.get\\(': 'bru.getEnvVar(',
'pm\\.environment\\.set\\(([\'"])([^\'"]*)\\1, ([\'"])([^\'"]*)\\3\\)': 'bru.setEnvVar($1$2$1, $3$4$3)', 'pm\\.environment\\.set\\(': 'bru.setEnvVar(',
'pm\\.variables\\.get\\(([\'"])([^\'"]*)\\1\\)': 'bru.getVar($1$2$1)', 'pm\\.variables\\.get\\(': 'bru.getVar(',
'pm\\.variables\\.set\\(([\'"])([^\'"]*)\\1, ([\'"])([^\'"]*)\\3\\)': 'bru.setVar($1$2$1, $3$4$3)' '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) => { export const postmanTranslation = (script) => {
try { try {
const modifiedScript = Object.entries(replacements || {}) let modifiedScript = script;
.map(([pattern, replacement]) => { let modified = false;
const regex = new RegExp(pattern, 'g'); for (const { regex, replacement } of compiledReplacements) {
return script?.replace(regex, replacement); if (regex.test(modifiedScript)) {
}) modifiedScript = modifiedScript.replace(regex, replacement);
.find((modified) => modified !== script); modified = true;
if (modifiedScript) { }
// translation successful
return modifiedScript;
} else {
// non-translatable script
return script?.includes('pm.') ? `// ${script}` : script;
} }
if (modified && modifiedScript.includes('pm.')) {
modifiedScript = modifiedScript.replace(/^(.*pm\..*)$/gm, '// $1');
}
return modifiedScript;
} catch (e) { } catch (e) {
// non-translatable script return script;
return script?.includes('pm.') ? `// ${script}` : script;
} }
}; };