diff --git a/src/common/utils/environment-variables.injectable.ts b/src/common/utils/environment-variables.injectable.ts deleted file mode 100644 index 899d012f78..0000000000 --- a/src/common/utils/environment-variables.injectable.ts +++ /dev/null @@ -1,27 +0,0 @@ -/** - * Copyright (c) OpenLens Authors. All rights reserved. - * Licensed under MIT License. See LICENSE in root directory for more information. - */ -import { getInjectable } from "@ogre-tools/injectable"; - -const environmentVariablesInjectable = getInjectable({ - id: "environment-variables", - - instantiate: () => { - const JEST_WORKER_ID = process.env.JEST_WORKER_ID; - const CICD = process.env.CICD; - - return { - // Compile-time environment variables - JEST_WORKER_ID, - CICD, - - // Runtime environment variables - LENS_DISABLE_GPU: process.env.LENS_DISABLE_GPU, - }; - }, - - causesSideEffects: true, -}); - -export default environmentVariablesInjectable; diff --git a/src/common/utils/get-random-id.global-override-for-injectable.ts b/src/common/utils/get-random-id.global-override-for-injectable.ts new file mode 100644 index 0000000000..a0f87b4180 --- /dev/null +++ b/src/common/utils/get-random-id.global-override-for-injectable.ts @@ -0,0 +1,9 @@ +/** + * Copyright (c) OpenLens Authors. All rights reserved. + * Licensed under MIT License. See LICENSE in root directory for more information. + */ + +import { getGlobalOverride } from "../test-utils/get-global-override"; +import getRandomIdInjectable from "./get-random-id.injectable"; + +export default getGlobalOverride(getRandomIdInjectable, () => () => "some-irrelevant-random-id"); diff --git a/src/common/vars/lens-resources-dir.global-override-for-injectable.ts b/src/common/vars/lens-resources-dir.global-override-for-injectable.ts new file mode 100644 index 0000000000..1a72b0ccf7 --- /dev/null +++ b/src/common/vars/lens-resources-dir.global-override-for-injectable.ts @@ -0,0 +1,9 @@ +/** + * Copyright (c) OpenLens Authors. All rights reserved. + * Licensed under MIT License. See LICENSE in root directory for more information. + */ + +import { getGlobalOverride } from "../test-utils/get-global-override"; +import lensResourcesDirInjectable from "./lens-resources-dir.injectable"; + +export default getGlobalOverride(lensResourcesDirInjectable, () => "/irrelavent-dir-for-lens-resources"); diff --git a/src/common/vars/normalized-platform-architecture.injectable.ts b/src/common/vars/normalized-platform-architecture.injectable.ts index c2053d2d8e..6b98856268 100644 --- a/src/common/vars/normalized-platform-architecture.injectable.ts +++ b/src/common/vars/normalized-platform-architecture.injectable.ts @@ -3,11 +3,14 @@ * Licensed under MIT License. See LICENSE in root directory for more information. */ import { getInjectable } from "@ogre-tools/injectable"; +import processArchInjectable from "./process-arch.injectable"; const normalizedPlatformArchitectureInjectable = getInjectable({ id: "normalized-platform-architecture", - instantiate: () => { - switch (process.arch) { + instantiate: (di) => { + const platformArch = di.inject(processArchInjectable); + + switch (platformArch) { case "arm64": return "arm64"; case "x64": @@ -18,10 +21,9 @@ const normalizedPlatformArchitectureInjectable = getInjectable({ case "ia32": return "ia32"; default: - throw new Error(`arch=${process.arch} is unsupported`); + throw new Error(`arch=${platformArch} is unsupported`); } }, - causesSideEffects: true, }); export default normalizedPlatformArchitectureInjectable; diff --git a/src/common/vars/platform.global-override-for-injectable.ts b/src/common/vars/platform.global-override-for-injectable.ts new file mode 100644 index 0000000000..4bb06dec5e --- /dev/null +++ b/src/common/vars/platform.global-override-for-injectable.ts @@ -0,0 +1,9 @@ +/** + * Copyright (c) OpenLens Authors. All rights reserved. + * Licensed under MIT License. See LICENSE in root directory for more information. + */ + +import { getGlobalOverride } from "../test-utils/get-global-override"; +import platformInjectable from "./platform.injectable"; + +export default getGlobalOverride(platformInjectable, () => "darwin"); diff --git a/src/common/vars/platform.injectable.ts b/src/common/vars/platform.injectable.ts index f2c681d657..00d6e42aca 100644 --- a/src/common/vars/platform.injectable.ts +++ b/src/common/vars/platform.injectable.ts @@ -4,7 +4,6 @@ */ import { getInjectable } from "@ogre-tools/injectable"; -// Todo: OCP by creating distinct injectables for platforms. export const allPlatforms = ["win32", "darwin", "linux"] as const; const platformInjectable = getInjectable({ diff --git a/src/common/vars/process-arch.global-override-for-injectable.ts b/src/common/vars/process-arch.global-override-for-injectable.ts new file mode 100644 index 0000000000..42d74d4ec8 --- /dev/null +++ b/src/common/vars/process-arch.global-override-for-injectable.ts @@ -0,0 +1,9 @@ +/** + * Copyright (c) OpenLens Authors. All rights reserved. + * Licensed under MIT License. See LICENSE in root directory for more information. + */ + +import { getGlobalOverride } from "../test-utils/get-global-override"; +import processArchInjectable from "./process-arch.injectable"; + +export default getGlobalOverride(processArchInjectable, () => "x64"); diff --git a/src/common/vars/process-arch.injectable.ts b/src/common/vars/process-arch.injectable.ts new file mode 100644 index 0000000000..5504855341 --- /dev/null +++ b/src/common/vars/process-arch.injectable.ts @@ -0,0 +1,13 @@ +/** + * Copyright (c) OpenLens Authors. All rights reserved. + * Licensed under MIT License. See LICENSE in root directory for more information. + */ +import { getInjectable } from "@ogre-tools/injectable"; + +const processArchInjectable = getInjectable({ + id: "process-arch", + instantiate: () => process.arch, + causesSideEffects: true, +}); + +export default processArchInjectable; diff --git a/src/main/app-paths/directory-for-integration-testing/directory-for-integration-testing.global-override-for-injectable.ts b/src/main/app-paths/directory-for-integration-testing/directory-for-integration-testing.global-override-for-injectable.ts new file mode 100644 index 0000000000..f4e7b5674e --- /dev/null +++ b/src/main/app-paths/directory-for-integration-testing/directory-for-integration-testing.global-override-for-injectable.ts @@ -0,0 +1,9 @@ +/** + * Copyright (c) OpenLens Authors. All rights reserved. + * Licensed under MIT License. See LICENSE in root directory for more information. + */ + +import { getGlobalOverride } from "../../../common/test-utils/get-global-override"; +import directoryForIntegrationTestingInjectable from "./directory-for-integration-testing.injectable"; + +export default getGlobalOverride(directoryForIntegrationTestingInjectable, () => undefined); diff --git a/src/main/app-paths/directory-for-integration-testing/directory-for-integration-testing.injectable.ts b/src/main/app-paths/directory-for-integration-testing/directory-for-integration-testing.injectable.ts index 73647a3270..7a98699fee 100644 --- a/src/main/app-paths/directory-for-integration-testing/directory-for-integration-testing.injectable.ts +++ b/src/main/app-paths/directory-for-integration-testing/directory-for-integration-testing.injectable.ts @@ -3,16 +3,11 @@ * Licensed under MIT License. See LICENSE in root directory for more information. */ import { getInjectable } from "@ogre-tools/injectable"; -import environmentVariablesInjectable from "../../../common/utils/environment-variables.injectable"; const directoryForIntegrationTestingInjectable = getInjectable({ id: "directory-for-integration-testing", - - instantiate: (di) => { - const environmentVariables = di.inject(environmentVariablesInjectable); - - return environmentVariables.CICD; - }, + instantiate: () => process.env.CICD, + causesSideEffects: true, }); export default directoryForIntegrationTestingInjectable; diff --git a/src/main/electron-app/electron-app.global-override-for-injectable.ts b/src/main/electron-app/electron-app.global-override-for-injectable.ts index 5f81e8298b..e225745ca3 100644 --- a/src/main/electron-app/electron-app.global-override-for-injectable.ts +++ b/src/main/electron-app/electron-app.global-override-for-injectable.ts @@ -28,6 +28,17 @@ export default getGlobalOverride(electronAppInjectable, () => { hasSwitch: (key) => chromiumArgs.has(key), removeSwitch: (key) => chromiumArgs.delete(key), }, + disableHardwareAcceleration: () => {}, + requestSingleInstanceLock: () => true, + getLoginItemSettings: () => ({ + executableWillLaunchAtLogin: false, + openAtLogin: false, + openAsHidden: false, + wasOpenedAtLogin: false, + wasOpenedAsHidden: false, + restoreState: false, + launchItems: [], + }), exit: () => {}, } as Partial as Electron.App); diff --git a/src/main/electron-app/features/electron-dialog.global-override-for-injectable.ts b/src/main/electron-app/features/electron-dialog.global-override-for-injectable.ts new file mode 100644 index 0000000000..8ccafcfe7a --- /dev/null +++ b/src/main/electron-app/features/electron-dialog.global-override-for-injectable.ts @@ -0,0 +1,26 @@ +/** + * Copyright (c) OpenLens Authors. All rights reserved. + * Licensed under MIT License. See LICENSE in root directory for more information. + */ + +import { getGlobalOverride } from "../../../common/test-utils/get-global-override"; +import electronDialogInjectable from "./electron-dialog.injectable"; + +export default getGlobalOverride(electronDialogInjectable, () => ({ + showCertificateTrustDialog: async () => {}, + showErrorBox: () => {}, + showMessageBox: async () => ({ + checkboxChecked: false, + response: 0, + }), + showMessageBoxSync: () => 0, + showOpenDialog: async () => ({ + canceled: true, + filePaths: [], + }), + showOpenDialogSync: () => [], + showSaveDialog: async () => ({ + canceled: true, + }), + showSaveDialogSync: () => "", +})); diff --git a/src/main/getDiForUnitTesting.ts b/src/main/getDiForUnitTesting.ts index a0f5bc7620..1698997e36 100644 --- a/src/main/getDiForUnitTesting.ts +++ b/src/main/getDiForUnitTesting.ts @@ -8,38 +8,25 @@ import type { DiContainer, Injectable } from "@ogre-tools/injectable"; import { createContainer, isInjectable, getInjectable } from "@ogre-tools/injectable"; import { Environments, setLegacyGlobalDiForExtensionApi } from "../extensions/as-legacy-globals-for-extension-api/legacy-global-di-for-extension-api"; import spawnInjectable from "./child-process/spawn.injectable"; -import commandLineArgumentsInjectable from "./utils/command-line-arguments.injectable"; import initializeExtensionsInjectable from "./start-main-application/runnables/initialize-extensions.injectable"; -import lensResourcesDirInjectable from "../common/vars/lens-resources-dir.injectable"; -import environmentVariablesInjectable from "../common/utils/environment-variables.injectable"; import setupIpcMainHandlersInjectable from "./electron-app/runnables/setup-ipc-main-handlers/setup-ipc-main-handlers.injectable"; import setupLensProxyInjectable from "./start-main-application/runnables/setup-lens-proxy.injectable"; import setupSyncingOfWeblinksInjectable from "./start-main-application/runnables/setup-syncing-of-weblinks.injectable"; -import stopServicesAndExitAppInjectable from "./stop-services-and-exit-app.injectable"; import setupDeepLinkingInjectable from "./electron-app/runnables/setup-deep-linking.injectable"; -import requestSingleInstanceLockInjectable from "./electron-app/features/request-single-instance-lock.injectable"; -import disableHardwareAccelerationInjectable from "./electron-app/features/disable-hardware-acceleration.injectable"; -import shouldStartHiddenInjectable from "./electron-app/features/should-start-hidden.injectable"; import setupMainWindowVisibilityAfterActivationInjectable from "./electron-app/runnables/setup-main-window-visibility-after-activation.injectable"; import setupDeviceShutdownInjectable from "./electron-app/runnables/setup-device-shutdown.injectable"; import setupApplicationNameInjectable from "./electron-app/runnables/setup-application-name.injectable"; import setupRunnablesBeforeClosingOfApplicationInjectable from "./electron-app/runnables/setup-runnables-before-closing-of-application.injectable"; -import showMessagePopupInjectable from "./electron-app/features/show-message-popup.injectable"; import clusterFramesInjectable from "../common/cluster-frames.injectable"; import type { ClusterFrameInfo } from "../common/cluster-frames"; import { observable, runInAction } from "mobx"; import broadcastMessageInjectable from "../common/ipc/broadcast-message.injectable"; -import platformInjectable from "../common/vars/platform.injectable"; import electronQuitAndInstallUpdateInjectable from "./electron-app/features/electron-quit-and-install-update.injectable"; import electronUpdaterIsActiveInjectable from "./electron-app/features/electron-updater-is-active.injectable"; -import baseBundledBinariesDirectoryInjectable from "../common/vars/base-bundled-binaries-dir.injectable"; import setUpdateOnQuitInjectable from "./electron-app/features/set-update-on-quit.injectable"; -import getRandomIdInjectable from "../common/utils/get-random-id.injectable"; -import normalizedPlatformArchitectureInjectable from "../common/vars/normalized-platform-architecture.injectable"; import waitUntilBundledExtensionsAreLoadedInjectable from "./start-main-application/lens-window/application-window/wait-until-bundled-extensions-are-loaded.injectable"; import { registerMobX } from "@ogre-tools/injectable-extension-for-mobx"; import electronInjectable from "./utils/resolve-system-proxy/electron.injectable"; -import kubectlDownloadingNormalizedArchInjectable from "./kubectl/normalized-arch.injectable"; import initializeClusterManagerInjectable from "./cluster/initialize-manager.injectable"; import type { GlobalOverride } from "../common/test-utils/get-global-override"; import applicationInformationInjectable from "../common/vars/application-information-injectable"; @@ -88,26 +75,16 @@ export function getDiForUnitTesting(opts: { doGeneralOverrides?: boolean } = {}) di.override(electronInjectable, () => ({})); di.override(waitUntilBundledExtensionsAreLoadedInjectable, () => async () => {}); - di.override(getRandomIdInjectable, () => () => "some-irrelevant-random-id"); - di.override(kubectlDownloadingNormalizedArchInjectable, () => "amd64"); - overrideOperatingSystem(di); overrideRunnablesHavingSideEffects(di); overrideElectronFeatures(di); getOverrideFsWithFakes()(di); - di.override(environmentVariablesInjectable, () => ({})); - di.override(commandLineArgumentsInjectable, () => []); - di.override(clusterFramesInjectable, () => observable.map()); - di.override(stopServicesAndExitAppInjectable, () => () => {}); - di.override(lensResourcesDirInjectable, () => "/irrelevant"); - di.override(broadcastMessageInjectable, () => (channel) => { throw new Error(`Tried to broadcast message to channel "${channel}" over IPC without explicit override.`); }); - di.override(baseBundledBinariesDirectoryInjectable, () => "some-bin-directory"); di.override(spawnInjectable, () => () => { return { stderr: { on: jest.fn(), removeAllListeners: jest.fn() }, @@ -136,11 +113,6 @@ const overrideRunnablesHavingSideEffects = (di: DiContainer) => { }); }; -const overrideOperatingSystem = (di: DiContainer) => { - di.override(platformInjectable, () => "darwin"); - di.override(normalizedPlatformArchitectureInjectable, () => "arm64"); -}; - const overrideElectronFeatures = (di: DiContainer) => { [ setupMainWindowVisibilityAfterActivationInjectable, @@ -155,10 +127,6 @@ const overrideElectronFeatures = (di: DiContainer) => { })); }); - di.override(requestSingleInstanceLockInjectable, () => () => true); - di.override(disableHardwareAccelerationInjectable, () => () => {}); - di.override(shouldStartHiddenInjectable, () => false); - di.override(showMessagePopupInjectable, () => () => {}); di.override(electronQuitAndInstallUpdateInjectable, () => () => {}); di.override(setUpdateOnQuitInjectable, () => () => {}); di.override(electronUpdaterIsActiveInjectable, () => false); diff --git a/src/main/kubectl/normalized-arch.injectable.ts b/src/main/kubectl/normalized-arch.injectable.ts index 88ec6b1067..e68d3da304 100644 --- a/src/main/kubectl/normalized-arch.injectable.ts +++ b/src/main/kubectl/normalized-arch.injectable.ts @@ -3,11 +3,14 @@ * Licensed under MIT License. See LICENSE in root directory for more information. */ import { getInjectable } from "@ogre-tools/injectable"; +import processArchInjectable from "../../common/vars/process-arch.injectable"; const kubectlDownloadingNormalizedArchInjectable = getInjectable({ id: "kubectl-downloading-normalized-arch", - instantiate: () => { - switch (process.arch) { + instantiate: (di) => { + const processArch = di.inject(processArchInjectable); + + switch (processArch) { case "arm64": return "arm64"; case "x64": @@ -18,10 +21,9 @@ const kubectlDownloadingNormalizedArchInjectable = getInjectable({ case "ia32": return "386"; default: - throw new Error(`arch=${process.arch} is unsupported`); + throw new Error(`arch=${processArch} is unsupported`); } }, - causesSideEffects: true, }); export default kubectlDownloadingNormalizedArchInjectable; diff --git a/src/main/start-main-application/runnables/setup-hardware-acceleration.injectable.ts b/src/main/start-main-application/runnables/setup-hardware-acceleration.injectable.ts index 905cb9e130..ffcff08434 100644 --- a/src/main/start-main-application/runnables/setup-hardware-acceleration.injectable.ts +++ b/src/main/start-main-application/runnables/setup-hardware-acceleration.injectable.ts @@ -3,15 +3,15 @@ * Licensed under MIT License. See LICENSE in root directory for more information. */ import { getInjectable } from "@ogre-tools/injectable"; -import environmentVariablesInjectable from "../../../common/utils/environment-variables.injectable"; import disableHardwareAccelerationInjectable from "../../electron-app/features/disable-hardware-acceleration.injectable"; +import hardwareAccelerationShouldBeDisabledInjectable from "../../vars/hardware-acceleration-should-be-disabled.injectable"; import { beforeElectronIsReadyInjectionToken } from "../runnable-tokens/before-electron-is-ready-injection-token"; const setupHardwareAccelerationInjectable = getInjectable({ id: "setup-hardware-acceleration", instantiate: (di) => { - const { LENS_DISABLE_GPU: hardwareAccelerationShouldBeDisabled } = di.inject(environmentVariablesInjectable); + const hardwareAccelerationShouldBeDisabled = di.inject(hardwareAccelerationShouldBeDisabledInjectable); const disableHardwareAcceleration = di.inject(disableHardwareAccelerationInjectable); return { diff --git a/src/main/utils/command-line-arguments.global-override-for-injectable.ts b/src/main/utils/command-line-arguments.global-override-for-injectable.ts new file mode 100644 index 0000000000..edd33d824a --- /dev/null +++ b/src/main/utils/command-line-arguments.global-override-for-injectable.ts @@ -0,0 +1,9 @@ +/** + * Copyright (c) OpenLens Authors. All rights reserved. + * Licensed under MIT License. See LICENSE in root directory for more information. + */ + +import { getGlobalOverride } from "../../common/test-utils/get-global-override"; +import commandLineArgumentsInjectable from "./command-line-arguments.injectable"; + +export default getGlobalOverride(commandLineArgumentsInjectable, () => []); diff --git a/src/main/vars/hardware-acceleration-should-be-disabled.global-override-for-injectable.ts b/src/main/vars/hardware-acceleration-should-be-disabled.global-override-for-injectable.ts new file mode 100644 index 0000000000..2f66f9fc18 --- /dev/null +++ b/src/main/vars/hardware-acceleration-should-be-disabled.global-override-for-injectable.ts @@ -0,0 +1,9 @@ +/** + * Copyright (c) OpenLens Authors. All rights reserved. + * Licensed under MIT License. See LICENSE in root directory for more information. + */ + +import { getGlobalOverride } from "../../common/test-utils/get-global-override"; +import hardwareAccelerationShouldBeDisabledInjectable from "./hardware-acceleration-should-be-disabled.injectable"; + +export default getGlobalOverride(hardwareAccelerationShouldBeDisabledInjectable, () => false); diff --git a/src/main/vars/hardware-acceleration-should-be-disabled.injectable.ts b/src/main/vars/hardware-acceleration-should-be-disabled.injectable.ts new file mode 100644 index 0000000000..2801b1051d --- /dev/null +++ b/src/main/vars/hardware-acceleration-should-be-disabled.injectable.ts @@ -0,0 +1,13 @@ +/** + * Copyright (c) OpenLens Authors. All rights reserved. + * Licensed under MIT License. See LICENSE in root directory for more information. + */ +import { getInjectable } from "@ogre-tools/injectable"; + +const hardwareAccelerationShouldBeDisabledInjectable = getInjectable({ + id: "hardware-acceleration-should-be-disabled", + instantiate: () => Boolean(process.env.LENS_DISABLE_GPU), + causesSideEffects: true, +}); + +export default hardwareAccelerationShouldBeDisabledInjectable; diff --git a/src/renderer/getDiForUnitTesting.tsx b/src/renderer/getDiForUnitTesting.tsx index 1c3dde2fe4..ac42796d9b 100644 --- a/src/renderer/getDiForUnitTesting.tsx +++ b/src/renderer/getDiForUnitTesting.tsx @@ -11,16 +11,12 @@ import requestFromChannelInjectable from "./utils/channel/request-from-channel.i import { getOverrideFsWithFakes } from "../test-utils/override-fs-with-fakes"; import terminalSpawningPoolInjectable from "./components/dock/terminal/terminal-spawning-pool.injectable"; import hostedClusterIdInjectable from "./cluster-frame-context/hosted-cluster-id.injectable"; -import lensResourcesDirInjectable from "../common/vars/lens-resources-dir.injectable"; import { runInAction } from "mobx"; import requestAnimationFrameInjectable from "./components/animate/request-animation-frame.injectable"; -import getRandomIdInjectable from "../common/utils/get-random-id.injectable"; -import platformInjectable from "../common/vars/platform.injectable"; import startTopbarStateSyncInjectable from "./components/layout/top-bar/start-state-sync.injectable"; import { registerMobX } from "@ogre-tools/injectable-extension-for-mobx"; import watchHistoryStateInjectable from "./remote-helpers/watch-history-state.injectable"; import legacyOnChannelListenInjectable from "./ipc/legacy-channel-listen.injectable"; -import environmentVariablesInjectable from "../common/utils/environment-variables.injectable"; import type { GlobalOverride } from "../common/test-utils/get-global-override"; import applicationInformationInjectable from "../common/vars/application-information-injectable"; import nodeEnvInjectionToken from "../common/vars/node-env-injection-token"; @@ -65,9 +61,6 @@ export const getDiForUnitTesting = ( di.override(globalOverride.injectable, globalOverride.overridingInstantiate); } - di.override(getRandomIdInjectable, () => () => "some-irrelevant-random-id"); - di.override(platformInjectable, () => "darwin"); - [ startTopbarStateSyncInjectable, ].forEach((injectable) => { @@ -83,8 +76,6 @@ export const getDiForUnitTesting = ( di.override(legacyOnChannelListenInjectable, () => () => noop); di.override(requestAnimationFrameInjectable, () => (callback) => callback()); - di.override(lensResourcesDirInjectable, () => "/irrelevant"); - di.override(environmentVariablesInjectable, () => ({})); di.override(watchHistoryStateInjectable, () => () => () => {}); di.override(requestFromChannelInjectable, () => () => Promise.resolve(undefined as never));