mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
Cascade different refactorings to eliminate global shared state from app paths
Co-authored-by: Mikko Aspiala <mikko.aspiala@gmail.com> Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>
This commit is contained in:
1 parent
908a40975e
commit
9d33fff906
336 files changed
+9105
-3307
No files matched your search
@@ -21,20 +21,32 @@
|
||||
|
||||
import glob from "glob";
|
||||
import { memoize } from "lodash/fp";
|
||||
import { kebabCase } from "lodash/fp";
|
||||
|
||||
import {
|
||||
createContainer,
|
||||
ConfigurableDependencyInjectionContainer,
|
||||
} from "@ogre-tools/injectable";
|
||||
import { setLegacyGlobalDiForExtensionApi } from "../extensions/as-legacy-global-function-for-extension-api/legacy-global-di-for-extension-api";
|
||||
|
||||
export const getDiForUnitTesting = () => {
|
||||
import { setLegacyGlobalDiForExtensionApi } from "../extensions/as-legacy-global-function-for-extension-api/legacy-global-di-for-extension-api";
|
||||
import getElectronAppPathInjectable from "./app-paths/get-electron-app-path/get-electron-app-path.injectable";
|
||||
import setElectronAppPathInjectable from "./app-paths/set-electron-app-path/set-electron-app-path.injectable";
|
||||
import appNameInjectable from "./app-paths/app-name/app-name.injectable";
|
||||
import registerChannelInjectable from "./app-paths/register-channel/register-channel.injectable";
|
||||
import writeJsonFileInjectable
|
||||
from "../common/fs/write-json-file/write-json-file.injectable";
|
||||
import readJsonFileInjectable
|
||||
from "../common/fs/read-json-file/read-json-file.injectable";
|
||||
|
||||
export const getDiForUnitTesting = (
|
||||
{ doGeneralOverrides } = { doGeneralOverrides: false },
|
||||
) => {
|
||||
const di: ConfigurableDependencyInjectionContainer = createContainer();
|
||||
|
||||
setLegacyGlobalDiForExtensionApi(di);
|
||||
|
||||
getInjectableFilePaths()
|
||||
.map(key => {
|
||||
.map((key) => {
|
||||
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
||||
const injectable = require(key).default;
|
||||
|
||||
@@ -45,14 +57,34 @@ export const getDiForUnitTesting = () => {
|
||||
};
|
||||
})
|
||||
|
||||
.forEach(injectable => di.register(injectable));
|
||||
.forEach((injectable) => di.register(injectable));
|
||||
|
||||
di.preventSideEffects();
|
||||
|
||||
if (doGeneralOverrides) {
|
||||
di.override(
|
||||
getElectronAppPathInjectable,
|
||||
() => (name: string) => `some-electron-app-path-for-${kebabCase(name)}`,
|
||||
);
|
||||
|
||||
di.override(setElectronAppPathInjectable, () => () => undefined);
|
||||
di.override(appNameInjectable, () => "some-electron-app-name");
|
||||
di.override(registerChannelInjectable, () => () => undefined);
|
||||
|
||||
di.override(writeJsonFileInjectable, () => () => {
|
||||
throw new Error("Tried to write JSON file to file system without specifying explicit override.");
|
||||
});
|
||||
|
||||
di.override(readJsonFileInjectable, () => () => {
|
||||
throw new Error("Tried to read JSON file from file system without specifying explicit override.");
|
||||
});
|
||||
}
|
||||
|
||||
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 }),
|
||||
]);
|
||||
Reference in new issue
Block a user