@contember/cli-common 2.2.0-alpha.3 → 2.2.0-alpha.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/development/index.cjs +20 -0
- package/dist/development/index.cjs.map +1 -1
- package/dist/development/index.js +22 -2
- package/dist/development/index.js.map +1 -1
- package/dist/development/src/application/Application.cjs +116 -59
- package/dist/development/src/application/Application.cjs.map +1 -1
- package/dist/development/src/application/Application.js +116 -59
- package/dist/development/src/application/Application.js.map +1 -1
- package/dist/development/src/application/CliError.cjs +104 -0
- package/dist/development/src/application/CliError.cjs.map +1 -0
- package/dist/development/src/application/CliError.js +104 -0
- package/dist/development/src/application/CliError.js.map +1 -0
- package/dist/development/src/application/Command.cjs +18 -5
- package/dist/development/src/application/Command.cjs.map +1 -1
- package/dist/development/src/application/Command.js +18 -5
- package/dist/development/src/application/Command.js.map +1 -1
- package/dist/development/src/application/CommandConfiguration.cjs +21 -3
- package/dist/development/src/application/CommandConfiguration.cjs.map +1 -1
- package/dist/development/src/application/CommandConfiguration.js +23 -5
- package/dist/development/src/application/CommandConfiguration.js.map +1 -1
- package/dist/development/src/application/CommandManager.cjs +139 -21
- package/dist/development/src/application/CommandManager.cjs.map +1 -1
- package/dist/development/src/application/CommandManager.js +139 -21
- package/dist/development/src/application/CommandManager.js.map +1 -1
- package/dist/development/src/application/ErrorRenderer.cjs +38 -0
- package/dist/development/src/application/ErrorRenderer.cjs.map +1 -0
- package/dist/development/src/application/ErrorRenderer.js +38 -0
- package/dist/development/src/application/ErrorRenderer.js.map +1 -0
- package/dist/development/src/application/GlobalOptions.cjs +54 -0
- package/dist/development/src/application/GlobalOptions.cjs.map +1 -0
- package/dist/development/src/application/GlobalOptions.js +54 -0
- package/dist/development/src/application/GlobalOptions.js.map +1 -0
- package/dist/development/src/application/Input.cjs +5 -0
- package/dist/development/src/application/Input.cjs.map +1 -1
- package/dist/development/src/application/Input.js +5 -0
- package/dist/development/src/application/Input.js.map +1 -1
- package/dist/development/src/application/InputParser.cjs +7 -3
- package/dist/development/src/application/InputParser.cjs.map +1 -1
- package/dist/development/src/application/InputParser.js +7 -3
- package/dist/development/src/application/InputParser.js.map +1 -1
- package/dist/development/src/application/Option.cjs +11 -0
- package/dist/development/src/application/Option.cjs.map +1 -1
- package/dist/development/src/application/Option.js +12 -1
- package/dist/development/src/application/Option.js.map +1 -1
- package/dist/development/src/application/Output.cjs +242 -0
- package/dist/development/src/application/Output.cjs.map +1 -0
- package/dist/development/src/application/Output.js +242 -0
- package/dist/development/src/application/Output.js.map +1 -0
- package/dist/development/src/application/exitProcess.cjs +7 -0
- package/dist/development/src/application/exitProcess.cjs.map +1 -0
- package/dist/development/src/application/exitProcess.js +7 -0
- package/dist/development/src/application/exitProcess.js.map +1 -0
- package/dist/development/src/npm/CommandRunner.cjs +126 -24
- package/dist/development/src/npm/CommandRunner.cjs.map +1 -1
- package/dist/development/src/npm/CommandRunner.js +126 -24
- package/dist/development/src/npm/CommandRunner.js.map +1 -1
- package/dist/development/src/npm/packageManagers/Bun.cjs +1 -2
- package/dist/development/src/npm/packageManagers/Bun.cjs.map +1 -1
- package/dist/development/src/npm/packageManagers/Bun.js +1 -2
- package/dist/development/src/npm/packageManagers/Bun.js.map +1 -1
- package/dist/development/src/npm/packageManagers/Npm.cjs +1 -2
- package/dist/development/src/npm/packageManagers/Npm.cjs.map +1 -1
- package/dist/development/src/npm/packageManagers/Npm.js +1 -2
- package/dist/development/src/npm/packageManagers/Npm.js.map +1 -1
- package/dist/development/src/npm/packageManagers/Pnpm.cjs +1 -2
- package/dist/development/src/npm/packageManagers/Pnpm.cjs.map +1 -1
- package/dist/development/src/npm/packageManagers/Pnpm.js +1 -2
- package/dist/development/src/npm/packageManagers/Pnpm.js.map +1 -1
- package/dist/development/src/npm/packageManagers/Yarn.cjs +1 -2
- package/dist/development/src/npm/packageManagers/Yarn.cjs.map +1 -1
- package/dist/development/src/npm/packageManagers/Yarn.js +1 -2
- package/dist/development/src/npm/packageManagers/Yarn.js.map +1 -1
- package/dist/development/src/npm/packageManagers/YarnClassic.cjs +1 -2
- package/dist/development/src/npm/packageManagers/YarnClassic.cjs.map +1 -1
- package/dist/development/src/npm/packageManagers/YarnClassic.js +1 -2
- package/dist/development/src/npm/packageManagers/YarnClassic.js.map +1 -1
- package/dist/production/index.cjs +20 -0
- package/dist/production/index.cjs.map +1 -1
- package/dist/production/index.js +22 -2
- package/dist/production/index.js.map +1 -1
- package/dist/production/src/application/Application.cjs +116 -59
- package/dist/production/src/application/Application.cjs.map +1 -1
- package/dist/production/src/application/Application.js +116 -59
- package/dist/production/src/application/Application.js.map +1 -1
- package/dist/production/src/application/CliError.cjs +104 -0
- package/dist/production/src/application/CliError.cjs.map +1 -0
- package/dist/production/src/application/CliError.js +104 -0
- package/dist/production/src/application/CliError.js.map +1 -0
- package/dist/production/src/application/Command.cjs +18 -5
- package/dist/production/src/application/Command.cjs.map +1 -1
- package/dist/production/src/application/Command.js +18 -5
- package/dist/production/src/application/Command.js.map +1 -1
- package/dist/production/src/application/CommandConfiguration.cjs +21 -3
- package/dist/production/src/application/CommandConfiguration.cjs.map +1 -1
- package/dist/production/src/application/CommandConfiguration.js +23 -5
- package/dist/production/src/application/CommandConfiguration.js.map +1 -1
- package/dist/production/src/application/CommandManager.cjs +139 -21
- package/dist/production/src/application/CommandManager.cjs.map +1 -1
- package/dist/production/src/application/CommandManager.js +139 -21
- package/dist/production/src/application/CommandManager.js.map +1 -1
- package/dist/production/src/application/ErrorRenderer.cjs +38 -0
- package/dist/production/src/application/ErrorRenderer.cjs.map +1 -0
- package/dist/production/src/application/ErrorRenderer.js +38 -0
- package/dist/production/src/application/ErrorRenderer.js.map +1 -0
- package/dist/production/src/application/GlobalOptions.cjs +54 -0
- package/dist/production/src/application/GlobalOptions.cjs.map +1 -0
- package/dist/production/src/application/GlobalOptions.js +54 -0
- package/dist/production/src/application/GlobalOptions.js.map +1 -0
- package/dist/production/src/application/Input.cjs +5 -0
- package/dist/production/src/application/Input.cjs.map +1 -1
- package/dist/production/src/application/Input.js +5 -0
- package/dist/production/src/application/Input.js.map +1 -1
- package/dist/production/src/application/InputParser.cjs +7 -3
- package/dist/production/src/application/InputParser.cjs.map +1 -1
- package/dist/production/src/application/InputParser.js +7 -3
- package/dist/production/src/application/InputParser.js.map +1 -1
- package/dist/production/src/application/Option.cjs +11 -0
- package/dist/production/src/application/Option.cjs.map +1 -1
- package/dist/production/src/application/Option.js +12 -1
- package/dist/production/src/application/Option.js.map +1 -1
- package/dist/production/src/application/Output.cjs +242 -0
- package/dist/production/src/application/Output.cjs.map +1 -0
- package/dist/production/src/application/Output.js +242 -0
- package/dist/production/src/application/Output.js.map +1 -0
- package/dist/production/src/application/exitProcess.cjs +7 -0
- package/dist/production/src/application/exitProcess.cjs.map +1 -0
- package/dist/production/src/application/exitProcess.js +7 -0
- package/dist/production/src/application/exitProcess.js.map +1 -0
- package/dist/production/src/npm/CommandRunner.cjs +126 -24
- package/dist/production/src/npm/CommandRunner.cjs.map +1 -1
- package/dist/production/src/npm/CommandRunner.js +126 -24
- package/dist/production/src/npm/CommandRunner.js.map +1 -1
- package/dist/production/src/npm/packageManagers/Bun.cjs +1 -2
- package/dist/production/src/npm/packageManagers/Bun.cjs.map +1 -1
- package/dist/production/src/npm/packageManagers/Bun.js +1 -2
- package/dist/production/src/npm/packageManagers/Bun.js.map +1 -1
- package/dist/production/src/npm/packageManagers/Npm.cjs +1 -2
- package/dist/production/src/npm/packageManagers/Npm.cjs.map +1 -1
- package/dist/production/src/npm/packageManagers/Npm.js +1 -2
- package/dist/production/src/npm/packageManagers/Npm.js.map +1 -1
- package/dist/production/src/npm/packageManagers/Pnpm.cjs +1 -2
- package/dist/production/src/npm/packageManagers/Pnpm.cjs.map +1 -1
- package/dist/production/src/npm/packageManagers/Pnpm.js +1 -2
- package/dist/production/src/npm/packageManagers/Pnpm.js.map +1 -1
- package/dist/production/src/npm/packageManagers/Yarn.cjs +1 -2
- package/dist/production/src/npm/packageManagers/Yarn.cjs.map +1 -1
- package/dist/production/src/npm/packageManagers/Yarn.js +1 -2
- package/dist/production/src/npm/packageManagers/Yarn.js.map +1 -1
- package/dist/production/src/npm/packageManagers/YarnClassic.cjs +1 -2
- package/dist/production/src/npm/packageManagers/YarnClassic.cjs.map +1 -1
- package/dist/production/src/npm/packageManagers/YarnClassic.js +1 -2
- package/dist/production/src/npm/packageManagers/YarnClassic.js.map +1 -1
- package/dist/types/application/Application.d.ts +22 -11
- package/dist/types/application/Application.d.ts.map +1 -1
- package/dist/types/application/Argument.d.ts.map +1 -1
- package/dist/types/application/CliError.d.ts +40 -0
- package/dist/types/application/CliError.d.ts.map +1 -0
- package/dist/types/application/Command.d.ts +12 -2
- package/dist/types/application/Command.d.ts.map +1 -1
- package/dist/types/application/CommandConfiguration.d.ts +7 -4
- package/dist/types/application/CommandConfiguration.d.ts.map +1 -1
- package/dist/types/application/CommandManager.d.ts +40 -2
- package/dist/types/application/CommandManager.d.ts.map +1 -1
- package/dist/types/application/ErrorRenderer.d.ts +8 -0
- package/dist/types/application/ErrorRenderer.d.ts.map +1 -0
- package/dist/types/application/GlobalOptions.d.ts +28 -0
- package/dist/types/application/GlobalOptions.d.ts.map +1 -0
- package/dist/types/application/Input.d.ts +2 -0
- package/dist/types/application/Input.d.ts.map +1 -1
- package/dist/types/application/InputParser.d.ts +5 -1
- package/dist/types/application/InputParser.d.ts.map +1 -1
- package/dist/types/application/Option.d.ts +2 -0
- package/dist/types/application/Option.d.ts.map +1 -1
- package/dist/types/application/Output.d.ts +81 -0
- package/dist/types/application/Output.d.ts.map +1 -0
- package/dist/types/application/UsageFormatter.d.ts.map +1 -1
- package/dist/types/application/exitProcess.d.ts +11 -0
- package/dist/types/application/exitProcess.d.ts.map +1 -0
- package/dist/types/application/index.d.ts +5 -0
- package/dist/types/application/index.d.ts.map +1 -1
- package/dist/types/index.d.ts +2 -2
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/npm/CommandRunner.d.ts +13 -0
- package/dist/types/npm/CommandRunner.d.ts.map +1 -1
- package/dist/types/npm/Package.d.ts.map +1 -1
- package/dist/types/npm/PackageWorkspace.d.ts.map +1 -1
- package/dist/types/npm/PackageWorkspaceResolver.d.ts.map +1 -1
- package/dist/types/npm/packageManagers/Bun.d.ts.map +1 -1
- package/dist/types/npm/packageManagers/Npm.d.ts.map +1 -1
- package/dist/types/npm/packageManagers/PackageManagerHelpers.d.ts.map +1 -1
- package/dist/types/npm/packageManagers/Pnpm.d.ts.map +1 -1
- package/dist/types/npm/packageManagers/Yarn.d.ts.map +1 -1
- package/dist/types/npm/packageManagers/YarnClassic.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/dist/types/utils/findPackageRoot.d.ts.map +1 -1
- package/package.json +3 -2
- package/src/application/Application.ts +137 -75
- package/src/application/CliError.ts +117 -0
- package/src/application/Command.ts +27 -6
- package/src/application/CommandConfiguration.ts +22 -4
- package/src/application/CommandManager.ts +172 -22
- package/src/application/ErrorRenderer.ts +40 -0
- package/src/application/GlobalOptions.ts +76 -0
- package/src/application/Input.ts +6 -0
- package/src/application/InputParser.ts +13 -4
- package/src/application/Option.ts +12 -0
- package/src/application/Output.ts +329 -0
- package/src/application/exitProcess.ts +12 -0
- package/src/application/index.ts +5 -0
- package/src/index.ts +40 -2
- package/src/npm/CommandRunner.ts +166 -23
- package/src/npm/packageManagers/Bun.ts +1 -2
- package/src/npm/packageManagers/Npm.ts +1 -2
- package/src/npm/packageManagers/Pnpm.ts +1 -2
- package/src/npm/packageManagers/Yarn.ts +1 -2
- package/src/npm/packageManagers/YarnClassic.ts +1 -2
- package/tests/cases/unit/application.test.ts +426 -0
- package/tests/cases/unit/cliError.test.ts +127 -0
- package/tests/cases/unit/commandManager.test.ts +270 -0
- package/tests/cases/unit/commandRunner.test.ts +192 -0
- package/tests/cases/unit/inputParser.test.ts +21 -0
- package/tests/cases/unit/output.test.ts +393 -0
- package/tests/lib/testOutput.ts +35 -0
- package/types/chalk-table/index.d.ts +9 -0
|
@@ -0,0 +1,270 @@
|
|
|
1
|
+
import { describe, expect, test } from 'bun:test'
|
|
2
|
+
import {
|
|
3
|
+
CliError,
|
|
4
|
+
Command,
|
|
5
|
+
CommandConfiguration,
|
|
6
|
+
CommandFactoryList,
|
|
7
|
+
CommandManager,
|
|
8
|
+
ExitCode,
|
|
9
|
+
Input,
|
|
10
|
+
InvalidConfigurationError,
|
|
11
|
+
Output,
|
|
12
|
+
} from '../../../src/index.js'
|
|
13
|
+
|
|
14
|
+
class NoopCommand extends Command<{}, {}> {
|
|
15
|
+
constructor(private readonly commandDescription: string = '') {
|
|
16
|
+
super()
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
protected configure(configuration: CommandConfiguration<{}, {}>): void {
|
|
20
|
+
configuration.description(this.commandDescription)
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
protected async execute(input: Input<{}, {}>, output: Output): Promise<void> {
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const createManager = (names: string[][]): CommandManager => {
|
|
28
|
+
const commands: CommandFactoryList = {}
|
|
29
|
+
for (const [canonical, ...aliases] of names) {
|
|
30
|
+
const factory = () => new NoopCommand(`description of ${canonical}`)
|
|
31
|
+
commands[canonical] = factory
|
|
32
|
+
for (const alias of aliases) {
|
|
33
|
+
commands[alias] = factory
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
return new CommandManager(commands)
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const defaultManager = () =>
|
|
40
|
+
createManager([
|
|
41
|
+
['deploy'],
|
|
42
|
+
['version'],
|
|
43
|
+
['data export', 'data:export'],
|
|
44
|
+
['data import', 'data:import'],
|
|
45
|
+
['migrations diff', 'migrations:diff'],
|
|
46
|
+
['migrations describe', 'migrations:describe'],
|
|
47
|
+
['migrations execute', 'migrations:execute'],
|
|
48
|
+
['workspace update api', 'workspace:update:api'],
|
|
49
|
+
['tenant apply', 'tenant:apply'],
|
|
50
|
+
])
|
|
51
|
+
|
|
52
|
+
const resolveCommand = (manager: CommandManager, args: string[]) => {
|
|
53
|
+
const resolution = manager.resolve(args)
|
|
54
|
+
if (resolution.type !== 'command') {
|
|
55
|
+
throw new Error(`expected a command, got a ${resolution.type}`)
|
|
56
|
+
}
|
|
57
|
+
return { name: resolution.entry.name, args: resolution.args }
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
describe('resolution', () => {
|
|
61
|
+
test('resolves a single token command', () => {
|
|
62
|
+
expect(resolveCommand(defaultManager(), ['deploy'])).toStrictEqual({ name: 'deploy', args: [] })
|
|
63
|
+
})
|
|
64
|
+
|
|
65
|
+
test('resolves the space separated form', () => {
|
|
66
|
+
expect(resolveCommand(defaultManager(), ['migrations', 'diff'])).toStrictEqual({ name: 'migrations diff', args: [] })
|
|
67
|
+
})
|
|
68
|
+
|
|
69
|
+
test('resolves the colon separated form', () => {
|
|
70
|
+
expect(resolveCommand(defaultManager(), ['migrations:diff'])).toStrictEqual({ name: 'migrations diff', args: [] })
|
|
71
|
+
})
|
|
72
|
+
|
|
73
|
+
test('resolves a mixed form', () => {
|
|
74
|
+
expect(resolveCommand(defaultManager(), ['workspace:update', 'api'])).toStrictEqual({ name: 'workspace update api', args: [] })
|
|
75
|
+
})
|
|
76
|
+
|
|
77
|
+
test('the longest prefix wins and the rest becomes arguments', () => {
|
|
78
|
+
expect(resolveCommand(defaultManager(), ['migrations', 'diff', 'my-migration', '--execute'])).toStrictEqual({
|
|
79
|
+
name: 'migrations diff',
|
|
80
|
+
args: ['my-migration', '--execute'],
|
|
81
|
+
})
|
|
82
|
+
})
|
|
83
|
+
|
|
84
|
+
test('an argument is never split into command tokens', () => {
|
|
85
|
+
expect(resolveCommand(defaultManager(), ['data:export', 'contember://token@localhost/app'])).toStrictEqual({
|
|
86
|
+
name: 'data export',
|
|
87
|
+
args: ['contember://token@localhost/app'],
|
|
88
|
+
})
|
|
89
|
+
})
|
|
90
|
+
|
|
91
|
+
test('a leading option stops the command lookup', () => {
|
|
92
|
+
expect(resolveCommand(defaultManager(), ['deploy', '--admin', 'admin'])).toStrictEqual({ name: 'deploy', args: ['--admin', 'admin'] })
|
|
93
|
+
})
|
|
94
|
+
|
|
95
|
+
test('an unknown command is reported', () => {
|
|
96
|
+
expect(() => defaultManager().resolve(['nonsense'])).toThrow(CliError)
|
|
97
|
+
try {
|
|
98
|
+
defaultManager().resolve(['nonsense'])
|
|
99
|
+
} catch (e) {
|
|
100
|
+
expect(e).toBeInstanceOf(CliError)
|
|
101
|
+
if (e instanceof CliError) {
|
|
102
|
+
expect(e.code).toBe('COMMAND_NOT_FOUND')
|
|
103
|
+
expect(e.exitCode).toBe(ExitCode.InputError)
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
})
|
|
107
|
+
|
|
108
|
+
test('an unknown verb of a known group is reported with the group listing', () => {
|
|
109
|
+
try {
|
|
110
|
+
defaultManager().resolve(['migrations', 'nonsense'])
|
|
111
|
+
throw new Error('should have thrown')
|
|
112
|
+
} catch (e) {
|
|
113
|
+
expect(e).toBeInstanceOf(CliError)
|
|
114
|
+
if (e instanceof CliError) {
|
|
115
|
+
expect(e.code).toBe('COMMAND_NOT_FOUND')
|
|
116
|
+
expect(e.message).toContain('migrations diff')
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
})
|
|
120
|
+
|
|
121
|
+
test('a group with a single command does not swallow an unknown verb', () => {
|
|
122
|
+
expect(() => defaultManager().resolve(['tenant', 'nonsense'])).toThrow(CliError)
|
|
123
|
+
})
|
|
124
|
+
})
|
|
125
|
+
|
|
126
|
+
describe('aliases', () => {
|
|
127
|
+
test('the first registered name is canonical, the rest are aliases', () => {
|
|
128
|
+
const entry = defaultManager().getCommands().find(it => it.name === 'data export')
|
|
129
|
+
expect(entry?.aliases).toStrictEqual(['data:export'])
|
|
130
|
+
})
|
|
131
|
+
|
|
132
|
+
test('an alias is silent', () => {
|
|
133
|
+
const warnings: unknown[] = []
|
|
134
|
+
const originalWarn = console.warn
|
|
135
|
+
console.warn = (...args: unknown[]) => warnings.push(args)
|
|
136
|
+
try {
|
|
137
|
+
resolveCommand(defaultManager(), ['migrations:diff'])
|
|
138
|
+
defaultManager().createCommand('migrations:diff')
|
|
139
|
+
} finally {
|
|
140
|
+
console.warn = originalWarn
|
|
141
|
+
}
|
|
142
|
+
expect(warnings).toStrictEqual([])
|
|
143
|
+
})
|
|
144
|
+
|
|
145
|
+
test('a command is listed only once', () => {
|
|
146
|
+
expect(defaultManager().getCommands().map(it => it.name)).toStrictEqual([
|
|
147
|
+
'data export',
|
|
148
|
+
'data import',
|
|
149
|
+
'deploy',
|
|
150
|
+
'migrations describe',
|
|
151
|
+
'migrations diff',
|
|
152
|
+
'migrations execute',
|
|
153
|
+
'tenant apply',
|
|
154
|
+
'version',
|
|
155
|
+
'workspace update api',
|
|
156
|
+
])
|
|
157
|
+
})
|
|
158
|
+
|
|
159
|
+
test('accepts aliases of the same factory that normalize to the canonical name', () => {
|
|
160
|
+
expect(() => createManager([['foo bar', 'foo:bar']])).not.toThrow()
|
|
161
|
+
})
|
|
162
|
+
|
|
163
|
+
test('rejects different factories whose names normalize to the same token sequence', () => {
|
|
164
|
+
expect(() => createManager([['foo bar'], ['foo:bar']])).toThrow(InvalidConfigurationError)
|
|
165
|
+
})
|
|
166
|
+
})
|
|
167
|
+
|
|
168
|
+
describe('shorthand', () => {
|
|
169
|
+
test('resolves an abbreviated colon form', () => {
|
|
170
|
+
expect(resolveCommand(defaultManager(), ['m:di'])).toStrictEqual({ name: 'migrations diff', args: [] })
|
|
171
|
+
})
|
|
172
|
+
|
|
173
|
+
test('resolves migr:exe', () => {
|
|
174
|
+
expect(resolveCommand(defaultManager(), ['migr:exe'])).toStrictEqual({ name: 'migrations execute', args: [] })
|
|
175
|
+
})
|
|
176
|
+
|
|
177
|
+
test('resolves an abbreviated space form', () => {
|
|
178
|
+
expect(resolveCommand(defaultManager(), ['migr', 'exe'])).toStrictEqual({ name: 'migrations execute', args: [] })
|
|
179
|
+
})
|
|
180
|
+
|
|
181
|
+
test('resolves a partial prefix of a longer name', () => {
|
|
182
|
+
expect(resolveCommand(defaultManager(), ['w:u'])).toStrictEqual({ name: 'workspace update api', args: [] })
|
|
183
|
+
})
|
|
184
|
+
|
|
185
|
+
test('keeps the remaining tokens as arguments', () => {
|
|
186
|
+
expect(resolveCommand(defaultManager(), ['migr:diff', 'my-migration'])).toStrictEqual({ name: 'migrations diff', args: ['my-migration'] })
|
|
187
|
+
})
|
|
188
|
+
|
|
189
|
+
test('an ambiguous shorthand is reported', () => {
|
|
190
|
+
try {
|
|
191
|
+
defaultManager().resolve(['migrations:d'])
|
|
192
|
+
throw new Error('should have thrown')
|
|
193
|
+
} catch (e) {
|
|
194
|
+
expect(e).toBeInstanceOf(CliError)
|
|
195
|
+
if (e instanceof CliError) {
|
|
196
|
+
expect(e.code).toBe('COMMAND_AMBIGUOUS')
|
|
197
|
+
expect(e.exitCode).toBe(ExitCode.InputError)
|
|
198
|
+
expect(e.message).toContain('migrations diff')
|
|
199
|
+
expect(e.message).toContain('migrations describe')
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
})
|
|
203
|
+
})
|
|
204
|
+
|
|
205
|
+
describe('groups', () => {
|
|
206
|
+
test('a bare group token resolves to a group', () => {
|
|
207
|
+
const resolution = defaultManager().resolve(['migrations'])
|
|
208
|
+
expect(resolution.type).toBe('group')
|
|
209
|
+
if (resolution.type === 'group') {
|
|
210
|
+
expect(resolution.name).toBe('migrations')
|
|
211
|
+
expect(resolution.args).toStrictEqual([])
|
|
212
|
+
expect(resolution.entries.map(it => it.name).sort()).toStrictEqual(['migrations describe', 'migrations diff', 'migrations execute'])
|
|
213
|
+
}
|
|
214
|
+
})
|
|
215
|
+
|
|
216
|
+
test('an intermediate group token resolves to a group', () => {
|
|
217
|
+
const resolution = defaultManager().resolve(['workspace', 'update'])
|
|
218
|
+
expect(resolution.type).toBe('group')
|
|
219
|
+
if (resolution.type === 'group') {
|
|
220
|
+
expect(resolution.entries.map(it => it.name)).toStrictEqual(['workspace update api'])
|
|
221
|
+
}
|
|
222
|
+
})
|
|
223
|
+
|
|
224
|
+
test('a group followed by an option still resolves to a group', () => {
|
|
225
|
+
const resolution = defaultManager().resolve(['tenant', '--help'])
|
|
226
|
+
expect(resolution.type).toBe('group')
|
|
227
|
+
if (resolution.type === 'group') {
|
|
228
|
+
expect(resolution.args).toStrictEqual(['--help'])
|
|
229
|
+
}
|
|
230
|
+
})
|
|
231
|
+
|
|
232
|
+
test('an intermediate group retains its trailing argv', () => {
|
|
233
|
+
const resolution = defaultManager().resolve(['workspace', 'update', '--execute'])
|
|
234
|
+
expect(resolution.type).toBe('group')
|
|
235
|
+
if (resolution.type === 'group') {
|
|
236
|
+
expect(resolution.name).toBe('workspace update')
|
|
237
|
+
expect(resolution.args).toStrictEqual(['--execute'])
|
|
238
|
+
}
|
|
239
|
+
})
|
|
240
|
+
})
|
|
241
|
+
|
|
242
|
+
describe('prefix collision invariant', () => {
|
|
243
|
+
test('rejects a command name that is a prefix of another one', () => {
|
|
244
|
+
expect(() => createManager([['migrations'], ['migrations diff']])).toThrow(InvalidConfigurationError)
|
|
245
|
+
})
|
|
246
|
+
|
|
247
|
+
test('rejects a colon alias that is a prefix of another command', () => {
|
|
248
|
+
expect(() => createManager([['tenant apply'], ['other', 'tenant']])).toThrow(InvalidConfigurationError)
|
|
249
|
+
})
|
|
250
|
+
|
|
251
|
+
test('accepts names that only share a prefix', () => {
|
|
252
|
+
expect(() => createManager([['migrations diff'], ['migrations describe']])).not.toThrow()
|
|
253
|
+
})
|
|
254
|
+
|
|
255
|
+
test('accepts a name that is a substring but not a token prefix', () => {
|
|
256
|
+
expect(() => createManager([['deploy'], ['deploy-admin']])).not.toThrow()
|
|
257
|
+
})
|
|
258
|
+
})
|
|
259
|
+
|
|
260
|
+
describe('createCommand', () => {
|
|
261
|
+
test('returns the canonical name', () => {
|
|
262
|
+
const [name, command] = defaultManager().createCommand('migrations:diff')
|
|
263
|
+
expect(name).toBe('migrations diff')
|
|
264
|
+
expect(command).toBeInstanceOf(NoopCommand)
|
|
265
|
+
})
|
|
266
|
+
|
|
267
|
+
test('throws on a group', () => {
|
|
268
|
+
expect(() => defaultManager().createCommand('migrations')).toThrow(CliError)
|
|
269
|
+
})
|
|
270
|
+
})
|
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
import { describe, expect, test } from 'bun:test'
|
|
2
|
+
import { Readable, Writable } from 'node:stream'
|
|
3
|
+
import { ChildProcessError, CommandRunner } from '../../../src/npm/CommandRunner.js'
|
|
4
|
+
import { createTestOutput } from '../../lib/testOutput.js'
|
|
5
|
+
|
|
6
|
+
const childScript = `
|
|
7
|
+
process.stdout.write('child stdout\\n')
|
|
8
|
+
process.stderr.write('child stderr\\n')
|
|
9
|
+
`
|
|
10
|
+
|
|
11
|
+
describe('CommandRunner', () => {
|
|
12
|
+
test('streams child output as human diagnostics while returning stdout', async () => {
|
|
13
|
+
const io = createTestOutput()
|
|
14
|
+
const runner = new CommandRunner(io.output)
|
|
15
|
+
|
|
16
|
+
const { output } = runner.runCommand(process.execPath, ['-e', childScript], {
|
|
17
|
+
cwd: process.cwd(),
|
|
18
|
+
display: 'runtime child task',
|
|
19
|
+
})
|
|
20
|
+
|
|
21
|
+
expect(await output).toBe('child stdout\n')
|
|
22
|
+
expect(io.stdout.text).toBe('')
|
|
23
|
+
expect(io.stderr.lines).toContain('$ runtime child task')
|
|
24
|
+
expect(io.stderr.lines).toContain('child stdout')
|
|
25
|
+
expect(io.stderr.lines).toContain('child stderr')
|
|
26
|
+
})
|
|
27
|
+
|
|
28
|
+
test('keeps the zero-argument constructor, legacy error constructor and human stream forwarding', async () => {
|
|
29
|
+
const stdout: string[] = []
|
|
30
|
+
const stderr: string[] = []
|
|
31
|
+
const stdoutStream = collectingWritable(stdout)
|
|
32
|
+
const stderrStream = collectingWritable(stderr)
|
|
33
|
+
const runner = new CommandRunner()
|
|
34
|
+
|
|
35
|
+
const { output } = runner.runCommand(process.execPath, ['-e', childScript], {
|
|
36
|
+
cwd: process.cwd(),
|
|
37
|
+
stdout: stdoutStream,
|
|
38
|
+
stderr: stderrStream,
|
|
39
|
+
})
|
|
40
|
+
|
|
41
|
+
expect(await output).toBe('child stdout\n')
|
|
42
|
+
expect(stdout.join('')).toBe('child stdout\n')
|
|
43
|
+
expect(stderr.join('')).toBe('child stderr\n')
|
|
44
|
+
const legacyError = new ChildProcessError(9, 'legacy stderr')
|
|
45
|
+
expect(legacyError.stdout).toBe('')
|
|
46
|
+
expect(legacyError.stderr).toBe('legacy stderr')
|
|
47
|
+
})
|
|
48
|
+
|
|
49
|
+
for (const mode of ['json', 'quiet'] as const) {
|
|
50
|
+
test(`suppresses command and child diagnostics in ${mode} mode`, async () => {
|
|
51
|
+
const io = createTestOutput()
|
|
52
|
+
io.output.setMode(mode)
|
|
53
|
+
const runner = new CommandRunner(io.output)
|
|
54
|
+
|
|
55
|
+
const forwarded: string[] = []
|
|
56
|
+
const { output } = runner.runCommand(process.execPath, ['-e', childScript], {
|
|
57
|
+
cwd: process.cwd(),
|
|
58
|
+
stdout: collectingWritable(forwarded),
|
|
59
|
+
stderr: collectingWritable(forwarded),
|
|
60
|
+
display: 'safe child task',
|
|
61
|
+
})
|
|
62
|
+
|
|
63
|
+
expect(await output).toBe('child stdout\n')
|
|
64
|
+
expect(forwarded).toEqual([])
|
|
65
|
+
expect(io.stdout.text).toBe('')
|
|
66
|
+
expect(io.stderr.text).toBe('')
|
|
67
|
+
})
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
test('retains captured output on a non-zero child failure without printing in JSON mode', async () => {
|
|
71
|
+
const io = createTestOutput()
|
|
72
|
+
io.output.setMode('json')
|
|
73
|
+
const runner = new CommandRunner(io.output)
|
|
74
|
+
const script = `${childScript}\nprocess.exitCode = 7`
|
|
75
|
+
|
|
76
|
+
const { output } = runner.runCommand(process.execPath, ['-e', script], { cwd: process.cwd() })
|
|
77
|
+
const error = await output.then(() => null, (reason: unknown) => reason)
|
|
78
|
+
|
|
79
|
+
if (!(error instanceof ChildProcessError)) {
|
|
80
|
+
throw new Error('Expected ChildProcessError')
|
|
81
|
+
}
|
|
82
|
+
expect(error.exitCode).toBe(7)
|
|
83
|
+
expect(error.stdout).toBe('child stdout\n')
|
|
84
|
+
expect(error.stderr).toBe('child stderr\n')
|
|
85
|
+
expect(error.message).not.toContain('child stderr')
|
|
86
|
+
expect(io.stdout.text).toBe('')
|
|
87
|
+
expect(io.stderr.text).toBe('')
|
|
88
|
+
})
|
|
89
|
+
|
|
90
|
+
test('does not expose raw arguments in diagnostics or failure messages', async () => {
|
|
91
|
+
const secret = 'sentinel-secret-do-not-print'
|
|
92
|
+
const io = createTestOutput()
|
|
93
|
+
const runner = new CommandRunner(io.output)
|
|
94
|
+
const script = `process.stderr.write('dependency install failed\\n'); process.exitCode = 8`
|
|
95
|
+
|
|
96
|
+
const { output } = runner.runCommand(process.execPath, ['-e', script, secret], {
|
|
97
|
+
cwd: process.cwd(),
|
|
98
|
+
display: 'install dependencies',
|
|
99
|
+
})
|
|
100
|
+
const error = await output.then(() => null, (reason: unknown) => reason)
|
|
101
|
+
|
|
102
|
+
if (!(error instanceof ChildProcessError)) {
|
|
103
|
+
throw new Error('Expected ChildProcessError')
|
|
104
|
+
}
|
|
105
|
+
expect([io.stdout.text, io.stderr.text, error.message, error.stdout, error.stderr].join('\n')).not.toContain(secret)
|
|
106
|
+
expect(io.stderr.lines[0]).toBe('$ install dependencies')
|
|
107
|
+
})
|
|
108
|
+
|
|
109
|
+
test('does not hang when a child closes before consuming a large stdin stream', async () => {
|
|
110
|
+
const io = createTestOutput()
|
|
111
|
+
const runner = new CommandRunner(io.output)
|
|
112
|
+
const stdin = Readable.from(largeInput())
|
|
113
|
+
|
|
114
|
+
const { output } = runner.runCommand(process.execPath, ['-e', 'process.exit(0)'], { cwd: process.cwd(), stdin })
|
|
115
|
+
|
|
116
|
+
expect(await output).toBe('')
|
|
117
|
+
})
|
|
118
|
+
|
|
119
|
+
test('preserves source stdin failures', async () => {
|
|
120
|
+
const io = createTestOutput()
|
|
121
|
+
io.output.setMode('json')
|
|
122
|
+
const runner = new CommandRunner(io.output)
|
|
123
|
+
const sourceError = Object.assign(new Error('stdin source failed'), { code: 'EIO' })
|
|
124
|
+
const stdin = new Readable({
|
|
125
|
+
read() {
|
|
126
|
+
this.destroy(sourceError)
|
|
127
|
+
},
|
|
128
|
+
})
|
|
129
|
+
|
|
130
|
+
const { output } = runner.runCommand(process.execPath, ['-e', 'process.stdin.resume(); setTimeout(() => {}, 10000)'], {
|
|
131
|
+
cwd: process.cwd(),
|
|
132
|
+
stdin,
|
|
133
|
+
})
|
|
134
|
+
|
|
135
|
+
await expect(output).rejects.toBe(sourceError)
|
|
136
|
+
expect(io.stdout.text).toBe('')
|
|
137
|
+
expect(io.stderr.text).toBe('')
|
|
138
|
+
})
|
|
139
|
+
|
|
140
|
+
test('returns a typed failure when the child cannot start', async () => {
|
|
141
|
+
const io = createTestOutput()
|
|
142
|
+
io.output.setMode('quiet')
|
|
143
|
+
const runner = new CommandRunner(io.output)
|
|
144
|
+
|
|
145
|
+
const { output } = runner.runCommand('contember-command-that-does-not-exist', [], { cwd: process.cwd() })
|
|
146
|
+
const error = await output.then(() => null, (reason: unknown) => reason)
|
|
147
|
+
|
|
148
|
+
if (!(error instanceof ChildProcessError)) {
|
|
149
|
+
throw new Error('Expected ChildProcessError')
|
|
150
|
+
}
|
|
151
|
+
expect(error.exitCode).toBeNull()
|
|
152
|
+
expect(error.stdout).toBe('')
|
|
153
|
+
expect(error.stderr).toBe('')
|
|
154
|
+
expect(error.message).toBe('Command could not be started.')
|
|
155
|
+
expect(error.cause).toBeInstanceOf(Error)
|
|
156
|
+
expect(error.cause?.message).toContain('contember-command-that-does-not-exist')
|
|
157
|
+
expect(error.signal).toBeNull()
|
|
158
|
+
expect(io.stdout.text).toBe('')
|
|
159
|
+
expect(io.stderr.text).toBe('')
|
|
160
|
+
})
|
|
161
|
+
|
|
162
|
+
test('distinguishes signal termination from spawn and exit-code failures', async () => {
|
|
163
|
+
const io = createTestOutput()
|
|
164
|
+
io.output.setMode('quiet')
|
|
165
|
+
const runner = new CommandRunner(io.output)
|
|
166
|
+
|
|
167
|
+
const { output } = runner.runCommand(process.execPath, ['-e', `process.kill(process.pid, 'SIGTERM')`], { cwd: process.cwd() })
|
|
168
|
+
const error = await output.then(() => null, (reason: unknown) => reason)
|
|
169
|
+
|
|
170
|
+
if (!(error instanceof ChildProcessError)) {
|
|
171
|
+
throw new Error('Expected ChildProcessError')
|
|
172
|
+
}
|
|
173
|
+
expect(error.exitCode).toBeNull()
|
|
174
|
+
expect(error.signal).toBe('SIGTERM')
|
|
175
|
+
expect(error.cause).toBeUndefined()
|
|
176
|
+
expect(error.message).toBe('Command terminated by signal SIGTERM.')
|
|
177
|
+
})
|
|
178
|
+
})
|
|
179
|
+
|
|
180
|
+
const collectingWritable = (chunks: string[]): Writable =>
|
|
181
|
+
new Writable({
|
|
182
|
+
write(chunk: Buffer, _encoding, callback) {
|
|
183
|
+
chunks.push(chunk.toString())
|
|
184
|
+
callback()
|
|
185
|
+
},
|
|
186
|
+
})
|
|
187
|
+
|
|
188
|
+
function* largeInput(): Generator<Buffer> {
|
|
189
|
+
for (let index = 0; index < 1024; index++) {
|
|
190
|
+
yield Buffer.alloc(64 * 1024, 'x')
|
|
191
|
+
}
|
|
192
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { describe, expect, test } from 'bun:test'
|
|
2
|
+
import { InputParser } from '../../../src/application/InputParser.js'
|
|
3
|
+
import { OptionMode } from '../../../src/index.js'
|
|
4
|
+
|
|
5
|
+
describe('inline option values', () => {
|
|
6
|
+
test('preserves separators after the first equals sign', () => {
|
|
7
|
+
const parser = new InputParser([], [{ name: 'value', required: false, deprecated: false, mode: OptionMode.VALUE_REQUIRED }])
|
|
8
|
+
|
|
9
|
+
const parsed = parser.parse<{}, { value?: string }>(['--value=left=middle=right'])
|
|
10
|
+
|
|
11
|
+
expect(parsed.input.getOption('value')).toBe('left=middle=right')
|
|
12
|
+
})
|
|
13
|
+
|
|
14
|
+
test('preserves an empty inline value', () => {
|
|
15
|
+
const parser = new InputParser([], [{ name: 'value', required: false, deprecated: false, mode: OptionMode.VALUE_REQUIRED }])
|
|
16
|
+
|
|
17
|
+
const parsed = parser.parse<{}, { value?: string }>(['--value='])
|
|
18
|
+
|
|
19
|
+
expect(parsed.input.getOption('value')).toBe('')
|
|
20
|
+
})
|
|
21
|
+
})
|