Repository navigation
Expand file tree
/
Copy pathinstall.test.ts
More file actions
227 lines (207 loc) · 7.91 KB
/
Copy pathinstall.test.ts
File metadata and controls
227 lines (207 loc) · 7.91 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
import { execFileSync, spawnSync } from 'node:child_process'
import {
chmodSync,
existsSync,
mkdirSync,
mkdtempSync,
readFileSync,
rmSync,
writeFileSync,
} from 'node:fs'
import { tmpdir } from 'node:os'
import { delimiter, dirname, join } from 'node:path'
import { fileURLToPath } from 'node:url'
import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest'
import { CliUpdateError, installUpdate } from '#sim-cli/update/install'
let directory: string
let packageRoot: string
let modulePath: string
let output: string[]
function writeVersion(version: string, root = packageRoot): void {
writeFileSync(join(root, 'package.json'), JSON.stringify({ name: 'sim', version }))
}
beforeEach(() => {
directory = mkdtempSync(join(tmpdir(), 'sim-cli-install-update-'))
packageRoot = join(directory, 'node_modules/sim')
modulePath = join(packageRoot, 'dist/index.js')
mkdirSync(dirname(modulePath), { recursive: true })
writeFileSync(modulePath, '')
writeVersion('2.1.2')
output = []
})
afterEach(() => {
rmSync(directory, { recursive: true, force: true })
})
function options() {
return {
modulePath,
env: {},
currentVersion: '2.1.2',
write: (message: string) => output.push(message),
}
}
describe('installing a CLI update', () => {
it('does not pass the Sim API key to the package manager or change the parent environment', async () => {
const env = { SIM_API_KEY: 'private', npm_config_registry: 'https://registry.example' }
const run = vi
.fn()
.mockResolvedValueOnce(join(directory, 'node_modules'))
.mockResolvedValueOnce(JSON.stringify('2.1.2'))
await installUpdate({ ...options(), env, run })
expect(run.mock.calls[1][2].env).toEqual({
npm_config_registry: 'https://registry.example',
SIM_NO_UPDATE_CHECK: '1',
})
expect(env.SIM_API_KEY).toBe('private')
})
it.each([
['2.1.5', '2.1.2'],
['2.1.10', '2.1.9'],
['3.0.0', '2.99.99'],
['2.1.5-preview.10.1', '2.1.5-preview.9.9'],
['2.1.5-dev.10.2', '2.1.5-dev.10.1'],
])('refuses to downgrade %s to %s before installation', async (currentVersion, candidate) => {
writeVersion(currentVersion)
const run = vi
.fn()
.mockResolvedValueOnce(join(directory, 'node_modules'))
.mockResolvedValueOnce(JSON.stringify(candidate))
await expect(installUpdate({ ...options(), currentVersion, run })).rejects.toThrow(
'Refusing to downgrade'
)
expect(run).toHaveBeenCalledTimes(2)
expect(readFileSync(join(packageRoot, 'package.json'), 'utf8')).toContain(currentVersion)
expect(existsSync(`${packageRoot}.lock`)).toBe(false)
})
it('refuses to install if the current installation changed while locating it', async () => {
const run = vi.fn().mockImplementationOnce(async () => {
writeVersion('2.1.6')
return join(directory, 'node_modules')
})
await expect(installUpdate({ ...options(), run })).rejects.toThrow('installation changed')
expect(run).toHaveBeenCalledTimes(1)
})
it('refuses to update another installation even when both versions match', async () => {
const other = join(directory, 'other/node_modules')
mkdirSync(join(other, 'sim/dist'), { recursive: true })
writeFileSync(join(other, 'sim/dist/index.js'), '')
const run = vi.fn().mockResolvedValue(other)
await expect(installUpdate({ ...options(), run })).rejects.toThrow('different Sim installation')
expect(run).toHaveBeenCalledTimes(1)
})
it('rejects executable text in the version before running anything', async () => {
const run = vi.fn()
await expect(
installUpdate({ ...options(), currentVersion: '2.1.5; echo unsafe', run })
).rejects.toThrow('release channel')
expect(run).not.toHaveBeenCalled()
})
it('propagates installer failure, releases the lock, and never reports success', async () => {
const run = vi
.fn()
.mockResolvedValueOnce(join(directory, 'node_modules'))
.mockResolvedValueOnce(JSON.stringify('2.1.5'))
.mockRejectedValueOnce(new Error('permission denied'))
await expect(installUpdate({ ...options(), run })).rejects.toThrow('permission denied')
expect(existsSync(`${packageRoot}.lock`)).toBe(false)
expect(output.join('')).not.toContain('Updated Sim')
})
it('refuses concurrent updates before a second installer starts', async () => {
mkdirSync(`${packageRoot}.lock`)
const run = vi.fn().mockResolvedValue(join(directory, 'node_modules'))
await expect(installUpdate({ ...options(), run })).rejects.toMatchObject({
constructor: CliUpdateError,
message: expect.stringContaining('already being held'),
})
expect(run).toHaveBeenCalledTimes(1)
})
})
describe.skipIf(process.platform === 'win32')('the bundled sim update command', () => {
let directory: string
let entrypoint: string
let manifest: string
let bin: string
let modules: string
beforeAll(() => {
directory = mkdtempSync(join(tmpdir(), 'sim-cli-update-process-'))
modules = join(directory, 'node_modules')
entrypoint = join(modules, 'sim/dist/index.js')
manifest = join(modules, 'sim/package.json')
bin = join(directory, 'bin')
mkdirSync(bin)
execFileSync('bun', [
'build',
fileURLToPath(new URL('../index.ts', import.meta.url)),
'--target=node',
'--format=esm',
'--packages=bundle',
'--outfile',
entrypoint,
])
})
beforeEach(() => {
writeFileSync(manifest, JSON.stringify({ name: 'sim', type: 'module', version: '2.1.2' }))
})
afterAll(() => {
rmSync(directory, { recursive: true, force: true })
})
function fakePackageManager(
exitCode = 0,
version = '2.1.5',
manifestBody?: string,
globalDirectory = modules
): void {
const script = `
if (process.env.SIM_API_KEY) throw new Error('Sim API key leaked to package manager')
const args = process.argv.slice(2)
if (args.join(' ') === 'root -g') {
process.stdout.write(${JSON.stringify(globalDirectory)})
} else if (args.join(' ') === 'view sim@latest version --json') {
process.stdout.write(JSON.stringify(${JSON.stringify(version)}))
} else if (args.join(' ') === 'install -g sim@' + ${JSON.stringify(version)}) {
process.stdout.write('package manager stdout\\n')
process.stderr.write('package manager stderr\\n')
if (${exitCode} !== 0) process.exit(${exitCode})
require('node:fs').writeFileSync(${JSON.stringify(manifest)}, ${JSON.stringify(manifestBody ?? JSON.stringify({ name: 'sim', type: 'module', version }))})
} else {
throw new Error('Unexpected arguments: ' + args.join(' '))
}
`
const executable = join(bin, 'npm')
writeFileSync(executable, `#!/usr/bin/env node\n${script}`)
chmodSync(executable, 0o755)
}
function run(args: string[]) {
return spawnSync(process.execPath, [entrypoint, ...args], {
encoding: 'utf8',
timeout: 15_000,
env: {
...process.env,
PATH: `${bin}${delimiter}${process.env.PATH}`,
SIM_CONFIG_DIR: join(directory, 'no-profile'),
SIM_API_KEY: 'must-not-reach-installer',
SIM_NO_UPDATE_CHECK: '1',
npm_command: '',
npm_config_user_agent: '',
},
})
}
it('exits unsuccessfully on an installer failure without printing success', () => {
fakePackageManager(17)
const result = run(['update'])
expect(result.status).toBe(1)
expect(result.stdout).toBe('')
expect(result.stderr).toContain('exit 17')
expect(result.stderr).not.toContain('Updated Sim')
expect(run(['--version']).stdout.trim()).toBe('2.1.2')
})
it('refuses an older registry release without running the installer', () => {
fakePackageManager(0, '2.1.1')
const result = run(['update'])
expect(result.status).toBe(1)
expect(result.stderr).toContain('Refusing to downgrade')
expect(result.stderr).not.toContain('package manager stdout')
expect(result.stderr).not.toMatch(/\n\s+at /)
expect(run(['--version']).stdout.trim()).toBe('2.1.2')
})
})