1
0
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:
Janne Savolainen 2022-08-11 16:42:52 +03:00 committed by GitHub
parent 17ddee2bde
commit 33834e34b7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 31 additions and 46 deletions

View File

@ -4,6 +4,11 @@
*/ */
import type { Injectable } from "@ogre-tools/injectable"; 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>>( export const getGlobalOverride = <T extends Injectable<any, any, any>>(
injectable: T, injectable: T,
overridingInstantiate: T["instantiate"], overridingInstantiate: T["instantiate"],

View File

@ -8,6 +8,8 @@ import configurePackages from "./common/configure-packages";
import { configure } from "mobx"; import { configure } from "mobx";
import { setImmediate } from "timers"; import { setImmediate } from "timers";
import { TextEncoder, TextDecoder as TextDecoderNode } from "util"; import { TextEncoder, TextDecoder as TextDecoderNode } from "util";
import glob from "glob";
import path from "path";
// setup default configuration for external npm-packages // setup default configuration for external npm-packages
configurePackages(); configurePackages();
@ -45,3 +47,13 @@ global.ResizeObserver = class {
jest.mock("./renderer/components/monaco-editor/monaco-editor"); jest.mock("./renderer/components/monaco-editor/monaco-editor");
jest.mock("./renderer/components/tooltip/withTooltip"); 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}");

View File

@ -3,8 +3,7 @@
* Licensed under MIT License. See LICENSE in root directory for more information. * Licensed under MIT License. See LICENSE in root directory for more information.
*/ */
import glob from "glob"; import { kebabCase, noop, chunk } from "lodash/fp";
import { kebabCase, memoize, noop, chunk } from "lodash/fp";
import type { DiContainer, Injectable } from "@ogre-tools/injectable"; import type { DiContainer, Injectable } from "@ogre-tools/injectable";
import { createContainer } 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"; 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 electronInjectable from "./utils/resolve-system-proxy/electron.injectable";
import type { HotbarStore } from "../common/hotbars/store"; import type { HotbarStore } from "../common/hotbars/store";
import focusApplicationInjectable from "./electron-app/features/focus-application.injectable"; 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 } = {}) { export function getDiForUnitTesting(opts: { doGeneralOverrides?: boolean } = {}) {
const { const {
@ -113,9 +113,9 @@ export function getDiForUnitTesting(opts: { doGeneralOverrides?: boolean } = {})
setLegacyGlobalDiForExtensionApi(di, Environments.main); setLegacyGlobalDiForExtensionApi(di, Environments.main);
const filePaths = getInjectableFilePaths(); const injectables: Injectable<any, any, any>[] = (global as any).mainInjectablePaths.map(
(filePath: string) => require(filePath).default,
const injectables = filePaths.map(filePath => require(filePath).default); );
chunk(100)(injectables).forEach(chunkInjectables => { chunk(100)(injectables).forEach(chunkInjectables => {
di.register(...chunkInjectables); di.register(...chunkInjectables);
@ -124,10 +124,8 @@ export function getDiForUnitTesting(opts: { doGeneralOverrides?: boolean } = {})
di.preventSideEffects(); di.preventSideEffects();
if (doGeneralOverrides) { if (doGeneralOverrides) {
const globalOverrideFilePaths = getGlobalOverridePaths(); const globalOverrides: GlobalOverride[] = (global as any).mainGlobalOverridePaths.map(
(filePath: string) => require(filePath).default,
const globalOverrides = globalOverrideFilePaths.map(
(filePath) => require(filePath).default,
); );
globalOverrides.forEach(globalOverride => { globalOverrides.forEach(globalOverride => {
@ -215,20 +213,6 @@ export function getDiForUnitTesting(opts: { doGeneralOverrides?: boolean } = {})
return di; 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 // TODO: Reorganize code in Runnables to get rid of requirement for override
const overrideRunnablesHavingSideEffects = (di: DiContainer) => { const overrideRunnablesHavingSideEffects = (di: DiContainer) => {
[ [

View File

@ -3,8 +3,7 @@
* Licensed under MIT License. See LICENSE in root directory for more information. * Licensed under MIT License. See LICENSE in root directory for more information.
*/ */
import glob from "glob"; import { noop, chunk } from "lodash/fp";
import { memoize, noop, chunk } from "lodash/fp";
import type { DiContainer, Injectable } from "@ogre-tools/injectable"; import type { DiContainer, Injectable } from "@ogre-tools/injectable";
import { import {
createContainer, createContainer,
@ -73,6 +72,7 @@ import forceUpdateModalRootFrameComponentInjectable from "./application-update/f
import legacyOnChannelListenInjectable from "./ipc/legacy-channel-listen.injectable"; import legacyOnChannelListenInjectable from "./ipc/legacy-channel-listen.injectable";
import getEntitySettingCommandsInjectable from "./components/command-palette/registered-commands/get-entity-setting-commands.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 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 } = {}) => { export const getDiForUnitTesting = (opts: { doGeneralOverrides?: boolean } = {}) => {
const { const {
@ -85,9 +85,9 @@ export const getDiForUnitTesting = (opts: { doGeneralOverrides?: boolean } = {})
setLegacyGlobalDiForExtensionApi(di, Environments.renderer); setLegacyGlobalDiForExtensionApi(di, Environments.renderer);
const filePaths = getInjectableFilePaths(); const injectables: Injectable<any, any, any>[] = (global as any).rendererInjectablePaths.map(
(filePath: string) => require(filePath).default,
const injectables = filePaths.map(filePath => require(filePath).default); );
chunk(100)(injectables).forEach(chunkInjectables => { chunk(100)(injectables).forEach(chunkInjectables => {
di.register(...chunkInjectables); di.register(...chunkInjectables);
@ -96,10 +96,8 @@ export const getDiForUnitTesting = (opts: { doGeneralOverrides?: boolean } = {})
di.preventSideEffects(); di.preventSideEffects();
if (doGeneralOverrides) { if (doGeneralOverrides) {
const globalOverrideFilePaths = getGlobalOverridePaths(); const globalOverrides: GlobalOverride[] = (global as any).rendererGlobalOverridePaths.map(
(filePath: string) => require(filePath).default,
const globalOverrides = globalOverrideFilePaths.map(
(filePath) => require(filePath).default,
); );
globalOverrides.forEach(globalOverride => { globalOverrides.forEach(globalOverride => {
@ -232,20 +230,6 @@ export const getDiForUnitTesting = (opts: { doGeneralOverrides?: boolean } = {})
return di; 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>[]) => { const overrideFunctionalInjectables = (di: DiContainer, injectables: Injectable<any, any, any>[]) => {
injectables.forEach(injectable => { injectables.forEach(injectable => {
di.override(injectable, () => () => { di.override(injectable, () => () => {