-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathupdate.test.ts
More file actions
490 lines (412 loc) · 20.1 KB
/
Copy pathupdate.test.ts
File metadata and controls
490 lines (412 loc) · 20.1 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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
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, stat, symlink, utimes, 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
}
const setOldMtime = async (filePath: string): Promise<void> => {
const oldDate = new Date()
oldDate.setDate(oldDate.getDate() - 43)
await utimes(filePath, oldDate, oldDate)
}
const setupTidyClientRoot = async (config: Interfaces.Config): Promise<string> => {
const root = config.scopedEnvVar('OCLIF_CLIENT_HOME') || path.join(config.dataDir, 'client')
await mkdir(root, {recursive: true})
// Create current version directory (matches config.version = '2.0.0')
const versionDir = path.join(root, '2.0.0')
await mkdir(path.join(versionDir, 'bin'), {recursive: true})
await writeFile(path.join(versionDir, 'bin', config.bin), 'binary', 'utf8')
// Create bin/ directory with launcher script
await mkdir(path.join(root, 'bin'), {recursive: true})
await writeFile(path.join(root, 'bin', config.bin), '../2.0.0/bin', 'utf8')
// Create current symlink
if (!existsSync(path.join(root, 'current'))) {
await symlink(path.join(root, '2.0.0'), path.join(root, 'current'))
}
return root
}
// Mirrors a real install where the active directory is named "<version>-<sha>"
// (e.g. "2.0.0-a2559bd") while config.version remains plain semver ("2.0.0").
const setupShaSuffixedTidyClientRoot = async (config: Interfaces.Config, activeDirName: string): Promise<string> => {
const root = config.scopedEnvVar('OCLIF_CLIENT_HOME') || path.join(config.dataDir, 'client')
await mkdir(root, {recursive: true})
// Active version directory, named "<version>-<sha>"
const versionDir = path.join(root, activeDirName)
await mkdir(path.join(versionDir, 'bin'), {recursive: true})
await writeFile(path.join(versionDir, 'bin', config.bin), 'binary', 'utf8')
// bin/ directory with launcher script pointing at the sha-suffixed dir
await mkdir(path.join(root, 'bin'), {recursive: true})
await writeFile(path.join(root, 'bin', config.bin), `../${activeDirName}/bin`, 'utf8')
// current symlink -> sha-suffixed dir
if (!existsSync(path.join(root, 'current'))) {
await symlink(path.join(root, activeDirName), path.join(root, 'current'))
}
return root
}
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 platformRegex = new RegExp(`tarballs\\/example-cli\\/${config.platform}-${config.arch}`)
const manifestRegex = new RegExp(`channels\\/stable\\/example-cli-${config.platform}-${config.arch}-buildmanifest`)
nock(/oclif-staging.s3.amazonaws.com/)
.get(platformRegex)
.reply(200, {version: '2.0.0'})
.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 platformRegex = new RegExp(`tarballs\\/example-cli\\/${config.platform}-${config.arch}`)
const manifestRegex = new RegExp(`channels\\/stable\\/example-cli-${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 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(platformRegex)
.reply(200, {version: '2.0.1'})
.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 platformRegex = new RegExp(`tarballs\\/example-cli\\/${config.platform}-${config.arch}`)
const manifestRegex = new RegExp(`channels\\/stable\\/example-cli-${config.platform}-${config.arch}-buildmanifest`)
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(platformRegex)
.reply(200, {version: '2.0.1'})
.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, 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('tidy', () => {
it('should preserve bin, current, and active version directories during tidy', async () => {
clientRoot = await setupTidyClientRoot(config)
// Create old version directory that should be cleaned up
const oldVersionDir = path.join(clientRoot, '1.0.0-abc1234')
await mkdir(path.join(oldVersionDir, 'bin'), {recursive: true})
await writeFile(path.join(oldVersionDir, 'bin', 'example-cli'), 'old version', 'utf8')
// Backdate the old version directory to be older than 42 days
await setOldMtime(oldVersionDir)
// Also backdate bin/ and current to verify they are preserved even when old
await setOldMtime(path.join(clientRoot, 'bin'))
await setOldMtime(path.join(clientRoot, 'current'))
// Backdate the active version directory too - it should still be preserved
await setOldMtime(path.join(clientRoot, '2.0.0'))
// Trigger tidy via runUpdate (already on same version, but tidy still runs)
const manifestRegex = new RegExp(
`channels\\/stable\\/example-cli-${config.platform}-${config.arch}-buildmanifest`,
)
nock(/oclif-staging.s3.amazonaws.com/)
.get(manifestRegex)
.reply(200, {version: '2.0.0'})
updater = initUpdater(config)
await updater.runUpdate({autoUpdate: false})
// Verify bin/ and current survived even though they are old
expect(existsSync(path.join(clientRoot, 'bin'))).to.be.true
expect(existsSync(path.join(clientRoot, 'current'))).to.be.true
// Verify active version directory survived even though it is old
expect(existsSync(path.join(clientRoot, '2.0.0'))).to.be.true
// Verify old version was cleaned up
expect(existsSync(oldVersionDir)).to.be.false
})
it('should not delete entries newer than 42 days', async () => {
clientRoot = await setupTidyClientRoot(config)
// Create a recent version directory (should survive)
const recentVersionDir = path.join(clientRoot, '1.9.0-def5678')
await mkdir(path.join(recentVersionDir, 'bin'), {recursive: true})
await writeFile(path.join(recentVersionDir, 'bin', 'example-cli'), 'recent version', 'utf8')
const manifestRegex = new RegExp(
`channels\\/stable\\/example-cli-${config.platform}-${config.arch}-buildmanifest`,
)
nock(/oclif-staging.s3.amazonaws.com/)
.get(manifestRegex)
.reply(200, {version: '2.0.0'})
updater = initUpdater(config)
await updater.runUpdate({autoUpdate: false})
// Recent version should survive
expect(existsSync(recentVersionDir)).to.be.true
})
// Regression test for #1361: installs are named "<version>-<sha>" but
// config.version is plain semver, so the exact-match guard never matched the
// active dir. Once it aged past 42 days, tidy() deleted the running CLI,
// leaving `current` dangling. The active dir must be preserved by name even
// when it is sha-suffixed and old.
it('should preserve a sha-suffixed active version directory even when old', async () => {
const activeDirName = '2.0.0-a2559bd'
clientRoot = await setupShaSuffixedTidyClientRoot(config, activeDirName)
// Create an old, non-active version that should be cleaned up
const oldVersionDir = path.join(clientRoot, '1.0.0-abc1234')
await mkdir(path.join(oldVersionDir, 'bin'), {recursive: true})
await writeFile(path.join(oldVersionDir, 'bin', 'example-cli'), 'old version', 'utf8')
await setOldMtime(oldVersionDir)
// Backdate bin/, current, and the active dir past the 42-day threshold to
// exercise the exact scenario from the report: staying on latest 42+ days.
await setOldMtime(path.join(clientRoot, 'bin'))
await setOldMtime(path.join(clientRoot, 'current'))
await setOldMtime(path.join(clientRoot, activeDirName))
// Manifest carries a sha so `updated` becomes "2.0.0-a2559bd", matching
// the current install and short-circuiting before any download, while
// touch() and tidy() still run.
const manifestRegex = new RegExp(
`channels\\/stable\\/example-cli-${config.platform}-${config.arch}-buildmanifest`,
)
nock(/oclif-staging.s3.amazonaws.com/)
.get(manifestRegex)
.reply(200, {sha: 'a2559bd', version: '2.0.0'})
updater = initUpdater(config)
await updater.runUpdate({autoUpdate: false})
// Active sha-suffixed dir must survive, along with bin/ and current.
expect(existsSync(path.join(clientRoot, activeDirName))).to.be.true
expect(existsSync(path.join(clientRoot, 'bin'))).to.be.true
expect(existsSync(path.join(clientRoot, 'current'))).to.be.true
// The `current` symlink must still resolve to the active dir (not dangling).
expect(existsSync(path.join(clientRoot, 'current', 'bin', config.bin))).to.be.true
// Genuinely old, non-active version is still cleaned up.
expect(existsSync(oldVersionDir)).to.be.false
})
// touch() must refresh the sha-suffixed active dir's mtime so it is never
// considered "old" in the first place. Previously it joined the bare
// config.version ("2.0.0"), which does not exist on disk, making it a no-op.
it('touch() refreshes the sha-suffixed active version directory mtime', async () => {
const activeDirName = '2.0.0-a2559bd'
clientRoot = await setupShaSuffixedTidyClientRoot(config, activeDirName)
const activeDir = path.join(clientRoot, activeDirName)
// Backdate the active dir well past the threshold.
await setOldMtime(activeDir)
const beforeMtime = (await stat(activeDir)).mtime.getTime()
const manifestRegex = new RegExp(
`channels\\/stable\\/example-cli-${config.platform}-${config.arch}-buildmanifest`,
)
nock(/oclif-staging.s3.amazonaws.com/)
.get(manifestRegex)
.reply(200, {sha: 'a2559bd', version: '2.0.0'})
updater = initUpdater(config)
await updater.runUpdate({autoUpdate: false})
// mtime should have been bumped to ~now, proving touch() found the dir.
const afterMtime = (await stat(activeDir)).mtime.getTime()
expect(afterMtime).to.be.greaterThan(beforeMtime)
expect(Date.now() - afterMtime).to.be.lessThan(60_000)
})
})
})