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-mock-extended": "^1.0.18",
|
||||
"make-plural": "^6.2.2",
|
||||
"memfs": "^3.4.1",
|
||||
"mini-css-extract-plugin": "^2.6.0",
|
||||
"mock-http": "^1.1.0",
|
||||
"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.
|
||||
*/
|
||||
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 kubeconfigSyncsPreferencesLoggerInjectable from "./logger.injectable";
|
||||
import type { SyncValue } from "./view";
|
||||
@ -14,7 +14,7 @@ const getSyncEntryInjectable = getInjectable({
|
||||
id: "get-sync-entry",
|
||||
instantiate: (di): GetSyncEntry => {
|
||||
const logger = di.inject(kubeconfigSyncsPreferencesLoggerInjectable);
|
||||
const { stat } = di.inject(fsInjectable);
|
||||
const stat = di.inject(statInjectable);
|
||||
|
||||
return async ({ filePath, ...data }) => {
|
||||
try {
|
||||
|
||||
@ -7,59 +7,25 @@ import readFileInjectable from "../common/fs/read-file.injectable";
|
||||
import writeJsonFileInjectable from "../common/fs/write-json-file.injectable";
|
||||
import readJsonFileInjectable from "../common/fs/read-json-file.injectable";
|
||||
import pathExistsInjectable from "../common/fs/path-exists.injectable";
|
||||
|
||||
type Override = (di: DiContainer) => void;
|
||||
import { createFsFromVolume, Volume } from "memfs";
|
||||
import statInjectable from "../common/fs/stat.injectable";
|
||||
|
||||
export const overrideFsWithFakes = (di: DiContainer) => {
|
||||
const state = new Map();
|
||||
const inMemoryFs = createFsFromVolume(Volume.fromJSON({}));
|
||||
|
||||
const readFile = readFileFor(state);
|
||||
|
||||
const overrides: Override[] = [
|
||||
(di) => {
|
||||
di.override(readFileInjectable, () => readFile);
|
||||
},
|
||||
|
||||
(di) => {
|
||||
di.override(
|
||||
writeJsonFileInjectable,
|
||||
() => (filePath: string, contents: object) => {
|
||||
state.set(filePath, JSON.stringify(contents));
|
||||
|
||||
return Promise.resolve();
|
||||
},
|
||||
);
|
||||
},
|
||||
|
||||
(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);
|
||||
di.override(readFileInjectable, () => (
|
||||
(filePath) => Promise.resolve(inMemoryFs.readFileSync(filePath, { encoding: "utf-8" }))
|
||||
));
|
||||
di.override(writeJsonFileInjectable, () => (
|
||||
(filePath, contents) => Promise.resolve(inMemoryFs.writeFileSync(filePath, JSON.stringify(contents)))
|
||||
));
|
||||
di.override(readJsonFileInjectable, () => (
|
||||
(filePath) => Promise.resolve(JSON.parse(inMemoryFs.readFileSync(filePath, { encoding: "utf-8" }) as string))
|
||||
));
|
||||
di.override(pathExistsInjectable, () => (
|
||||
(filePath) => Promise.resolve(inMemoryFs.existsSync(filePath))
|
||||
));
|
||||
di.override(statInjectable, () => (
|
||||
(filePath) => Promise.resolve(inMemoryFs.statSync(filePath))
|
||||
));
|
||||
};
|
||||
|
||||
Loading…
Reference in New Issue
Block a user