-
Notifications
You must be signed in to change notification settings - Fork 11
/
run-command.test.ts
151 lines (136 loc) · 4.86 KB
/
run-command.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import {Config} from '@oclif/core'
import {expect} from 'chai'
import {join} from 'node:path'
import {runCommand} from '../src'
describe('runCommand', () => {
// eslint-disable-next-line unicorn/prefer-module
const root = join(__dirname, 'fixtures/multi')
it('should run a command', async () => {
const {result, stdout} = await runCommand<{name: string}>(['foo:bar'], {root})
expect(stdout).to.equal('hello world!\n')
expect(result?.name).to.equal('world')
})
it('should run a command with a flag', async () => {
const {result, stdout} = await runCommand<{name: string}>(['foo:bar', '--name=foo'], {root})
expect(stdout).to.equal('hello foo!\n')
expect(result?.name).to.equal('foo')
})
it('should run a command using spaces', async () => {
const {result, stdout} = await runCommand<{name: string}>(['foo bar', '--name=foo'], {root})
expect(stdout).to.equal('hello foo!\n')
expect(result?.name).to.equal('foo')
})
it('should handle expected exit codes', async () => {
const {error, stdout} = await runCommand(['exit', '--code=101'], {root})
expect(stdout).to.equal('exiting with code 101\n')
expect(error?.message).to.equal('EEXIT: 101')
expect(error?.oclif?.exit).to.equal(101)
})
it('should take existing Config instance', async () => {
const config = await Config.load(root)
const {result, stdout} = await runCommand<{name: string}>(['foo:bar'], config)
expect(stdout).to.equal('hello world!\n')
expect(result?.name).to.equal('world')
})
it('should find root dynamically if not provided', async () => {
const {stdout} = await runCommand(['--help'])
expect(stdout).to.include('$ @oclif/test [COMMAND]')
})
describe('single command cli', () => {
// eslint-disable-next-line unicorn/prefer-module
const root = join(__dirname, 'fixtures/single')
it('should run a single command cli', async () => {
const {result, stdout} = await runCommand<{name: string}>(['.'], {root})
expect(stdout).to.equal('hello world!\n')
expect(result?.name).to.equal('world')
})
})
const cases = [
{
description: 'should handle single string',
expected: 'foo',
input: 'foo%sbar --name foo',
},
{
description: 'should handle an array of strings',
expected: 'foo',
input: ['foo%sbar', '--name', 'foo'],
},
{
description: 'should handle a string with =',
expected: 'foo',
input: 'foo%sbar --name=foo',
},
{
description: 'should handle an array of strings with =',
expected: 'foo',
input: ['foo%sbar', '--name=foo'],
},
{
description: 'should handle a string with quotes',
expected: 'foo',
input: 'foo%sbar --name "foo"',
},
{
description: 'should handle an array of strings with quotes',
expected: 'foo',
input: ['foo%sbar', '--name', '"foo"'],
},
{
description: 'should handle a string with quotes and with =',
expected: 'foo',
input: 'foo%sbar --name="foo"',
},
{
description: 'should handle an array of strings with quotes and with =',
expected: 'foo',
input: ['foo%sbar', '--name="foo"'],
},
{
description: 'should handle a string with spaces in quotes',
expected: 'foo bar',
input: 'foo%sbar --name "foo bar"',
},
{
description: 'should handle an array of strings with spaces in quotes',
expected: 'foo bar',
input: ['foo%sbar', '--name', '"foo bar"'],
},
{
description: 'should handle a string with spaces in quotes and with =',
expected: 'foo bar',
input: 'foo%sbar --name="foo bar"',
},
{
description: 'should handle an array of strings with spaces in quotes and with =',
expected: 'foo bar',
input: ['foo%sbar', '--name="foo bar"'],
},
]
const makeTestCases = (separator: string) =>
cases.map(({description, expected, input}) => ({
description: description.replace('%s', separator),
expected,
input: Array.isArray(input) ? input.map((i) => i.replace('%s', separator)) : input.replace('%s', separator),
}))
describe('arg input (colon separator)', () => {
const testCases = makeTestCases(':')
for (const {description, expected, input} of testCases) {
it(description, async () => {
const {result, stdout} = await runCommand<{name: string}>(input, {root})
expect(stdout).to.equal(`hello ${expected}!\n`)
expect(result?.name).to.equal(expected)
})
}
})
describe('arg input (space separator)', () => {
const testCases = makeTestCases(' ')
for (const {description, expected, input} of testCases) {
it(description, async () => {
const {result, stdout} = await runCommand<{name: string}>(input, {root})
expect(stdout).to.equal(`hello ${expected}!\n`)
expect(result?.name).to.equal(expected)
})
}
})
})