Skip to content

Commit

Permalink
fix: jsonEnabled not handling after pass-through (#687)
Browse files Browse the repository at this point in the history
* fix:  jsonEnabled does not consider json flag appearing after pass-through

@W-13012400@

* test: with some extra params

---------

Co-authored-by: mshanemc <[email protected]>
  • Loading branch information
peternhale and mshanemc authored Apr 12, 2023
1 parent 42d8cfc commit 5c7e534
Show file tree
Hide file tree
Showing 3 changed files with 150 additions and 1 deletion.
30 changes: 29 additions & 1 deletion src/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ export abstract class Command {

public static hasDynamicHelp = false

protected static '_--' = false

protected static _enableJsonFlag = false

public static get enableJsonFlag(): boolean {
Expand All @@ -142,6 +144,18 @@ export abstract class Command {
}
}

public static get '--'(): boolean {
return Command['_--']
}

public static set '--'(value: boolean) {
Command['_--'] = value
}

public get passThroughEnabled(): boolean {
return Command['_--']
}

/**
* instantiate and run the command
*
Expand Down Expand Up @@ -259,8 +273,22 @@ export abstract class Command {
}
}

/**
* Determine if the command is being run with the --json flag in a command that supports it.
*
* @returns {boolean} true if the command supports json and the --json flag is present
*/
public jsonEnabled(): boolean {
return this.ctor.enableJsonFlag && this.argv.includes('--json')
// if the command doesn't support json, return false
if (!this.ctor.enableJsonFlag) return false
// if the command parameter pass through is enabled, return true if the --json flag is before the '--' separator
if (this.passThroughEnabled) {
const ptIndex = this.argv.indexOf('--')
const jsonIndex = this.argv.indexOf('--json')
return jsonIndex > -1 && (ptIndex === -1 || jsonIndex < ptIndex)
}

return this.argv.includes('--json')
}

/**
Expand Down
119 changes: 119 additions & 0 deletions test/command/command.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -449,4 +449,123 @@ describe('command', () => {
.do(ctx => expect(ctx.stdout).to.equal('json output: {"a":"foobar"}\n'))
.it('test stdout EPIPE swallowed')
})
describe('json enabled and pass-through tests', () => {
fancy
.stdout()
.do(async () => {
class CMD extends Command {
static enableJsonFlag = true
async run() {
this.log('not json output')
}
}
const cmd = new CMD([], {} as any)
expect(cmd.jsonEnabled()).to.equal(false)
})
.it('json enabled/pass through disabled/no --json flag/jsonEnabled() should be false')

fancy
.stdout()
.do(async () => {
class CMD extends Command {
static enableJsonFlag = true
async run() {}
}
const cmd = new CMD(['--json'], {} as any)
expect(cmd.jsonEnabled()).to.equal(true)
})
.it('json enabled/pass through disabled/--json flag before --/jsonEnabled() should be true')

fancy
.stdout()
.do(async () => {
class CMD extends Command {
static enableJsonFlag = true
static '--' = true
async run() {
const {flags} = await cmd.parse(CMD, ['--json'])
expect(flags.json).to.equal(true, 'json flag should be true')
}
}
const cmd = new CMD(['--json'], {} as any)
expect(cmd.jsonEnabled()).to.equal(true)
})
.it('json enabled/pass through enabled/--json flag before --/jsonEnabled() should be true')

fancy
.stdout()
.do(async () => {
class CMD extends Command {
static enableJsonFlag = true
static '--' = true
async run() {
const {flags} = await cmd.parse(CMD, ['--', '--json'])
expect(flags.json).to.equal(false, 'json flag should be false')
expect(this.passThroughEnabled).to.equal(true, 'pass through should be true')
}
}
const cmd = new CMD(['--', '--json'], {} as any)
expect(cmd.jsonEnabled()).to.equal(false)
})
.it('json enabled/pass through enabled/--json flag after --/jsonEnabled() should be false')

fancy
.stdout()
.do(async () => {
class CMD extends Command {
static enableJsonFlag = true
static '--' = true
async run() {
const {flags} = await cmd.parse(CMD, ['--foo', '--json'])
expect(flags.json).to.equal(true, 'json flag should be true')
}
}
const cmd = new CMD(['foo', '--json'], {} as any)
expect(cmd.jsonEnabled()).to.equal(true)
})
.it('json enabled/pass through enabled/--json flag before --/extra param/jsonEnabled() should be true')

fancy
.stdout()
.do(async () => {
class CMD extends Command {
static enableJsonFlag = true
static '--' = true
async run() {
const {flags} = await cmd.parse(CMD, ['--foo', '--', '--json'])
expect(flags.json).to.equal(false, 'json flag should be false')
expect(this.passThroughEnabled).to.equal(true, 'pass through should be true')
}
}
const cmd = new CMD(['--foo', '--', '--json'], {} as any)
expect(cmd.jsonEnabled()).to.equal(false)
})
.it('json enabled/pass through enabled/--json flag after --/extra param/jsonEnabled() should be false')

fancy
.stdout()
.do(async () => {
class CMD extends Command {
static enableJsonFlag = true
static '--' = true
async run() {}
}
const cmd = new CMD(['--json', '--'], {} as any)
expect(cmd.jsonEnabled()).to.equal(true)
})
.it('json enabled/pass through enabled/--json flag before --/jsonEnabled() should be true')

fancy
.stdout()
.do(async () => {
class CMD extends Command {
static enableJsonFlag = false
static '--' = true
async run() {}
}
const cmd = new CMD(['--json'], {} as any)
expect(cmd.jsonEnabled()).to.equal(false)
})
.it('json disabled/pass through enable/--json flag before --/jsonEnabled() should be false')
})
})
2 changes: 2 additions & 0 deletions test/interfaces/flags.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ class MyCommand extends BaseCommand {
defaultArrayFlag: arrayFlag({default: ['foo', 'bar']}),
}

public static '--' = true

public flags!: MyFlags

public async run(): Promise<MyFlags> {
Expand Down

0 comments on commit 5c7e534

Please sign in to comment.