From 8409d56fdfabb8d069c4462d23458afef1ed64e4 Mon Sep 17 00:00:00 2001 From: Sebastian Malton Date: Fri, 2 Dec 2022 16:15:46 -0500 Subject: [PATCH] Add ensureDir/Sync support to fake FS Signed-off-by: Sebastian Malton --- src/common/fs/fs.injectable.ts | 4 ++++ src/test-utils/override-fs-with-fakes.ts | 10 ++++++++++ 2 files changed, 14 insertions(+) diff --git a/src/common/fs/fs.injectable.ts b/src/common/fs/fs.injectable.ts index 05ea3b2032..1c4a8fc0b3 100644 --- a/src/common/fs/fs.injectable.ts +++ b/src/common/fs/fs.injectable.ts @@ -21,6 +21,8 @@ const fsInjectable = getInjectable({ rm, access, }, + ensureDir, + ensureDirSync, readFileSync, readJson, writeJson, @@ -48,6 +50,8 @@ const fsInjectable = getInjectable({ rm, access, copy: copy as (src: string, dest: string, options?: fse.CopyOptions) => Promise, + ensureDir: ensureDir as (path: string, options?: number | fse.EnsureOptions ) => Promise, + ensureDirSync, }; }, causesSideEffects: true, diff --git a/src/test-utils/override-fs-with-fakes.ts b/src/test-utils/override-fs-with-fakes.ts index a144e99d97..eea08c7943 100644 --- a/src/test-utils/override-fs-with-fakes.ts +++ b/src/test-utils/override-fs-with-fakes.ts @@ -6,6 +6,7 @@ import type { DiContainer } from "@ogre-tools/injectable"; import fsInjectable from "../common/fs/fs.injectable"; import { createFsFromVolume, Volume } from "memfs"; import type { + ensureDirSync as ensureDirSyncImpl, readJsonSync as readJsonSyncImpl, writeJsonSync as writeJsonSyncImpl, } from "fs-extra"; @@ -32,6 +33,13 @@ export const getOverrideFsWithFakes = () => { root.writeFileSync(file, JSON.stringify(object, options?.replacer, options?.spaces), options as any); }) as typeof writeJsonSyncImpl; + const ensureDirSync = ((path, opts) => { + const mode = typeof opts === "number" + ? opts + : opts?.mode; + + root.mkdirpSync(path, mode); + }) as typeof ensureDirSyncImpl; return (di: DiContainer) => { di.override(fsInjectable, () => ({ @@ -50,6 +58,8 @@ export const getOverrideFsWithFakes = () => { rm: root.promises.rm, access: root.promises.access, copy: async (src, dest) => { throw new Error(`Tried to copy '${src}' to '${dest}'. Copying is not yet supported`); }, + ensureDir: async (path, opts) => ensureDirSync(path, opts), + ensureDirSync, })); }; };