Replace jest.mock's with overriding dependencies (#6014)

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>
This commit is contained in:
Janne Savolainen authored and GitHub committed 2022-08-23 13:04:00 +03:00
1 parent 03d2389f01
commit 81f7bd3c2c
50 files changed
+788 -605

No files matched your search

@@ -0,0 +1,10 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import statInjectable from "./stat.injectable";
import { getGlobalOverride } from "../../test-utils/get-global-override";
export default getGlobalOverride(statInjectable, () => () => {
throw new Error("Tried to call stat without explicit override");
});
+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 fsInjectable from "../fs.injectable";
const statInjectable = getInjectable({
id: "stat",
instantiate: (di) => di.inject(fsInjectable).stat,
});
export default statInjectable;
@@ -0,0 +1,76 @@
/**
* 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 { AsyncResult } from "../utils/async-result";
import { isErrnoException } from "../utils";
import type { Stats } from "fs-extra";
import { lowerFirst } from "lodash/fp";
import statInjectable from "./stat/stat.injectable";
export type ValidateDirectory = (path: string) => Promise<AsyncResult<undefined>>;
function getUserReadableFileType(stats: Stats): string {
if (stats.isFile()) {
return "a file";
}
if (stats.isFIFO()) {
return "a pipe";
}
if (stats.isSocket()) {
return "a socket";
}
if (stats.isBlockDevice()) {
return "a block device";
}
if (stats.isCharacterDevice()) {
return "a character device";
}
return "an unknown file type";
}
const validateDirectoryInjectable = getInjectable({
id: "validate-directory",
instantiate: (di): ValidateDirectory => {
const stat = di.inject(statInjectable);
return async (path) => {
try {
const stats = await stat(path);
if (stats.isDirectory()) {
return { callWasSuccessful: true, response: undefined };
}
return { callWasSuccessful: false, error: `the provided path is ${getUserReadableFileType(stats)} and not a directory.` };
} catch (error) {
if (!isErrnoException(error)) {
return { callWasSuccessful: false, error: "of an unknown error, please try again." };
}
const humanReadableErrors: Record<string, string> = {
ENOENT: "the provided path does not exist.",
EACCES: "search permissions is denied for one of the directories in the prefix of the provided path.",
ELOOP: "the provided path is a sym-link which points to a chain of sym-links that is too long to resolve. Perhaps it is cyclic.",
ENAMETOOLONG: "the pathname is too long to be used.",
ENOTDIR: "a prefix of the provided path is not a directory.",
};
const humanReadableError = error.code
? humanReadableErrors[error.code]
: lowerFirst(String(error));
return { callWasSuccessful: false, error: humanReadableError };
}
};
},
});
export default validateDirectoryInjectable;
@@ -0,0 +1,10 @@
/**
* 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 watchInjectable from "./watch.injectable";
export default getGlobalOverride(watchInjectable, () => () => {
throw new Error("Tried to call file system watch without explicit override");
});
+18
View File
@@ -0,0 +1,18 @@
/**
* 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 { FSWatcher, WatchOptions } from "chokidar";
import { watch } from "chokidar";
export type Watch = (path: string, options?: WatchOptions) => FSWatcher;
// TODO: Introduce wrapper to allow simpler API
const watchInjectable = getInjectable({
id: "watch",
instantiate: (): Watch => watch,
causesSideEffects: true,
});
export default watchInjectable;