1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00

Introduce abstraction for getting absolute paths

Co-authored-by: Mikko Aspiala <mikko.aspiala@gmail.com>

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>
This commit is contained in:
Janne Savolainen 2022-03-30 10:48:48 +03:00
parent 21f815bdea
commit 5ca3082572
No known key found for this signature in database
GPG Key ID: 8C6CFB2FFFE8F68A
4 changed files with 45 additions and 0 deletions

View File

@ -0,0 +1,20 @@
/**
* 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 GetAbsolutePath = (...args: string[]) => string;
const getAbsolutePathInjectable = getInjectable({
id: "get-absolute-path",
instantiate: (): GetAbsolutePath => path.resolve,
// This causes side effect e.g. Windows creates different kinds of
// absolute paths than linux
causesSideEffects: true,
});
export default getAbsolutePathInjectable;

View File

@ -0,0 +1,17 @@
/**
* 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("/");

View File

@ -30,6 +30,8 @@ import type { UserStore } from "../common/user-store";
import isMacInjectable from "../common/vars/is-mac.injectable";
import isWindowsInjectable from "../common/vars/is-windows.injectable";
import isLinuxInjectable from "../common/vars/is-linux.injectable";
import getAbsolutePathInjectable from "../common/path/get-absolute-path.injectable";
import { getAbsolutePathFake } from "../common/test-utils/get-absolute-path-fake";
export const getDiForUnitTesting = (
{ doGeneralOverrides } = { doGeneralOverrides: false },
@ -55,6 +57,8 @@ export const getDiForUnitTesting = (
di.override(isWindowsInjectable, () => false);
di.override(isLinuxInjectable, () => false);
di.override(getAbsolutePathInjectable, () => getAbsolutePathFake);
// eslint-disable-next-line unused-imports/no-unused-vars-ts
di.override(extensionsStoreInjectable, () => ({ isEnabled: ({ id, isBundled }) => false }) as ExtensionsStore);

View File

@ -28,6 +28,8 @@ import type { UserStore } from "../common/user-store";
import isMacInjectable from "../common/vars/is-mac.injectable";
import isWindowsInjectable from "../common/vars/is-windows.injectable";
import isLinuxInjectable from "../common/vars/is-linux.injectable";
import getAbsolutePathInjectable from "../common/path/get-absolute-path.injectable";
import { getAbsolutePathFake } from "../common/test-utils/get-absolute-path-fake";
export const getDiForUnitTesting = (
{ doGeneralOverrides } = { doGeneralOverrides: false },
@ -52,6 +54,8 @@ export const getDiForUnitTesting = (
di.override(isWindowsInjectable, () => false);
di.override(isLinuxInjectable, () => false);
di.override(getAbsolutePathInjectable, () => getAbsolutePathFake);
// eslint-disable-next-line unused-imports/no-unused-vars-ts
di.override(extensionsStoreInjectable, () => ({ isEnabled: ({ id, isBundled }) => false }) as ExtensionsStore);