/** * The guide for coding agents that the gunshi package ships (`packages/gunshi/AGENTS.md`). * * Coding agents copy the example of the guide and follow its rules, so the example must type-check and run as the * guide says, and the rules must be true of the built package. These tests write small CLIs into a temporary * directory, with `node_modules/gunshi` linked to `packages/gunshi`, and run them with Node.js, so run `pnpm build` first. */ import { spawnSync } from 'node:child_process' import { existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, symlinkSync, writeFileSync } from 'node:fs' import os from 'node:os' import path from 'node:path' import { afterAll, beforeAll, describe, expect, test } from 'vite-plus/test' const ROOT = path.resolve(import.meta.dirname, '..') const GUNSHI = path.join(ROOT, 'packages', 'gunshi') const GUIDE = path.join(GUNSHI, 'AGENTS.md') const TSC = path.join(ROOT, 'node_modules', '.bin', 'tsc') const TSC_OPTIONS = [ '--noEmit', '--strict', '--module', 'nodenext', '--moduleResolution', 'nodenext', '--target', 'es2022', '--types', 'node', '--skipLibCheck' ] interface Result { code: number | null stdout: string stderr: string } let dir = '' /** * Run a file of the temporary directory with Node.js. * * @param file - The file name in the temporary directory * @param args - The arguments of the CLI * @param env - The environment variables, instead of the ones of this process * @returns The exit code and the output */ function run(file: string, args: string[] = [], env: NodeJS.ProcessEnv = process.env): Result { const result = spawnSync(process.execPath, [file, ...args], { cwd: dir, encoding: 'utf8', env }) return { code: result.status, stdout: result.stdout, stderr: result.stderr } } /** * Write a file into the temporary directory. * * @param name - The file name * @param source - The content * @returns The file name */ function write(name: string, source: string): string { writeFileSync(path.join(dir, name), source) return name } /** * The environment variables of this process, with some added or removed. * * @param changes - The variables to set, or to remove with `undefined` * @returns The environment variables */ function envWith(changes: Record): NodeJS.ProcessEnv { const env = { ...process.env } for (const [name, value] of Object.entries(changes)) { if (value === undefined) { delete env[name] } else { env[name] = value } } return env } /** * Whether the output has a stack trace. * * @param output - The output of a CLI * @returns `true` when a line of the output is a frame of a stack trace */ function hasStackTrace(output: string): boolean { return /\n\s+at /.test(output) } /** * Type-check files of the temporary directory with tsc, as the example of the guide is type-checked. * * @param files - The file names in the temporary directory * @returns The whole output of tsc, and the errors of each file */ function typeCheck(files: string[]): { output: string; errors: Record } { const result = spawnSync(TSC, [...TSC_OPTIONS, ...files], { cwd: dir, encoding: 'utf8' }) const output = `${result.stdout}${result.stderr}` const errors: Record = {} let file = '' for (const line of output.split('\n')) { file = /^(\S+\.ts)\(\d+,\d+\): error /.exec(line)?.[1] ?? file if (file) { errors[file] = `${errors[file] ?? ''}${line}\n` } } return { output, errors } } /** * The error that a CLI printed with `--json`, as one line of JSON to stderr. * * @param result - The result of the CLI * @returns The error */ function jsonFailure(result: Result): { status: string reason: string message: string next?: { command: string; argv: string[] }[] } { expect(result.stderr).toMatch(/^[^\n]+\n$/) return JSON.parse(result.stderr) as ReturnType } /** * A CLI with a sub-command `add` that prints its values. * * @param args - The `args` of `add`, as source code * @param extra - More options of `cli()`, as source code that starts with a comma * @returns The source code of the CLI */ function withAdd(args: string, extra = ''): string { return `import { cli, define } from 'gunshi' const add = define({ name: 'add', args: ${args}, run: ctx => console.log(JSON.stringify(ctx.values)) }) const main = define({ name: 'todo', run: () => console.log('main') }) const result = await cli(process.argv.slice(2), main, { name: 'todo', version: '1.0.0', subCommands: { add } ${extra} }) if (process.env.SHOW) console.error('returned:' + JSON.stringify(result)) ` } /** * A CLI with a command `tag` that groups the sub-command `add`. * * @param groupRun - The `run` of `tag`, as source code that starts with a comma, or nothing * @returns The source code of the CLI */ function withGroup(groupRun = ''): string { return `import { cli, define } from 'gunshi' const add = define({ name: 'add', args: { url: { type: 'positional' } }, run: ctx => console.log('add ' + ctx.values.url) }) const tag = define({ name: 'tag', description: 'Manage tags', subCommands: { add } ${groupRun} }) const main = define({ name: 'bm', run: () => console.log('main') }) await cli(process.argv.slice(2), main, { name: 'bm', version: '1.0.0', subCommands: { tag } }) ` } beforeAll(() => { if (!existsSync(path.join(GUNSHI, 'lib', 'index.js'))) { throw new Error('packages/gunshi/lib/index.js is not found. Run `pnpm build` first.') } dir = mkdtempSync(path.join(os.tmpdir(), 'gunshi-agents-guide-')) mkdirSync(path.join(dir, 'node_modules')) symlinkSync(GUNSHI, path.join(dir, 'node_modules', 'gunshi')) symlinkSync(path.join(ROOT, 'node_modules', '@types'), path.join(dir, 'node_modules', '@types')) writeFileSync(path.join(dir, 'package.json'), '{ "type": "module" }\n') const example = /```ts\n([\s\S]*?)\n```/.exec(readFileSync(GUIDE, 'utf8')) if (!example) { throw new Error('AGENTS.md has no ts code block') } writeFileSync(path.join(dir, 'ship.ts'), `${example[1]}\n`) }) afterAll(() => { if (dir) { rmSync(dir, { recursive: true, force: true }) } }) describe('the example of AGENTS.md', () => { test('type-checks', () => { const result = spawnSync(TSC, [...TSC_OPTIONS, 'ship.ts'], { cwd: dir, encoding: 'utf8' }) expect(result.status, `${result.stdout}${result.stderr}`).toBe(0) }) const cases: [string[], number, RegExp, RegExp | ''][] = [ [[], 0, /USAGE:\n {2}ship \[COMMANDS\]/, ''], [['--help'], 0, /USAGE:\n {2}ship \[COMMANDS\]/, ''], [ ['deploy', '--help'], 0, /^(?=[\s\S]*--json)(?=[\s\S]*--dry-run)(?=[\s\S]*choices: dev \| prod)/, '' ], [['--version'], 0, /^1\.0\.0\n$/, ''], [['deploy', 'api'], 0, /^Deployed api to dev\n$/, ''], [ ['deploy', 'api', '--json'], 0, /^\{"deployment":\{"service":"api","target":"dev","dryRun":false,"config":"ship.json"\}\}\n$/, '' ], [ ['deploy', 'api', '-t', 'prod', '--dry-run', '--config', 'x.json', '--json'], 0, /^\{"deployment":\{"service":"api","target":"prod","dryRun":true,"config":"x.json"\}\}\n$/, '' ], [['deploy', 'api', '--unknown'], 1, /^$/, /^Unknown option: --unknown\n/], [['deploy', 'api', '--target', 'qa'], 1, /^$/, /--target/], [ ['deploy', 'api', '--target', 'qa', '--json'], 1, /^$/, /^\{"status":"action_required","reason":"invalid_choice",.*\}\n$/ ], [['rollback', '3'], 0, /^Rolled back to release 3\n$/, ''], [['rollback', '0'], 1, /^$/, /Invalid value for 'release': expected an integer from 1 to 999/], [['rollback', '1000'], 1, /^$/, /expected an integer from 1 to 999/], [['rollback', '1e2'], 1, /^$/, /expected an integer from 1 to 999/], [['rollback', '11'], 1, /^$/, /^Release 11 not found\n$/], [ ['rollback', '11', '--json'], 1, /^$/, /^\{"status":"action_required","reason":"json_not_supported",.*\}\n$/ ], [['rollback', 'abc'], 1, /^$/, /expected an integer from 1 to 999/], [['frobnicate'], 1, /^$/, /Command not found: frobnicate/] ] test.each(cases)('ship %j', (args, code, stdout, stderr) => { const result = run('ship.ts', args) expect(result.code).toBe(code) expect(result.stdout).toMatch(stdout) if (stderr === '') { expect(result.stderr).toBe('') } else { expect(result.stderr).toMatch(stderr) } expect(hasStackTrace(result.stderr)).toBe(false) }) }) describe('the rules of AGENTS.md', () => { test('gunshi/bone has no --help, no --version and no --json', () => { const file = write( 'bone.mjs', `import { cli } from 'gunshi/bone' import { define, jsonArgs } from 'gunshi' await cli(process.argv.slice(2), define({ name: 'x', args: { ...jsonArgs }, run: () => ({ item: 1 }) }), { name: 'x', version: '1.0.0' }) ` ) // given to it, `--help` and `--version` are unknown options, and cli() rejects with them const help = run(file, ['--help']) const version = run(file, ['--version']) expect(help).toMatchObject({ code: 1, stdout: '' }) expect(help.stderr).toMatch(/Unknown option: --help/) expect(version).toMatchObject({ code: 1, stdout: '' }) expect(version.stderr).toMatch(/Unknown option: --version/) // `--json` is an argument of the command here (jsonArgs), which gunshi/bone does not print expect(run(file, ['--json'])).toEqual({ code: 0, stdout: '', stderr: '' }) }) test('a positional argument is required unless it has required: false', () => { const file = write( 'positional.mjs', withAdd(`{ title: { type: 'positional' }, note: { type: 'positional', required: false } }`) ) const missing = run(file, ['add']) expect(missing.code).toBe(1) expect(missing.stderr).toMatch(/title/) expect(run(file, ['add', 'milk']).stdout).toBe('{"title":"milk"}\n') }) test('a positional argument with multiple: true is optional, and undefined without values, unless it has required: true', () => { const guide = readFileSync(GUIDE, 'utf8') const releases = "releases: { type: 'positional', multiple: true, required: true, description: 'The release numbers', parse: integerIn(1, 999) }" expect(guide).toContain(releases) // the rule for a value of the example, `integerIn()` const integerIn = /^const integerIn = [\s\S]+?^\}$/m.exec(guide)?.[0] expect(integerIn).toBeDefined() const file = write( 'positional-multiple.ts', `import { cli, define } from 'gunshi' ${integerIn} const promote = define({ name: 'promote', args: { ${releases} }, run: ctx => { const releases: number[] = ctx.values.releases console.log(JSON.stringify(releases)) } }) const status = define({ name: 'status', args: { releases: { type: 'positional', multiple: true, parse: integerIn(1, 999) } }, run: ctx => { const releases: number[] | undefined = ctx.values.releases // @ts-expect-error without required: true, the value may be undefined const all: number[] = ctx.values.releases console.log(JSON.stringify({ releases: releases ?? 'undefined', all: all ?? 'undefined' })) } }) await cli(process.argv.slice(2), define({ name: 'ship' }), { name: 'ship', subCommands: { promote, status } }) ` ) expect(typeCheck([file]).output).toBe('') expect(run(file, ['promote', '3', '12']).stdout).toBe('[3,12]\n') const none = run(file, ['promote']) expect(none.code).toBe(1) expect(none.stderr).toMatch(/releases/) // `parse` reads each of them const wrong = run(file, ['promote', '3', '1000']) expect(wrong.code).toBe(1) expect(wrong.stdout).toBe('') expect(wrong.stderr).toContain('expected an integer from 1 to 999') expect(run(file, ['status']).stdout).toBe('{"releases":"undefined","all":"undefined"}\n') expect(run(file, ['status', '7']).stdout).toBe('{"releases":[7],"all":[7]}\n') }) test('an option with multiple: true is undefined when it is not given, and its default is one value, not an array', () => { const file = write( 'option-multiple.ts', `import { cli, define } from 'gunshi' const deploy = define({ name: 'deploy', args: { var: { type: 'string', multiple: true }, region: { type: 'string', multiple: true, default: 'eu' } }, run: ctx => { const vars: string[] = ctx.values.var ?? [] const regions: string[] = ctx.values.region console.log(JSON.stringify({ given: ctx.values.var ?? 'undefined', vars, regions })) } }) await cli(process.argv.slice(2), define({ name: 'ship' }), { name: 'ship', subCommands: { deploy } }) ` ) expect(typeCheck([file]).output).toBe('') expect(run(file, ['deploy', '--var', 'A=1', '--var', 'B=2']).stdout).toBe( '{"given":["A=1","B=2"],"vars":["A=1","B=2"],"regions":["eu"]}\n' ) expect(run(file, ['deploy']).stdout).toBe('{"given":"undefined","vars":[],"regions":["eu"]}\n') const arrayDefault = write( 'option-multiple-array-default.ts', `import { define } from 'gunshi' define({ name: 'deploy', args: { var: { type: 'string', multiple: true, default: [] } }, run: () => {} }) ` ) // the type error that the guide quotes expect(typeCheck([arrayDefault]).output).toContain( "Type 'never[]' is not assignable to type 'string | number | boolean | undefined'" ) expect(readFileSync(GUIDE, 'utf8')).toContain( "Type 'never[]' is not assignable to type 'string | number | boolean | undefined'" ) }) test('an option is named after its key, or in kebab-case with toKebab', () => { const file = write( 'kebab.mjs', withAdd(`{ dryRun: { type: 'boolean', toKebab: true }, keepGoing: { type: 'boolean' } }`) ) expect(run(file, ['add', '--dry-run', '--keepGoing']).stdout).toBe( '{"dryRun":true,"keepGoing":true}\n' ) }) test('an argument that is not positional is an option: a required --name', () => { const file = write( 'option-not-positional.mjs', withAdd(`{ name: { type: 'string', required: true } }`) ) const missing = run(file, ['add']) expect(missing.code).toBe(1) expect(missing.stderr).toMatch(/--name/) expect(run(file, ['add', 'x']).code).toBe(1) expect(run(file, ['add', '--name', 'x']).stdout).toBe('{"name":"x"}\n') }) test('positional() alone is a string, and a combinator is the whole schema', () => { const file = write( 'combinator.mjs', `import { cli, define } from 'gunshi' import { describe, integer, positional } from 'gunshi/combinators' const add = define({ name: 'add', args: { title: describe(positional(), 'The title'), id: positional(integer({ min: 1 })) }, run: ctx => console.log(JSON.stringify(ctx.values)) }) await cli(process.argv.slice(2), add, { name: 'add' }) ` ) expect(run(file, ['milk', '2']).stdout).toBe('{"title":"milk","id":2}\n') expect(run(file, ['--help']).stdout).toMatch(/title\s+The title/) }) test('an option made with a combinator is optional unless its combinator takes required: true, or it is wrapped in required() or withDefault()', () => { const file = write( 'combinator-optional.ts', `import { define } from 'gunshi' import { integer, required, withDefault } from 'gunshi/combinators' define({ name: 'resize', args: { plain: integer({ min: 1 }), inOptions: integer({ min: 1, required: true }), needed: required(integer({ min: 1 })), defaulted: withDefault(integer({ min: 1 }), 1) }, run: ctx => { const plain: number | undefined = ctx.values.plain const inOptions: number = ctx.values.inOptions const needed: number = ctx.values.needed const defaulted: number = ctx.values.defaulted // @ts-expect-error without required: true, required() or withDefault(), the value may be undefined const wrong: number = ctx.values.plain console.log(plain, inOptions, needed, defaulted, wrong) } }) ` ) expect(typeCheck([file]).output).toBe('') }) test('subCommands is an object, a Map or an array', () => { const object = write('subcommands-object.mjs', withAdd('{}')) const map = write( 'subcommands-map.mjs', `import { cli, define } from 'gunshi' const add = define({ name: 'add', run: () => console.log('add') }) await cli(process.argv.slice(2), define({ name: 'todo', run: () => console.log('main') }), { subCommands: new Map([['add', add]]) }) ` ) const array = write( 'subcommands-array.mjs', `import { cli, define } from 'gunshi' const add = define({ name: 'add', run: () => console.log('add') }) await cli(process.argv.slice(2), define({ name: 'todo', run: () => console.log('main') }), { subCommands: [add] }) ` ) expect(run(object, ['add']).stdout).toBe('{}\n') expect(run(map, ['add']).stdout).toBe('add\n') expect(run(array, ['add']).stdout).toBe('add\n') }) test('the command given to cli() runs when no command is given', () => { const file = write('entry.mjs', withAdd('{}')) expect(run(file).stdout).toBe('main\n') }) test('a command can have its own subCommands', () => { const file = write('group.mjs', withGroup()) expect(run(file, ['tag', 'add', 'u'])).toMatchObject({ code: 0, stdout: 'add u\n' }) }) test('a command that only groups sub-commands prints its usage when it is run by itself', () => { const file = write('group-without-run.mjs', withGroup()) const result = run(file, ['tag']) expect(result).toMatchObject({ code: 0, stderr: '' }) expect(result.stdout).toMatch(/USAGE:\n {2}bm tag \[COMMANDS\] \[OPTIONS\]/) expect(result.stdout).toBe(run(file, ['tag', '--help']).stdout) }) test('any other command needs run, and fails without it when it runs', () => { const file = write( 'leaf-without-run.mjs', `import { cli, define } from 'gunshi' const add = define({ name: 'add', description: 'Add a bookmark' }) await cli(process.argv.slice(2), define({ name: 'bm', run: () => {} }), { name: 'bm', subCommands: { add } }) ` ) const result = run(file, ['add']) expect(result.code).toBe(1) expect(result.stderr).toMatch(/'run' is required in command: add/) expect(run(file, ['add', '--help'])).toMatchObject({ code: 0, stderr: '' }) }) test('a command that groups sub-commands runs its run, when it has one', () => { const file = write('group-with-run.mjs', withGroup(", run: () => console.log('tag')")) expect(run(file, ['tag']).stdout).toBe('tag\n') expect(run(file, ['tag', 'add', 'u']).stdout).toBe('add u\n') }) test('the sub-commands do not see the args of the main command', () => { const file = write( 'own-args.mjs', `import { cli, define } from 'gunshi' const add = define({ name: 'add', run: ctx => console.log(JSON.stringify(ctx.values)) }) const main = define({ name: 'todo', args: { file: { type: 'string' } }, run: () => {} }) await cli(process.argv.slice(2), main, { subCommands: { add }, strict: process.env.STRICT !== 'false' }) ` ) // an option of the main command given with a sub-command is an unknown option of the sub-command const reported = run(file, ['add', '--file', 'a.json']) expect(reported).toMatchObject({ code: 1, stdout: '' }) expect(reported.stderr).toMatch(/^Unknown option: --file\n/) // without strict, it is ignored: the sub-command does not see it expect(run(file, ['add', '--file', 'a.json'], envWith({ STRICT: 'false' })).stdout).toBe('{}\n') }) test('an option that a command does not declare is reported, unless strict: false', () => { const file = write('strict.mjs', withAdd('{}', ", strict: process.env.STRICT !== 'false'")) const reported = run(file, ['add', '--file', 'a.json']) expect(reported).toMatchObject({ code: 1, stdout: '' }) expect(reported.stderr).toMatch(/^Unknown option: --file\n/) expect(run(file, ['add', '--file', 'a.json'], envWith({ STRICT: 'false' }))).toMatchObject({ code: 0, stdout: '{}\n' }) // the arguments after `--` stay in `ctx.rest`, with or without strict const rest = write( 'strict-rest.mjs', `import { cli, define } from 'gunshi' await cli(process.argv.slice(2), define({ name: 'exec', run: ctx => console.log(JSON.stringify(ctx.rest)) })) ` ) expect(run(rest, ['--', '--file', 'a.json'])).toMatchObject({ code: 0, stdout: '["--file","a.json"]\n' }) // the rule applies to the requirements that let such an option pass, whatever they call it const guide = readFileSync(GUIDE, 'utf8') expect(guide).toContain( 'Keep this default unless the requirements say otherwise, in whatever words they use.' ) expect(guide).toContain( 'When they say that such an option is ignored, give `strict: false` to `cli()`.' ) }) test('a command with passthrough: true gets the arguments that it does not take in ctx.passthrough', () => { const file = write( 'passthrough.mjs', `import { cli, define } from 'gunshi' const exec = define({ name: 'exec', passthrough: true, args: { script: { type: 'positional' }, env: { type: 'string', short: 'e' } }, run: ctx => console.log(JSON.stringify({ values: ctx.values, passthrough: ctx.passthrough, rest: ctx.rest })) }) const build = define({ name: 'build', run: () => console.log('build') }) const main = define({ name: 'app', run: () => console.log('main') }) await cli(process.argv.slice(2), main, { name: 'app', version: '1.0.0', subCommands: { exec, build } }) ` ) // as typed and in order, with the arguments after `--` at the end, which are in `ctx.rest` too expect( run(file, ['exec', 'run.js', '-e', 'prod', '--watch', 'src', '-qk', '--', '--env', 'x']) ).toMatchObject({ code: 0, stdout: '{"values":{"script":"run.js","env":"prod"},"passthrough":["--watch","src","-qk","--env","x"],"rest":["--env","x"]}\n' }) // the other commands report the options that they do not declare const reported = run(file, ['build', '--watch']) expect(reported).toMatchObject({ code: 1, stdout: '' }) expect(reported.stderr).toMatch(/^Unknown option: --watch\n/) expect(readFileSync(GUIDE, 'utf8')).toContain( 'give the command `passthrough: true`: it then reports none of them, and `ctx.passthrough` has them, as typed and in order, with the arguments after `--` at the end. Pass `ctx.passthrough` on, not `ctx.rest` too.' ) }) test('a positional argument that a command does not declare is kept, unless strictPositionals: true', () => { const file = write( 'strict-positionals.mjs', withAdd( "{ title: { type: 'positional' } }", ", strictPositionals: process.env.STRICT === '1'" ) ) expect(run(file, ['add', 'milk', 'eggs'])).toMatchObject({ code: 0, stdout: '{"title":"milk"}\n' }) const strict = run(file, ['add', 'milk', 'eggs'], envWith({ STRICT: '1' })) expect(strict.code).toBe(1) expect(strict.stderr).toMatch(/Unexpected positional argument: eggs/) const many = write( 'strict-positionals-many.mjs', withAdd("{ titles: { type: 'positional', multiple: true } }", ', strictPositionals: true') ) expect(run(many, ['add', 'milk', 'eggs'])).toMatchObject({ code: 0, stdout: '{"titles":["milk","eggs"]}\n' }) }) test('strictPositionals is not part of strict: strict alone keeps an extra positional argument', () => { const file = write( 'strict-only.mjs', withAdd("{ title: { type: 'positional' } }", ', strict: true') ) expect(run(file, ['add', 'milk', 'eggs'])).toMatchObject({ code: 0, stdout: '{"title":"milk"}\n' }) }) test('--json is not ignored: a command without jsonArgs stops before it runs, with or without strict', () => { const file = write( 'json-not-supported.mjs', withAdd('{}', ", strict: process.env.STRICT === '1'") ) for (const strict of ['', '1']) { const result = run(file, ['add', '--json'], envWith({ STRICT: strict })) expect(result.code, `STRICT=${strict}`).toBe(1) expect(result.stdout).toBe('') expect(jsonFailure(result)).toMatchObject({ status: 'action_required', reason: 'json_not_supported' }) } }) test('the usage shows the name given to cli(), or COMMAND', () => { const named = write('named.mjs', withAdd('{}')) const unnamed = write( 'unnamed.mjs', withAdd('{}').replace("{ name: 'todo', version: '1.0.0',", "{ version: '1.0.0',") ) expect(run(named, ['--help']).stdout).toMatch(/USAGE:\n {2}todo /) expect(run(unnamed, ['--help']).stdout).toMatch(/USAGE:\n {2}COMMAND /) }) test('the errors of --json tell the command to run next with the name given to cli()', () => { const named = write('next-named.mjs', withAdd('{}')) const unnamed = write( 'next-unnamed.mjs', withAdd('{}').replace("{ name: 'todo', version: '1.0.0',", "{ version: '1.0.0',") ) expect(jsonFailure(run(named, ['add', '--json'])).next?.[0]?.argv).toEqual([ 'todo', 'add', '--help' ]) expect(jsonFailure(run(unnamed, ['add', '--json'])).next?.[0]?.argv).toEqual([ '', 'add', '--help' ]) }) test('cli() prints the version itself, and returns it', () => { const file = write('version.mjs', withAdd('{}')) const version = run(file, ['--version'], envWith({ SHOW: '1' })) expect(version.stdout).toBe('1.0.0\n') expect(version.stderr).toMatch(/returned:"1\.0\.0"/) }) test('the errors of the arguments go to stderr with the exit code 1', () => { const file = write( 'errors.mjs', withAdd(`{ title: { type: 'positional' }, level: { type: 'enum', choices: ['a', 'b'] } }`) ) for (const args of [['add'], ['add', 'x', '--level', 'c'], ['nope']]) { const result = run(file, args) expect(result.code, args.join(' ')).toBe(1) expect(result.stdout).toBe('') expect(result.stderr).not.toBe('') expect(hasStackTrace(result.stderr)).toBe(false) } }) test('onErrorCommand can set another exit code for the errors of the arguments', () => { const file = write( 'exit-code.mjs', `import { cli, define } from 'gunshi' const create = define({ name: 'create', args: { name: { type: 'positional' }, replicas: { type: 'number' } }, run: ctx => console.log(JSON.stringify(ctx.values)) }) const main = define({ name: 'cloud', run: () => console.log('main') }) await cli(process.argv.slice(2), main, { name: 'cloud', version: '1.0.0', subCommands: { create }, strict: true, onErrorCommand: process.env.TWO === '1' ? () => { process.exitCode = 2 } : undefined }) ` ) const inputs = [ ['create', 'db', '--unknown', 'x'], ['nope'], ['create', 'db', '--replicas', 'abc'], ['create'] ] for (const [two, code] of [ ['', 1], ['1', 2] ] as const) { for (const args of inputs) { const result = run(file, args, envWith({ TWO: two })) expect(result.code, `${args.join(' ')}: ${result.stderr}`).toBe(code) expect(result.stdout).toBe('') expect(result.stderr).not.toBe('') expect(hasStackTrace(result.stderr)).toBe(false) } } expect(run(file, ['create', 'db', '--replicas', '2'], envWith({ TWO: '1' })).code).toBe(0) }) test('validationErrorExitCode sets another exit code for the errors of the arguments only', () => { const file = write( 'validation-exit-code.mjs', `import { cli, CommandError, define, jsonArgs } from 'gunshi' const create = define({ name: 'create', args: { name: { type: 'positional' }, replicas: { type: 'number' } }, run: ctx => console.log(JSON.stringify(ctx.values)) }) const remove = define({ name: 'remove', args: { ...jsonArgs }, run: () => { throw new CommandError('Release v9 does not exist', { exitCode: 3 }) } }) const status = define({ name: 'status', run: () => console.log('ok') }) const main = define({ name: 'cloud', run: () => console.log('main') }) await cli(process.argv.slice(2), main, { name: 'cloud', version: '1.0.0', subCommands: { create, remove, status }, strict: true, validationErrorExitCode: 2 }) ` ) for (const args of [ ['create', 'db', '--unknown', 'x'], ['nope'], ['create', 'db', '--replicas', 'abc'], ['create'], ['status', '--json'] ]) { const result = run(file, args) expect(result.code, `${args.join(' ')}: ${result.stderr}`).toBe(2) expect(result.stdout).toBe('') expect(result.stderr).not.toBe('') expect(hasStackTrace(result.stderr)).toBe(false) } expect(run(file, ['remove']).code).toBe(3) expect(run(file, ['remove', '--json']).code).toBe(3) expect(run(file, ['create', '--help']).code).toBe(0) expect(run(file, ['create', 'db', '--replicas', '2']).code).toBe(0) }) test('validationErrorExitCode does not make an unknown option an error: without strict, the command runs', () => { const file = write( 'validation-exit-code-strict.mjs', withAdd( "{ title: { type: 'positional' } }", ", validationErrorExitCode: 2, strict: process.env.STRICT === '1'" ) ) expect(run(file, ['add', 'milk', '--urgent'])).toMatchObject({ code: 0, stdout: '{"title":"milk"}\n' }) const strict = run(file, ['add', 'milk', '--urgent'], envWith({ STRICT: '1' })) expect(strict.code).toBe(2) expect(strict.stdout).toBe('') expect(strict.stderr).toMatch(/Unknown option: --urgent/) }) test('the values that args checks are errors of the arguments: a range, and a parser that throws', () => { const guide = readFileSync(GUIDE, 'utf8') const wait = "wait: { type: 'custom', short: 'w', default: 60, description: 'Seconds to wait for the service', parse: integerIn(0, 600) }" expect(guide).toContain(wait) // the rule for a value of the example, `integerIn()` const integerIn = /^const integerIn = [\s\S]+?^\}$/m.exec(guide)?.[0] expect(integerIn).toBeDefined() const file = write( 'check-in-args.ts', `import { cli, define, jsonArgs } from 'gunshi' ${integerIn} const deploy = define({ name: 'deploy', args: { ...jsonArgs, service: { type: 'positional', parse: value => { if (!/^[a-z0-9-]+$/.test(value)) { throw new Error('use lowercase letters, digits and hyphens') } return value } }, ${wait} }, run: ctx => ({ deployment: { service: ctx.values.service, wait: ctx.values.wait } }) }) await cli(process.argv.slice(2), define({ name: 'ship' }), { name: 'ship', subCommands: { deploy }, validationErrorExitCode: 2 }) ` ) const { output } = typeCheck([file]) expect(output).toBe('') expect(run(file, ['deploy', 'api', '--json'])).toEqual({ code: 0, stdout: '{"deployment":{"service":"api","wait":60}}\n', stderr: '' }) expect(run(file, ['deploy', 'api', '-w', '30', '--json']).stdout).toBe( '{"deployment":{"service":"api","wait":30}}\n' ) for (const args of [ ['deploy', 'api', '-w', '700'], ['deploy', 'Bad_Name'] ]) { const result = run(file, args) expect(result.code, `${args.join(' ')}: ${result.stderr}`).toBe(2) expect(result.stdout).toBe('') expect(result.stderr).toContain('ship deploy --help') expect(hasStackTrace(result.stderr)).toBe(false) const json = run(file, [...args, '--json']) expect(json.code).toBe(2) const failure = jsonFailure(json) expect(failure.status).toBe('action_required') expect(failure.next?.[0]?.command).toBe('ship deploy --help') } }) test('a CommandError prints its message to stderr without a stack trace, and sets the exit code', () => { const file = write( 'command-error.mjs', `import { cli, CommandError, define } from 'gunshi' const rollback = define({ name: 'rollback', args: { exitCode: { type: 'number' } }, run: ctx => { const cause = process.env.CAUSE ? new Error(process.env.CAUSE) : undefined throw new CommandError('Release 11 not found', { code: 'not_found', exitCode: ctx.values.exitCode, cause }) } }) await cli(process.argv.slice(2), rollback, { name: 'ship' }).then( () => console.log('resolved'), () => console.log('rejected') ) ` ) expect(run(file)).toEqual({ code: 1, stdout: 'resolved\n', stderr: 'Release 11 not found\n' }) expect(run(file, ['--exitCode', '2'])).toEqual({ code: 2, stdout: 'resolved\n', stderr: 'Release 11 not found\n' }) expect(run(file, [], envWith({ CAUSE: 'ENOENT: no such file' }))).toEqual({ code: 1, stdout: 'resolved\n', stderr: 'Release 11 not found\nCaused by: ENOENT: no such file\n' }) }) test('any other error thrown from run ends in a stack trace', () => { const file = write( 'throw.mjs', `import { cli, define } from 'gunshi' await cli(process.argv.slice(2), define({ name: 'x', run: () => { throw new Error('boom') } })) ` ) const result = run(file) expect(result.code).not.toBe(0) expect(result.stderr).toMatch(/Error: boom/) expect(hasStackTrace(result.stderr)).toBe(true) }) test('with --json, a CommandError is one line of JSON on stderr, and its code is the reason', () => { const file = write( 'command-error-json.mjs', `import { cli, CommandError, define, jsonArgs } from 'gunshi' const show = define({ name: 'show', args: { ...jsonArgs, name: { type: 'positional' } }, run: ctx => { throw new CommandError('Project ' + ctx.values.name + ' not found', { code: 'not_found', exitCode: 2 }) } }) await cli(process.argv.slice(2), show, { name: 'projects' }) ` ) expect(run(file, ['nope'])).toEqual({ code: 2, stdout: '', stderr: 'Project nope not found\n' }) expect(run(file, ['nope', '--json'])).toEqual({ code: 2, stdout: '', stderr: '{"status":"error","reason":"not_found","message":"Project nope not found"}\n' }) }) test('a lazy command with jsonArgs in its definition takes --json before the name of the command too', () => { const file = write( 'lazy-json.mjs', `import { cli, define, jsonArgs, lazy } from 'gunshi' const args = { ...jsonArgs, name: { type: 'positional' } } const show = lazy(async () => define({ name: 'show', args, run: ctx => ({ project: { name: ctx.values.name } }) }), { name: 'show', args }) await cli(process.argv.slice(2), define({ name: 'projects', run: () => {} }), { name: 'projects', subCommands: { show } }) ` ) const result = { code: 0, stdout: '{"project":{"name":"acme"}}\n', stderr: '' } expect(run(file, ['show', 'acme', '--json'])).toEqual(result) expect(run(file, ['--json', 'show', 'acme'])).toEqual(result) }) test('gunshi does not touch a json option of its own', () => { const file = write( 'own-json.mjs', `import { cli, define } from 'gunshi' const list = define({ name: 'list', args: { json: { type: 'boolean' } }, run: ctx => { const names = ['acme-web', 'acme-api'] console.log(ctx.values.json ? JSON.stringify(names) : names.join('\\n')) } }) await cli(process.argv.slice(2), define({ name: 'projects', run: () => {} }), { name: 'projects', subCommands: { list } }) ` ) expect(run(file, ['list'])).toEqual({ code: 0, stdout: 'acme-web\nacme-api\n', stderr: '' }) expect(run(file, ['list', '--json'])).toEqual({ code: 0, stdout: '["acme-web","acme-api"]\n', stderr: '' }) }) test('the type errors that it explains are the ones that tsc prints', () => { const guide = readFileSync(GUIDE, 'utf8') const head = `import { define, jsonArgs, lazy } from 'gunshi' interface Project { name: string } declare function find(): Project ` // the file, the code, a line of the type error, and what the guide quotes of it const cases: [string, string, string, string][] = [ [ 'returns-nothing.ts', `export const c = define({ name: 'c', args: { ...jsonArgs }, run: ctx => { ctx.log('c') } })`, "Type 'void' is not assignable to type 'Awaitable'", "'Awaitable'" ], [ 'returns-nothing-async.ts', `export const c = define({ name: 'c', args: { ...jsonArgs }, run: async ctx => { ctx.log('c') } })`, "Type 'void' is not assignable to type 'JsonResult'", "Type 'void' is not assignable to type 'JsonResult'" ], [ 'returns-in-one-branch.ts', `export const c = define({ name: 'c', args: { ...jsonArgs }, run: ctx => { if (ctx.values.json) { return { project: find() } } ctx.log('c') } })`, "Type 'undefined' is not assignable to type 'Awaitable'", "or the same with `'undefined'`" ], [ 'returns-a-value.ts', `export const c = define({ name: 'c', args: { ...jsonArgs }, run: () => find() })`, "Index signature for type 'string' is missing in type 'Project'", "Index signature for type 'string' is missing in type 'Project'" ], [ 'returns-an-array.ts', `export const c = define({ name: 'c', args: { ...jsonArgs }, run: () => [find()] })`, "Index signature for type 'string' is missing in type 'Project[]'", "'Project[]'" ], [ 'lazy-without-json-args.ts', `export const c = lazy(async () => define({ name: 'c', args: { ...jsonArgs }, run: () => ({ project: find() }) }), { name: 'c' })`, 'No overload matches this call', 'No overload matches this call' ], [ 'lazy-with-run.ts', `export const c = lazy(async () => () => {}, { name: 'c', run: () => {} })`, "Type '() => void' is not assignable to type 'never'", "is not assignable to type 'never'" ], [ 'returns-an-object-without-json-args.ts', `export const c = define({ name: 'c', run: () => ({ project: find() }) })`, "is not assignable to type 'Awaitable'", "'Awaitable'" ] ] for (const [file, code] of cases) { write(file, `${head}${code}\n`) } const { output, errors } = typeCheck(cases.map(([file]) => file)) for (const [file, , line, quote] of cases) { expect(errors[file], `the output of tsc:\n${output}`).toContain(line) expect(guide).toContain(quote) } }) test('gunshi does not read environment variables, and run writes the order of the fallbacks', () => { const file = write( 'env.mjs', `import { cli, define } from 'gunshi' const deploy = define({ name: 'deploy', args: { region: { type: 'string' } }, run: ctx => console.log(ctx.values.region ?? process.env.SHIP_REGION ?? 'us-east-1') }) await cli(process.argv.slice(2), define({ name: 'ship', run: () => {} }), { subCommands: { deploy } }) ` ) expect( run(file, ['deploy'], envWith({ SHIP_REGION: undefined, REGION: 'ignored' })).stdout ).toBe('us-east-1\n') expect(run(file, ['deploy'], envWith({ SHIP_REGION: 'eu-west-1' })).stdout).toBe('eu-west-1\n') expect( run(file, ['deploy', '--region', 'ap-1'], envWith({ SHIP_REGION: 'eu-west-1' })).stdout ).toBe('ap-1\n') }) test('stdout has only what the command prints, and only the JSON document with --json', () => { const file = write( 'stdout.mjs', `import { cli, define, jsonArgs } from 'gunshi' const list = define({ name: 'list', args: { ...jsonArgs }, run: ctx => { ctx.log('acme-web') ctx.log('acme-api') return { projects: [{ name: 'acme-web' }, { name: 'acme-api' }] } } }) await cli(process.argv.slice(2), define({ name: 'projects', run: () => {} }), { name: 'projects', version: '1.0.0', subCommands: { list } }) ` ) expect(run(file, ['list'])).toEqual({ code: 0, stdout: 'acme-web\nacme-api\n', stderr: '' }) expect(run(file, ['list', '--json'])).toEqual({ code: 0, stdout: '{"projects":[{"name":"acme-web"},{"name":"acme-api"}]}\n', stderr: '' }) }) })