|
20 | 20 |
|
21 | 21 | import * as extensionApi from '@podman-desktop/api';
|
22 | 22 | import { afterEach, beforeEach, expect, test, vi } from 'vitest';
|
23 |
| -import { detectMinikube, getMinikubePath, installBinaryToSystem, runCliCommand } from './util'; |
24 |
| -import * as childProcess from 'node:child_process'; |
| 23 | +import { detectMinikube, getMinikubePath, installBinaryToSystem } from './util'; |
25 | 24 | import type { MinikubeInstaller } from './minikube-installer';
|
26 | 25 | import * as fs from 'node:fs';
|
27 | 26 | import * as path from 'node:path';
|
@@ -86,126 +85,27 @@ test('getMinikubePath on macOS with existing PATH', async () => {
|
86 | 85 | expect(computedPath).toEqual(`${existingPATH}:/usr/local/bin:/opt/homebrew/bin:/opt/local/bin:/opt/podman/bin`);
|
87 | 86 | });
|
88 | 87 |
|
89 |
| -test.each([ |
90 |
| - ['macOS', true, false], |
91 |
| - ['windows', false, true], |
92 |
| -])('detectMinikube on %s', async (operatingSystem, isMac, isWindows) => { |
93 |
| - vi.mocked(extensionApi.env).isMac = isMac; |
94 |
| - vi.mocked(extensionApi.env).isWindows = isWindows; |
95 |
| - |
96 |
| - // spy on runCliCommand |
97 |
| - const spawnSpy = vi.spyOn(childProcess, 'spawn'); |
98 |
| - |
99 |
| - const onEventMock = vi.fn(); |
100 |
| - |
101 |
| - onEventMock.mockImplementation((event: string, callback: (data: string) => void) => { |
102 |
| - // delay execution |
103 |
| - if (event === 'close') { |
104 |
| - setTimeout(() => { |
105 |
| - callback(0 as unknown as string); |
106 |
| - }, 500); |
107 |
| - } |
108 |
| - }); |
109 |
| - |
110 |
| - spawnSpy.mockReturnValue({ |
111 |
| - on: onEventMock, |
112 |
| - stdout: { setEncoding: vi.fn(), on: vi.fn() }, |
113 |
| - stderr: { setEncoding: vi.fn(), on: vi.fn() }, |
114 |
| - } as unknown as childProcess.ChildProcessWithoutNullStreams); |
115 |
| - |
| 88 | +test('detectMinikube', async () => { |
116 | 89 | const fakeMinikubeInstaller = {
|
117 | 90 | getAssetInfo: vi.fn(),
|
118 | 91 | } as unknown as MinikubeInstaller;
|
119 | 92 |
|
| 93 | + const execMock = vi.spyOn(extensionApi.process, 'exec').mockResolvedValue({ |
| 94 | + command: '', |
| 95 | + stderr: '', |
| 96 | + stdout: '', |
| 97 | + }); |
| 98 | + |
120 | 99 | const result = await detectMinikube('', fakeMinikubeInstaller);
|
121 | 100 | expect(result).toEqual('minikube');
|
122 | 101 |
|
123 | 102 | // expect not called getAssetInfo
|
124 | 103 | expect(fakeMinikubeInstaller.getAssetInfo).not.toBeCalled();
|
125 | 104 |
|
126 |
| - expect(spawnSpy).toBeCalled(); |
| 105 | + expect(execMock).toBeCalled(); |
127 | 106 | // expect right parameters
|
128 |
| - if (isMac) { |
129 |
| - expect(spawnSpy.mock.calls[0][0]).toEqual('minikube'); |
130 |
| - } else if (isWindows) { |
131 |
| - expect(spawnSpy.mock.calls[0][0]).toEqual('"minikube"'); |
132 |
| - } |
133 |
| - expect(spawnSpy.mock.calls[0][1]).toEqual(['version']); |
134 |
| -}); |
135 |
| - |
136 |
| -test('runCliCommand/killProcess on macOS', async () => { |
137 |
| - vi.mocked(extensionApi.env).isMac = true; |
138 |
| - vi.mocked(extensionApi.env).isWindows = false; |
139 |
| - |
140 |
| - // spy on runCliCommand |
141 |
| - const spawnSpy = vi.spyOn(childProcess, 'spawn'); |
142 |
| - |
143 |
| - const killMock = vi.fn(); |
144 |
| - |
145 |
| - spawnSpy.mockReturnValue({ |
146 |
| - kill: killMock, |
147 |
| - on: vi.fn(), |
148 |
| - stdout: { setEncoding: vi.fn(), on: vi.fn() }, |
149 |
| - stderr: { setEncoding: vi.fn(), on: vi.fn() }, |
150 |
| - } as unknown as childProcess.ChildProcessWithoutNullStreams); |
151 |
| - |
152 |
| - const fakeToken = { |
153 |
| - onCancellationRequested: vi.fn(), |
154 |
| - } as unknown as extensionApi.CancellationToken; |
155 |
| - |
156 |
| - vi.mocked(fakeToken.onCancellationRequested).mockImplementation((callback: any): extensionApi.Disposable => { |
157 |
| - // abort execution after 500ms |
158 |
| - setTimeout(() => { |
159 |
| - callback(); |
160 |
| - }, 500); |
161 |
| - |
162 |
| - return extensionApi.Disposable.from({ dispose: vi.fn() }); |
163 |
| - }); |
164 |
| - |
165 |
| - await expect(runCliCommand('fooCommand', [], undefined, fakeToken)).rejects.toThrow('Execution cancelled'); |
166 |
| - |
167 |
| - expect(spawnSpy.mock.calls[0][0]).toEqual('fooCommand'); |
168 |
| - |
169 |
| - expect(killMock).toBeCalled(); |
170 |
| -}); |
171 |
| - |
172 |
| -test('runCliCommand/killProcess on Windows', async () => { |
173 |
| - vi.mocked(extensionApi.env).isMac = false; |
174 |
| - vi.mocked(extensionApi.env).isWindows = true; |
175 |
| - |
176 |
| - // spy on runCliCommand |
177 |
| - const spawnSpy = vi.spyOn(childProcess, 'spawn'); |
178 |
| - |
179 |
| - const killMock = vi.fn(); |
180 |
| - |
181 |
| - spawnSpy.mockReturnValue({ |
182 |
| - kill: killMock, |
183 |
| - pid: 'pid123', |
184 |
| - on: vi.fn(), |
185 |
| - stdout: { setEncoding: vi.fn(), on: vi.fn() }, |
186 |
| - stderr: { setEncoding: vi.fn(), on: vi.fn() }, |
187 |
| - } as unknown as childProcess.ChildProcessWithoutNullStreams); |
188 |
| - |
189 |
| - const fakeToken = { |
190 |
| - onCancellationRequested: vi.fn(), |
191 |
| - } as unknown as extensionApi.CancellationToken; |
192 |
| - |
193 |
| - vi.mocked(fakeToken.onCancellationRequested).mockImplementation((callback: any): extensionApi.Disposable => { |
194 |
| - // abort execution after 500ms |
195 |
| - setTimeout(() => { |
196 |
| - callback(); |
197 |
| - }, 500); |
198 |
| - |
199 |
| - return extensionApi.Disposable.from({ dispose: vi.fn() }); |
200 |
| - }); |
201 |
| - |
202 |
| - await expect(runCliCommand('fooCommand', [], undefined, fakeToken)).rejects.toThrow('Execution cancelled'); |
203 |
| - |
204 |
| - expect(spawnSpy.mock.calls[0][0]).toEqual('"fooCommand"'); |
205 |
| - // on windows we don't use killProcess but run taskkill |
206 |
| - expect(killMock).not.toBeCalled(); |
207 |
| - |
208 |
| - expect(spawnSpy.mock.calls[1]).toEqual(['taskkill', ['/pid', 'pid123', '/f', '/t']]); |
| 107 | + expect(execMock.mock.calls[0][0]).toEqual('minikube'); |
| 108 | + expect(execMock.mock.calls[0][1]).toEqual(['version']); |
209 | 109 | });
|
210 | 110 |
|
211 | 111 | test('error: expect installBinaryToSystem to fail with a non existing binary', async () => {
|
|
0 commit comments