Skip to content

Commit

Permalink
Merge branch 'sm/enable-json-via-env' into sm/one-config-aggregator
Browse files Browse the repository at this point in the history
  • Loading branch information
mshanemc committed Apr 28, 2023
2 parents 3d6d611 + 92d2100 commit 8693208
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/sfCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,16 @@ export abstract class SfCommand<T> extends Command {
return this.constructor as typeof SfCommand;
}

public jsonEnabled(): boolean {
// https://developer.salesforce.com/docs/atlas.en-us.sfdx_setup.meta/sfdx_setup/sfdx_dev_cli_json_support.htm
// 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() ||
(this.statics.enableJsonFlag !== false &&
envVars.getString(EnvironmentVariable.SF_CONTENT_TYPE)?.toUpperCase() === 'JSON')
);
}
/**
* Log a success message that has the standard success message color applied.
*
Expand Down
63 changes: 63 additions & 0 deletions test/unit/sfCommand.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> {
Expand Down Expand Up @@ -42,6 +43,68 @@ class TestCommand extends SfCommand<void> {
}
}

class NonJsonCommand extends SfCommand<void> {
public static enableJsonFlag = false;
public async run(): Promise<void> {
await this.parse(TestCommand);
}
}
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('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', () => {
const $$ = new TestContext();
beforeEach(() => {
Expand Down

0 comments on commit 8693208

Please sign in to comment.