-
-
Notifications
You must be signed in to change notification settings - Fork 433
Expand file tree
/
Copy pathrun-command.spec.ts
More file actions
244 lines (219 loc) · 7.25 KB
/
run-command.spec.ts
File metadata and controls
244 lines (219 loc) · 7.25 KB
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
import { describe, expect, it } from 'vitest'
import { getExecutableInfo, getRunCommand, getRunCommandParts } from '~/utils/run-command'
import { isBinaryOnlyPackage, isCreatePackage } from '#shared/utils/binary-detection'
import type { JsrPackageInfo } from '#shared/types/jsr'
describe('executable detection and run commands', () => {
const jsrNotAvailable: JsrPackageInfo = { exists: false }
describe('getExecutableInfo', () => {
it('returns hasExecutable: false for undefined bin', () => {
const info = getExecutableInfo('some-package', undefined)
expect(info).toEqual({
primaryCommand: '',
commands: [],
hasExecutable: false,
})
})
it('handles string bin format (package name becomes command)', () => {
const info = getExecutableInfo('eslint', './bin/eslint.js')
expect(info).toEqual({
primaryCommand: 'eslint',
commands: ['eslint'],
hasExecutable: true,
})
})
it('handles object bin format with single command', () => {
const info = getExecutableInfo('cowsay', { cowsay: './index.js' })
expect(info).toEqual({
primaryCommand: 'cowsay',
commands: ['cowsay'],
hasExecutable: true,
})
})
it('handles object bin format with multiple commands', () => {
const info = getExecutableInfo('typescript', {
tsc: './bin/tsc',
tsserver: './bin/tsserver',
})
expect(info).toEqual({
primaryCommand: 'tsc',
commands: ['tsc', 'tsserver'],
hasExecutable: true,
})
})
it('prefers command matching package name as primary', () => {
const info = getExecutableInfo('eslint', {
'eslint-cli': './cli.js',
'eslint': './index.js',
})
expect(info.primaryCommand).toBe('eslint')
})
it('prefers command matching base name for scoped packages', () => {
const info = getExecutableInfo('@scope/myapp', {
'myapp': './index.js',
'myapp-extra': './extra.js',
})
expect(info.primaryCommand).toBe('myapp')
})
it('returns empty for empty bin object', () => {
const info = getExecutableInfo('some-package', {})
expect(info).toEqual({
primaryCommand: '',
commands: [],
hasExecutable: false,
})
})
})
describe('getRunCommandParts', () => {
// Default behavior uses local execute (for installed packages)
it.each([
['npm', ['npx', 'eslint']],
['pnpm', ['pnpm', 'exec', 'eslint']],
['yarn', ['npx', 'eslint']],
['bun', ['bunx', 'eslint']],
['deno', ['deno', 'run', 'npm:eslint']],
['vlt', ['vlx', 'eslint']],
] as const)('%s (local) → %s', (pm, expected) => {
expect(
getRunCommandParts({
packageName: 'eslint',
packageManager: pm,
jsrInfo: jsrNotAvailable,
}),
).toEqual(expected)
})
// Binary-only packages use remote execute (download & run)
it.each([
['npm', ['npx', 'create-vite']],
['pnpm', ['pnpm', 'dlx', 'create-vite']],
['yarn', ['yarn', 'dlx', 'create-vite']],
['bun', ['bunx', 'create-vite']],
['deno', ['deno', 'run', 'npm:create-vite']],
['vlt', ['vlx', 'create-vite']],
] as const)('%s (remote) → %s', (pm, expected) => {
expect(
getRunCommandParts({
packageName: 'create-vite',
packageManager: pm,
jsrInfo: jsrNotAvailable,
isBinaryOnly: true,
}),
).toEqual(expected)
})
it('uses command name directly for multi-bin packages', () => {
const parts = getRunCommandParts({
packageName: 'typescript',
packageManager: 'npm',
command: 'tsserver',
jsrInfo: jsrNotAvailable,
})
// npx tsserver runs the tsserver command (not npx typescript/tsserver)
expect(parts).toEqual(['npx', 'tsserver'])
})
it('uses base name directly when command matches package base name', () => {
const parts = getRunCommandParts({
packageName: '@scope/myapp',
packageManager: 'npm',
command: 'myapp',
jsrInfo: jsrNotAvailable,
})
expect(parts).toEqual(['npx', '@scope/myapp'])
})
it('returns empty array for invalid package manager', () => {
const parts = getRunCommandParts({
packageName: 'eslint',
packageManager: 'invalid' as any,
jsrInfo: jsrNotAvailable,
})
expect(parts).toEqual([])
})
})
describe('getRunCommand', () => {
it('generates full run command string', () => {
expect(
getRunCommand({
packageName: 'eslint',
packageManager: 'npm',
jsrInfo: jsrNotAvailable,
}),
).toBe('npx eslint')
})
it('generates correct bun run command with specific command', () => {
expect(
getRunCommand({
packageName: 'typescript',
packageManager: 'bun',
command: 'tsserver',
jsrInfo: jsrNotAvailable,
}),
).toBe('bunx tsserver')
})
it('joined parts match getRunCommand output', () => {
const options = {
packageName: 'eslint',
packageManager: 'pnpm' as const,
jsrInfo: jsrNotAvailable,
}
const parts = getRunCommandParts(options)
const command = getRunCommand(options)
expect(parts.join(' ')).toBe(command)
})
})
describe('isBinaryOnlyPackage', () => {
it('returns true for create-* packages', () => {
expect(isBinaryOnlyPackage({ name: 'create-vite' })).toBe(true)
expect(isBinaryOnlyPackage({ name: 'create-next-app' })).toBe(true)
})
it('returns true for scoped create packages', () => {
expect(isBinaryOnlyPackage({ name: '@vue/create-app' })).toBe(true)
expect(isBinaryOnlyPackage({ name: '@scope/create-something' })).toBe(true)
})
it('returns true for packages with bin but no entry points', () => {
expect(
isBinaryOnlyPackage({
name: 'degit',
bin: { degit: './bin.js' },
}),
).toBe(true)
})
it('returns false for packages with bin AND entry points', () => {
expect(
isBinaryOnlyPackage({
name: 'eslint',
bin: { eslint: './bin/eslint.js' },
main: './lib/api.js',
}),
).toBe(false)
})
it('returns false for packages with exports', () => {
expect(
isBinaryOnlyPackage({
name: 'some-package',
bin: { cmd: './bin.js' },
exports: { '.': './index.js' },
}),
).toBe(false)
})
it('returns false for packages without bin', () => {
expect(
isBinaryOnlyPackage({
name: 'lodash',
main: './lodash.js',
}),
).toBe(false)
})
})
describe('isCreatePackage', () => {
it('returns true for create-* packages', () => {
expect(isCreatePackage('create-vite')).toBe(true)
expect(isCreatePackage('create-next-app')).toBe(true)
})
it('returns true for scoped create packages', () => {
expect(isCreatePackage('@vue/create-app')).toBe(true)
})
it('returns false for regular packages', () => {
expect(isCreatePackage('eslint')).toBe(false)
expect(isCreatePackage('lodash')).toBe(false)
expect(isCreatePackage('@scope/utils')).toBe(false)
})
})
})