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

View File

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

View File

@ -1,19 +1,39 @@
const Handlebars = require('handlebars');
const { cloneDeep } = require('lodash');
class Bru {
constructor(envVariables, collectionVariables) {
constructor(envVariables, collectionVariables, processEnvVars) {
this.envVariables = envVariables;
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() {
return this.envVariables.__name__;
}
getProcessEnv(key) {
return process.env[key];
return this.processEnvVars[key];
}
getEnvVar(key) {
return this.envVariables[key];
return this._interpolateEnvVar(this.envVariables[key]);
}
setEnvVar(key, value) {

View File

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

View File

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

View File

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