Skip to content

Commit

Permalink
refactor: environment mode not using getKeyOf
Browse files Browse the repository at this point in the history
  • Loading branch information
mshanemc committed Dec 29, 2023
1 parent c20bba8 commit 2934091
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 4 deletions.
5 changes: 4 additions & 1 deletion src/global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,10 @@ export class Global {
* ```
*/
public static getEnvironmentMode(): Mode {
return Mode[env.getKeyOf('SFDX_ENV', Mode, Mode.PRODUCTION, (value) => value.toUpperCase())];
const envValue = env.getString('SF_ENV') ?? env.getString('SFDX_ENV', Mode.PRODUCTION);
return envValue in Mode || envValue.toUpperCase() in Mode
? Mode[envValue.toUpperCase() as keyof typeof Mode]
: Mode.PRODUCTION;
}

/**
Expand Down
52 changes: 49 additions & 3 deletions test/unit/globalTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,16 @@ import { Global, Mode } from '../../src/global';

describe('Global', () => {
describe('environmentMode', () => {
const sfdxEnv = process.env.SFDX_ENV;
const original = { SFDX_ENV: process.env.SFDX_ENV, SF_ENV: process.env.SF_ENV };

beforeEach(() => {
delete process.env.SFDX_ENV;
delete process.env.SF_ENV;
});

after(() => {
process.env.SFDX_ENV = sfdxEnv;
process.env.SFDX_ENV = original.SFDX_ENV;
process.env.SF_ENV = original.SF_ENV;
});

it('uses SFDX_ENV mode', () => {
Expand All @@ -23,8 +29,48 @@ describe('Global', () => {
expect(Global.getEnvironmentMode() === Mode.TEST).to.be.false;
});

it('prefers SF_ENV mode', () => {
process.env.SF_ENV = 'test';
process.env.SFDX_ENV = 'development';
expect(Global.getEnvironmentMode() === Mode.DEVELOPMENT).to.be.false;
expect(Global.getEnvironmentMode() === Mode.PRODUCTION).to.be.false;
expect(Global.getEnvironmentMode() === Mode.DEMO).to.be.false;
expect(Global.getEnvironmentMode() === Mode.TEST).to.be.true;
});

it('finds uppercase', () => {
process.env.SF_ENV = 'TEST';
expect(Global.getEnvironmentMode() === Mode.DEVELOPMENT).to.be.false;
expect(Global.getEnvironmentMode() === Mode.PRODUCTION).to.be.false;
expect(Global.getEnvironmentMode() === Mode.DEMO).to.be.false;
expect(Global.getEnvironmentMode() === Mode.TEST).to.be.true;
});

it('finds lowercase', () => {
process.env.SF_ENV = 'demo';
expect(Global.getEnvironmentMode() === Mode.DEVELOPMENT).to.be.false;
expect(Global.getEnvironmentMode() === Mode.PRODUCTION).to.be.false;
expect(Global.getEnvironmentMode() === Mode.DEMO).to.be.true;
expect(Global.getEnvironmentMode() === Mode.TEST).to.be.false;
});

it('finds mixed case', () => {
process.env.SF_ENV = 'dEvelOpment';
expect(Global.getEnvironmentMode() === Mode.DEVELOPMENT).to.be.true;
expect(Global.getEnvironmentMode() === Mode.PRODUCTION).to.be.false;
expect(Global.getEnvironmentMode() === Mode.DEMO).to.be.false;
expect(Global.getEnvironmentMode() === Mode.TEST).to.be.false;
});

it('is production by default', () => {
delete process.env.SFDX_ENV;
expect(Global.getEnvironmentMode() === Mode.DEVELOPMENT).to.be.false;
expect(Global.getEnvironmentMode() === Mode.PRODUCTION).to.be.true;
expect(Global.getEnvironmentMode() === Mode.DEMO).to.be.false;
expect(Global.getEnvironmentMode() === Mode.TEST).to.be.false;
});

it('defaults to production when invalid values are specified', () => {
process.env.SFDX_ENV = 'notARealMode';
expect(Global.getEnvironmentMode() === Mode.DEVELOPMENT).to.be.false;
expect(Global.getEnvironmentMode() === Mode.PRODUCTION).to.be.true;
expect(Global.getEnvironmentMode() === Mode.DEMO).to.be.false;
Expand Down

0 comments on commit 2934091

Please sign in to comment.