BREAKING CHANGE(process.env): treat environment as immutable

This commit is contained in:
Philipp Kunz 2019-01-15 23:54:29 +01:00
parent 8e7909e367
commit 5f6f434998
2 changed files with 13 additions and 22 deletions

View File

@ -4,30 +4,30 @@ import * as qenv from '../ts/index';
process.env['key1'] = 'original'; process.env['key1'] = 'original';
let qenvTestObject: qenv.Qenv; let testQenv: qenv.Qenv;
tap.test('should create a new class', async () => { tap.test('should create a new class', async () => {
qenvTestObject = new qenv.Qenv( testQenv = new qenv.Qenv(
path.join(__dirname, 'assets'), path.join(__dirname, 'assets'),
path.join(__dirname, 'assets'), path.join(__dirname, 'assets'),
false false
); );
expect(qenvTestObject).to.be.instanceof(qenv.Qenv); expect(testQenv).to.be.instanceof(qenv.Qenv);
}); });
tap.test('key1 should be not be overwritten since it is already present', async () => { tap.test('key1 should be not be overwritten since it is already present', async () => {
expect(process.env.key1).to.equal('original'); expect(testQenv.getEnvVarRequired('key1')).to.equal('original');
expect(qenvTestObject.getEnvVarOnDemand('key1')).to.equal('original'); expect(testQenv.getEnvVarOnDemand('key1')).to.equal('original');
}); });
tap.test('key2 should be read from Yml', async () => { tap.test('key2 should be read from Yml', async () => {
expect(process.env.key2).to.equal('fromYml'); expect(testQenv.getEnvVarRequired('key2')).to.equal('fromYml');
expect(qenvTestObject.getEnvVarOnDemand('key2')).to.equal('fromYml'); expect(testQenv.getEnvVarOnDemand('key2')).to.equal('fromYml');
}); });
tap.test('keyValueObjectArray should hold all retrieved values', async () => { tap.test('keyValueObjectArray should hold all retrieved values', async () => {
expect(qenvTestObject.keyValueObjectArray[0].value).to.equal('original'); expect(testQenv.keyValueObject.key1).to.equal('original');
expect(qenvTestObject.keyValueObjectArray[1].value).to.equal('fromYml'); expect(testQenv.keyValueObject.key2).to.equal('fromYml');
}); });
tap.start(); tap.start();

View File

@ -1,11 +1,6 @@
import * as plugins from './qenv.plugins'; import * as plugins from './qenv.plugins';
plugins.smartlog.defaultLogger.enableConsole(); plugins.smartlog.defaultLogger.enableConsole();
export interface IKeyValueObject {
key: string;
value: string;
}
/** /**
* class Qenv * class Qenv
* allows to make assertions about the environments while being more flexibel in how to meet them * allows to make assertions about the environments while being more flexibel in how to meet them
@ -14,7 +9,7 @@ export class Qenv {
public requiredEnvVars: string[] = []; public requiredEnvVars: string[] = [];
public availableEnvVars: string[] = []; public availableEnvVars: string[] = [];
public missingEnvVars: string[] = []; public missingEnvVars: string[] = [];
public keyValueObjectArray: IKeyValueObject[] = []; public keyValueObject: {[key: string]: any } = {};
public logger: plugins.smartlog.Smartlog; public logger: plugins.smartlog.Smartlog;
// filePaths // filePaths
@ -62,7 +57,7 @@ export class Qenv {
* @param envVarName * @param envVarName
*/ */
public getEnvVarRequired(envVarName): string { public getEnvVarRequired(envVarName): string {
return process.env[envVarName]; return this.keyValueObject[envVarName];
} }
/** /**
@ -173,14 +168,10 @@ export class Qenv {
const chosenVar = this.getEnvVarOnDemand(requiredEnvVar); const chosenVar = this.getEnvVarOnDemand(requiredEnvVar);
if (chosenVar) { if (chosenVar) {
this.availableEnvVars.push(requiredEnvVar); this.availableEnvVars.push(requiredEnvVar);
process.env[requiredEnvVar] = chosenVar; this.keyValueObject[requiredEnvVar] = chosenVar;
this.keyValueObjectArray.push({
key: requiredEnvVar,
value: chosenVar
});
} }
} }
}; }
/** /**
* gets missing env vars * gets missing env vars