diff --git a/extensions/podman/packages/extension/src/extension.spec.ts b/extensions/podman/packages/extension/src/extension.spec.ts index c2bc55e48aba2..f2ca1a786b3f3 100644 --- a/extensions/podman/packages/extension/src/extension.spec.ts +++ b/extensions/podman/packages/extension/src/extension.spec.ts @@ -41,7 +41,7 @@ import * as podmanCli from './podman-cli'; import { PodmanConfiguration } from './podman-configuration'; import type { UpdateCheck } from './podman-install'; import { PodmanInstall } from './podman-install'; -import { getAssetsFolder, isLinux, isMac, LIBKRUN_LABEL, LoggerDelegator, VMTYPE } from './util'; +import { getAssetsFolder, isMac, LIBKRUN_LABEL, LoggerDelegator, VMTYPE } from './util'; import * as util from './util'; const config: Configuration = { @@ -232,9 +232,9 @@ vi.mock('@podman-desktop/api', async () => { }, env: { createTelemetryLogger: vi.fn(), - isWindows: (): (() => boolean) => vi.fn(), - isMac: (): (() => boolean) => vi.fn(), - isLinux: (): (() => boolean) => vi.fn(), + isWindows: false, + isMac: false, + isLinux: false, }, containerEngine: { info: vi.fn(), @@ -322,7 +322,6 @@ vi.mock('./util', async () => { ...actual, isMac: vi.fn(), isWindows: vi.fn(), - isLinux: vi.fn(), getAssetsFolder: vi.fn(), }; }); @@ -332,7 +331,7 @@ beforeEach(() => { console.error = consoleErrorMock; (extensionApi.env.isMac as boolean) = true; - (extensionApi.env.isLinux as boolean) = false; + vi.mocked(extensionApi.env).isLinux = false; vi.mocked(extensionApi.env).isWindows = false; const mock = vi.spyOn(compatibilityModeLib, 'getSocketCompatibility'); @@ -1231,7 +1230,7 @@ test('handlecompatibilitymodesetting: disable compatibility called when configur }); test('ensure started machine reports default configuration', async () => { - vi.mocked(isLinux).mockReturnValue(true); + vi.mocked(extensionApi.env).isLinux = true; extension.initExtensionContext({ subscriptions: [] } as unknown as extensionApi.ExtensionContext); vi.spyOn(extensionApi.process, 'exec').mockImplementation( (_command, args) => @@ -1261,7 +1260,7 @@ test('ensure started machine reports default configuration', async () => { test('ensure stopped machine reports stopped provider', async () => { extension.initExtensionContext({ subscriptions: [] } as unknown as extensionApi.ExtensionContext); - vi.mocked(isLinux).mockReturnValue(false); + vi.mocked(extensionApi.env).isLinux = false; vi.mocked(isMac).mockReturnValue(true); vi.spyOn(extensionApi.process, 'exec').mockImplementation( (_command, args) => @@ -1291,7 +1290,7 @@ test('ensure stopped machine reports stopped provider', async () => { test('ensure running and starting machine reports starting provider', async () => { extension.initExtensionContext({ subscriptions: [] } as unknown as extensionApi.ExtensionContext); - vi.mocked(isLinux).mockReturnValue(false); + vi.mocked(extensionApi.env).isLinux = false; vi.mocked(isMac).mockReturnValue(true); vi.spyOn(extensionApi.process, 'exec').mockImplementation( (_command, args) => @@ -1322,7 +1321,7 @@ test('ensure running and starting machine reports starting provider', async () = test('ensure running and not starting machine reports ready provider', async () => { extension.initExtensionContext({ subscriptions: [] } as unknown as extensionApi.ExtensionContext); - vi.mocked(isLinux).mockReturnValue(false); + vi.mocked(extensionApi.env).isLinux = false; vi.mocked(isMac).mockReturnValue(true); vi.spyOn(extensionApi.process, 'exec').mockImplementation( (_command, args) => @@ -1390,7 +1389,7 @@ test('ensure started machine reports configuration', async () => { test('ensure stopped machine reports configuration', async () => { extension.initExtensionContext({ subscriptions: [] } as unknown as extensionApi.ExtensionContext); - vi.mocked(isLinux).mockReturnValue(true); + vi.mocked(extensionApi.env).isLinux = true; vi.spyOn(extensionApi.process, 'exec').mockImplementation( (_command, args) => new Promise(resolve => { @@ -1540,7 +1539,7 @@ test('provider is registered without edit capabilities on Windows', async () => }); test('provider is registered without edit capabilities on Linux', async () => { - vi.mocked(isLinux).mockReturnValue(true); + vi.mocked(extensionApi.env).isLinux = true; extension.initExtensionContext({ subscriptions: [] } as unknown as extensionApi.ExtensionContext); const spyExecPromise = vi.spyOn(extensionApi.process, 'exec'); spyExecPromise.mockImplementation(() => { @@ -1558,19 +1557,19 @@ test('provider is registered without edit capabilities on Linux', async () => { }); test('checkDisguisedPodmanSocket: does not run updateWarnings when called with Linux', async () => { - vi.mocked(isLinux).mockReturnValue(true); + vi.mocked(extensionApi.env).isLinux = true; await checkDisguisedPodmanSocket(provider); expect(updateWarningsMock).not.toBeCalled(); }); test('checkDisguisedPodmanSocket: runs updateWarnings when called not on Linux', async () => { - vi.mocked(isLinux).mockReturnValue(false); + vi.mocked(extensionApi.env).isLinux = false; await checkDisguisedPodmanSocket(provider); expect(updateWarningsMock).toBeCalled(); }); test('Even with getJSONMachineList erroring, do not show setup notification on Linux', async () => { - vi.mocked(isLinux).mockReturnValue(true); + vi.mocked(extensionApi.env).isLinux = true; vi.spyOn(extensionApi.process, 'exec').mockRejectedValue({ name: 'name', message: 'description', @@ -1581,7 +1580,7 @@ test('Even with getJSONMachineList erroring, do not show setup notification on L }); test('If machine list is empty, do not show setup notification on Linux', async () => { - vi.mocked(isLinux).mockReturnValue(true); + vi.mocked(extensionApi.env).isLinux = true; const spyExecPromise = vi.spyOn(extensionApi.process, 'exec'); spyExecPromise.mockResolvedValue({ stdout: '[]' } as extensionApi.RunResult); await extension.updateMachines(provider, podmanConfiguration); @@ -2471,7 +2470,7 @@ describe('sendTelemetryRecords', () => { } as Record, false, ); - (extensionApi.env.isLinux as boolean) = true; + vi.mocked(extensionApi.env).isLinux = true; (extensionApi.env.isMac as boolean) = false; vi.mocked(extensionApi.env).isWindows = false; @@ -2644,7 +2643,7 @@ async function testAudit(path: string, uri: string, condition: typeof expect | t test('activate on mac register commands for setting compatibility moide ', async () => { vi.mocked(isMac).mockReturnValue(true); vi.mocked(extensionApi.env).isWindows = false; - vi.mocked(isLinux).mockReturnValue(false); + vi.mocked(extensionApi.env).isLinux = false; vi.spyOn(PodmanInstall.prototype, 'checkForUpdate').mockResolvedValue({ hasUpdate: false, } as unknown as UpdateCheck); @@ -2726,7 +2725,7 @@ describe.each(['windows', 'mac', 'linux'])('podman machine properties audit on % beforeEach(() => { vi.mocked(extensionApi.env).isWindows = os === 'windows'; vi.mocked(util.isMac).mockReturnValue(os === 'mac'); - vi.mocked(util.isLinux).mockReturnValue(os === 'linux'); + vi.mocked(extensionApi.env).isLinux = os === 'windows'; }); if (os === 'linux') { test('is not used', async () => { @@ -3112,7 +3111,7 @@ describe('connectionAuditor', () => { test('activate and autostart should not duplicate machines ', async () => { vi.mocked(isMac).mockReturnValue(true); vi.mocked(extensionApi.env).isWindows = false; - vi.mocked(isLinux).mockReturnValue(false); + vi.mocked(extensionApi.env).isLinux = false; vi.spyOn(PodmanInstall.prototype, 'checkForUpdate').mockResolvedValue({ hasUpdate: false, } as unknown as UpdateCheck); diff --git a/extensions/podman/packages/extension/src/extension.ts b/extensions/podman/packages/extension/src/extension.ts index 47105f791b702..745c4ee36be6f 100644 --- a/extensions/podman/packages/extension/src/extension.ts +++ b/extensions/podman/packages/extension/src/extension.ts @@ -50,7 +50,6 @@ import { getAssetsFolder, getProviderByLabel, getProviderLabel, - isLinux, isMac, LoggerDelegator, VMTYPE, @@ -71,8 +70,8 @@ let autoMachineStarted = false; let autoMachineName: string | undefined; // System default notifier -let defaultMachineNotify = !isLinux(); -let defaultConnectionNotify = !isLinux(); +let defaultMachineNotify = !extensionApi.env.isLinux; +let defaultConnectionNotify = !extensionApi.env.isLinux; let defaultMachineMonitor = true; // current status of machines @@ -202,7 +201,7 @@ export async function updateMachines( // Only on macOS and Windows should we show the setup notification // if for some reason doing getJSONMachineList fails.. - if (shouldNotifySetup && !isLinux()) { + if (shouldNotifySetup && !extensionApi.env.isLinux) { // push setup notification notifySetupPodman(); shouldNotifySetup = false; @@ -224,7 +223,7 @@ export async function updateMachines( } // invalid machines is not making the provider working properly so always notify - if (shouldCleanMachine && shouldNotifySetup && !isLinux()) { + if (shouldCleanMachine && shouldNotifySetup && !extensionApi.env.isLinux) { // push setup notification notifySetupPodman(); shouldNotifySetup = false; @@ -234,7 +233,7 @@ export async function updateMachines( // Only show the notification on macOS and Windows // as Podman is already installed on Linux and machine is OPTIONAL. - if (shouldNotifySetup && machines.length === 0 && !isLinux()) { + if (shouldNotifySetup && machines.length === 0 && !extensionApi.env.isLinux) { // push setup notification notifySetupPodman(); shouldNotifySetup = false; @@ -243,7 +242,7 @@ export async function updateMachines( // if there is at least one machine whihc does not need to be cleaned and the OS is not Linux // podman is correctly setup so if there is an old notification asking the user to take action // we dispose it as not needed anymore - if (!shouldCleanMachine && machines.length > 0 && !isLinux()) { + if (!shouldCleanMachine && machines.length > 0 && !extensionApi.env.isLinux) { notificationDisposable?.dispose(); shouldNotifySetup = true; } @@ -350,7 +349,7 @@ export async function updateMachines( if (!socketPath) { if (isMac()) { socketPath = calcMacosSocketPath(machineName); - } else if (isLinux()) { + } else if (extensionApi.env.isLinux) { socketPath = calcLinuxSocketPath(machineName); } else if (extensionApi.env.isWindows) { socketPath = calcWinPipeName(machineName); @@ -376,7 +375,7 @@ export async function updateMachines( // we will update the provider as being 'installed', or ready / starting / configured if there is a machine // if we are on Linux, ignore this as podman machine is OPTIONAL and the provider status in Linux is based upon // the native podman installation / not machine. - if (!isLinux()) { + if (!extensionApi.env.isLinux) { if (machines.length === 0) { if (provider.status !== 'configuring') { provider.updateStatus('installed'); @@ -720,7 +719,7 @@ async function monitorProvider(provider: extensionApi.Provider): Promise { // if podman is not installed and the OS is linux we show the podman onboarding notification (if it has not been shown earlier) // this should be limited to Linux as in other OSes the onboarding workflow is enabled based on the podman machine existance // and the notification is handled by checking the machine - if (isLinux() && shouldNotifySetup) { + if (extensionApi.env.isLinux && shouldNotifySetup) { // push setup notification notifySetupPodman(); shouldNotifySetup = false; @@ -734,7 +733,7 @@ async function monitorProvider(provider: extensionApi.Provider): Promise { extensionApi.context.setValue('podmanIsNotInstalled', false, 'onboarding'); // if podman has been installed, we reset the notification flag so if podman is uninstalled in future we can show the notification again - if (isLinux()) { + if (extensionApi.env.isLinux) { shouldNotifySetup = true; // notification is no more required notificationDisposable?.dispose(); @@ -1579,7 +1578,7 @@ export async function start( // Below is Linux specific code: // * Monitors the system service for an unlimited time // * Uses the native system socket - if (isLinux()) { + if (extensionApi.env.isLinux) { // on Linux, need to run the system service for unlimited time let command = 'podman'; let args = ['system', 'service', '--time=0']; @@ -1903,7 +1902,9 @@ export function isRootfulMachineInitSupported(podmanVersion: string): boolean { const PODMAN_MINIMUM_VERSION_FOR_NEW_SOCKET_LOCATION = '4.5.0'; export function isPodmanSocketLocationMoved(podmanVersion: string): boolean { - return isLinux() && compareVersions(podmanVersion, PODMAN_MINIMUM_VERSION_FOR_NEW_SOCKET_LOCATION) >= 0; + return ( + extensionApi.env.isLinux && compareVersions(podmanVersion, PODMAN_MINIMUM_VERSION_FOR_NEW_SOCKET_LOCATION) >= 0 + ); } const PODMAN_MINIMUM_VERSION_FOR_USER_MODE_NETWORKING = '4.6.0'; @@ -2218,7 +2219,7 @@ function setupDisguisedPodmanSocketWatcher( }); let socketWatcher: extensionApi.FileSystemWatcher | undefined = undefined; - if (isLinux() || isMac()) { + if (extensionApi.env.isLinux || isMac()) { socketWatcher = extensionApi.fs.createFileSystemWatcher(socketFile); } @@ -2261,7 +2262,7 @@ export async function checkDisguisedPodmanSocket(provider: extensionApi.Provider // NOTE: LINUX SUPPORT // Linux does not support compatibility mode button, so do not sending the warning - if (!isLinux()) { + if (!extensionApi.env.isLinux) { const retrievedWarnings = isDisguisedPodmanSocket ? [] : [getDisguisedPodmanInformation()]; provider.updateWarnings(retrievedWarnings); } diff --git a/extensions/podman/packages/extension/src/podman-configuration.ts b/extensions/podman/packages/extension/src/podman-configuration.ts index 2a979b0688bfc..49fae416258d5 100644 --- a/extensions/podman/packages/extension/src/podman-configuration.ts +++ b/extensions/podman/packages/extension/src/podman-configuration.ts @@ -24,7 +24,7 @@ import type { ProxySettings } from '@podman-desktop/api'; import * as extensionApi from '@podman-desktop/api'; import * as toml from 'smol-toml'; -import { isLinux, isMac } from './util'; +import { isMac } from './util'; const configurationRosetta = 'setting.rosetta'; @@ -321,7 +321,7 @@ export class PodmanConfiguration { podmanConfigContainersPath = path.resolve(os.homedir(), '.config', 'containers'); } else if (extensionApi.env.isWindows) { podmanConfigContainersPath = path.resolve(os.homedir(), 'AppData', 'Roaming', 'containers'); - } else if (isLinux()) { + } else if (extensionApi.env.isLinux) { const xdgRuntimeDirectory = process.env['XDG_RUNTIME_DIR'] ?? ''; podmanConfigContainersPath = path.resolve(xdgRuntimeDirectory, 'containers'); } diff --git a/extensions/podman/packages/extension/src/registry-setup.ts b/extensions/podman/packages/extension/src/registry-setup.ts index bb6a0fb9bec76..f254b705af527 100644 --- a/extensions/podman/packages/extension/src/registry-setup.ts +++ b/extensions/podman/packages/extension/src/registry-setup.ts @@ -22,7 +22,7 @@ import * as path from 'node:path'; import * as extensionApi from '@podman-desktop/api'; -import { isLinux, isMac } from './util'; +import { isMac } from './util'; export type ContainerAuthConfigEntry = { [key: string]: { @@ -43,7 +43,7 @@ export class RegistrySetup { if (isMac() || extensionApi.env.isWindows) { podmanConfigContainersPath = path.resolve(os.homedir(), '.config/containers'); - } else if (isLinux()) { + } else if (extensionApi.env.isLinux) { const xdgRuntimeDirectory = process.env['XDG_RUNTIME_DIR'] ?? ''; podmanConfigContainersPath = path.resolve(xdgRuntimeDirectory, 'containers'); } diff --git a/extensions/podman/packages/extension/src/util.ts b/extensions/podman/packages/extension/src/util.ts index 2be98b6002f29..2f0fb85e641cb 100644 --- a/extensions/podman/packages/extension/src/util.ts +++ b/extensions/podman/packages/extension/src/util.ts @@ -27,10 +27,6 @@ const mac = os.platform() === 'darwin'; export function isMac(): boolean { return mac; } -const linux = os.platform() === 'linux'; -export function isLinux(): boolean { - return linux; -} const xdgDataDirectory = '.local/share/containers'; export function appHomeDir(): string { return xdgDataDirectory + '/podman';