Fix <Extensions> tests by removing mockFs and making everything injectable

Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
Sebastian Malton committed 2022-10-04 17:55:33 -04:00
1 parent 46db3a6b7a
commit 69bd42357f
39 files changed
+483 -189

No files matched your search

@@ -0,0 +1,11 @@
/**
* 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 accessPathInjectable from "./access-path.injectable";
export default getGlobalOverride(accessPathInjectable, () => async () => {
throw new Error("tried to verify path access without override");
});
+27
View File
@@ -0,0 +1,27 @@
/**
* 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";
export type AccessPath = (path: string, mode?: number) => Promise<boolean>;
const accessPathInjectable = getInjectable({
id: "access-path",
instantiate: (di): AccessPath => {
const { access } = di.inject(fsInjectable);
return async (path, mode) => {
try {
await access(path, mode);
return true;
} catch {
return false;
}
};
},
});
export default accessPathInjectable;
@@ -0,0 +1,11 @@
/**
* 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 copyInjectable from "./copy.injectable";
export default getGlobalOverride(copyInjectable, () => async () => {
throw new Error("tried to copy filepaths without override");
});
+16
View File
@@ -0,0 +1,16 @@
/**
* 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 type { CopyOptions } from "fs-extra";
import fsInjectable from "./fs.injectable";
export type Copy = (src: string, dest: string, options?: CopyOptions | undefined) => Promise<void>;
const copyInjectable = getInjectable({
id: "copy",
instantiate: (di): Copy => di.inject(fsInjectable).copy,
});
export default copyInjectable;
@@ -0,0 +1,11 @@
/**
* 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 ensureDirInjectable from "./ensure-dir.injectable";
export default getGlobalOverride(ensureDirInjectable, () => async () => {
throw new Error("tried to ensure directory without override");
});
+3 -3
View File
@@ -5,14 +5,14 @@
import { getInjectable } from "@ogre-tools/injectable";
import fsInjectable from "./fs.injectable";
export type EnsureDirectory = (dirPath: string) => Promise<void>;
const ensureDirInjectable = getInjectable({
id: "ensure-dir",
// TODO: Remove usages of ensureDir from business logic.
// TODO: Read, Write, Watch etc. operations should do this internally.
instantiate: (di) => di.inject(fsInjectable).ensureDir,
causesSideEffects: true,
instantiate: (di): EnsureDirectory => di.inject(fsInjectable).ensureDir,
});
export default ensureDirInjectable;
@@ -0,0 +1,11 @@
/**
* 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 lstatInjectable from "./lstat.injectable";
export default getGlobalOverride(lstatInjectable, () => async () => {
throw new Error("tried to lstat a filepath without override");
});
+16
View File
@@ -0,0 +1,16 @@
/**
* 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 type { Stats } from "fs";
import fsInjectable from "./fs.injectable";
export type LStat = (path: string) => Promise<Stats>;
const lstatInjectable = getInjectable({
id: "lstat",
instantiate: (di): LStat => di.inject(fsInjectable).lstat,
});
export default lstatInjectable;
@@ -0,0 +1,11 @@
/**
* 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 readDirInjectable from "./read-dir.injectable";
export default getGlobalOverride(readDirInjectable, () => async () => {
throw new Error("tried to read a directory's content without override");
});
+25 -1
View File
@@ -3,11 +3,35 @@
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { getInjectable } from "@ogre-tools/injectable";
import type { Dirent } from "fs";
import fsInjectable from "./fs.injectable";
export interface ReadDirectory {
(
path: string,
options: "buffer" | { encoding: "buffer"; withFileTypes?: false | undefined }
): Promise<Buffer[]>;
(
path: string,
options?:
| { encoding: BufferEncoding | string | null; withFileTypes?: false | undefined }
| BufferEncoding
| string
| null,
): Promise<string[]>;
(
path: string,
options?: { encoding?: BufferEncoding | string | null | undefined; withFileTypes?: false | undefined },
): Promise<string[] | Buffer[]>;
(
path: string,
options: { encoding?: BufferEncoding | string | null | undefined; withFileTypes: true },
): Promise<Dirent[]>;
}
const readDirInjectable = getInjectable({
id: "read-dir",
instantiate: (di) => di.inject(fsInjectable).readdir,
instantiate: (di): ReadDirectory => di.inject(fsInjectable).readdir,
});
export default readDirInjectable;
@@ -0,0 +1,10 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import path from "path";
import { getGlobalOverride } from "../test-utils/get-global-override";
import getAbsolutePathInjectable from "./get-absolute-path.injectable";
export default getGlobalOverride(getAbsolutePathInjectable, () => path.posix.resolve);
@@ -0,0 +1,10 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import path from "path";
import { getGlobalOverride } from "../test-utils/get-global-override";
import getBasenameOfPathInjectable from "./get-basename.injectable";
export default getGlobalOverride(getBasenameOfPathInjectable, () => path.posix.basename);
@@ -0,0 +1,16 @@
/**
* 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 path from "path";
export type GetBasenameOfPath = (path: string) => string;
const getBasenameOfPathInjectable = getInjectable({
id: "get-basename-of-path",
instantiate: (): GetBasenameOfPath => path.basename,
causesSideEffects: true,
});
export default getBasenameOfPathInjectable;
@@ -0,0 +1,10 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import path from "path";
import { getGlobalOverride } from "../test-utils/get-global-override";
import getDirnameOfPathInjectable from "./get-dirname.injectable";
export default getGlobalOverride(getDirnameOfPathInjectable, () => path.posix.dirname);
+16
View File
@@ -0,0 +1,16 @@
/**
* 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 path from "path";
export type GetDirnameOfPath = (path: string) => string;
const getDirnameOfPathInjectable = getInjectable({
id: "get-dirname-of-path",
instantiate: (): GetDirnameOfPath => path.dirname,
causesSideEffects: true,
});
export default getDirnameOfPathInjectable;
@@ -0,0 +1,10 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import path from "path";
import { getGlobalOverride } from "../test-utils/get-global-override";
import getRelativePathInjectable from "./get-relative-path.injectable";
export default getGlobalOverride(getRelativePathInjectable, () => path.posix.relative);
@@ -0,0 +1,16 @@
/**
* 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 path from "path";
export type GetRelativePath = (from: string, to: string) => string;
const getRelativePathInjectable = getInjectable({
id: "get-relative-path",
instantiate: (): GetRelativePath => path.relative,
causesSideEffects: true,
});
export default getRelativePathInjectable;
@@ -0,0 +1,10 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import path from "path";
import { getGlobalOverride } from "../test-utils/get-global-override";
import joinPathsInjectable from "./join-paths.injectable";
export default getGlobalOverride(joinPathsInjectable, () => path.posix.join);
@@ -0,0 +1,10 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import path from "path";
import { getGlobalOverride } from "../test-utils/get-global-override";
import fileSystemSeparatorInjectable from "./separator.injectable";
export default getGlobalOverride(fileSystemSeparatorInjectable, () => path.posix.sep);
+14
View File
@@ -0,0 +1,14 @@
/**
* 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 path from "path";
const fileSystemSeparatorInjectable = getInjectable({
id: "file-system-separator",
instantiate: () => path.sep,
causesSideEffects: true,
});
export default fileSystemSeparatorInjectable;
@@ -1,17 +0,0 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import type { GetAbsolutePath } from "../path/get-absolute-path.injectable";
export const getAbsolutePathFake: GetAbsolutePath = (...args) => {
const maybeAbsolutePath = args.join("/");
if (isAbsolutePath(maybeAbsolutePath)) {
return maybeAbsolutePath;
}
return `/some-absolute-root-directory/${maybeAbsolutePath}`;
};
const isAbsolutePath = (path: string) => path.startsWith("/");
-7
View File
@@ -1,7 +0,0 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import type { JoinPaths } from "../path/join-paths.injectable";
export const joinPathsFake: JoinPaths = (...args) => args.join("/");