From 964610229854b707fde3602d2b3c5ae990f07ebb Mon Sep 17 00:00:00 2001 From: axel7083 <42176370+axel7083@users.noreply.github.com> Date: Fri, 18 Oct 2024 12:45:39 +0200 Subject: [PATCH 1/3] feat: adding select version function Signed-off-by: axel7083 <42176370+axel7083@users.noreply.github.com> --- src/download.spec.ts | 80 +++++++++++++++++++++++++++++++++++++++++++- src/download.ts | 26 ++++++++++++-- 2 files changed, 103 insertions(+), 3 deletions(-) diff --git a/src/download.spec.ts b/src/download.spec.ts index 2a89f2a..d5c1087 100644 --- a/src/download.spec.ts +++ b/src/download.spec.ts @@ -22,7 +22,7 @@ import { beforeEach } from 'node:test'; import type { Octokit } from '@octokit/rest'; import type * as extensionApi from '@podman-desktop/api'; -import { env, process as processCore } from '@podman-desktop/api'; +import { env, process as processCore, window } from '@podman-desktop/api'; import { afterEach, describe, expect, test, vi } from 'vitest'; import type { MinikubeGithubReleaseArtifactMetadata } from './download'; @@ -59,6 +59,9 @@ vi.mock('@podman-desktop/api', () => ({ process: { exec: vi.fn(), }, + window: { + showQuickPick: vi.fn(), + }, })); const listReleaseAssetsMock = vi.fn(); @@ -161,6 +164,81 @@ describe('getMinikubeExtensionPath', () => { }); }); +describe('selectVersion', () => { + test('should throw an error if no releases has been found', async () => { + const minikubeDownload = new MinikubeDownload(extensionContext, octokitMock); + vi.spyOn(minikubeDownload, 'grabLatestsReleasesMetadata').mockResolvedValue([]); + + await expect(async () => { + await minikubeDownload.selectVersion(); + }).rejects.toThrowError('cannot grab minikube releases'); + }); + + test('should throw an error if user did not select any version', async () => { + const minikubeDownload = new MinikubeDownload(extensionContext, octokitMock); + vi.spyOn(minikubeDownload, 'grabLatestsReleasesMetadata').mockResolvedValue([ + { + label: 'v1.5.2', + tag: 'v1.5.2', + id: 55, + }, + ]); + + vi.mocked(window.showQuickPick).mockResolvedValue(undefined); + + await expect(async () => { + await minikubeDownload.selectVersion(); + }).rejects.toThrowError('No version selected'); + }); + + test('should use quick pick api to request user to select version', async () => { + const minikubeDownload = new MinikubeDownload(extensionContext, octokitMock); + const release = { + label: 'v1.5.2', + tag: 'v1.5.2', + id: 55, + }; + vi.spyOn(minikubeDownload, 'grabLatestsReleasesMetadata').mockResolvedValue([release]); + + vi.mocked(window.showQuickPick).mockResolvedValue(release); + + const result = await minikubeDownload.selectVersion(); + expect(result).toStrictEqual(release); + + expect(window.showQuickPick).toHaveBeenCalledWith([release], { + placeHolder: 'Select Kind version to download', + }); + }); + + test('should filter out existing version if cliTool is provided as argument', async () => { + const minikubeDownload = new MinikubeDownload(extensionContext, octokitMock); + const release = { + label: 'v1.5.2', + tag: 'v1.5.2', + id: 55, + }; + vi.spyOn(minikubeDownload, 'grabLatestsReleasesMetadata').mockResolvedValue([ + { + label: 'v1.5.1', + tag: 'v1.5.1', + id: 54, + }, + release, + ]); + + vi.mocked(window.showQuickPick).mockResolvedValue(release); + + const result = await minikubeDownload.selectVersion({ + version: '1.5.1', + } as unknown as extensionApi.CliTool); + expect(result).toStrictEqual(release); + + expect(window.showQuickPick).toHaveBeenCalledWith([release], { + placeHolder: 'Select Kind version to download', + }); + }); +}); + describe('findMinikube', () => { test('should use system wide first', async () => { (env.isWindows as boolean) = true; diff --git a/src/download.ts b/src/download.ts index bdb0189..50dc499 100644 --- a/src/download.ts +++ b/src/download.ts @@ -23,11 +23,11 @@ import * as path from 'node:path'; import type { Octokit } from '@octokit/rest'; import type * as extensionApi from '@podman-desktop/api'; -import { env } from '@podman-desktop/api'; +import { env, window } from '@podman-desktop/api'; import { whereBinary } from './util'; -export interface MinikubeGithubReleaseArtifactMetadata { +export interface MinikubeGithubReleaseArtifactMetadata extends extensionApi.QuickPickItem { tag: string; id: number; } @@ -68,6 +68,28 @@ export class MinikubeDownload { return latestReleases[0]; } + async selectVersion(cliInfo?: extensionApi.CliTool): Promise { + let lastReleasesMetadata = await this.grabLatestsReleasesMetadata(); + + if (lastReleasesMetadata.length === 0) throw new Error('cannot grab minikube releases'); + + // if the user already has an installed version, we remove it from the list + if (cliInfo) { + lastReleasesMetadata = lastReleasesMetadata.filter(release => release.tag.slice(1) !== cliInfo.version); + } + + // Show the quickpick + const selectedRelease = await window.showQuickPick(lastReleasesMetadata, { + placeHolder: 'Select Kind version to download', + }); + + if (selectedRelease) { + return selectedRelease; + } else { + throw new Error('No version selected'); + } + } + getMinikubeExtensionPath(): string { let fileExtension = ''; if (env.isWindows) { From 9ec506e28307bfbd3419da37f191f6919bd5aecf Mon Sep 17 00:00:00 2001 From: axel7083 <42176370+axel7083@users.noreply.github.com> Date: Fri, 18 Oct 2024 13:20:42 +0200 Subject: [PATCH 2/3] Update src/download.ts Co-authored-by: Florent BENOIT Signed-off-by: axel7083 <42176370+axel7083@users.noreply.github.com> --- src/download.ts | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/download.ts b/src/download.ts index 50dc499..00145b5 100644 --- a/src/download.ts +++ b/src/download.ts @@ -83,11 +83,10 @@ export class MinikubeDownload { placeHolder: 'Select Kind version to download', }); - if (selectedRelease) { - return selectedRelease; - } else { + if (!selectedRelease) { throw new Error('No version selected'); } + return selectedRelease; } getMinikubeExtensionPath(): string { From 8b8bdc4996cd94c15025a1a450481fd81ac10677 Mon Sep 17 00:00:00 2001 From: axel7083 <42176370+axel7083@users.noreply.github.com> Date: Fri, 18 Oct 2024 14:05:30 +0200 Subject: [PATCH 3/3] fix: renaming variable Signed-off-by: axel7083 <42176370+axel7083@users.noreply.github.com> --- src/download.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/download.ts b/src/download.ts index 00145b5..a57c1c2 100644 --- a/src/download.ts +++ b/src/download.ts @@ -69,17 +69,17 @@ export class MinikubeDownload { } async selectVersion(cliInfo?: extensionApi.CliTool): Promise { - let lastReleasesMetadata = await this.grabLatestsReleasesMetadata(); + let releasesMetadata = await this.grabLatestsReleasesMetadata(); - if (lastReleasesMetadata.length === 0) throw new Error('cannot grab minikube releases'); + if (releasesMetadata.length === 0) throw new Error('cannot grab minikube releases'); // if the user already has an installed version, we remove it from the list if (cliInfo) { - lastReleasesMetadata = lastReleasesMetadata.filter(release => release.tag.slice(1) !== cliInfo.version); + releasesMetadata = releasesMetadata.filter(release => release.tag.slice(1) !== cliInfo.version); } // Show the quickpick - const selectedRelease = await window.showQuickPick(lastReleasesMetadata, { + const selectedRelease = await window.showQuickPick(releasesMetadata, { placeHolder: 'Select Kind version to download', });