mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
Add global override for getConfigurationFileModel to fix tests
Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
parent
59657fcea4
commit
bea52f9f80
13
src/common/fs/write-json-sync.injectable.ts
Normal file
13
src/common/fs/write-json-sync.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 writeJsonSyncInjectable = getInjectable({
|
||||
id: "write-json-sync",
|
||||
instantiate: (di) => di.inject(fsInjectable).writeJsonSync,
|
||||
});
|
||||
|
||||
export default writeJsonSyncInjectable;
|
||||
@ -0,0 +1,50 @@
|
||||
/**
|
||||
* Copyright (c) OpenLens Authors. All rights reserved.
|
||||
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||
*/
|
||||
|
||||
import assert from "assert";
|
||||
import { action } from "mobx";
|
||||
import path from "path";
|
||||
import { getGlobalOverride } from "../test-utils/get-global-override";
|
||||
import getConfigurationFileModelInjectable from "./get-configuration-file-model.injectable";
|
||||
import type Config from "conf";
|
||||
import readJsonSyncInjectable from "../fs/read-json-sync.injectable";
|
||||
import writeJsonSyncInjectable from "../fs/write-json-sync.injectable";
|
||||
|
||||
export default getGlobalOverride(getConfigurationFileModelInjectable, (di) => {
|
||||
const readJsonSync = di.inject(readJsonSyncInjectable);
|
||||
const writeJsonSync = di.inject(writeJsonSyncInjectable);
|
||||
|
||||
return (options) => {
|
||||
assert(options.cwd, "Missing options.cwd");
|
||||
assert(options.configName, "Missing options.configName");
|
||||
|
||||
const configFilePath = path.posix.join(options.cwd, options.configName);
|
||||
|
||||
return {
|
||||
get store() {
|
||||
try {
|
||||
return readJsonSync(configFilePath);
|
||||
} catch {
|
||||
return {};
|
||||
}
|
||||
},
|
||||
path: configFilePath,
|
||||
set: action((key: string, value: unknown) => {
|
||||
let currentState: object;
|
||||
|
||||
try {
|
||||
currentState = readJsonSync(configFilePath);
|
||||
} catch {
|
||||
currentState = {};
|
||||
}
|
||||
|
||||
writeJsonSync(configFilePath, {
|
||||
...currentState,
|
||||
[key]: value,
|
||||
});
|
||||
}),
|
||||
} as Partial<Config> as Config<any>;
|
||||
};
|
||||
});
|
||||
@ -8,21 +8,29 @@ 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";
|
||||
import deleteFileInjectable from "../common/fs/delete-file.injectable";
|
||||
import writeJsonSyncInjectable from "../common/fs/write-json-sync.injectable";
|
||||
import readJsonSyncInjectable from "../common/fs/read-json-sync.injectable";
|
||||
|
||||
export const overrideFsWithFakes = (di: DiContainer, state = new Map()) => {
|
||||
const readFile = readFileFor(state);
|
||||
|
||||
di.override(readFileInjectable, () => readFile);
|
||||
di.override(readFileInjectable, () => async (filePath) => readFile(filePath));
|
||||
di.override(writeJsonFileInjectable, () => (
|
||||
async (filePath, contents) => {
|
||||
state.set(filePath, JSON.stringify(contents));
|
||||
}
|
||||
));
|
||||
di.override(readJsonFileInjectable, () => (
|
||||
async (filePath: string) => JSON.parse(await readFile(filePath))
|
||||
async (filePath: string) => JSON.parse(readFile(filePath))
|
||||
));
|
||||
di.override(writeJsonSyncInjectable, () => (
|
||||
(filePath, data) => state.set(filePath, JSON.stringify(data))
|
||||
));
|
||||
di.override(readJsonSyncInjectable, () => (
|
||||
(filePath) => JSON.parse(readFile(filePath))
|
||||
));
|
||||
di.override(pathExistsInjectable, () => (
|
||||
(filePath: string) => Promise.resolve(state.has(filePath))
|
||||
async (filePath: string) => state.has(filePath)
|
||||
));
|
||||
di.override(deleteFileInjectable, () => async (filePath: string) => {
|
||||
state.delete(filePath);
|
||||
@ -40,5 +48,5 @@ const readFileFor = (state: Map<string, string>) => (filePath: string) => {
|
||||
);
|
||||
}
|
||||
|
||||
return Promise.resolve(fileContent);
|
||||
return fileContent;
|
||||
};
|
||||
|
||||
Loading…
Reference in New Issue
Block a user