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 install method on minikube download #173

Merged
merged 3 commits into from
Oct 21, 2024
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: 80 additions & 0 deletions src/download.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ vi.mock('@podman-desktop/api', () => ({
},
window: {
showQuickPick: vi.fn(),
showErrorMessage: vi.fn(),
showInformationMessage: vi.fn(),
},
}));

Expand Down Expand Up @@ -239,6 +241,84 @@ describe('selectVersion', () => {
});
});

describe('install', () => {
test('install should download the asset provided', async () => {
(env.isWindows as boolean) = true;
const release: MinikubeGithubReleaseArtifactMetadata = {
tag: 'v1.2.3',
id: 55,
label: 'v1.5.2',
};
vi.mocked(window.showInformationMessage).mockResolvedValue('Yes');

const minikubeDownload = new MinikubeDownload(extensionContext, octokitMock);

vi.spyOn(minikubeDownload, 'download').mockResolvedValue('/download/asset/path');

vi.mocked(processCore.exec).mockResolvedValue({
stdout: '',
stderr: '',
command: '',
});

await minikubeDownload.install(release);
expect(minikubeDownload.download).toHaveBeenCalledWith(release);
});

test('install fallback to download path if user declined system wide install', async () => {
(env.isWindows as boolean) = true;
const release: MinikubeGithubReleaseArtifactMetadata = {
tag: 'v1.2.3',
id: 55,
label: 'v1.5.2',
};

// refuse system wide installed
vi.mocked(window.showInformationMessage).mockResolvedValue('No');

const minikubeDownload = new MinikubeDownload(extensionContext, octokitMock);

vi.spyOn(minikubeDownload, 'download').mockResolvedValue('/download/asset/path');

const file = await minikubeDownload.install(release);
expect(file).toBe('/download/asset/path');
expect(processCore.exec).not.toHaveBeenCalled();
expect(window.showInformationMessage).toHaveBeenCalledWith(
'minikube binary has been successfully downloaded.\n\nWould you like to install it system-wide for accessibility on the command line? This will require administrative privileges.',
'Yes',
'Cancel',
);
});

test('user should be notified if system-wide installed failed', async () => {
Object.defineProperty(process, 'platform', {
value: 'linux',
});
(env.isLinux as boolean) = true;
const release: MinikubeGithubReleaseArtifactMetadata = {
tag: 'v1.2.3',
id: 55,
label: 'v1.5.2',
};

// refuse system wide installed
vi.mocked(window.showInformationMessage).mockResolvedValue('Yes');
vi.mocked(processCore.exec).mockRejectedValue(new Error('Something horrible'));

const minikubeDownload = new MinikubeDownload(extensionContext, octokitMock);

vi.spyOn(minikubeDownload, 'download').mockResolvedValue('/download/asset/path');

const file = await minikubeDownload.install(release);

expect(file).toBe('/download/asset/path');
expect(processCore.exec).toHaveBeenCalled();
expect(window.showErrorMessage).toHaveBeenCalledWith(
'Something went wrong while trying to install minikube system-wide: Error: Error making binary executable: Error: Something horrible',
);
});
});

describe('findMinikube', () => {
test('should use system wide first', async () => {
(env.isWindows as boolean) = true;
Expand Down
28 changes: 27 additions & 1 deletion src/download.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import type { Octokit } from '@octokit/rest';
import type * as extensionApi from '@podman-desktop/api';
import { env, window } from '@podman-desktop/api';

import { whereBinary } from './util';
import { installBinaryToSystem, whereBinary } from './util';

export interface MinikubeGithubReleaseArtifactMetadata extends extensionApi.QuickPickItem {
tag: string;
Expand Down Expand Up @@ -113,6 +113,32 @@ export class MinikubeDownload {
}
}

/**
* Given a {@link MinikubeGithubReleaseArtifactMetadata} it will be downloaded to the
* extension folder, then on user approval tried to be installed system-wide
* @param release the release to download and install
*/
async install(release: MinikubeGithubReleaseArtifactMetadata): Promise<string> {
let destFile = await this.download(release);

const result = await window.showInformationMessage(
`minikube binary has been successfully downloaded.\n\nWould you like to install it system-wide for accessibility on the command line? This will require administrative privileges.`,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe add the version of minikube in the text

'Yes',
'Cancel',
);

if (result !== 'Yes') return destFile;

try {
destFile = await installBinaryToSystem(destFile, 'minikube');
} catch (err: unknown) {
console.error(err);
await window.showErrorMessage(`Something went wrong while trying to install minikube system-wide: ${err}`);
// we yet return the extension-folder path to still allow the binary downloaded to be registered by the extension
}
return destFile;
}

// Download minikube from the artifact metadata: MinikubeGithubReleaseArtifactMetadata
// this will download it to the storage bin folder as well as make it executable
// return the path where the file has been downloaded
Expand Down