Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
exKAZUu committed Oct 30, 2023
1 parent 2e82118 commit 11766a9
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 9 deletions.
2 changes: 1 addition & 1 deletion packages/shared-lib-node/src/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ export function readEnvironmentVariables(
/**
* This function read environment variables from `.env` files and assign them in `process.env`.
* */
export function readAndApplyEnvironmentVariables(
export function readAndUpdateEnvironmentVariables(
argv: EnvReaderOptions,
cwd: string,
cacheEnabled = true
Expand Down
2 changes: 1 addition & 1 deletion packages/shared-lib-node/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export {
readEnvironmentVariables,
readAndApplyEnvironmentVariables,
readAndUpdateEnvironmentVariables,
removeNpmAndYarnEnvironmentVariables,
yargsOptionsBuilderForEnv,
} from './env.js';
Expand Down
17 changes: 10 additions & 7 deletions packages/shared-lib-node/tests/env.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { beforeEach, describe, expect, it } from 'vitest';

import { readAndApplyEnvironmentVariables } from '../src/env.js';
import { readAndUpdateEnvironmentVariables } from '../src/env.js';

describe('readAndApplyEnvironmentVariables()', () => {
beforeEach(() => {
Expand All @@ -9,36 +9,39 @@ describe('readAndApplyEnvironmentVariables()', () => {
});

it('should load no env vars with empty options', () => {
const envVars = readAndApplyEnvironmentVariables({}, 'test-fixtures/app1');
const envVars = readAndUpdateEnvironmentVariables({}, 'test-fixtures/app1');
expect(envVars).toEqual({});
});

it('should load env vars with --auto-cascade-env', () => {
const envVars = readAndApplyEnvironmentVariables({ autoCascadeEnv: true }, 'test-fixtures/app1');
const envVars = readAndUpdateEnvironmentVariables({ autoCascadeEnv: true }, 'test-fixtures/app1');
expect(envVars).toEqual({ NAME: 'app1', ENV: 'development1' });
});

it('should load env vars with --cascade-env=production', () => {
const envVars = readAndApplyEnvironmentVariables({ cascadeEnv: 'production', env: ['.env'] }, 'test-fixtures/app1');
const envVars = readAndUpdateEnvironmentVariables(
{ cascadeEnv: 'production', env: ['.env'] },
'test-fixtures/app1'
);
expect(envVars).toEqual({ NAME: 'app1', ENV: 'production1' });
});

it('should load env vars with --cascade-node-env and NODE_ENV=""', () => {
process.env.NODE_ENV = '';
const envVars = readAndApplyEnvironmentVariables({ cascadeNodeEnv: true, env: ['.env'] }, 'test-fixtures/app1');
const envVars = readAndUpdateEnvironmentVariables({ cascadeNodeEnv: true, env: ['.env'] }, 'test-fixtures/app1');
expect(envVars).toEqual({ NAME: 'app1', ENV: 'development1' });
});

it('should load env vars with --cascade-node-env and NODE_ENV=test', () => {
process.env.NODE_ENV = 'test';
const envVars = readAndApplyEnvironmentVariables({ cascadeNodeEnv: true, env: ['.env'] }, 'test-fixtures/app1');
const envVars = readAndUpdateEnvironmentVariables({ cascadeNodeEnv: true, env: ['.env'] }, 'test-fixtures/app1');
expect(envVars).toEqual({ NAME: 'app1', ENV: 'test1' });
});

it('should load env vars with --env=test-fixtures/app2/.env --auto-cascade-env, WB_ENV=test and NODE_ENV=production', () => {
process.env.WB_ENV = 'test';
process.env.NODE_ENV = 'production';
const envVars = readAndApplyEnvironmentVariables(
const envVars = readAndUpdateEnvironmentVariables(
{ autoCascadeEnv: true, env: ['../app2/.env'] },
'test-fixtures/app1'
);
Expand Down

0 comments on commit 11766a9

Please sign in to comment.