Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: adding select version function #172

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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