feat(converters): re-enable variable scripting translations (#8388)

* feat(translations): restore commented-out collection and global variable methods

- Reintroduced translations for `setCollectionVar`, `deleteCollectionVar`, `deleteAllCollectionVars`, and their global counterparts in the Postman to Bruno and Bruno to Postman translators.
- Updated tests to validate the translations for these methods, ensuring accurate functionality and consistency across variable management.
- Removed outdated comments regarding UI updates, reflecting the current state of the implementation.

* feat(tests): add translations for bru.getAllGlobalEnvVars and pm.globals.toObject

- Introduced new test cases to validate the translation of `bru.getAllGlobalEnvVars` to `pm.globals.toObject` and vice versa.
- Enhanced the test suite for both Bruno to Postman and Postman to Bruno translations, ensuring comprehensive coverage for variable management methods.
This commit is contained in:
sanish chirayath
2026-06-30 00:22:56 +05:30
committed by GitHub
parent f6f76423bc
commit 49b9af1046
11 changed files with 114 additions and 63 deletions

View File

@@ -1,8 +1,5 @@
import translateCode from '../utils/postman-to-bruno-translator';
// TODO: Restore the commented-out translations once the UI update fixes are live.
// Currently these APIs only work within the request lifecycle but fail to update the UI tables.
// e.g., setCollectionVar only sets the variable in the request lifecycle, fails to update the table in the UI.
const replacements = {
'pm\\.environment\\.get\\(': 'bru.getEnvVar(',
'pm\\.environment\\.set\\(': 'bru.setEnvVar(',
@@ -10,11 +7,11 @@ const replacements = {
'pm\\.variables\\.set\\(': 'bru.setVar(',
'pm\\.variables\\.replaceIn\\(': 'bru.interpolate(',
'pm\\.collectionVariables\\.get\\(': 'bru.getCollectionVar(',
// 'pm\\.collectionVariables\\.set\\(': 'bru.setCollectionVar(',
'pm\\.collectionVariables\\.set\\(': 'bru.setCollectionVar(',
'pm\\.collectionVariables\\.has\\(': 'bru.hasCollectionVar(',
// 'pm\\.collectionVariables\\.unset\\(': 'bru.deleteCollectionVar(',
// 'pm\\.collectionVariables\\.clear\\(': 'bru.deleteAllCollectionVars(',
// 'pm\\.collectionVariables\\.toObject\\(': 'bru.getAllCollectionVars(',
'pm\\.collectionVariables\\.unset\\(': 'bru.deleteCollectionVar(',
'pm\\.collectionVariables\\.clear\\(': 'bru.deleteAllCollectionVars(',
'pm\\.collectionVariables\\.toObject\\(': 'bru.getAllCollectionVars(',
// Only the actual null literal stops the runner; the string 'null' is a valid
// request name and falls through to setNextRequest.
'pm\\.setNextRequest\\(null\\)': 'bru.runner.stopExecution()',
@@ -32,9 +29,9 @@ const replacements = {
'pm\\.globals\\.set\\(': 'bru.setGlobalEnvVar(',
'pm\\.globals\\.get\\(': 'bru.getGlobalEnvVar(',
'pm\\.globals\\.has\\(': 'bru.hasGlobalEnvVar(',
// 'pm\\.globals\\.unset\\(': 'bru.deleteGlobalEnvVar(',
'pm\\.globals\\.unset\\(': 'bru.deleteGlobalEnvVar(',
'pm\\.globals\\.toObject\\(': 'bru.getAllGlobalEnvVars(',
// 'pm\\.globals\\.clear\\(': 'bru.deleteAllGlobalEnvVars(',
'pm\\.globals\\.clear\\(': 'bru.deleteAllGlobalEnvVars(',
'pm\\.environment\\.toObject\\(': 'bru.getAllEnvVars(',
'pm\\.environment\\.clear\\(': 'bru.deleteAllEnvVars(',
'pm\\.variables\\.toObject\\(': 'bru.getAllVars(',

View File

@@ -13,17 +13,14 @@ const j = require('jscodeshift');
* Simple 1:1 translations from Bruno helpers to Postman helpers.
* These are direct member expression replacements.
*/
// TODO: Restore the commented-out translations once the UI update fixes are live.
// Currently these APIs only work within the request lifecycle but fail to update the UI tables.
// e.g., setCollectionVar only sets the variable in the request lifecycle, fails to update the table in the UI.
const simpleTranslations = {
// Global variables
'bru.getGlobalEnvVar': 'pm.globals.get',
'bru.setGlobalEnvVar': 'pm.globals.set',
'bru.hasGlobalEnvVar': 'pm.globals.has',
// 'bru.deleteGlobalEnvVar': 'pm.globals.unset',
'bru.deleteGlobalEnvVar': 'pm.globals.unset',
'bru.getAllGlobalEnvVars': 'pm.globals.toObject',
// 'bru.deleteAllGlobalEnvVars': 'pm.globals.clear',
'bru.deleteAllGlobalEnvVars': 'pm.globals.clear',
// Environment variables
'bru.getEnvVar': 'pm.environment.get',
@@ -44,11 +41,11 @@ const simpleTranslations = {
// Collection variables
'bru.getCollectionVar': 'pm.collectionVariables.get',
// 'bru.setCollectionVar': 'pm.collectionVariables.set',
'bru.setCollectionVar': 'pm.collectionVariables.set',
'bru.hasCollectionVar': 'pm.collectionVariables.has',
// 'bru.deleteCollectionVar': 'pm.collectionVariables.unset',
// 'bru.getAllCollectionVars': 'pm.collectionVariables.toObject',
// 'bru.deleteAllCollectionVars': 'pm.collectionVariables.clear',
'bru.deleteCollectionVar': 'pm.collectionVariables.unset',
'bru.getAllCollectionVars': 'pm.collectionVariables.toObject',
'bru.deleteAllCollectionVars': 'pm.collectionVariables.clear',
// Folder variables
'bru.getFolderVar': 'pm.variables.get',

View File

@@ -5,18 +5,15 @@ const cloneDeep = require('lodash/cloneDeep');
import { buildStatusAssertionEntries } from './postman-status-assertions';
// Simple 1:1 translations for straightforward replacements
// TODO: Restore the commented-out translations once the UI update fixes are live.
// Currently these APIs only work within the request lifecycle but fail to update the UI tables.
// e.g., setCollectionVar only sets the variable in the request lifecycle, fails to update the table in the UI.
const simpleTranslations = {
// Global Variables
'pm.globals.get': 'bru.getGlobalEnvVar',
'pm.globals.set': 'bru.setGlobalEnvVar',
'pm.globals.has': 'bru.hasGlobalEnvVar',
'pm.globals.replaceIn': 'bru.interpolate',
// 'pm.globals.unset': 'bru.deleteGlobalEnvVar',
'pm.globals.unset': 'bru.deleteGlobalEnvVar',
'pm.globals.toObject': 'bru.getAllGlobalEnvVars',
// 'pm.globals.clear': 'bru.deleteAllGlobalEnvVars',
'pm.globals.clear': 'bru.deleteAllGlobalEnvVars',
// Environment variables
'pm.environment.get': 'bru.getEnvVar',
@@ -35,12 +32,12 @@ const simpleTranslations = {
'pm.variables.replaceIn': 'bru.interpolate',
// Collection variables
'pm.collectionVariables.get': 'bru.getCollectionVar',
// 'pm.collectionVariables.set': 'bru.setCollectionVar',
'pm.collectionVariables.set': 'bru.setCollectionVar',
'pm.collectionVariables.has': 'bru.hasCollectionVar',
// 'pm.collectionVariables.unset': 'bru.deleteCollectionVar',
'pm.collectionVariables.unset': 'bru.deleteCollectionVar',
'pm.collectionVariables.replaceIn': 'bru.interpolate',
// 'pm.collectionVariables.clear': 'bru.deleteAllCollectionVars',
// 'pm.collectionVariables.toObject': 'bru.getAllCollectionVars',
'pm.collectionVariables.clear': 'bru.deleteAllCollectionVars',
'pm.collectionVariables.toObject': 'bru.getAllCollectionVars',
// Testing
'pm.test': 'test',

View File

@@ -57,6 +57,24 @@ describe('Bruno to Postman Variables Translation', () => {
expect(translatedCode).toBe('pm.globals.has("token");');
});
it('should translate bru.deleteGlobalEnvVar', () => {
const code = 'bru.deleteGlobalEnvVar("token");';
const translatedCode = translateBruToPostman(code);
expect(translatedCode).toBe('pm.globals.unset("token");');
});
it('should translate bru.deleteAllGlobalEnvVars', () => {
const code = 'bru.deleteAllGlobalEnvVars();';
const translatedCode = translateBruToPostman(code);
expect(translatedCode).toBe('pm.globals.clear();');
});
it('should translate bru.getAllGlobalEnvVars', () => {
const code = 'const globals = bru.getAllGlobalEnvVars();';
const translatedCode = translateBruToPostman(code);
expect(translatedCode).toBe('const globals = pm.globals.toObject();');
});
// Collection variables tests
it('should translate bru.getCollectionVar', () => {
const code = 'bru.getCollectionVar("baseUrl");';
@@ -64,8 +82,7 @@ describe('Bruno to Postman Variables Translation', () => {
expect(translatedCode).toBe('pm.collectionVariables.get("baseUrl");');
});
// TODO: Restore once UI update fixes are live for setCollectionVar
it.skip('should translate bru.setCollectionVar', () => {
it('should translate bru.setCollectionVar', () => {
const code = 'bru.setCollectionVar("baseUrl", "https://api.example.com");';
const translatedCode = translateBruToPostman(code);
expect(translatedCode).toBe('pm.collectionVariables.set("baseUrl", "https://api.example.com");');
@@ -77,22 +94,19 @@ describe('Bruno to Postman Variables Translation', () => {
expect(translatedCode).toBe('pm.collectionVariables.has("baseUrl");');
});
// TODO: Restore once UI update fixes are live for deleteCollectionVar
it.skip('should translate bru.deleteCollectionVar', () => {
it('should translate bru.deleteCollectionVar', () => {
const code = 'bru.deleteCollectionVar("baseUrl");';
const translatedCode = translateBruToPostman(code);
expect(translatedCode).toBe('pm.collectionVariables.unset("baseUrl");');
});
// TODO: Restore once UI update fixes are live for getAllCollectionVars
it.skip('should translate bru.getAllCollectionVars', () => {
it('should translate bru.getAllCollectionVars', () => {
const code = 'const vars = bru.getAllCollectionVars();';
const translatedCode = translateBruToPostman(code);
expect(translatedCode).toBe('const vars = pm.collectionVariables.toObject();');
});
// TODO: Restore once UI update fixes are live for deleteAllCollectionVars
it.skip('should translate bru.deleteAllCollectionVars', () => {
it('should translate bru.deleteAllCollectionVars', () => {
const code = 'bru.deleteAllCollectionVars();';
const translatedCode = translateBruToPostman(code);
expect(translatedCode).toBe('pm.collectionVariables.clear();');

View File

@@ -12,8 +12,7 @@ describe('postmanTranslations - comment handling', () => {
expect(result).toContain('const data = bru.getEnvVar(\'key\');');
});
// TODO: Restore once UI update fixes are live for setCollectionVar
test.skip('should translate pm.collectionVariables.set to bru.setCollectionVar', () => {
test('should translate pm.collectionVariables.set to bru.setCollectionVar', () => {
const inputScript = 'pm.collectionVariables.set(\'key\', data);';
const expectedOutput = 'bru.setCollectionVar(\'key\', data);';
expect(postmanTranslation(inputScript)).toBe(expectedOutput);

View File

@@ -34,9 +34,33 @@ describe('postmanTranslations - variables commands', () => {
expect(result).toContain('.to.be.true');
});
// TODO: Restore once UI update fixes are live for setCollectionVar
test.skip('should translate pm.collectionVariables.set to bru.setCollectionVar', () => {
test('should translate pm.collectionVariables.set to bru.setCollectionVar', () => {
const inputScript = 'pm.collectionVariables.set(\'key\', \'value\');';
expect(postmanTranslation(inputScript)).toBe('bru.setCollectionVar(\'key\', \'value\');');
});
test('should translate pm.collectionVariables.unset to bru.deleteCollectionVar', () => {
const inputScript = 'pm.collectionVariables.unset(\'key\');';
expect(postmanTranslation(inputScript)).toBe('bru.deleteCollectionVar(\'key\');');
});
test('should translate pm.collectionVariables.clear to bru.deleteAllCollectionVars', () => {
const inputScript = 'pm.collectionVariables.clear();';
expect(postmanTranslation(inputScript)).toBe('bru.deleteAllCollectionVars();');
});
test('should translate pm.collectionVariables.toObject to bru.getAllCollectionVars', () => {
const inputScript = 'const vars = pm.collectionVariables.toObject();';
expect(postmanTranslation(inputScript)).toBe('const vars = bru.getAllCollectionVars();');
});
test('should translate pm.globals.unset to bru.deleteGlobalEnvVar', () => {
const inputScript = 'pm.globals.unset(\'token\');';
expect(postmanTranslation(inputScript)).toBe('bru.deleteGlobalEnvVar(\'token\');');
});
test('should translate pm.globals.clear to bru.deleteAllGlobalEnvVars', () => {
const inputScript = 'pm.globals.clear();';
expect(postmanTranslation(inputScript)).toBe('bru.deleteAllGlobalEnvVars();');
});
});

View File

@@ -100,8 +100,7 @@ describe('Combined API Features Translation', () => {
expect(translatedCode).toContain('bru.setEnvVar("userId", response.user.id);');
});
// TODO: Restore once UI update fixes are live for setCollectionVar
it.skip('should translate pm.collectionVariables.set in a combined code block', () => {
it('should translate pm.collectionVariables.set in a combined code block', () => {
const code = 'pm.collectionVariables.set("sessionId", response.session.id);';
const translatedCode = translateCode(code);
expect(translatedCode).toBe('bru.setCollectionVar("sessionId", response.session.id);');
@@ -114,8 +113,7 @@ describe('Combined API Features Translation', () => {
expect(translatedCode).toBe('bru.setEnvVar("computed", bru.getVar("base") + "-suffix");');
});
// TODO: Restore once UI update fixes are live for setCollectionVar
it.skip('should handle more complex nested expressions', () => {
it('should handle more complex nested expressions', () => {
const code = 'pm.collectionVariables.set("fullPath", pm.environment.get("baseUrl") + pm.variables.get("endpoint"));';
const translatedCode = translateCode(code);
expect(translatedCode).toBe('bru.setCollectionVar("fullPath", bru.getEnvVar("baseUrl") + bru.getVar("endpoint"));');
@@ -358,8 +356,7 @@ describe('Combined API Features Translation', () => {
expect(translatedCode).toContain('bru.setVar("token", res.getBody().token);');
});
// TODO: Restore once UI update fixes are live for setCollectionVar
it.skip('should translate pm.collectionVariables alias set inside functions', () => {
it('should translate pm.collectionVariables alias set inside functions', () => {
const code = `
const tempCollVars = pm.collectionVariables;
tempCollVars.set("sessionId", "value");

View File

@@ -43,8 +43,7 @@ describe('Multiline Syntax Handling', () => {
expect(translatedCode).toContain('const apiKey = bru.getCollectionVar("apiKey")');
});
// TODO: Restore once UI update fixes are live for setCollectionVar
it.skip('should handle multiline collection variable set syntax', () => {
it('should handle multiline collection variable set syntax', () => {
const code = `
pm.collectionVariables
.set("lastRun", new Date().toISOString());
@@ -287,8 +286,7 @@ describe('Multiline Syntax Handling', () => {
expect(translatedCode).toContain('bru.runner.setNextRequest("Next API Call")');
});
// TODO: Restore once UI update fixes are live for setCollectionVar
it.skip('should translate multiline pm.collectionVariables.set in comprehensive script', () => {
it('should translate multiline pm.collectionVariables.set in comprehensive script', () => {
const code = `
pm.collectionVariables
.set("lastRun", new Date());

View File

@@ -283,8 +283,7 @@ describe('Response Translation', () => {
expect(translatedCode).toContain('const items = res.getBody().items;');
});
// TODO: Restore once UI update fixes are live for setCollectionVar
it.skip('should translate pm.collectionVariables.set with array access pattern', () => {
it('should translate pm.collectionVariables.set with array access pattern', () => {
const code = 'pm.collectionVariables.set("item_" + i, items[i].id);';
const translatedCode = translateCode(code);
expect(translatedCode).toContain('bru.setCollectionVar("item_" + i, items[i].id);');

View File

@@ -69,8 +69,7 @@ describe('Testing Framework Translation', () => {
expect(translatedCode).toContain('bru.setEnvVar("userId", response.user.id);');
});
// TODO: Restore once UI update fixes are live for setCollectionVar
it.skip('should translate pm.collectionVariables.set inside test functions', () => {
it('should translate pm.collectionVariables.set inside test functions', () => {
const code = 'pm.collectionVariables.set("sessionId", response.session.id);';
const translatedCode = translateCode(code);
expect(translatedCode).toContain('bru.setCollectionVar("sessionId", response.session.id);');

View File

@@ -71,8 +71,7 @@ describe('Variables Translation', () => {
expect(translatedCode).toBe('bru.getCollectionVar("apiUrl");');
});
// TODO: Restore once UI update fixes are live for setCollectionVar
it.skip('should translate pm.collectionVariables.set', () => {
it('should translate pm.collectionVariables.set', () => {
const code = 'pm.collectionVariables.set("token", jsonData.token);';
const translatedCode = translateCode(code);
@@ -86,8 +85,7 @@ describe('Variables Translation', () => {
expect(translatedCode).toBe('bru.hasCollectionVar("authToken");');
});
// TODO: Restore once UI update fixes are live for deleteCollectionVar
it.skip('should translate pm.collectionVariables.unset', () => {
it('should translate pm.collectionVariables.unset', () => {
const code = 'pm.collectionVariables.unset("tempVar");';
const translatedCode = translateCode(code);
@@ -108,6 +106,41 @@ describe('Variables Translation', () => {
expect(translatedCode).toBe('bru.setGlobalEnvVar("test", "value");');
});
it('should translate pm.globals.unset', () => {
const code = 'pm.globals.unset("token");';
const translatedCode = translateCode(code);
expect(translatedCode).toBe('bru.deleteGlobalEnvVar("token");');
});
it('should translate pm.globals.clear', () => {
const code = 'pm.globals.clear();';
const translatedCode = translateCode(code);
expect(translatedCode).toBe('bru.deleteAllGlobalEnvVars();');
});
it('should translate pm.globals.toObject', () => {
const code = 'const globals = pm.globals.toObject();';
const translatedCode = translateCode(code);
expect(translatedCode).toBe('const globals = bru.getAllGlobalEnvVars();');
});
it('should translate pm.collectionVariables.clear', () => {
const code = 'pm.collectionVariables.clear();';
const translatedCode = translateCode(code);
expect(translatedCode).toBe('bru.deleteAllCollectionVars();');
});
it('should translate pm.collectionVariables.toObject', () => {
const code = 'const vars = pm.collectionVariables.toObject();';
const translatedCode = translateCode(code);
expect(translatedCode).toBe('const vars = bru.getAllCollectionVars();');
});
// Alias tests for variables
it('should handle variables aliases', () => {
const code = `
@@ -126,8 +159,7 @@ describe('Variables Translation', () => {
});
// Alias tests for collection variables
// TODO: Restore once UI update fixes are live for setCollectionVar/deleteCollectionVar
it.skip('should handle collection variables aliases', () => {
it('should handle collection variables aliases', () => {
const code = `
const collVars = pm.collectionVariables;
const has = collVars.has("test");
@@ -183,8 +215,7 @@ describe('Variables Translation', () => {
expect(translatedCode).toContain('bru.setVar("requestTime", new Date().toISOString());');
});
// TODO: Restore once UI update fixes are live for setCollectionVar/deleteCollectionVar
it.skip('should handle all collection variable methods together', () => {
it('should handle all collection variable methods together', () => {
const code = `
// All collection variable methods
const hasApiUrl = pm.collectionVariables.has("apiUrl");
@@ -202,8 +233,7 @@ describe('Variables Translation', () => {
expect(translatedCode).toContain('bru.deleteCollectionVar("tempVar");');
});
// TODO: Restore once UI update fixes are live for setCollectionVar
it.skip('should handle more complex nested expressions with variables', () => {
it('should handle more complex nested expressions with variables', () => {
const code = 'pm.collectionVariables.set("fullPath", pm.environment.get("baseUrl") + pm.variables.get("endpoint"));';
const translatedCode = translateCode(code);