Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: restore support for single command cli test #532

Merged
merged 1 commit into from
Apr 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
]
}
Loading