From 0c630517ce547e9bb71edda70f4081728f1e361a Mon Sep 17 00:00:00 2001 From: lstocchi Date: Fri, 2 Aug 2024 15:28:07 +0200 Subject: [PATCH] fix: clean + test Signed-off-by: lstocchi --- src/crc-setup.ts | 3 +-- src/crc-start.spec.ts | 5 +++-- src/extension.ts | 5 ++--- src/log-provider.ts | 1 - src/util.spec.ts | 43 +++++++++++++++++++++++++++++++++++++++++++ src/util.ts | 2 +- 6 files changed, 50 insertions(+), 9 deletions(-) create mode 100644 src/util.spec.ts diff --git a/src/crc-setup.ts b/src/crc-setup.ts index b2f0fa24..87ea2052 100644 --- a/src/crc-setup.ts +++ b/src/crc-setup.ts @@ -25,12 +25,11 @@ export async function needSetup(): Promise { await execPromise(getCrcCli(), ['setup', '--check-only']); return false; } catch (e) { - console.log(e) + console.log(e); return true; } } -// eslint-disable-next-line @typescript-eslint/no-unused-vars export async function setUpCrc(askForPreset = false): Promise { if (askForPreset) { const preset = await extensionApi.window.showInformationMessage( diff --git a/src/crc-start.spec.ts b/src/crc-start.spec.ts index a024d989..a8ec5ede 100644 --- a/src/crc-start.spec.ts +++ b/src/crc-start.spec.ts @@ -24,6 +24,7 @@ import { startCrc } from './crc-start'; import * as logProvider from './log-provider'; import * as daemon from './daemon-commander'; import type { StartInfo } from './types'; +import { getLoggerCallback } from './util'; vi.mock('@podman-desktop/api', async () => { return { @@ -44,7 +45,7 @@ test('setUpCRC is skipped if already setup, it just perform the daemon start com { updateStatus: vi.fn(), } as unknown as extensionApi.Provider, - {} as extensionApi.Logger, + getLoggerCallback(), { logUsage: vi.fn() } as unknown as extensionApi.TelemetryLogger, ); expect(setUpMock).not.toBeCalled(); @@ -65,7 +66,7 @@ test('set up CRC and then start the daemon', async () => { { updateStatus: vi.fn(), } as unknown as extensionApi.Provider, - {} as extensionApi.Logger, + getLoggerCallback(), { logUsage: vi.fn() } as unknown as extensionApi.TelemetryLogger, ); expect(setUpMock).toBeCalled(); diff --git a/src/extension.ts b/src/extension.ts index 01964e29..2a693393 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -30,7 +30,6 @@ import { CrcInstall } from './install/crc-install'; import { crcStatus } from './crc-status'; import { startCrc } from './crc-start'; -import { needSetup, setUpCrc } from './crc-setup'; import { deleteCrc, registerDeleteCommand } from './crc-delete'; import { presetChangedEvent, saveConfig, syncPreferences } from './preferences'; import { stopCrc } from './crc-stop'; @@ -332,9 +331,9 @@ function registerOpenShiftLocalCluster( start: async (ctx, logger) => { provider.updateStatus('starting'); try { - await startCrc(provider, getLoggerCallback(ctx, logger,), telemetryLogger); + await startCrc(provider, getLoggerCallback(ctx, logger), telemetryLogger); } catch (e) { - logger?.error(e); + logger.error(e); throw e; } }, diff --git a/src/log-provider.ts b/src/log-provider.ts index d567a808..6cb25138 100644 --- a/src/log-provider.ts +++ b/src/log-provider.ts @@ -16,7 +16,6 @@ * SPDX-License-Identifier: Apache-2.0 ***********************************************************************/ -import type { Logger } from '@podman-desktop/api'; import type { DaemonCommander } from './daemon-commander'; import { commander } from './daemon-commander'; diff --git a/src/util.spec.ts b/src/util.spec.ts new file mode 100644 index 00000000..74ed5680 --- /dev/null +++ b/src/util.spec.ts @@ -0,0 +1,43 @@ +/********************************************************************** + * Copyright (C) 2024 Red Hat, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + ***********************************************************************/ + +import type * as extensionApi from '@podman-desktop/api'; +import { expect, test, vi } from 'vitest'; +import { getLoggerCallback } from './util'; + +test('check logger passed to getLoggerCallback is actually called with data', async () => { + const logMock = vi.fn(); + const logger = { + log: logMock, + } as unknown as extensionApi.Logger; + const callback = getLoggerCallback(undefined, logger); + callback('data'); + expect(logMock).toBeCalledWith('data'); +}); + +test('check logger passed to getLoggerCallback is actually called with data', async () => { + const logMock = vi.fn(); + const context = { + log: { + log: logMock, + }, + } as unknown as extensionApi.LifecycleContext; + const callback = getLoggerCallback(context); + callback('data2'); + expect(logMock).toBeCalledWith('data2'); +}); \ No newline at end of file diff --git a/src/util.ts b/src/util.ts index b94e1a13..f688ac7d 100644 --- a/src/util.ts +++ b/src/util.ts @@ -20,7 +20,7 @@ import * as os from 'node:os'; import { spawn } from 'node:child_process'; import * as fs from 'node:fs/promises'; import type { Preset } from './types'; -import { LifecycleContext, Logger } from '@podman-desktop/api'; +import type { LifecycleContext, Logger } from '@podman-desktop/api'; export const productName = 'OpenShift Local'; export const defaultPreset: Preset = 'openshift';