From c5b14c48d8a89960f73869bfd43db59798bab637 Mon Sep 17 00:00:00 2001 From: Janne Savolainen Date: Wed, 30 Mar 2022 12:04:15 +0300 Subject: [PATCH] Introduce abstraction for joining paths Signed-off-by: Janne Savolainen --- src/common/path/join-paths.injectable.ts | 18 ++++++++++++++++++ src/common/test-utils/join-paths-fake.ts | 8 ++++++++ src/main/getDiForUnitTesting.ts | 3 +++ src/renderer/getDiForUnitTesting.tsx | 3 +++ 4 files changed, 32 insertions(+) create mode 100644 src/common/path/join-paths.injectable.ts create mode 100644 src/common/test-utils/join-paths-fake.ts diff --git a/src/common/path/join-paths.injectable.ts b/src/common/path/join-paths.injectable.ts new file mode 100644 index 0000000000..dc63b48307 --- /dev/null +++ b/src/common/path/join-paths.injectable.ts @@ -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 path from "path"; + +export type JoinPaths = (...args: string[]) => string; + +const joinPathsInjectable = getInjectable({ + id: "join-paths", + instantiate: (): JoinPaths => path.join, + + // This causes side effect e.g. Windows uses different separator than e.g. linux + causesSideEffects: true, +}); + +export default joinPathsInjectable; diff --git a/src/common/test-utils/join-paths-fake.ts b/src/common/test-utils/join-paths-fake.ts new file mode 100644 index 0000000000..99dd42a8ec --- /dev/null +++ b/src/common/test-utils/join-paths-fake.ts @@ -0,0 +1,8 @@ +/** + * Copyright (c) OpenLens Authors. All rights reserved. + * Licensed under MIT License. See LICENSE in root directory for more information. + */ +import { join } from "lodash/fp"; +import type { JoinPaths } from "../path/join-paths.injectable"; + +export const joinPathsFake: JoinPaths = join("/"); diff --git a/src/main/getDiForUnitTesting.ts b/src/main/getDiForUnitTesting.ts index 8015128359..78a4b69f3c 100644 --- a/src/main/getDiForUnitTesting.ts +++ b/src/main/getDiForUnitTesting.ts @@ -32,6 +32,8 @@ 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"; +import joinPathsInjectable from "../common/path/join-paths.injectable"; +import { joinPathsFake } from "../common/test-utils/join-paths-fake"; export const getDiForUnitTesting = ( { doGeneralOverrides } = { doGeneralOverrides: false }, @@ -58,6 +60,7 @@ export const getDiForUnitTesting = ( di.override(isLinuxInjectable, () => false); di.override(getAbsolutePathInjectable, () => getAbsolutePathFake); + di.override(joinPathsInjectable, () => joinPathsFake); // 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 78ed414798..8c603525c8 100644 --- a/src/renderer/getDiForUnitTesting.tsx +++ b/src/renderer/getDiForUnitTesting.tsx @@ -30,6 +30,8 @@ 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"; +import joinPathsInjectable from "../common/path/join-paths.injectable"; +import { joinPathsFake } from "../common/test-utils/join-paths-fake"; export const getDiForUnitTesting = ( { doGeneralOverrides } = { doGeneralOverrides: false }, @@ -55,6 +57,7 @@ export const getDiForUnitTesting = ( di.override(isLinuxInjectable, () => false); di.override(getAbsolutePathInjectable, () => getAbsolutePathFake); + di.override(joinPathsInjectable, () => joinPathsFake); // eslint-disable-next-line unused-imports/no-unused-vars-ts di.override(extensionsStoreInjectable, () => ({ isEnabled: ({ id, isBundled }) => false }) as ExtensionsStore);