|
| 1 | +/*! |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +import assert from 'assert' |
| 7 | +import sinon from 'sinon' |
| 8 | +import { AmazonQLSPResolver, supportedLspServerVersions } from '../../../src/lsp/lspInstaller' |
| 9 | +import { |
| 10 | + fs, |
| 11 | + LanguageServerResolver, |
| 12 | + makeTemporaryToolkitFolder, |
| 13 | + ManifestResolver, |
| 14 | + request, |
| 15 | +} from 'aws-core-vscode/shared' |
| 16 | +import * as semver from 'semver' |
| 17 | + |
| 18 | +function createVersion(version: string) { |
| 19 | + return { |
| 20 | + isDelisted: false, |
| 21 | + serverVersion: version, |
| 22 | + targets: [ |
| 23 | + { |
| 24 | + arch: process.arch, |
| 25 | + platform: process.platform, |
| 26 | + contents: [ |
| 27 | + { |
| 28 | + bytes: 0, |
| 29 | + filename: 'servers.zip', |
| 30 | + hashes: [], |
| 31 | + url: 'http://fakeurl', |
| 32 | + }, |
| 33 | + ], |
| 34 | + }, |
| 35 | + ], |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +describe('AmazonQLSPInstaller', () => { |
| 40 | + let resolver: AmazonQLSPResolver |
| 41 | + let sandbox: sinon.SinonSandbox |
| 42 | + let tempDir: string |
| 43 | + |
| 44 | + beforeEach(async () => { |
| 45 | + sandbox = sinon.createSandbox() |
| 46 | + resolver = new AmazonQLSPResolver() |
| 47 | + tempDir = await makeTemporaryToolkitFolder() |
| 48 | + sandbox.stub(LanguageServerResolver.prototype, 'defaultDownloadFolder').returns(tempDir) |
| 49 | + }) |
| 50 | + |
| 51 | + afterEach(async () => { |
| 52 | + delete process.env.AWS_LANGUAGE_SERVER_OVERRIDE |
| 53 | + sandbox.restore() |
| 54 | + await fs.delete(tempDir, { |
| 55 | + recursive: true, |
| 56 | + }) |
| 57 | + }) |
| 58 | + |
| 59 | + describe('resolve()', () => { |
| 60 | + it('uses AWS_LANGUAGE_SERVER_OVERRIDE', async () => { |
| 61 | + const overridePath = '/custom/path/to/lsp' |
| 62 | + process.env.AWS_LANGUAGE_SERVER_OVERRIDE = overridePath |
| 63 | + |
| 64 | + const result = await resolver.resolve() |
| 65 | + |
| 66 | + assert.strictEqual(result.assetDirectory, overridePath) |
| 67 | + assert.strictEqual(result.location, 'override') |
| 68 | + assert.strictEqual(result.version, '0.0.0') |
| 69 | + }) |
| 70 | + |
| 71 | + it('resolves', async () => { |
| 72 | + // First try - should download the file |
| 73 | + const download = await resolver.resolve() |
| 74 | + |
| 75 | + assert.ok(download.assetDirectory.startsWith(tempDir)) |
| 76 | + assert.deepStrictEqual(download.location, 'remote') |
| 77 | + assert.ok(semver.satisfies(download.version, supportedLspServerVersions)) |
| 78 | + |
| 79 | + // Second try - Should see the contents in the cache |
| 80 | + const cache = await resolver.resolve() |
| 81 | + |
| 82 | + assert.ok(cache.assetDirectory.startsWith(tempDir)) |
| 83 | + assert.deepStrictEqual(cache.location, 'cache') |
| 84 | + assert.ok(semver.satisfies(cache.version, supportedLspServerVersions)) |
| 85 | + |
| 86 | + /** |
| 87 | + * Always make sure the latest version is one patch higher. This stops a problem |
| 88 | + * where the fallback can't be used because the latest compatible version |
| 89 | + * is equal to the min version, so if the cache isn't valid, then there |
| 90 | + * would be no fallback location |
| 91 | + * |
| 92 | + * Instead, increasing the latest compatible lsp version means we can just |
| 93 | + * use the one we downloaded earlier in the test as the fallback |
| 94 | + */ |
| 95 | + const nextVer = semver.inc(cache.version, 'patch', true) |
| 96 | + if (!nextVer) { |
| 97 | + throw new Error('Could not increment version') |
| 98 | + } |
| 99 | + sandbox.stub(ManifestResolver.prototype, 'resolve').resolves({ |
| 100 | + manifestSchemaVersion: '0.0.0', |
| 101 | + artifactId: 'foo', |
| 102 | + artifactDescription: 'foo', |
| 103 | + isManifestDeprecated: false, |
| 104 | + versions: [createVersion(nextVer), createVersion(cache.version)], |
| 105 | + }) |
| 106 | + |
| 107 | + // fail the next http request for the language server |
| 108 | + sandbox.stub(request, 'fetch').returns({ |
| 109 | + response: Promise.resolve({ |
| 110 | + ok: false, |
| 111 | + }), |
| 112 | + } as any) |
| 113 | + |
| 114 | + // Third try - Cache doesn't exist and we couldn't download from the internet, fallback to a local version |
| 115 | + const fallback = await resolver.resolve() |
| 116 | + |
| 117 | + assert.ok(fallback.assetDirectory.startsWith(tempDir)) |
| 118 | + assert.deepStrictEqual(fallback.location, 'fallback') |
| 119 | + assert.ok(semver.satisfies(fallback.version, supportedLspServerVersions)) |
| 120 | + }) |
| 121 | + }) |
| 122 | +}) |
0 commit comments