From a02f2698d9d8fd650ecf33bfb311403fb9c71a11 Mon Sep 17 00:00:00 2001 From: mshanemc Date: Tue, 25 Apr 2023 10:47:10 -0500 Subject: [PATCH 1/4] fix: support setting json = true via env --- src/sfCommand.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/sfCommand.ts b/src/sfCommand.ts index a4560bcc..4b4285cb 100644 --- a/src/sfCommand.ts +++ b/src/sfCommand.ts @@ -209,6 +209,11 @@ export abstract class SfCommand extends Command { return this.constructor as typeof SfCommand; } + public jsonEnabled(): boolean { + // can come from either oclif's detection of the flag's presence and truthiness OR from the env + // https://developer.salesforce.com/docs/atlas.en-us.sfdx_setup.meta/sfdx_setup/sfdx_dev_cli_json_support.htm + return super.jsonEnabled() || envVars.getString(EnvironmentVariable.SF_CONTENT_TYPE) === 'JSON'; + } /** * Log a success message that has the standard success message color applied. * From a6d02bf57820b0b70da8de5281017d370c7907a3 Mon Sep 17 00:00:00 2001 From: mshanemc Date: Tue, 25 Apr 2023 12:19:32 -0500 Subject: [PATCH 2/4] feat: support json env in any case --- src/sfCommand.ts | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/sfCommand.ts b/src/sfCommand.ts index 4b4285cb..5763be28 100644 --- a/src/sfCommand.ts +++ b/src/sfCommand.ts @@ -210,9 +210,14 @@ export abstract class SfCommand extends Command { } public jsonEnabled(): boolean { - // can come from either oclif's detection of the flag's presence and truthiness OR from the env // https://developer.salesforce.com/docs/atlas.en-us.sfdx_setup.meta/sfdx_setup/sfdx_dev_cli_json_support.htm - return super.jsonEnabled() || envVars.getString(EnvironmentVariable.SF_CONTENT_TYPE) === 'JSON'; + // can come from either oclif's detection of the flag's presence and truthiness OR from the env + // unless it's been explicitly disabled on the command's statics + return ( + super.jsonEnabled() || + (SfCommand.enableJsonFlag !== false && + envVars.getString(EnvironmentVariable.SF_CONTENT_TYPE)?.toUpperCase() === 'JSON') + ); } /** * Log a success message that has the standard success message color applied. From 6f3a250ba83dc450da6f2011bc15f02881180b83 Mon Sep 17 00:00:00 2001 From: mshanemc Date: Tue, 25 Apr 2023 12:20:06 -0500 Subject: [PATCH 3/4] test: ut for jsonEnabled --- test/unit/sfCommand.test.ts | 43 +++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/test/unit/sfCommand.test.ts b/test/unit/sfCommand.test.ts index b5a9c4fb..1a8fcb49 100644 --- a/test/unit/sfCommand.test.ts +++ b/test/unit/sfCommand.test.ts @@ -10,6 +10,7 @@ import { TestContext } from '@salesforce/core/lib/testSetup'; import { stubMethod } from '@salesforce/ts-sinon'; import { expect } from 'chai'; import { SfError } from '@salesforce/core'; +import { Config } from '@oclif/core/lib/interfaces'; import { SfCommand } from '../../src/sfCommand'; class TestCommand extends SfCommand { @@ -42,6 +43,48 @@ class TestCommand extends SfCommand { } } +describe('jsonEnabled', () => { + beforeEach(() => { + delete process.env.SF_CONTENT_TYPE; + }); + + const oclifConfig = {} as unknown as Config; + it('not json', () => { + // @ts-expect-error not really an oclif config + const cmd = new TestCommand([], oclifConfig); + expect(cmd.jsonEnabled()).to.be.false; + }); + it('json via flag but not env', () => { + // @ts-expect-error not really an oclif config + const cmd = new TestCommand(['--json'], oclifConfig); + expect(cmd.jsonEnabled()).to.be.true; + }); + it('json via env but not flag', () => { + process.env.SF_CONTENT_TYPE = 'JSON'; + // @ts-expect-error not really an oclif config + const cmd = new TestCommand([], oclifConfig); + expect(cmd.jsonEnabled()).to.be.true; + }); + it('json via env lowercase', () => { + process.env.SF_CONTENT_TYPE = 'json'; + // @ts-expect-error not really an oclif config + const cmd = new TestCommand([], oclifConfig); + expect(cmd.jsonEnabled()).to.be.true; + }); + it('not json via env that is not json', () => { + process.env.SF_CONTENT_TYPE = 'foo'; + // @ts-expect-error not really an oclif config + const cmd = new TestCommand([], oclifConfig); + expect(cmd.jsonEnabled()).to.be.false; + }); + it('json via both flag and env', () => { + process.env.SF_CONTENT_TYPE = 'JSON'; + // @ts-expect-error not really an oclif config + const cmd = new TestCommand(['--json'], oclifConfig); + expect(cmd.jsonEnabled()).to.be.true; + }); +}); + describe('info messages', () => { const $$ = new TestContext(); beforeEach(() => { From 92d21006c7d53e27d376652a6fbb5f19ca8bb814 Mon Sep 17 00:00:00 2001 From: mshanemc Date: Tue, 25 Apr 2023 12:24:53 -0500 Subject: [PATCH 4/4] test: non-json cases --- src/sfCommand.ts | 2 +- test/unit/sfCommand.test.ts | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/sfCommand.ts b/src/sfCommand.ts index 5763be28..f2267706 100644 --- a/src/sfCommand.ts +++ b/src/sfCommand.ts @@ -215,7 +215,7 @@ export abstract class SfCommand extends Command { // unless it's been explicitly disabled on the command's statics return ( super.jsonEnabled() || - (SfCommand.enableJsonFlag !== false && + (this.statics.enableJsonFlag !== false && envVars.getString(EnvironmentVariable.SF_CONTENT_TYPE)?.toUpperCase() === 'JSON') ); } diff --git a/test/unit/sfCommand.test.ts b/test/unit/sfCommand.test.ts index 1a8fcb49..77eff995 100644 --- a/test/unit/sfCommand.test.ts +++ b/test/unit/sfCommand.test.ts @@ -43,6 +43,12 @@ class TestCommand extends SfCommand { } } +class NonJsonCommand extends SfCommand { + public static enableJsonFlag = false; + public async run(): Promise { + await this.parse(TestCommand); + } +} describe('jsonEnabled', () => { beforeEach(() => { delete process.env.SF_CONTENT_TYPE; @@ -83,6 +89,20 @@ describe('jsonEnabled', () => { const cmd = new TestCommand(['--json'], oclifConfig); expect(cmd.jsonEnabled()).to.be.true; }); + + describe('non json command', () => { + it('non-json command base case', () => { + // @ts-expect-error not really an oclif config + const cmd = new NonJsonCommand([], oclifConfig); + expect(cmd.jsonEnabled()).to.be.false; + }); + it('non-json command is not affected by env', () => { + process.env.SF_CONTENT_TYPE = 'JSON'; + // @ts-expect-error not really an oclif config + const cmd = new NonJsonCommand([], oclifConfig); + expect(cmd.jsonEnabled()).to.be.false; + }); + }); }); describe('info messages', () => {