diff --git a/src/common/path/get-absolute-path.injectable.ts b/src/common/path/get-absolute-path.injectable.ts new file mode 100644 index 0000000000..8919605942 --- /dev/null +++ b/src/common/path/get-absolute-path.injectable.ts @@ -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; diff --git a/src/common/test-utils/get-absolute-path-fake.ts b/src/common/test-utils/get-absolute-path-fake.ts new file mode 100644 index 0000000000..89b5faa446 --- /dev/null +++ b/src/common/test-utils/get-absolute-path-fake.ts @@ -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("/"); diff --git a/src/main/getDiForUnitTesting.ts b/src/main/getDiForUnitTesting.ts index 94ce4a9519..8015128359 100644 --- a/src/main/getDiForUnitTesting.ts +++ b/src/main/getDiForUnitTesting.ts @@ -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); diff --git a/src/renderer/getDiForUnitTesting.tsx b/src/renderer/getDiForUnitTesting.tsx index 7cf93a661d..78ed414798 100644 --- a/src/renderer/getDiForUnitTesting.tsx +++ b/src/renderer/getDiForUnitTesting.tsx @@ -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);