1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00

Abstract even more Electron specifics to make them overridable in unit tests

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>
This commit is contained in:
Janne Savolainen 2022-04-12 08:25:23 +03:00
parent 068f9f67da
commit b76ff351ac
No known key found for this signature in database
GPG Key ID: 8C6CFB2FFFE8F68A
9 changed files with 76 additions and 44 deletions

View File

@ -44,9 +44,8 @@ autoUpdater.logger = {
/**
* starts the automatic update checking
* @param interval milliseconds between interval to check on, defaults to 24h
*/
export const startUpdateChecking = once(function (interval = 1000 * 60 * 60 * 24): void {
export const startUpdateChecking = (isAutoUpdateEnabled : () => boolean) => once(function (interval = 1000 * 60 * 60 * 24): void {
if (!isAutoUpdateEnabled() || isTestEnv) {
return;
}

View File

@ -0,0 +1,18 @@
/**
* 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";
import electronAppInjectable from "./electron-app.injectable";
const getCommandLineSwitchInjectable = getInjectable({
id: "get-command-line-switch",
instantiate: (di) => {
const app = di.inject(electronAppInjectable);
return (name: string) => app.commandLine.getSwitchValue(name);
},
});
export default getCommandLineSwitchInjectable;

View File

@ -42,6 +42,8 @@ import whenSecondInstanceInjectable from "./electron-app/when-second-instance.in
import whenSystemShutdownInjectable from "./electron-app/when-system-shutdown.injectable";
import exitAppInjectable from "./electron-app/exit-app.injectable";
import setApplicationNameInjectable from "./electron-app/set-application-name.injectable";
import getCommandLineSwitchInjectable from "./electron-app/get-command-line-switch.injectable";
import isAutoUpdateEnabledInjectable from "./is-auto-update-enabled.injectable";
export const getDiForUnitTesting = (
{ doGeneralOverrides } = { doGeneralOverrides: false },
@ -77,6 +79,8 @@ export const getDiForUnitTesting = (
di.override(whenSystemShutdownInjectable, () => () => {});
di.override(exitAppInjectable, () => () => {});
di.override(setApplicationNameInjectable, () => () => {});
di.override(getCommandLineSwitchInjectable, () => () => "irrelevant");
di.override(isAutoUpdateEnabledInjectable, () => () => false);
// eslint-disable-next-line unused-imports/no-unused-vars-ts
di.override(extensionsStoreInjectable, () => ({ isEnabled: ({ id, isBundled }) => false }) as ExtensionsStore);

View File

@ -3,11 +3,16 @@
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { getInjectable } from "@ogre-tools/injectable";
import { isAutoUpdateEnabled } from "./app-updater";
import { isPublishConfigured } from "../common/vars";
import { autoUpdater } from "electron-updater";
const isAutoUpdateEnabledInjectable = getInjectable({
id: "is-auto-update-enabled",
instantiate: () => isAutoUpdateEnabled,
instantiate: () => () => {
return autoUpdater.isUpdaterActive() && isPublishConfigured;
},
causesSideEffects: true,
});

View File

@ -1,27 +0,0 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { app } from "electron";
export function mangleProxyEnv() {
const switchValue = app.commandLine.getSwitchValue("proxy-server");
let httpsProxy = process.env.HTTPS_PROXY || process.env.HTTP_PROXY || "";
delete process.env.HTTPS_PROXY;
delete process.env.HTTP_PROXY;
if (switchValue !== "") {
httpsProxy = switchValue;
}
if (httpsProxy !== "") {
process.env.APP_HTTPS_PROXY = httpsProxy;
}
if (app.commandLine.getSwitchValue("proxy-server") !== "") {
process.env.HTTPS_PROXY = app.commandLine.getSwitchValue("proxy-server");
}
}

View File

@ -3,17 +3,39 @@
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { getInjectable } from "@ogre-tools/injectable";
import { mangleProxyEnv } from "../../../proxy-env";
import { beforeApplicationIsReadyInjectionToken } from "../before-application-is-ready-injection-token";
import getCommandLineSwitchInjectable from "../../../electron-app/get-command-line-switch.injectable";
const setupProxyEnvInjectable = getInjectable({
id: "setup-proxy-env",
instantiate: () => ({
run: () => {
mangleProxyEnv();
},
}),
instantiate: (di) => {
const getCommandLineSwitch = di.inject(getCommandLineSwitchInjectable);
return {
run: () => {
const switchValue = getCommandLineSwitch("proxy-server");
let httpsProxy =
process.env.HTTPS_PROXY || process.env.HTTP_PROXY || "";
delete process.env.HTTPS_PROXY;
delete process.env.HTTP_PROXY;
if (switchValue !== "") {
httpsProxy = switchValue;
}
if (httpsProxy !== "") {
process.env.APP_HTTPS_PROXY = httpsProxy;
}
if (getCommandLineSwitch("proxy-server") !== "") {
process.env.HTTPS_PROXY = getCommandLineSwitch("proxy-server");
}
},
};
},
injectionToken: beforeApplicationIsReadyInjectionToken,
});

View File

@ -5,15 +5,21 @@
import { getInjectable } from "@ogre-tools/injectable";
import { onRootFrameRenderInjectionToken } from "../on-root-frame-render-injection-token";
import { startUpdateChecking } from "../../../app-updater";
import isAutoUpdateEnabledInjectable from "../../../is-auto-update-enabled.injectable";
const startUpdateCheckingInjectable = getInjectable({
id: "start-update-checking",
instantiate: () => ({
run: () => {
startUpdateChecking();
},
}),
instantiate: (di) => {
const isAutoUpdateEnabled = di.inject(isAutoUpdateEnabledInjectable);
return {
run: () => {
startUpdateChecking(isAutoUpdateEnabled)();
},
};
},
causesSideEffects: true,

View File

@ -9,6 +9,7 @@ import trayMenuItemsInjectable from "./tray-menu-items.injectable";
import navigateToPreferencesInjectable from "../../common/front-end-routing/routes/preferences/navigate-to-preferences.injectable";
import stopServicesAndExitAppInjectable from "../stop-services-and-exit-app.injectable";
import { getStartableStoppable } from "../../common/utils/get-startable-stoppable";
import isAutoUpdateEnabledInjectable from "../is-auto-update-enabled.injectable";
const trayInjectable = getInjectable({
id: "tray",
@ -18,6 +19,7 @@ const trayInjectable = getInjectable({
const trayMenuItems = di.inject(trayMenuItemsInjectable);
const navigateToPreferences = di.inject(navigateToPreferencesInjectable);
const stopServicesAndExitApp = di.inject(stopServicesAndExitAppInjectable);
const isAutoUpdateEnabled = di.inject(isAutoUpdateEnabledInjectable);
return getStartableStoppable(() =>
initTray(
@ -25,6 +27,7 @@ const trayInjectable = getInjectable({
trayMenuItems,
navigateToPreferences,
stopServicesAndExitApp,
isAutoUpdateEnabled,
),
);
},

View File

@ -8,7 +8,7 @@ import { Menu, Tray } from "electron";
import type { IComputedValue } from "mobx";
import { autorun } from "mobx";
import { showAbout } from "../menu/menu";
import { checkForUpdates, isAutoUpdateEnabled } from "../app-updater";
import { checkForUpdates } from "../app-updater";
import type { WindowManager } from "../window-manager";
import logger from "../logger";
import { isDevelopment, isWindows, productName, staticFilesDirectory } from "../../common/vars";
@ -35,6 +35,7 @@ export function initTray(
trayMenuItems: IComputedValue<TrayMenuRegistration[]>,
navigateToPreferences: () => void,
stopServicesAndExitApp: () => void,
isAutoUpdateEnabled: () => boolean,
): Disposer {
const icon = getTrayIconPath();
@ -53,7 +54,7 @@ export function initTray(
return disposer(
autorun(() => {
try {
const menu = createTrayMenu(windowManager, toJS(trayMenuItems.get()), navigateToPreferences, stopServicesAndExitApp);
const menu = createTrayMenu(windowManager, toJS(trayMenuItems.get()), navigateToPreferences, stopServicesAndExitApp, isAutoUpdateEnabled);
tray.setContextMenu(menu);
} catch (error) {
@ -82,6 +83,7 @@ function createTrayMenu(
extensionTrayItems: TrayMenuRegistration[],
navigateToPreferences: () => void,
stopServicesAndExitApp: () => void,
isAutoUpdateEnabled: () => boolean,
): Menu {
let template: Electron.MenuItemConstructorOptions[] = [
{