Overhaul FS fakes with full in-memory filesystem

- This increases our confidence in fs related logic

Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
Sebastian Malton committed 2022-12-06 09:18:59 -05:00
1 parent 6cc89bf154
commit 8d55a3a08d
24 files changed
+156 -113

No files matched your search

@@ -1,11 +0,0 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { getGlobalOverride } from "../test-utils/get-global-override";
import deleteFileInjectable from "./delete-file.injectable";
export default getGlobalOverride(deleteFileInjectable, () => async () => {
throw new Error("tried to delete file without override");
});
+39 -1
View File
@@ -3,11 +3,49 @@
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { getInjectable } from "@ogre-tools/injectable";
import type { ReadOptions } from "fs-extra";
import fse from "fs-extra";
/**
* NOTE: Add corrisponding a corrisponding override of this injecable in `src/test-utils/override-fs-with-fakes.ts`
*/
const fsInjectable = getInjectable({
id: "fs",
instantiate: () => fse,
instantiate: () => {
const {
promises: {
readFile,
writeFile,
readdir,
lstat,
rm,
},
readFileSync,
readJson,
writeJson,
readJsonSync,
writeFileSync,
writeJsonSync,
pathExistsSync,
pathExists,
} = fse;
return {
readFile,
readJson: readJson as (file: string, options?: ReadOptions | BufferEncoding) => Promise<any>,
writeFile,
writeJson,
pathExists,
readdir,
readFileSync,
readJsonSync,
writeFileSync,
writeJsonSync,
pathExistsSync,
lstat,
rm,
};
},
causesSideEffects: true,
});
@@ -5,11 +5,9 @@
import { getInjectable } from "@ogre-tools/injectable";
import fsInjectable from "./fs.injectable";
export type DeleteFile = (filePath: string) => Promise<void>;
const deleteFileInjectable = getInjectable({
id: "delete-file",
instantiate: (di): DeleteFile => di.inject(fsInjectable).unlink,
const pathExistsSyncInjectable = getInjectable({
id: "path-exists-sync",
instantiate: (di) => di.inject(fsInjectable).pathExistsSync,
});
export default deleteFileInjectable;
export default pathExistsSyncInjectable;
+3 -5
View File
@@ -14,18 +14,16 @@ export interface ReadDirectory {
(
path: string,
options?:
| { encoding: BufferEncoding | string | null; withFileTypes?: false | undefined }
| { encoding: BufferEncoding; withFileTypes?: false | undefined }
| BufferEncoding
| string
| null,
): Promise<string[]>;
(
path: string,
options?: { encoding?: BufferEncoding | string | null | undefined; withFileTypes?: false | undefined },
options?: { encoding?: BufferEncoding; withFileTypes?: false | undefined },
): Promise<string[] | Buffer[]>;
(
path: string,
options: { encoding?: BufferEncoding | string | null | undefined; withFileTypes: true },
options: { encoding?: BufferEncoding; withFileTypes: true },
): Promise<Dirent[]>;
}
@@ -4,8 +4,8 @@
*/
import { getGlobalOverride } from "../test-utils/get-global-override";
import removePathInjectable from "./remove-path.injectable";
import removePathInjectable from "./remove.injectable";
export default getGlobalOverride(removePathInjectable, () => async () => {
throw new Error("tried to remove a path without override");
throw new Error("tried to remove path without override");
});
@@ -5,11 +5,11 @@
import { getInjectable } from "@ogre-tools/injectable";
import fsInjectable from "./fs.injectable";
export type RemovePath = (path: string) => Promise<void>;
export type RemovePath = (filePath: string) => Promise<void>;
const removePathInjectable = getInjectable({
id: "remove-path",
instantiate: (di): RemovePath => di.inject(fsInjectable).remove,
instantiate: (di): RemovePath => di.inject(fsInjectable).rm,
});
export default removePathInjectable;
@@ -0,0 +1,29 @@
/**
* 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 getDirnameOfPathInjectable from "../path/get-dirname.injectable";
import fsInjectable from "./fs.injectable";
export type WriteFileSync = (filePath: string, contents: string) => void;
const writeFileSyncInjectable = getInjectable({
id: "write-file-sync",
instantiate: (di): WriteFileSync => {
const {
writeFileSync,
ensureDirSync,
} = di.inject(fsInjectable);
const getDirnameOfPath = di.inject(getDirnameOfPathInjectable);
return (filePath, contents) => {
ensureDirSync(getDirnameOfPath(filePath), {
mode: 0o755,
});
writeFileSync(filePath, contents);
};
},
});
export default writeFileSyncInjectable;