Skip to content

Commit 5ba894d

Browse files
authored
feat: adding find minikube util method (#171)
* feat: adding find minikube util method Signed-off-by: axel7083 <[email protected]> * fix: moving findMinikube to MinikubeDownload class Signed-off-by: axel7083 <[email protected]> --------- Signed-off-by: axel7083 <[email protected]>
1 parent c700dd4 commit 5ba894d

File tree

4 files changed

+73
-3
lines changed

4 files changed

+73
-3
lines changed

src/download.spec.ts

+46-2
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,15 @@ import { beforeEach } from 'node:test';
2222

2323
import type { Octokit } from '@octokit/rest';
2424
import type * as extensionApi from '@podman-desktop/api';
25-
import { env } from '@podman-desktop/api';
25+
import { env, process as processCore } from '@podman-desktop/api';
2626
import { afterEach, describe, expect, test, vi } from 'vitest';
2727

2828
import type { MinikubeGithubReleaseArtifactMetadata } from './download';
2929
import { MinikubeDownload } from './download';
3030

3131
// Create the OS class as well as fake extensionContext
3232
const extensionContext: extensionApi.ExtensionContext = {
33-
storagePath: '/fake/path',
33+
storagePath: '/extension-folder/',
3434
subscriptions: [],
3535
} as unknown as extensionApi.ExtensionContext;
3636

@@ -56,6 +56,9 @@ vi.mock('@podman-desktop/api', () => ({
5656
isLinux: false,
5757
isMac: false,
5858
},
59+
process: {
60+
exec: vi.fn(),
61+
},
5962
}));
6063

6164
const listReleaseAssetsMock = vi.fn();
@@ -157,3 +160,44 @@ describe('getMinikubeExtensionPath', () => {
157160
expect(minikubeDownload.getMinikubeExtensionPath()).toStrictEqual(expect.stringMatching('.*\\minikube$'));
158161
});
159162
});
163+
164+
describe('findMinikube', () => {
165+
test('should use system wide first', async () => {
166+
(env.isWindows as boolean) = true;
167+
168+
const minikubeDownload = new MinikubeDownload(extensionContext, octokitMock);
169+
170+
vi.mocked(processCore.exec).mockResolvedValue({
171+
stdout: '/dummy/tmp/minikube',
172+
stderr: '',
173+
command: '',
174+
});
175+
176+
const result = await minikubeDownload.findMinikube();
177+
expect(result).toBe('/dummy/tmp/minikube');
178+
});
179+
180+
test('system wide missing should fallback to local extension folder', async () => {
181+
(env.isWindows as boolean) = false;
182+
vi.mocked(processCore.exec).mockRejectedValue(new Error('dummy error'));
183+
184+
const minikubeDownload = new MinikubeDownload(extensionContext, octokitMock);
185+
186+
vi.mocked(fs.existsSync).mockReturnValue(true);
187+
188+
const result = await minikubeDownload.findMinikube();
189+
expect(result).toStrictEqual(expect.stringContaining('extension-folder'));
190+
});
191+
192+
test('system wide missing should fallback to local extension folder', async () => {
193+
(env.isWindows as boolean) = false;
194+
vi.mocked(processCore.exec).mockRejectedValue(new Error('dummy error'));
195+
196+
const minikubeDownload = new MinikubeDownload(extensionContext, octokitMock);
197+
198+
vi.mocked(fs.existsSync).mockReturnValue(false);
199+
200+
const result = await minikubeDownload.findMinikube();
201+
expect(result).toBeUndefined();
202+
});
203+
});

src/download.ts

+18
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ import type { Octokit } from '@octokit/rest';
2525
import type * as extensionApi from '@podman-desktop/api';
2626
import { env } from '@podman-desktop/api';
2727

28+
import { whereBinary } from './util';
29+
2830
export interface MinikubeGithubReleaseArtifactMetadata {
2931
tag: string;
3032
id: number;
@@ -74,6 +76,22 @@ export class MinikubeDownload {
7476
return path.resolve(this.extensionContext.storagePath, `minikube${fileExtension}`);
7577
}
7678

79+
/**
80+
* search if minikube is available in the path or in the extension folder
81+
*/
82+
async findMinikube(): Promise<string | undefined> {
83+
try {
84+
return await whereBinary('minikube');
85+
} catch (err: unknown) {
86+
console.debug(err);
87+
}
88+
89+
const extensionPath = this.getMinikubeExtensionPath();
90+
if (fs.existsSync(extensionPath)) {
91+
return extensionPath;
92+
}
93+
}
94+
7795
// Download minikube from the artifact metadata: MinikubeGithubReleaseArtifactMetadata
7896
// this will download it to the storage bin folder as well as make it executable
7997
// return the path where the file has been downloaded

src/minikube-installer.ts

+3
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ const MACOS_X64_ASSET_NAME = 'minikube-darwin-amd64';
5656

5757
const MACOS_ARM64_ASSET_NAME = 'minikube-darwin-arm64';
5858

59+
/**
60+
* @deprecated use {@link MinikubeDownload} instead
61+
*/
5962
export class MinikubeInstaller {
6063
private assetNames = new Map<string, string>();
6164

src/util.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,12 @@ export function getMinikubeHome(): string | undefined {
5151
}
5252
}
5353

54-
// search if minikube is available in the path
54+
/**
55+
* search if minikube is available in the path
56+
* @param pathAddition
57+
* @param installer
58+
* @deprecated use {@link MinikubeDownload#findMinikube}
59+
*/
5560
export async function detectMinikube(pathAddition: string, installer: MinikubeInstaller): Promise<string> {
5661
try {
5762
await extensionApi.process.exec('minikube', ['version'], {

0 commit comments

Comments
 (0)