Skip to content

Commit

Permalink
Merge 8212d44 into f699d03
Browse files Browse the repository at this point in the history
  • Loading branch information
bosure19 committed Jan 9, 2020
2 parents f699d03 + 8212d44 commit ca648f8
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 1 deletion.
15 changes: 14 additions & 1 deletion js/DotEnvParser.js
Expand Up @@ -12,10 +12,23 @@ class DotEnvParser {
}
}

static removeComments(str) {
if (str.includes('="')) {
if(str.endsWith('"')) {
return str;
} else {
const values = /(.*="(?:[^"\\]|\\.)*").*/.exec(str);
return values[1].replace('\\"','"');
}
} else {
return str.replace(/#.*/, '');
}
}

static parseEnvStr(str) {
str = str.trim();
str = str.replace(/^export /, '');
str = str.replace(/#.*/, '');
str = DotEnvParser.removeComments(str);
let parts = str.split('=');
let name = parts[0].trim();
let valid = new RegExp(`^${config.regex}$`).test(name);
Expand Down
15 changes: 15 additions & 0 deletions test/_classes/optionsTestObjs.js
Expand Up @@ -135,6 +135,21 @@ let envsub = [
flags: `--env-file ${Tmp.ENVFILE8}`.split(' ')
}
},
{
testName: '--env-file should remove quotes surrounding environment variables and let all character on it ',
preFunc: () => {
},
templateFile: Tmp.ENV_TEMPLATE_NOT_A_COMMENT_FILE,
outputContents: Tmp.ENV_TEMPLATE_NOT_A_COMMENT_FILE_EXPECTED,
options: {
envFiles: [
Tmp.ENVFILE9
]
},
cli: {
flags: `--env-file ${Tmp.ENVFILE9}`.split(' ')
}
},
{
testName: '--env-file should not substitute environment variables with invalid names',
templateFile: Tmp.ENV_INVALID_TEMPLATE_FILE,
Expand Down
7 changes: 7 additions & 0 deletions test/_classes/templateFiles.js
Expand Up @@ -10,12 +10,16 @@ const ENVFILE5 = `${envsubDir}/envFile5.env`;
const ENVFILE6 = `${envsubDir}/envFile6.env`;
const ENVFILE7 = `${envsubDir}/envFile7.env`;
const ENVFILE8 = `${envsubDir}/envFile8.env`;
const ENVFILE9 = `${envsubDir}/envFile9.env`;

const COMBINED_TEMPLATE_FILE = `${envsubDir}/templateFileCombined`;
const COMBINED_TEMPLATE_FILE_EXPECTED = readFileSync(`${envsubDir}/templateFileCombined_E`, 'utf8');

const ENV_TEMPLATE_FILE = `${envsubDir}/templateFileEnv`;

const ENV_TEMPLATE_FILE_EXPECTED = readFileSync(`${envsubDir}/templateFileEnv_E`, 'utf8');
const ENV_TEMPLATE_NOT_A_COMMENT_FILE = `${envsubDir}/templateFileNotAComment`;
const ENV_TEMPLATE_NOT_A_COMMENT_FILE_EXPECTED = readFileSync(`${envsubDir}/templateFileNotAComment_E`, 'utf8');
const ENV_FILE_TEMPLATE_FILE = `${envsubDir}/templateFileEnvFile`;
const ENV_FILE_TEMPLATE_FILE_EXPECTED = readFileSync(`${envsubDir}/templateFileEnvFile_E`, 'utf8');
const ENV_INVALID_TEMPLATE_FILE = `${envsubDir}/templateFileEnvInvalid`;
Expand Down Expand Up @@ -74,6 +78,9 @@ const templateFiles = {
ENVFILE6,
ENVFILE7,
ENVFILE8,
ENVFILE9,
ENV_TEMPLATE_NOT_A_COMMENT_FILE,
ENV_TEMPLATE_NOT_A_COMMENT_FILE_EXPECTED,
COMBINED_TEMPLATE_FILE,
COMBINED_TEMPLATE_FILE_EXPECTED,
ENV_TEMPLATE_FILE,
Expand Down
1 change: 1 addition & 0 deletions test/envsub-global/envFile9.env
@@ -0,0 +1 @@
NOT_A_COMMENT="Daniel#not_a_comment\"" #comment
2 changes: 2 additions & 0 deletions test/envsub-global/templateFileNotAComment
@@ -0,0 +1,2 @@
xxx${ NOT_A_COMMENT }xxx xxx${ NOT_A_COMMENT }xxx
xxx${NOT_A_COMMENT}xxx xxx${ NOT_A_COMMENT }xxx
2 changes: 2 additions & 0 deletions test/envsub-global/templateFileNotAComment_E
@@ -0,0 +1,2 @@
xxxDaniel#not_a_comment"xxx xxxDaniel#not_a_comment"xxx
xxxDaniel#not_a_comment"xxx xxxDaniel#not_a_comment"xxx

0 comments on commit ca648f8

Please sign in to comment.