|
| 1 | +import assert from 'node:assert/strict'; |
| 2 | +import { spawnSync } from 'node:child_process'; |
| 3 | +import { mkdtempSync, rmSync } from 'node:fs'; |
| 4 | +import { createRequire } from 'node:module'; |
| 5 | +import { tmpdir } from 'node:os'; |
| 6 | +import { join } from 'node:path'; |
| 7 | +import process from 'node:process'; |
| 8 | +import test from 'node:test'; |
| 9 | +import { fileURLToPath, URL } from 'node:url'; |
| 10 | + |
| 11 | +const require = createRequire(import.meta.url); |
| 12 | +const extensionRoot = fileURLToPath(new URL('../', import.meta.url)); |
| 13 | + |
| 14 | +require('ts-node').register({ |
| 15 | + project: fileURLToPath(new URL('tsconfig.json', import.meta.url)), |
| 16 | + transpileOnly: true |
| 17 | +}); |
| 18 | +const proxyquire = require('proxyquire').noCallThru(); |
| 19 | +const sinon = require('sinon'); |
| 20 | +const { TimeoutError } = require('@vscode/test-electron/out/request'); |
| 21 | + |
| 22 | +function networkError(code) { |
| 23 | + return Object.assign(new Error('Controlled acquisition failure'), { code }); |
| 24 | +} |
| 25 | + |
| 26 | +function createInstaller() { |
| 27 | + const download = sinon.stub().resolves('test-executable'); |
| 28 | + const resolveCli = sinon.stub().returns(['test-cli', '--existing-argument', '--extensions-dir=default', '--user-data-dir=default']); |
| 29 | + const wait = sinon.stub().resolves(); |
| 30 | + const mkdir = sinon.stub().resolves(); |
| 31 | + const write = sinon.stub().resolves(); |
| 32 | + const warn = sinon.stub(); |
| 33 | + const installer = proxyquire(fileURLToPath(new URL('vscode.ts', import.meta.url)), { |
| 34 | + '@vscode/test-electron': { downloadAndUnzipVSCode: download, resolveCliArgsFromVSCodeExecutablePath: resolveCli }, |
| 35 | + 'timers/promises': { setTimeout: wait }, |
| 36 | + '../src/Utility/Text/streams': { verbose: sinon.stub() }, |
| 37 | + './common': { mkdir, write, warn, readJson: sinon.stub().resolves({}) }, |
| 38 | + './vscodeTestPath': { getVSCodeTestIsolate: () => join(tmpdir(), 'cpptools-acquisition-unit') } |
| 39 | + }); |
| 40 | + return { ...installer, download, resolveCli, wait, mkdir, write, warn }; |
| 41 | +} |
| 42 | + |
| 43 | +test('successful acquisition preserves the version, cache and CLI arguments without retries', async () => { |
| 44 | + const installer = createInstaller(); |
| 45 | + const result = await installer.install(); |
| 46 | + |
| 47 | + assert.equal(installer.download.callCount, 1); |
| 48 | + assert.equal(installer.download.firstCall.args[0], installer.options); |
| 49 | + assert.equal(installer.options.version, installer.testVSCodeVersion); |
| 50 | + assert.equal(installer.options.cachePath, `${installer.isolated}/cache`); |
| 51 | + assert.deepEqual(result, { |
| 52 | + cli: 'test-cli', |
| 53 | + args: ['--existing-argument', `--extensions-dir=${installer.extensionsDir}`, `--user-data-dir=${installer.userDir}`] |
| 54 | + }); |
| 55 | + assert.equal(installer.wait.callCount, 0); |
| 56 | + assert.equal(installer.warn.callCount, 0); |
| 57 | + assert.equal(installer.write.callCount, 1); |
| 58 | +}); |
| 59 | + |
| 60 | +for (const code of ['EAI_AGAIN', 'ECONNREFUSED', 'ECONNRESET', 'EHOSTUNREACH', 'ENETUNREACH', 'EPIPE', 'ETIMEDOUT']) { |
| 61 | + test(`retries ${code} once before a successful acquisition`, async () => { |
| 62 | + const installer = createInstaller(); |
| 63 | + installer.download.onFirstCall().rejects(networkError(code)); |
| 64 | + |
| 65 | + await installer.install(); |
| 66 | + |
| 67 | + assert.equal(installer.download.callCount, 2); |
| 68 | + assert.ok(installer.download.getCalls().every(call => call.args[0] === installer.options)); |
| 69 | + assert.deepEqual(installer.wait.args, [[1000]]); |
| 70 | + assert.equal(installer.warn.callCount, 1); |
| 71 | + assert.equal(installer.mkdir.callCount, 1); |
| 72 | + assert.equal(installer.write.callCount, 1); |
| 73 | + }); |
| 74 | +} |
| 75 | + |
| 76 | +test('retries the test-electron request timeout', async () => { |
| 77 | + const installer = createInstaller(); |
| 78 | + installer.download.onFirstCall().rejects(new TimeoutError(15000)); |
| 79 | + |
| 80 | + await installer.install(); |
| 81 | + |
| 82 | + assert.equal(installer.download.callCount, 2); |
| 83 | + assert.deepEqual(installer.wait.args, [[1000]]); |
| 84 | +}); |
| 85 | + |
| 86 | +test('exhausts transient aggregate errors after three attempts with bounded backoff', async () => { |
| 87 | + const installer = createInstaller(); |
| 88 | + installer.download.rejects(new AggregateError([networkError('ETIMEDOUT'), networkError('ENETUNREACH')])); |
| 89 | + |
| 90 | + await assert.rejects(installer.install(), /after 3 attempts: ETIMEDOUT:.*ENETUNREACH:/); |
| 91 | + |
| 92 | + assert.equal(installer.download.callCount, 3); |
| 93 | + assert.deepEqual(installer.wait.args, [[1000], [2000]]); |
| 94 | + assert.equal(installer.warn.callCount, 2); |
| 95 | + assert.equal(installer.resolveCli.callCount, 0); |
| 96 | + assert.equal(installer.write.callCount, 0); |
| 97 | +}); |
| 98 | + |
| 99 | +for (const [name, failure] of [ |
| 100 | + ['an invalid version', new Error('Invalid version')], |
| 101 | + ['a permissions error', networkError('EACCES')], |
| 102 | + ['a full disk', networkError('ENOSPC')], |
| 103 | + ['a certificate error', networkError('CERT_HAS_EXPIRED')], |
| 104 | + ['the library exhausting its archive retries', new Error('Failed to download and unzip VS Code 1.131.0')], |
| 105 | + ['an unclassified HTTP failure', 'Failed to get JSON'], |
| 106 | + ['an empty aggregate error', new AggregateError([])], |
| 107 | + ['an aggregate containing a permanent error', new AggregateError([networkError('ETIMEDOUT'), networkError('EACCES')])] |
| 108 | +]) { |
| 109 | + test(`does not retry ${name}`, async () => { |
| 110 | + const installer = createInstaller(); |
| 111 | + installer.download.callsFake(async () => { throw failure; }); |
| 112 | + |
| 113 | + await assert.rejects(installer.install(), /Failed to install VS Code:.*after 1 attempt/); |
| 114 | + |
| 115 | + assert.equal(installer.download.callCount, 1); |
| 116 | + assert.equal(installer.wait.callCount, 0); |
| 117 | + assert.equal(installer.warn.callCount, 0); |
| 118 | + assert.equal(installer.resolveCli.callCount, 0); |
| 119 | + assert.equal(installer.write.callCount, 0); |
| 120 | + }); |
| 121 | +} |
| 122 | + |
| 123 | +test('does not retry installation work after acquisition succeeds', async () => { |
| 124 | + const installer = createInstaller(); |
| 125 | + installer.write.rejects(networkError('EPIPE')); |
| 126 | + |
| 127 | + await assert.rejects(installer.install(), /Failed to install VS Code/); |
| 128 | + |
| 129 | + assert.equal(installer.download.callCount, 1); |
| 130 | + assert.equal(installer.write.callCount, 1); |
| 131 | + assert.equal(installer.wait.callCount, 0); |
| 132 | +}); |
| 133 | + |
| 134 | +for (const [name, code, attempts, delays] of [ |
| 135 | + ['a non-retryable failure', 'EACCES', 1, []], |
| 136 | + ['exhausted transient failures', 'ETIMEDOUT', 3, [1000, 2000]] |
| 137 | +]) { |
| 138 | + test(`acquisition CLI exits 1 without downstream work after ${name}`, () => { |
| 139 | + const testRoot = mkdtempSync(join(tmpdir(), 'cpptools-acquisition-')); |
| 140 | + const preload = ` |
| 141 | + import { EventEmitter } from 'node:events'; |
| 142 | + import { createRequire } from 'node:module'; |
| 143 | + import process from 'node:process'; |
| 144 | + const require = createRequire(${JSON.stringify(import.meta.url)}); |
| 145 | + require('https').get = () => { |
| 146 | + process.stdout.write('ACQUISITION_ATTEMPT\\n'); |
| 147 | + const request = new EventEmitter(); |
| 148 | + request.destroy = () => request; |
| 149 | + process.nextTick(() => { |
| 150 | + const failure = Object.assign(new Error('Controlled VS Code acquisition failure'), { code: '${code}' }); |
| 151 | + request.emit('error', '${code}' === 'ETIMEDOUT' |
| 152 | + ? new AggregateError([failure, Object.assign(new Error('Controlled IPv6 failure'), { code: 'ENETUNREACH' })]) |
| 153 | + : failure); |
| 154 | + }); |
| 155 | + return request; |
| 156 | + }; |
| 157 | + require('timers/promises').setTimeout = async (milliseconds) => { |
| 158 | + process.stdout.write('ACQUISITION_DELAY:' + milliseconds + '\\n'); |
| 159 | + }; |
| 160 | + const electronPath = require.resolve('@vscode/test-electron'); |
| 161 | + const electron = require(electronPath); |
| 162 | + require.cache[electronPath].exports = { |
| 163 | + ...electron, |
| 164 | + async runVSCodeCommand() { |
| 165 | + throw new Error('Unexpected extension installation'); |
| 166 | + } |
| 167 | + }; |
| 168 | + const copyPath = require.resolve('./copyExtensionBinaries.ts'); |
| 169 | + require.cache[copyPath] = { |
| 170 | + id: copyPath, |
| 171 | + filename: copyPath, |
| 172 | + loaded: true, |
| 173 | + exports: { |
| 174 | + async main() { |
| 175 | + throw new Error('Unexpected binary copying'); |
| 176 | + } |
| 177 | + } |
| 178 | + }; |
| 179 | + `; |
| 180 | + |
| 181 | + try { |
| 182 | + const result = spawnSync(process.execPath, [ |
| 183 | + '--import', `data:text/javascript,${encodeURIComponent(preload)}`, |
| 184 | + require.resolve('ts-node/dist/bin.js'), '-T', '.scripts/installAndCopyBinaries.ts' |
| 185 | + ], { |
| 186 | + cwd: extensionRoot, |
| 187 | + env: { ...process.env, CPPTOOLS_VSCODE_TEST_ROOT: testRoot }, |
| 188 | + encoding: 'utf8', |
| 189 | + timeout: 15000 |
| 190 | + }); |
| 191 | + |
| 192 | + assert.ifError(result.error); |
| 193 | + assert.equal(result.signal, null); |
| 194 | + const output = result.stdout + result.stderr; |
| 195 | + assert.match(output, /Controlled VS Code acquisition failure/); |
| 196 | + assert.match(output, new RegExp(`acquisition failed after ${attempts} attempt`)); |
| 197 | + assert.equal(output.match(/^ACQUISITION_ATTEMPT$/gm)?.length, attempts); |
| 198 | + assert.deepEqual([...output.matchAll(/^ACQUISITION_DELAY:(\d+)$/gm)].map(match => Number(match[1])), delays); |
| 199 | + assert.doesNotMatch(output, /Install latest C\/C\+\+ Extension|Unexpected extension installation|Unexpected binary copying/); |
| 200 | + assert.equal(result.status, 1, output); |
| 201 | + } finally { |
| 202 | + rmSync(testRoot, { recursive: true, force: true }); |
| 203 | + } |
| 204 | + }); |
| 205 | +} |
0 commit comments