|
| 1 | +/*! |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +import { Uri } from 'vscode' |
| 7 | +import { cleanLspDownloads, fs } from '../../../../shared' |
| 8 | +import { createTestWorkspaceFolder } from '../../../testUtil' |
| 9 | +import path from 'path' |
| 10 | +import assert from 'assert' |
| 11 | + |
| 12 | +async function fakeInstallVersion(version: string, installationDir: string): Promise<void> { |
| 13 | + const versionDir = path.join(installationDir, version) |
| 14 | + await fs.mkdir(versionDir) |
| 15 | + await fs.writeFile(path.join(versionDir, 'file.txt'), 'content') |
| 16 | +} |
| 17 | + |
| 18 | +async function fakeInstallVersions(versions: string[], installationDir: string): Promise<void> { |
| 19 | + for (const v of versions) { |
| 20 | + await fakeInstallVersion(v, installationDir) |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +describe('cleanLSPDownloads', function () { |
| 25 | + let installationDir: Uri |
| 26 | + |
| 27 | + before(async function () { |
| 28 | + installationDir = (await createTestWorkspaceFolder()).uri |
| 29 | + }) |
| 30 | + |
| 31 | + afterEach(async function () { |
| 32 | + const files = await fs.readdir(installationDir.fsPath) |
| 33 | + for (const [name, _type] of files) { |
| 34 | + await fs.delete(path.join(installationDir.fsPath, name), { force: true, recursive: true }) |
| 35 | + } |
| 36 | + }) |
| 37 | + |
| 38 | + after(async function () { |
| 39 | + await fs.delete(installationDir, { force: true, recursive: true }) |
| 40 | + }) |
| 41 | + |
| 42 | + it('keeps two newest versions', async function () { |
| 43 | + await fakeInstallVersions(['1.0.0', '1.0.1', '1.1.1', '2.1.1'], installationDir.fsPath) |
| 44 | + await cleanLspDownloads([], installationDir.fsPath) |
| 45 | + |
| 46 | + const result = (await fs.readdir(installationDir.fsPath)).map(([filename, _filetype], _index) => filename) |
| 47 | + assert.strictEqual(result.length, 2) |
| 48 | + assert.ok(result.includes('2.1.1')) |
| 49 | + assert.ok(result.includes('1.1.1')) |
| 50 | + }) |
| 51 | + |
| 52 | + it('deletes delisted versions', async function () { |
| 53 | + await fakeInstallVersions(['1.0.0', '1.0.1', '1.1.1', '2.1.1'], installationDir.fsPath) |
| 54 | + await cleanLspDownloads([{ serverVersion: '1.1.1', isDelisted: true, targets: [] }], installationDir.fsPath) |
| 55 | + |
| 56 | + const result = (await fs.readdir(installationDir.fsPath)).map(([filename, _filetype], _index) => filename) |
| 57 | + assert.strictEqual(result.length, 2) |
| 58 | + assert.ok(result.includes('2.1.1')) |
| 59 | + assert.ok(result.includes('1.0.1')) |
| 60 | + }) |
| 61 | + |
| 62 | + it('handles case where less than 2 versions are not delisted', async function () { |
| 63 | + await fakeInstallVersions(['1.0.0', '1.0.1', '1.1.1', '2.1.1'], installationDir.fsPath) |
| 64 | + await cleanLspDownloads( |
| 65 | + [ |
| 66 | + { serverVersion: '1.1.1', isDelisted: true, targets: [] }, |
| 67 | + { serverVersion: '2.1.1', isDelisted: true, targets: [] }, |
| 68 | + { serverVersion: '1.0.0', isDelisted: true, targets: [] }, |
| 69 | + ], |
| 70 | + installationDir.fsPath |
| 71 | + ) |
| 72 | + |
| 73 | + const result = (await fs.readdir(installationDir.fsPath)).map(([filename, _filetype], _index) => filename) |
| 74 | + assert.strictEqual(result.length, 1) |
| 75 | + assert.ok(result.includes('1.0.1')) |
| 76 | + }) |
| 77 | + |
| 78 | + it('handles case where less than 2 versions exist', async function () { |
| 79 | + await fakeInstallVersions(['1.0.0'], installationDir.fsPath) |
| 80 | + await cleanLspDownloads([], installationDir.fsPath) |
| 81 | + |
| 82 | + const result = (await fs.readdir(installationDir.fsPath)).map(([filename, _filetype], _index) => filename) |
| 83 | + assert.strictEqual(result.length, 1) |
| 84 | + }) |
| 85 | + |
| 86 | + it('does not install delisted version when no other option exists', async function () { |
| 87 | + await fakeInstallVersions(['1.0.0'], installationDir.fsPath) |
| 88 | + await cleanLspDownloads([{ serverVersion: '1.0.0', isDelisted: true, targets: [] }], installationDir.fsPath) |
| 89 | + |
| 90 | + const result = (await fs.readdir(installationDir.fsPath)).map(([filename, _filetype], _index) => filename) |
| 91 | + assert.strictEqual(result.length, 0) |
| 92 | + }) |
| 93 | +}) |
0 commit comments