Skip to content

Commit

Permalink
fix: --json flag should override -r json flag in `force:apex:test…
Browse files Browse the repository at this point in the history
…:run` and `force:apex:test:report` (#177) (#178)

* fix: fix double json

* refactor: rename logJson
  • Loading branch information
Xiaoyi Chen authored Apr 2, 2021
1 parent d8f1af7 commit 0b97751
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 7 deletions.
9 changes: 6 additions & 3 deletions packages/plugin-apex/src/commands/force/apex/test/report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export default class Report extends SfdxCommand {
this.flags.testrunid,
this.flags.codecoverage
);
const jsonOutput = this.logJson(result);
const jsonOutput = this.formatResultInJson(result);

if (this.flags.outputdir) {
const outputDirConfig = buildOutputDirConfig(
Expand All @@ -117,7 +117,10 @@ export default class Report extends SfdxCommand {
this.logJUnit(result);
break;
case 'json':
this.ux.logJson(jsonOutput);
// when --json flag is specified, we should log CLI json format
if (!this.flags.json) {
this.ux.logJson(jsonOutput);
}
break;
default:
this.logHuman(result, true, this.flags.outputdir);
Expand Down Expand Up @@ -155,7 +158,7 @@ export default class Report extends SfdxCommand {
this.ux.log(reporter.format(result));
}

private logJson(result: TestResult): CliJsonFormat {
private formatResultInJson(result: TestResult): CliJsonFormat {
try {
const reporter = new JsonReporter();
return reporter.format(result);
Expand Down
11 changes: 7 additions & 4 deletions packages/plugin-apex/src/commands/force/apex/test/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ export default class Run extends SfdxCommand {
}

if (this.flags.outputdir) {
const jsonOutput = this.logJson(result);
const jsonOutput = this.formatResultInJson(result);
const outputDirConfig = buildOutputDirConfig(
result,
jsonOutput,
Expand Down Expand Up @@ -205,7 +205,10 @@ export default class Run extends SfdxCommand {
this.logJUnit(result);
break;
case 'json':
this.ux.logJson(this.logJson(result));
// when --json flag is specified, we should log CLI json format
if (!this.flags.json) {
this.ux.logJson(this.formatResultInJson(result));
}
break;
default:
if (this.flags.synchronous) {
Expand All @@ -228,7 +231,7 @@ export default class Run extends SfdxCommand {
this.ux.error(msg);
}

return this.logJson(result) as AnyJson;
return this.formatResultInJson(result) as AnyJson;
}

public async validateFlags(): Promise<void> {
Expand Down Expand Up @@ -305,7 +308,7 @@ export default class Run extends SfdxCommand {
this.ux.log(reporter.format(result));
}

private logJson(result: TestResult): CliJsonFormat {
private formatResultInJson(result: TestResult): CliJsonFormat {
try {
const reporter = new JsonReporter();
return reporter.format(result);
Expand Down
26 changes: 26 additions & 0 deletions packages/plugin-apex/test/commands/force/apex/test/report.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,32 @@ describe('force:apex:test:report', () => {
}
);

test
.withOrg({ username: TEST_USERNAME }, true)
.loadConfig({
root: __dirname
})
.stub(process, 'cwd', () => projectPath)
.stub(TestService.prototype, 'reportAsyncResults', () => testRunSimple)
.stdout()
.command([
'force:apex:test:report',
'-i',
'01pxx00000NWwb3',
'--json',
'--resultformat',
'json'
])
.it(
'should return a CLI json result when both json flag and json result flag are specified',
ctx => {
const result = ctx.stdout;
expect(result).to.not.be.empty;
const resultJSON = JSON.parse(result);
expect(resultJSON).to.deep.equal(cliJsonResult);
}
);

test
.withOrg({ username: TEST_USERNAME }, true)
.loadConfig({
Expand Down
26 changes: 26 additions & 0 deletions packages/plugin-apex/test/commands/force/apex/test/run.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,32 @@ describe('force:apex:test:run', () => {
}
);

test
.withOrg({ username: TEST_USERNAME }, true)
.loadConfig({
root: __dirname
})
.stub(process, 'cwd', () => projectPath)
.stub(TestService.prototype, 'runTestAsynchronous', () => testRunSimple)
.stdout()
.command([
'force:apex:test:run',
'--tests',
'MyApexTests.testInsertRecord',
'--resultformat',
'json',
'--json'
])
.it(
'should return a CLI json result when both json flag and json result flag are specified',
ctx => {
const result = ctx.stdout;
expect(result).to.not.be.empty;
const resultJSON = JSON.parse(result);
expect(resultJSON).to.deep.equal(cliJsonResult);
}
);

test
.withOrg({ username: TEST_USERNAME }, true)
.loadConfig({
Expand Down

0 comments on commit 0b97751

Please sign in to comment.