fix(#216): fixed issue where .env vars were not injected into bru.getEnvVar()

This commit is contained in:
Anoop M D 2023-09-28 04:32:07 +05:30
parent c27f090583
commit a3050db6c4
6 changed files with 84 additions and 28 deletions

View File

@ -102,6 +102,7 @@ const registerNetworkIpc = (mainWindow) => {
const _request = item.draft ? item.draft.request : item.request; const _request = item.draft ? item.draft.request : item.request;
const request = prepareRequest(_request); const request = prepareRequest(_request);
const envVars = getEnvVars(environment); const envVars = getEnvVars(environment);
const processEnvVars = getProcessEnvVars(collectionUid);
try { try {
// make axios work in node using form data // make axios work in node using form data
@ -128,7 +129,8 @@ const registerNetworkIpc = (mainWindow) => {
request, request,
envVars, envVars,
collectionVariables, collectionVariables,
collectionPath collectionPath,
processEnvVars
); );
if (result) { if (result) {
@ -151,7 +153,8 @@ const registerNetworkIpc = (mainWindow) => {
envVars, envVars,
collectionVariables, collectionVariables,
collectionPath, collectionPath,
onConsoleLog onConsoleLog,
processEnvVars
); );
mainWindow.webContents.send('main:script-environment-update', { mainWindow.webContents.send('main:script-environment-update', {
@ -162,8 +165,6 @@ const registerNetworkIpc = (mainWindow) => {
}); });
} }
const processEnvVars = getProcessEnvVars(collectionUid);
const brunoConfig = getBrunoConfig(collectionUid); const brunoConfig = getBrunoConfig(collectionUid);
const proxyEnabled = get(brunoConfig, 'proxy.enabled', false); const proxyEnabled = get(brunoConfig, 'proxy.enabled', false);
if (proxyEnabled) { if (proxyEnabled) {
@ -250,7 +251,8 @@ const registerNetworkIpc = (mainWindow) => {
response, response,
envVars, envVars,
collectionVariables, collectionVariables,
collectionPath collectionPath,
processEnvVars
); );
if (result) { if (result) {
@ -274,7 +276,8 @@ const registerNetworkIpc = (mainWindow) => {
envVars, envVars,
collectionVariables, collectionVariables,
collectionPath, collectionPath,
onConsoleLog onConsoleLog,
processEnvVars
); );
mainWindow.webContents.send('main:script-environment-update', { mainWindow.webContents.send('main:script-environment-update', {
@ -318,7 +321,8 @@ const registerNetworkIpc = (mainWindow) => {
envVars, envVars,
collectionVariables, collectionVariables,
collectionPath, collectionPath,
onConsoleLog onConsoleLog,
processEnvVars
); );
mainWindow.webContents.send('main:run-request-event', { mainWindow.webContents.send('main:run-request-event', {
@ -391,7 +395,8 @@ const registerNetworkIpc = (mainWindow) => {
envVars, envVars,
collectionVariables, collectionVariables,
collectionPath, collectionPath,
onConsoleLog onConsoleLog,
processEnvVars
); );
mainWindow.webContents.send('main:run-request-event', { mainWindow.webContents.send('main:run-request-event', {
@ -536,6 +541,7 @@ const registerNetworkIpc = (mainWindow) => {
const _request = item.draft ? item.draft.request : item.request; const _request = item.draft ? item.draft.request : item.request;
const request = prepareRequest(_request); const request = prepareRequest(_request);
const processEnvVars = getProcessEnvVars(collectionUid);
try { try {
// make axios work in node using form data // make axios work in node using form data
@ -580,7 +586,8 @@ const registerNetworkIpc = (mainWindow) => {
envVars, envVars,
collectionVariables, collectionVariables,
collectionPath, collectionPath,
onConsoleLog onConsoleLog,
processEnvVars
); );
mainWindow.webContents.send('main:script-environment-update', { mainWindow.webContents.send('main:script-environment-update', {
@ -590,8 +597,6 @@ const registerNetworkIpc = (mainWindow) => {
}); });
} }
const processEnvVars = getProcessEnvVars(collectionUid);
// interpolate variables inside request // interpolate variables inside request
interpolateVars(request, envVars, collectionVariables, processEnvVars); interpolateVars(request, envVars, collectionVariables, processEnvVars);
@ -633,7 +638,8 @@ const registerNetworkIpc = (mainWindow) => {
response, response,
envVars, envVars,
collectionVariables, collectionVariables,
collectionPath collectionPath,
processEnvVars
); );
if (result) { if (result) {
@ -656,7 +662,8 @@ const registerNetworkIpc = (mainWindow) => {
envVars, envVars,
collectionVariables, collectionVariables,
collectionPath, collectionPath,
onConsoleLog onConsoleLog,
processEnvVars
); );
mainWindow.webContents.send('main:script-environment-update', { mainWindow.webContents.send('main:script-environment-update', {
@ -698,7 +705,8 @@ const registerNetworkIpc = (mainWindow) => {
envVars, envVars,
collectionVariables, collectionVariables,
collectionPath, collectionPath,
onConsoleLog onConsoleLog,
processEnvVars
); );
mainWindow.webContents.send('main:run-folder-event', { mainWindow.webContents.send('main:run-folder-event', {
@ -776,7 +784,8 @@ const registerNetworkIpc = (mainWindow) => {
envVars, envVars,
collectionVariables, collectionVariables,
collectionPath, collectionPath,
onConsoleLog onConsoleLog,
processEnvVars
); );
mainWindow.webContents.send('main:run-folder-event', { mainWindow.webContents.send('main:run-folder-event', {

View File

@ -21,6 +21,7 @@
"btoa": "^1.2.1", "btoa": "^1.2.1",
"chai": "^4.3.7", "chai": "^4.3.7",
"crypto-js": "^4.1.1", "crypto-js": "^4.1.1",
"handlebars": "^4.7.8",
"json-query": "^2.2.2", "json-query": "^2.2.2",
"lodash": "^4.17.21", "lodash": "^4.17.21",
"moment": "^2.29.4", "moment": "^2.29.4",

View File

@ -1,19 +1,39 @@
const Handlebars = require('handlebars');
const { cloneDeep } = require('lodash');
class Bru { class Bru {
constructor(envVariables, collectionVariables) { constructor(envVariables, collectionVariables, processEnvVars) {
this.envVariables = envVariables; this.envVariables = envVariables;
this.collectionVariables = collectionVariables; this.collectionVariables = collectionVariables;
this.processEnvVars = cloneDeep(processEnvVars || {});
} }
_interpolateEnvVar = (str) => {
if (!str || !str.length || typeof str !== 'string') {
return str;
}
const template = Handlebars.compile(str, { noEscape: true });
return template({
process: {
env: {
...this.processEnvVars
}
}
});
};
getEnvName() { getEnvName() {
return this.envVariables.__name__; return this.envVariables.__name__;
} }
getProcessEnv(key) { getProcessEnv(key) {
return process.env[key]; return this.processEnvVars[key];
} }
getEnvVar(key) { getEnvVar(key) {
return this.envVariables[key]; return this._interpolateEnvVar(this.envVariables[key]);
} }
setEnvVar(key, value) { setEnvVar(key, value) {

View File

@ -27,8 +27,16 @@ const CryptoJS = require('crypto-js');
class ScriptRuntime { class ScriptRuntime {
constructor() {} constructor() {}
async runRequestScript(script, request, envVariables, collectionVariables, collectionPath, onConsoleLog) { async runRequestScript(
const bru = new Bru(envVariables, collectionVariables); script,
request,
envVariables,
collectionVariables,
collectionPath,
onConsoleLog,
processEnvVars
) {
const bru = new Bru(envVariables, collectionVariables, processEnvVars);
const req = new BrunoRequest(request); const req = new BrunoRequest(request);
const context = { const context = {
@ -89,8 +97,17 @@ class ScriptRuntime {
}; };
} }
async runResponseScript(script, request, response, envVariables, collectionVariables, collectionPath, onConsoleLog) { async runResponseScript(
const bru = new Bru(envVariables, collectionVariables); script,
request,
response,
envVariables,
collectionVariables,
collectionPath,
onConsoleLog,
processEnvVars
) {
const bru = new Bru(envVariables, collectionVariables, processEnvVars);
const req = new BrunoRequest(request); const req = new BrunoRequest(request);
const res = new BrunoResponse(response); const res = new BrunoResponse(response);

View File

@ -20,8 +20,17 @@ const CryptoJS = require('crypto-js');
class TestRuntime { class TestRuntime {
constructor() {} constructor() {}
runTests(testsFile, request, response, envVariables, collectionVariables, collectionPath, onConsoleLog) { runTests(
const bru = new Bru(envVariables, collectionVariables); testsFile,
request,
response,
envVariables,
collectionVariables,
collectionPath,
onConsoleLog,
processEnvVars
) {
const bru = new Bru(envVariables, collectionVariables, processEnvVars);
const req = new BrunoRequest(request); const req = new BrunoRequest(request);
const res = new BrunoResponse(response); const res = new BrunoResponse(response);

View File

@ -4,13 +4,13 @@ const BrunoRequest = require('../bruno-request');
const { evaluateJsTemplateLiteral, evaluateJsExpression, createResponseParser } = require('../utils'); const { evaluateJsTemplateLiteral, evaluateJsExpression, createResponseParser } = require('../utils');
class VarsRuntime { class VarsRuntime {
runPreRequestVars(vars, request, envVariables, collectionVariables, collectionPath) { runPreRequestVars(vars, request, envVariables, collectionVariables, collectionPath, processEnvVars) {
const enabledVars = _.filter(vars, (v) => v.enabled); const enabledVars = _.filter(vars, (v) => v.enabled);
if (!enabledVars.length) { if (!enabledVars.length) {
return; return;
} }
const bru = new Bru(envVariables, collectionVariables); const bru = new Bru(envVariables, collectionVariables, processEnvVars);
const req = new BrunoRequest(request); const req = new BrunoRequest(request);
const bruContext = { const bruContext = {
@ -34,13 +34,13 @@ class VarsRuntime {
}; };
} }
runPostResponseVars(vars, request, response, envVariables, collectionVariables, collectionPath) { runPostResponseVars(vars, request, response, envVariables, collectionVariables, collectionPath, processEnvVars) {
const enabledVars = _.filter(vars, (v) => v.enabled); const enabledVars = _.filter(vars, (v) => v.enabled);
if (!enabledVars.length) { if (!enabledVars.length) {
return; return;
} }
const bru = new Bru(envVariables, collectionVariables); const bru = new Bru(envVariables, collectionVariables, processEnvVars);
const req = new BrunoRequest(request); const req = new BrunoRequest(request);
const res = createResponseParser(response); const res = createResponseParser(response);