Skip to content

Commit 563c2da

Browse files
committed
Merge branch 'main' into sophiadt/Neptune-Analytics---Logger
2 parents cda89f1 + 3e48485 commit 563c2da

File tree

1 file changed

+25
-2
lines changed

1 file changed

+25
-2
lines changed

test/testLib.js

+25-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ import axios from 'axios';
33
import fs from 'fs';
44
import AdmZip from 'adm-zip';
55

6+
const HOST_PLACEHOLDER = '<AIR_ROUTES_DB_HOST>';
7+
const PORT_PLACEHOLDER = '<AIR_ROUTES_DB_PORT>';
8+
69
async function queryNeptune(q, language, host, port, param) {
710
try {
811
let response = null;
@@ -17,11 +20,31 @@ async function queryNeptune(q, language, host, port, param) {
1720
}
1821
}
1922

23+
/**
24+
* Searches the given text for a placeholder text and if found replaces it with an environment variable value of the same name.
25+
* It is expected that the placeholder text is surrounded by angle brackets and the environment variable name is the placeholder text without the angle brackets.
26+
*
27+
* @param text the text to search and replace the placeholder
28+
* @param placeholder the placeholder text to search for
29+
* @returns text with replaced placeholder if found
30+
* @throws error if the placeholder text is found but the environment variable is not set
31+
*/
32+
function replacePlaceholderWithEnvironmentVariable(text, placeholder) {
33+
if (text.includes(placeholder)) {
34+
// remove angle brackets
35+
let envVarName = placeholder.replace(/[<>]/g, '');
36+
if (process.env[envVarName]) {
37+
return text.replace(placeholder, process.env[envVarName]);
38+
}
39+
throw new Error('Expected environment variable ' + envVarName + ' is not set');
40+
}
41+
return text;
42+
}
2043

2144
function readJSONFile(fileName) {
2245
let text = fs.readFileSync(fileName, 'utf8');
23-
text = text.replace('<AIR_ROUTES_DB_HOST>', process.env.AIR_ROUTES_DB_HOST);
24-
text = text.replace('<AIR_ROUTES_DB_PORT>', process.env.AIR_ROUTES_DB_PORT);
46+
text = replacePlaceholderWithEnvironmentVariable(text, HOST_PLACEHOLDER);
47+
text = replacePlaceholderWithEnvironmentVariable(text, PORT_PLACEHOLDER);
2548
return JSON.parse(text);
2649
}
2750

0 commit comments

Comments
 (0)