mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
Replace overrideFsWithFakes implementation
- Switch to using memfs package because we are now using statInjectable and a handrolled fs replacement will just become to complex Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
parent
50562f4eef
commit
c986135905
@ -373,6 +373,7 @@
|
|||||||
"jest-fetch-mock": "^3.0.3",
|
"jest-fetch-mock": "^3.0.3",
|
||||||
"jest-mock-extended": "^1.0.18",
|
"jest-mock-extended": "^1.0.18",
|
||||||
"make-plural": "^6.2.2",
|
"make-plural": "^6.2.2",
|
||||||
|
"memfs": "^3.4.1",
|
||||||
"mini-css-extract-plugin": "^2.6.0",
|
"mini-css-extract-plugin": "^2.6.0",
|
||||||
"mock-http": "^1.1.0",
|
"mock-http": "^1.1.0",
|
||||||
"node-gyp": "7.1.2",
|
"node-gyp": "7.1.2",
|
||||||
|
|||||||
13
src/common/fs/stat.injectable.ts
Normal file
13
src/common/fs/stat.injectable.ts
Normal file
@ -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";
|
||||||
|
import fsInjectable from "./fs.injectable";
|
||||||
|
|
||||||
|
const statInjectable = getInjectable({
|
||||||
|
id: "stat",
|
||||||
|
instantiate: (di) => di.inject(fsInjectable).stat,
|
||||||
|
});
|
||||||
|
|
||||||
|
export default statInjectable;
|
||||||
@ -3,7 +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 { getInjectable } from "@ogre-tools/injectable";
|
import { getInjectable } from "@ogre-tools/injectable";
|
||||||
import fsInjectable from "../../../../common/fs/fs.injectable";
|
import statInjectable from "../../../../common/fs/stat.injectable";
|
||||||
import type { KubeconfigSyncEntry } from "../../../../common/user-store";
|
import type { KubeconfigSyncEntry } from "../../../../common/user-store";
|
||||||
import kubeconfigSyncsPreferencesLoggerInjectable from "./logger.injectable";
|
import kubeconfigSyncsPreferencesLoggerInjectable from "./logger.injectable";
|
||||||
import type { SyncValue } from "./view";
|
import type { SyncValue } from "./view";
|
||||||
@ -14,7 +14,7 @@ const getSyncEntryInjectable = getInjectable({
|
|||||||
id: "get-sync-entry",
|
id: "get-sync-entry",
|
||||||
instantiate: (di): GetSyncEntry => {
|
instantiate: (di): GetSyncEntry => {
|
||||||
const logger = di.inject(kubeconfigSyncsPreferencesLoggerInjectable);
|
const logger = di.inject(kubeconfigSyncsPreferencesLoggerInjectable);
|
||||||
const { stat } = di.inject(fsInjectable);
|
const stat = di.inject(statInjectable);
|
||||||
|
|
||||||
return async ({ filePath, ...data }) => {
|
return async ({ filePath, ...data }) => {
|
||||||
try {
|
try {
|
||||||
|
|||||||
@ -7,59 +7,25 @@ import readFileInjectable from "../common/fs/read-file.injectable";
|
|||||||
import writeJsonFileInjectable from "../common/fs/write-json-file.injectable";
|
import writeJsonFileInjectable from "../common/fs/write-json-file.injectable";
|
||||||
import readJsonFileInjectable from "../common/fs/read-json-file.injectable";
|
import readJsonFileInjectable from "../common/fs/read-json-file.injectable";
|
||||||
import pathExistsInjectable from "../common/fs/path-exists.injectable";
|
import pathExistsInjectable from "../common/fs/path-exists.injectable";
|
||||||
|
import { createFsFromVolume, Volume } from "memfs";
|
||||||
type Override = (di: DiContainer) => void;
|
import statInjectable from "../common/fs/stat.injectable";
|
||||||
|
|
||||||
export const overrideFsWithFakes = (di: DiContainer) => {
|
export const overrideFsWithFakes = (di: DiContainer) => {
|
||||||
const state = new Map();
|
const inMemoryFs = createFsFromVolume(Volume.fromJSON({}));
|
||||||
|
|
||||||
const readFile = readFileFor(state);
|
di.override(readFileInjectable, () => (
|
||||||
|
(filePath) => Promise.resolve(inMemoryFs.readFileSync(filePath, { encoding: "utf-8" }))
|
||||||
const overrides: Override[] = [
|
));
|
||||||
(di) => {
|
di.override(writeJsonFileInjectable, () => (
|
||||||
di.override(readFileInjectable, () => readFile);
|
(filePath, contents) => Promise.resolve(inMemoryFs.writeFileSync(filePath, JSON.stringify(contents)))
|
||||||
},
|
));
|
||||||
|
di.override(readJsonFileInjectable, () => (
|
||||||
(di) => {
|
(filePath) => Promise.resolve(JSON.parse(inMemoryFs.readFileSync(filePath, { encoding: "utf-8" }) as string))
|
||||||
di.override(
|
));
|
||||||
writeJsonFileInjectable,
|
di.override(pathExistsInjectable, () => (
|
||||||
() => (filePath: string, contents: object) => {
|
(filePath) => Promise.resolve(inMemoryFs.existsSync(filePath))
|
||||||
state.set(filePath, JSON.stringify(contents));
|
));
|
||||||
|
di.override(statInjectable, () => (
|
||||||
return Promise.resolve();
|
(filePath) => Promise.resolve(inMemoryFs.statSync(filePath))
|
||||||
},
|
));
|
||||||
);
|
|
||||||
},
|
|
||||||
|
|
||||||
(di) => {
|
|
||||||
di.override(readJsonFileInjectable, () => async (filePath: string) => {
|
|
||||||
const fileContent = await readFile(filePath);
|
|
||||||
|
|
||||||
return JSON.parse(fileContent.toString());
|
|
||||||
});
|
|
||||||
},
|
|
||||||
|
|
||||||
(di) => {
|
|
||||||
di.override(
|
|
||||||
pathExistsInjectable,
|
|
||||||
() => async (filePath: string) => Promise.resolve(state.has(filePath)),
|
|
||||||
);
|
|
||||||
},
|
|
||||||
];
|
|
||||||
|
|
||||||
overrides.forEach(callback => callback(di));
|
|
||||||
};
|
|
||||||
|
|
||||||
const readFileFor = (state: Map<string, string>) => (filePath: string) => {
|
|
||||||
const fileContent = state.get(filePath);
|
|
||||||
|
|
||||||
if (!fileContent) {
|
|
||||||
const existingFilePaths = [...state.keys()].join('", "');
|
|
||||||
|
|
||||||
throw new Error(
|
|
||||||
`Tried to access file ${filePath} which does not exist. Existing file paths are: "${existingFilePaths}"`,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
return Promise.resolve(fileContent);
|
|
||||||
};
|
};
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user