Skip to content

Commit

Permalink
Merge pull request #532 from oclif/mdonnalley/single-cmd-cli-bug
Browse files Browse the repository at this point in the history
fix: restore support for single command cli test
  • Loading branch information
mdonnalley authored Apr 10, 2024
2 parents 3a3af7d + 30cf43f commit c8a92cc
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 23 deletions.
3 changes: 2 additions & 1 deletion src/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ export function command(
if (!ctx.config || opts.reset) ctx.config = await loadConfig(opts).run({} as Context)
args = castArray(args)
const [id, ...extra] = args
const cmdId = toStandardizedId(id, ctx.config)
let cmdId = toStandardizedId(id, ctx.config)
if (cmdId === '.') cmdId = Symbol('SINGLE_COMMAND_CLI').toString()
ctx.expectation ||= `runs ${args.join(' ')}`
await ctx.config.runHook('init', {argv: extra, id: cmdId})
ctx.returned = await ctx.config.runCommand(cmdId, extra)
Expand Down
58 changes: 36 additions & 22 deletions test/command.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,46 @@ import {join} from 'node:path'

import {expect, test} from '../src'

// eslint-disable-next-line unicorn/prefer-module
const root = join(__dirname, 'fixtures/multi')

describe('command', () => {
// eslint-disable-next-line unicorn/prefer-module
const root = join(__dirname, 'fixtures/multi')
test
.loadConfig({root})
.stdout()
.command(['foo:bar'])
.do((output) => {
expect(output.stdout).to.equal('hello world!\n')
const {name} = output.returned as {name: string}
expect(name).to.equal('world')
})
.it()

test
.loadConfig({root})
.stdout()
.command(['foo:bar'])
.do(output => {
expect(output.stdout).to.equal('hello world!\n')
const {name} = output.returned as {name: string}
expect(name).to.equal('world')
})
.it()
.loadConfig({root})
.stdout()
.command(['foo:bar', '--name=foo'])
.do((output) => expect(output.stdout).to.equal('hello foo!\n'))
.it()

test
.loadConfig({root})
.stdout()
.command(['foo:bar', '--name=foo'])
.do(output => expect(output.stdout).to.equal('hello foo!\n'))
.it()
.loadConfig({root})
.stdout()
.command(['foo bar', '--name=foo'])
.do((output) => expect(output.stdout).to.equal('hello foo!\n'))
.it()
})

describe('single command cli', () => {
// eslint-disable-next-line unicorn/prefer-module
const root = join(__dirname, 'fixtures/single')
test
.loadConfig({root})
.stdout()
.command(['foo bar', '--name=foo'])
.do(output => expect(output.stdout).to.equal('hello foo!\n'))
.it()
.loadConfig({root})
.stdout()
.command(['.'])
.do((output) => {
expect(output.stdout).to.equal('hello world!\n')
const {name} = output.returned as {name: string}
expect(name).to.equal('world')
})
.it()
})
20 changes: 20 additions & 0 deletions test/fixtures/single/dist/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const {Command, Flags} = require('@oclif/core')

class CLI extends Command {
constructor(args, opts) {
super(args, opts)
}

async run() {
const {flags} = await this.parse(CLI)
const name = flags.name || 'world'
this.log(`hello ${name}!`)
return {name}
}
}

CLI.flags = {
name: Flags.string({char: 'n', description: 'name to print'}),
}

module.exports = CLI
18 changes: 18 additions & 0 deletions test/fixtures/single/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "test",
"version": "0.0.0",
"private": true,
"oclif": {
"commands": {
"strategy": "single",
"target": "./dist/index.js"
},
"topicSeparator": " "
},
"engines": {
"node": ">=8.0.0"
},
"files": [
"/src"
]
}

0 comments on commit c8a92cc

Please sign in to comment.