diff --git a/src/global.ts b/src/global.ts index 0f5815819..ba2e23f9c 100644 --- a/src/global.ts +++ b/src/global.ts @@ -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; } /** diff --git a/test/unit/globalTest.ts b/test/unit/globalTest.ts index cae4620e4..828c1d906 100644 --- a/test/unit/globalTest.ts +++ b/test/unit/globalTest.ts @@ -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', () => { @@ -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;