Skip to content

Commit

Permalink
feat: adding select version function (#172)
Browse files Browse the repository at this point in the history
* feat: adding select version function

Signed-off-by: axel7083 <[email protected]>

* Update src/download.ts

Co-authored-by: Florent BENOIT <[email protected]>
Signed-off-by: axel7083 <[email protected]>

* fix: renaming variable

Signed-off-by: axel7083 <[email protected]>

---------

Signed-off-by: axel7083 <[email protected]>
Co-authored-by: Florent BENOIT <[email protected]>
  • Loading branch information
axel7083 and benoitf authored Oct 18, 2024
1 parent 5ba894d commit 381cff4
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 3 deletions.
80 changes: 79 additions & 1 deletion src/download.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -59,6 +59,9 @@ vi.mock('@podman-desktop/api', () => ({
process: {
exec: vi.fn(),
},
window: {
showQuickPick: vi.fn(),
},
}));

const listReleaseAssetsMock = vi.fn();
Expand Down Expand Up @@ -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;
Expand Down
25 changes: 23 additions & 2 deletions src/download.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -68,6 +68,27 @@ export class MinikubeDownload {
return latestReleases[0];
}

async selectVersion(cliInfo?: extensionApi.CliTool): Promise<MinikubeGithubReleaseArtifactMetadata> {
let releasesMetadata = await this.grabLatestsReleasesMetadata();

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) {
releasesMetadata = releasesMetadata.filter(release => release.tag.slice(1) !== cliInfo.version);
}

// Show the quickpick
const selectedRelease = await window.showQuickPick(releasesMetadata, {
placeHolder: 'Select Kind version to download',
});

if (!selectedRelease) {
throw new Error('No version selected');
}
return selectedRelease;
}

getMinikubeExtensionPath(): string {
let fileExtension = '';
if (env.isWindows) {
Expand Down

0 comments on commit 381cff4

Please sign in to comment.