-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathupdate.test.ts
More file actions
368 lines (316 loc) · 14.3 KB
/
update.test.ts
File metadata and controls
368 lines (316 loc) · 14.3 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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
import {Config, Interfaces, ux} from '@oclif/core'
import {expect} from 'chai'
import {got} from 'got'
import nock from 'nock'
import {existsSync} from 'node:fs'
import {mkdir, rm, symlink, writeFile} from 'node:fs/promises'
import path from 'node:path'
import zlib from 'node:zlib'
import sinon from 'sinon'
import stripAnsi from 'strip-ansi'
import {Extractor} from '../src/tar.js'
import {Updater} from '../src/update.js'
type OutputCollectors = {
stderr: string[]
stdout: string[]
}
async function loadConfig(options: {root: string}): Promise<Config> {
return Config.load(options.root)
}
const setupClientRoot = async (ctx: {config: Interfaces.Config}, createVersion?: string): Promise<string> => {
const clientRoot = ctx.config.scopedEnvVar('OCLIF_CLIENT_HOME') || path.join(ctx.config.dataDir, 'client')
// Ensure installed version structure is present
await mkdir(clientRoot, {recursive: true})
if (createVersion) {
await mkdir(path.join(clientRoot, 'bin'), {recursive: true})
if (!existsSync(path.join(clientRoot, '2.0.0'))) {
await symlink(path.join(clientRoot, '2.0.0'), path.join(clientRoot, 'current'))
}
await writeFile(path.join(clientRoot, 'bin', ctx.config.bin), '../2.0.0/bin', 'utf8')
}
return clientRoot
}
function initUpdater(config: Config): Updater {
const updater = new Updater(config)
expect(updater).to.be.ok
return updater
}
describe('update plugin', () => {
let config: Config
let updater: Updater
let collector: OutputCollectors
let clientRoot: string
beforeEach(async () => {
config = await loadConfig({root: path.join(process.cwd(), 'examples', 's3-update-example-cli')})
config.binPath = config.binPath || config.bin
collector = {stderr: [], stdout: []}
sinon.stub(ux, 'stdout').callsFake((lines) => {
const arr = Array.isArray(lines) ? lines : [lines ?? '']
collector.stdout.push(...arr)
})
sinon.stub(ux, 'warn').callsFake((line) => collector.stderr.push(line ? `${line}` : ''))
sinon.stub(ux.action, 'start').callsFake((line) => collector.stdout.push(line || ''))
sinon.stub(ux.action, 'stop').callsFake((line) => collector.stdout.push(line || ''))
// @ts-expect-error because private method
sinon.stub(Updater.prototype, 'refreshConfig').resolves()
})
afterEach(async () => {
nock.cleanAll()
if (existsSync(clientRoot)) {
await rm(clientRoot, {force: true, recursive: true})
}
sinon.restore()
})
it('should not update - already on same version', async () => {
clientRoot = await setupClientRoot({config}, '2.0.0')
const manifestRegex = new RegExp(`tarballs\\/example-cli\\/${config.platform}-${config.arch}`)
nock(/oclif-staging.s3.amazonaws.com/)
.get(manifestRegex)
.reply(200, {version: '2.0.0'})
updater = initUpdater(config)
await updater.runUpdate({autoUpdate: false})
const stdout = collector.stdout.join(' ')
expect(stdout).to.include('already on version 2.0.0')
})
it('should update to channel', async () => {
clientRoot = await setupClientRoot({config})
const manifestRegex = new RegExp(`tarballs\\/example-cli\\/${config.platform}-${config.arch}`)
const tarballRegex = new RegExp(
`tarballs\\/example-cli\\/example-cli-v2.0.1\\/example-cli-v2.0.1-${config.platform}-${config.arch}gz`,
)
const newVersionPath = path.join(clientRoot, '2.0.1')
await mkdir(path.join(`${newVersionPath}.partial.11111`, 'bin'), {recursive: true})
await writeFile(path.join(`${newVersionPath}.partial.11111`, 'bin', 'example-cli'), '../2.0.1/bin', 'utf8')
sinon.stub(Extractor, 'extract').resolves()
const gzContents = zlib.gzipSync(' ')
nock(/oclif-staging.s3.amazonaws.com/)
.get(manifestRegex)
.reply(200, {version: '2.0.1'})
.get(tarballRegex)
.reply(200, gzContents, {
'Content-Encoding': 'gzip',
'content-length': String(gzContents.length),
'X-Transfer-Length': String(gzContents.length),
})
updater = initUpdater(config)
await updater.runUpdate({autoUpdate: false})
const stdout = stripAnsi(collector.stdout.join(' '))
expect(stdout).to.matches(/Updating CLI from 2.0.0 to 2.0.1/)
})
it('should update to version', async () => {
const hash = 'f289627'
clientRoot = await setupClientRoot({config})
const platformRegex = new RegExp(`tarballs\\/example-cli\\/${config.platform}-${config.arch}`)
const manifestRegex = new RegExp(`channels\\/stable\\/example-cli-${config.platform}-${config.arch}-buildmanifest`)
const versionManifestRegex = new RegExp(
`example-cli-v2.0.1-${hash}-${config.platform}-${config.arch}-buildmanifest`,
)
const tarballRegex = new RegExp(
`tarballs\\/example-cli\\/example-cli-v2.0.1\\/example-cli-v2.0.1-${config.platform}-${config.arch}gz`,
)
const indexRegex = new RegExp(`example-cli-${config.platform}-${config.arch}-tar-gz.json`)
sinon.stub(Extractor, 'extract').resolves()
const gzContents = zlib.gzipSync(' ')
nock(/oclif-staging.s3.amazonaws.com/)
.get(platformRegex)
.reply(200, {version: '2.0.1'})
.get(manifestRegex)
.reply(200, {version: '2.0.1'})
.get(versionManifestRegex)
.reply(200, {version: '2.0.1'})
.get(tarballRegex)
.reply(200, gzContents, {
'Content-Encoding': 'gzip',
'content-length': String(gzContents.length),
'X-Transfer-Length': String(gzContents.length),
})
.get(indexRegex)
.reply(200, {
'2.0.1': `versions/example-cli/2.0.1/${hash}/example-cli-v2.0.1-${config.platform}-${config.arch}.gz`,
})
updater = initUpdater(config)
await updater.runUpdate({autoUpdate: false, version: '2.0.1'})
const stdout = stripAnsi(collector.stdout.join(' '))
expect(stdout).to.matches(/Updating CLI from 2.0.0 to 2.0.1/)
})
it('will get the correct channel and use default registry', async () => {
const request = sinon.spy(got, 'get')
const hash = 'f289627'
config.pjson.name = '@oclif/plugin-update'
clientRoot = await setupClientRoot({config})
const platformRegex = new RegExp(`tarballs\\/example-cli\\/${config.platform}-${config.arch}`)
const manifestRegex = new RegExp(`channels\\/stable\\/example-cli-${config.platform}-${config.arch}-buildmanifest`)
const versionManifestRegex = new RegExp(
`example-cli-v2.0.1-${hash}-${config.platform}-${config.arch}-buildmanifest`,
)
const tarballRegex = new RegExp(
`tarballs\\/example-cli\\/example-cli-v2.0.1\\/example-cli-v2.0.1-${config.platform}-${config.arch}gz`,
)
const indexRegex = new RegExp(`example-cli-${config.platform}-${config.arch}-tar-gz.json`)
sinon.stub(Extractor, 'extract').resolves()
const gzContents = zlib.gzipSync(' ')
nock(/oclif-staging.s3.amazonaws.com/)
.get(platformRegex)
.reply(200, {version: '2.0.1'})
.get(manifestRegex)
.reply(200, {version: '2.0.1'})
.get(versionManifestRegex)
.reply(200, {version: '2.0.1'})
.get(tarballRegex)
.reply(200, gzContents, {
'Content-Encoding': 'gzip',
'content-length': String(gzContents.length),
'X-Transfer-Length': String(gzContents.length),
})
.get(indexRegex)
.reply(200, {
'2.0.1': `versions/example-cli/2.0.1/${hash}/example-cli-v2.0.1-${config.platform}-${config.arch}.gz`,
})
updater = initUpdater(config)
await updater.runUpdate({autoUpdate: false, version: '2.0.1'})
expect(request.callCount).to.equal(3)
expect(request.firstCall.args[0]).to.include('https://registry.npmjs.org/@oclif/plugin-update')
})
it('will get the correct channel and use a custom registry', async () => {
const request = sinon.spy(got, 'get')
const hash = 'f289627'
config.pjson.name = '@oclif/plugin-update'
config.npmRegistry = 'https://myCustomRegistry.com'
clientRoot = await setupClientRoot({config})
const platformRegex = new RegExp(`tarballs\\/example-cli\\/${config.platform}-${config.arch}`)
const manifestRegex = new RegExp(`channels\\/stable\\/example-cli-${config.platform}-${config.arch}-buildmanifest`)
const versionManifestRegex = new RegExp(
`example-cli-v2.0.1-${hash}-${config.platform}-${config.arch}-buildmanifest`,
)
const tarballRegex = new RegExp(
`tarballs\\/example-cli\\/example-cli-v2.0.1\\/example-cli-v2.0.1-${config.platform}-${config.arch}gz`,
)
const indexRegex = new RegExp(`example-cli-${config.platform}-${config.arch}-tar-gz.json`)
sinon.stub(Extractor, 'extract').resolves()
const gzContents = zlib.gzipSync(' ')
nock(/oclif-staging.s3.amazonaws.com/)
.get(platformRegex)
.reply(200, {version: '2.0.1'})
.get(manifestRegex)
.reply(200, {version: '2.0.1'})
.get(versionManifestRegex)
.reply(200, {version: '2.0.1'})
.get(tarballRegex)
.reply(200, gzContents, {
'Content-Encoding': 'gzip',
'content-length': String(gzContents.length),
'X-Transfer-Length': String(gzContents.length),
})
.get(indexRegex)
.reply(200, {
'2.0.1': `versions/example-cli/2.0.1/${hash}/example-cli-v2.0.1-${config.platform}-${config.arch}.gz`,
})
updater = initUpdater(config)
await updater.runUpdate({autoUpdate: false, version: '2.0.1'})
expect(request.callCount).to.equal(3)
expect(request.firstCall.args[0]).to.include('https://myCustomRegistry.com/@oclif/plugin-update')
})
it('should not update - not updatable', async () => {
clientRoot = await setupClientRoot({config})
// unset binPath
config.binPath = undefined
nock(/oclif-staging.s3.amazonaws.com/)
.get(/tarballs\/example-cli\/.+?/)
.reply(200, {version: '2.0.0'})
.get(/channels\/stable\/example-cli-.+?-buildmanifest/)
.reply(200, {version: '2.0.0'})
updater = initUpdater(config)
await updater.runUpdate({autoUpdate: false})
const stdout = collector.stdout.join(' ')
expect(stdout).to.include('not updatable')
})
it('should update from local file', async () => {
clientRoot = await setupClientRoot({config})
const tarballRegex = new RegExp(
`tarballs\\/example-cli\\/example-cli-v2.0.0\\/example-cli-v2.0.1-${config.platform}-${config.arch}gz`,
)
const newVersionPath = path.join(clientRoot, '2.0.1')
await mkdir(path.join(newVersionPath, 'bin'), {recursive: true})
await mkdir(path.join(`${newVersionPath}.partial.11111`, 'bin'), {recursive: true})
await writeFile(path.join(`${newVersionPath}.partial.11111`, 'bin', 'example-cli'), '../2.0.1/bin', 'utf8')
await writeFile(path.join(newVersionPath, 'bin', 'example-cli'), '../2.0.1/bin', 'utf8')
sinon.stub(Extractor, 'extract').resolves()
const gzContents = zlib.gzipSync(' ')
nock(/oclif-staging.s3.amazonaws.com/)
.get(tarballRegex)
.reply(200, gzContents, {
'Content-Encoding': 'gzip',
'content-length': String(gzContents.length),
'X-Transfer-Length': String(gzContents.length),
})
updater = initUpdater(config)
await updater.runUpdate({autoUpdate: false, version: '2.0.1'})
const stdout = stripAnsi(collector.stdout.join(' '))
expect(stdout).to.matches(/Updating to a specific version will not update the channel/)
})
describe('custom S3 templates', () => {
it('should use config.s3Key() for manifest URL when custom templates are configured', async () => {
clientRoot = await setupClientRoot({config}, '2.0.0')
// The example config has custom S3 templates with target.manifest:
// "tarballs/<%- bin %>/<%- channel === 'stable' ? '' : 'channels/' + channel + '/' %><%- platform %>-<%- arch %>"
// For channel=stable, this produces: tarballs/example-cli/{platform}-{arch}
const customManifestRegex = new RegExp(`tarballs\\/example-cli\\/${config.platform}-${config.arch}$`)
// Verify the hardcoded format is NOT used
const hardcodedManifestRegex = new RegExp(
`channels\\/stable\\/example-cli-${config.platform}-${config.arch}-buildmanifest`,
)
const interceptedPaths: string[] = []
nock(/oclif-staging.s3.amazonaws.com/)
.get(customManifestRegex)
.reply(function () {
interceptedPaths.push(this.req.path)
return [200, {version: '2.0.0'}]
})
updater = initUpdater(config)
await updater.runUpdate({autoUpdate: false})
expect(interceptedPaths).to.have.lengthOf(1)
expect(interceptedPaths[0]).to.match(customManifestRegex)
expect(interceptedPaths[0]).to.not.match(hardcodedManifestRegex)
})
it('should use hardcoded manifest key when no custom templates are configured', async () => {
// Remove custom templates from the config
const originalTemplates = config.pjson.oclif.update!.s3!.templates
delete (config.pjson.oclif.update!.s3 as Record<string, unknown>).templates
clientRoot = await setupClientRoot({config}, '2.0.0')
const hardcodedManifestRegex = new RegExp(
`channels\\/stable\\/example-cli-${config.platform}-${config.arch}-buildmanifest`,
)
const interceptedPaths: string[] = []
nock(/oclif-staging.s3.amazonaws.com/)
.get(hardcodedManifestRegex)
.reply(function () {
interceptedPaths.push(this.req.path)
return [200, {version: '2.0.0'}]
})
updater = initUpdater(config)
await updater.runUpdate({autoUpdate: false})
expect(interceptedPaths).to.have.lengthOf(1)
expect(interceptedPaths[0]).to.match(hardcodedManifestRegex)
// Restore templates
config.pjson.oclif.update!.s3!.templates = originalTemplates
})
it('should use custom template manifest URL for non-stable channels', async () => {
clientRoot = await setupClientRoot({config}, '2.0.0')
// For channel=beta, the template produces: tarballs/example-cli/channels/beta/{platform}-{arch}
const betaManifestRegex = new RegExp(
`tarballs\\/example-cli\\/channels\\/beta\\/${config.platform}-${config.arch}`,
)
const interceptedPaths: string[] = []
nock(/oclif-staging.s3.amazonaws.com/)
.get(betaManifestRegex)
.reply(function () {
interceptedPaths.push(this.req.path)
return [200, {version: '2.0.0'}]
})
updater = initUpdater(config)
await updater.runUpdate({autoUpdate: false, channel: 'beta'})
expect(interceptedPaths).to.have.lengthOf(1)
expect(interceptedPaths[0]).to.match(betaManifestRegex)
})
})
})