mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
Optimize getting injectable paths only once for all test suites (#6013)
This commit is contained in:
parent
17ddee2bde
commit
33834e34b7
@ -4,6 +4,11 @@
|
||||
*/
|
||||
import type { Injectable } from "@ogre-tools/injectable";
|
||||
|
||||
export interface GlobalOverride {
|
||||
injectable: Injectable<any, any, any>;
|
||||
overridingInstantiate: any;
|
||||
}
|
||||
|
||||
export const getGlobalOverride = <T extends Injectable<any, any, any>>(
|
||||
injectable: T,
|
||||
overridingInstantiate: T["instantiate"],
|
||||
|
||||
@ -8,6 +8,8 @@ import configurePackages from "./common/configure-packages";
|
||||
import { configure } from "mobx";
|
||||
import { setImmediate } from "timers";
|
||||
import { TextEncoder, TextDecoder as TextDecoderNode } from "util";
|
||||
import glob from "glob";
|
||||
import path from "path";
|
||||
|
||||
// setup default configuration for external npm-packages
|
||||
configurePackages();
|
||||
@ -45,3 +47,13 @@ global.ResizeObserver = class {
|
||||
|
||||
jest.mock("./renderer/components/monaco-editor/monaco-editor");
|
||||
jest.mock("./renderer/components/tooltip/withTooltip");
|
||||
|
||||
const getInjectables = (environment: "renderer" | "main", filePathGlob: string) =>
|
||||
glob.sync(`./{common,extensions,${environment}}/**/${filePathGlob}`, {
|
||||
cwd: __dirname,
|
||||
}).map(x => path.resolve(__dirname, x));
|
||||
|
||||
(global as any).rendererInjectablePaths = getInjectables("renderer", "*.injectable.{ts,tsx}");
|
||||
(global as any).rendererGlobalOverridePaths = getInjectables("renderer", "*.global-override-for-injectable.{ts,tsx}");
|
||||
(global as any).mainInjectablePaths = getInjectables("main", "*.injectable.{ts,tsx}");
|
||||
(global as any).mainGlobalOverridePaths = getInjectables("main", "*.global-override-for-injectable.{ts,tsx}");
|
||||
|
||||
@ -3,8 +3,7 @@
|
||||
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||
*/
|
||||
|
||||
import glob from "glob";
|
||||
import { kebabCase, memoize, noop, chunk } from "lodash/fp";
|
||||
import { kebabCase, noop, chunk } from "lodash/fp";
|
||||
import type { DiContainer, Injectable } from "@ogre-tools/injectable";
|
||||
import { createContainer } from "@ogre-tools/injectable";
|
||||
import { Environments, setLegacyGlobalDiForExtensionApi } from "../extensions/as-legacy-globals-for-extension-api/legacy-global-di-for-extension-api";
|
||||
@ -101,6 +100,7 @@ import { registerMobX } from "@ogre-tools/injectable-extension-for-mobx";
|
||||
import electronInjectable from "./utils/resolve-system-proxy/electron.injectable";
|
||||
import type { HotbarStore } from "../common/hotbars/store";
|
||||
import focusApplicationInjectable from "./electron-app/features/focus-application.injectable";
|
||||
import type { GlobalOverride } from "../common/test-utils/get-global-override";
|
||||
|
||||
export function getDiForUnitTesting(opts: { doGeneralOverrides?: boolean } = {}) {
|
||||
const {
|
||||
@ -113,9 +113,9 @@ export function getDiForUnitTesting(opts: { doGeneralOverrides?: boolean } = {})
|
||||
|
||||
setLegacyGlobalDiForExtensionApi(di, Environments.main);
|
||||
|
||||
const filePaths = getInjectableFilePaths();
|
||||
|
||||
const injectables = filePaths.map(filePath => require(filePath).default);
|
||||
const injectables: Injectable<any, any, any>[] = (global as any).mainInjectablePaths.map(
|
||||
(filePath: string) => require(filePath).default,
|
||||
);
|
||||
|
||||
chunk(100)(injectables).forEach(chunkInjectables => {
|
||||
di.register(...chunkInjectables);
|
||||
@ -124,10 +124,8 @@ export function getDiForUnitTesting(opts: { doGeneralOverrides?: boolean } = {})
|
||||
di.preventSideEffects();
|
||||
|
||||
if (doGeneralOverrides) {
|
||||
const globalOverrideFilePaths = getGlobalOverridePaths();
|
||||
|
||||
const globalOverrides = globalOverrideFilePaths.map(
|
||||
(filePath) => require(filePath).default,
|
||||
const globalOverrides: GlobalOverride[] = (global as any).mainGlobalOverridePaths.map(
|
||||
(filePath: string) => require(filePath).default,
|
||||
);
|
||||
|
||||
globalOverrides.forEach(globalOverride => {
|
||||
@ -215,20 +213,6 @@ export function getDiForUnitTesting(opts: { doGeneralOverrides?: boolean } = {})
|
||||
return di;
|
||||
}
|
||||
|
||||
const getInjectableFilePaths = memoize(() => [
|
||||
...glob.sync("./**/*.injectable.{ts,tsx}", { cwd: __dirname }),
|
||||
...glob.sync("../extensions/**/*.injectable.{ts,tsx}", { cwd: __dirname }),
|
||||
...glob.sync("../common/**/*.injectable.{ts,tsx}", { cwd: __dirname }),
|
||||
]);
|
||||
|
||||
const getGlobalOverridePaths = memoize(() =>
|
||||
glob.sync(
|
||||
"../{common,extensions,main}/**/*.global-override-for-injectable.{ts,tsx}",
|
||||
|
||||
{ cwd: __dirname },
|
||||
),
|
||||
);
|
||||
|
||||
// TODO: Reorganize code in Runnables to get rid of requirement for override
|
||||
const overrideRunnablesHavingSideEffects = (di: DiContainer) => {
|
||||
[
|
||||
|
||||
@ -3,8 +3,7 @@
|
||||
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||
*/
|
||||
|
||||
import glob from "glob";
|
||||
import { memoize, noop, chunk } from "lodash/fp";
|
||||
import { noop, chunk } from "lodash/fp";
|
||||
import type { DiContainer, Injectable } from "@ogre-tools/injectable";
|
||||
import {
|
||||
createContainer,
|
||||
@ -73,6 +72,7 @@ import forceUpdateModalRootFrameComponentInjectable from "./application-update/f
|
||||
import legacyOnChannelListenInjectable from "./ipc/legacy-channel-listen.injectable";
|
||||
import getEntitySettingCommandsInjectable from "./components/command-palette/registered-commands/get-entity-setting-commands.injectable";
|
||||
import storageSaveDelayInjectable from "./utils/create-storage/storage-save-delay.injectable";
|
||||
import type { GlobalOverride } from "../common/test-utils/get-global-override";
|
||||
|
||||
export const getDiForUnitTesting = (opts: { doGeneralOverrides?: boolean } = {}) => {
|
||||
const {
|
||||
@ -85,9 +85,9 @@ export const getDiForUnitTesting = (opts: { doGeneralOverrides?: boolean } = {})
|
||||
|
||||
setLegacyGlobalDiForExtensionApi(di, Environments.renderer);
|
||||
|
||||
const filePaths = getInjectableFilePaths();
|
||||
|
||||
const injectables = filePaths.map(filePath => require(filePath).default);
|
||||
const injectables: Injectable<any, any, any>[] = (global as any).rendererInjectablePaths.map(
|
||||
(filePath: string) => require(filePath).default,
|
||||
);
|
||||
|
||||
chunk(100)(injectables).forEach(chunkInjectables => {
|
||||
di.register(...chunkInjectables);
|
||||
@ -96,10 +96,8 @@ export const getDiForUnitTesting = (opts: { doGeneralOverrides?: boolean } = {})
|
||||
di.preventSideEffects();
|
||||
|
||||
if (doGeneralOverrides) {
|
||||
const globalOverrideFilePaths = getGlobalOverridePaths();
|
||||
|
||||
const globalOverrides = globalOverrideFilePaths.map(
|
||||
(filePath) => require(filePath).default,
|
||||
const globalOverrides: GlobalOverride[] = (global as any).rendererGlobalOverridePaths.map(
|
||||
(filePath: string) => require(filePath).default,
|
||||
);
|
||||
|
||||
globalOverrides.forEach(globalOverride => {
|
||||
@ -232,20 +230,6 @@ export const getDiForUnitTesting = (opts: { doGeneralOverrides?: boolean } = {})
|
||||
return di;
|
||||
};
|
||||
|
||||
const getInjectableFilePaths = memoize(() => [
|
||||
...glob.sync("./**/*.injectable.{ts,tsx}", { cwd: __dirname }),
|
||||
...glob.sync("../common/**/*.injectable.{ts,tsx}", { cwd: __dirname }),
|
||||
...glob.sync("../extensions/**/*.injectable.{ts,tsx}", { cwd: __dirname }),
|
||||
]);
|
||||
|
||||
const getGlobalOverridePaths = memoize(() =>
|
||||
glob.sync(
|
||||
"../{common,extensions,renderer}/**/*.global-override-for-injectable.{ts,tsx}",
|
||||
|
||||
{ cwd: __dirname },
|
||||
),
|
||||
);
|
||||
|
||||
const overrideFunctionalInjectables = (di: DiContainer, injectables: Injectable<any, any, any>[]) => {
|
||||
injectables.forEach(injectable => {
|
||||
di.override(injectable, () => () => {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user