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:
parent
21f815bdea
commit
5ca3082572
20
src/common/path/get-absolute-path.injectable.ts
Normal file
20
src/common/path/get-absolute-path.injectable.ts
Normal 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;
|
||||
17
src/common/test-utils/get-absolute-path-fake.ts
Normal file
17
src/common/test-utils/get-absolute-path-fake.ts
Normal 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("/");
|
||||
@ -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);
|
||||
|
||||
|
||||
@ -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);
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user