diff --git a/pkg/rancher-desktop/utils/__tests__/paths.spec.ts b/pkg/rancher-desktop/utils/__tests__/paths.spec.ts index 51c71fb9925..5bf0e17071b 100644 --- a/pkg/rancher-desktop/utils/__tests__/paths.spec.ts +++ b/pkg/rancher-desktop/utils/__tests__/paths.spec.ts @@ -1,13 +1,24 @@ import os from 'os'; import path from 'path'; -import paths, { Paths, DarwinPaths, Win32Paths, LinuxPaths } from '../paths'; +import paths, { Paths } from '../paths'; -const CURRENT_DIR = path.resolve('.'); -const RESOURCES_PATH = path.join(CURRENT_DIR, 'resources'); +const RESOURCES_PATH = path.join(process.cwd(), 'resources'); -type platform = 'darwin' | 'linux' | 'win32'; -type expectedData = Record; +type Platform = 'darwin' | 'linux' | 'win32'; +type expectedData = Record; + +jest.mock('electron', () => { + return { + __esModule: true, + default: { + app: { + isPackaged: false, + getAppPath: () => process.cwd(), + }, + }, + }; +}); describe('paths', () => { const cases: Record = { @@ -84,8 +95,10 @@ describe('paths', () => { }; const table = Object.entries(cases).flatMap( - ([prop, data]) => Object.entries(data).map<[string, platform, string|Error]>( - ([platform, expected]) => [prop, platform as platform, expected])); + ([prop, data]) => Object.entries(data).map<[string, Platform, string|Error]>( + ([platform, expected]) => [prop, platform as Platform, expected], + ), + ).filter(([_, platform]) => platform === process.platform); // Make a fake environment, because these would not be available on mac. const env = Object.assign(process.env, { @@ -93,17 +106,8 @@ describe('paths', () => { LOCALAPPDATA: path.join(os.homedir(), 'AppData', 'Local'), }); - const pathsConstructor: Record Paths> = { - darwin: DarwinPaths, - linux: LinuxPaths, - win32: Win32Paths, - }; - - test.each(table)('.%s (%s)', (prop, platform, expected) => { - expect(pathsConstructor).toHaveProperty(platform); - + test.each(table)('.%s (%s)', (prop, _, expected) => { const propName = prop as keyof Paths; - const paths = new pathsConstructor[platform](); if (expected instanceof Error) { expect(() => paths[propName]).toThrow(); @@ -125,13 +129,6 @@ describe('paths', () => { } }); - it('should should be for the correct platform', () => { - const platform = os.platform(); - - expect(pathsConstructor).toHaveProperty(platform); - expect(paths).toBeInstanceOf(pathsConstructor[os.platform() as platform]); - }); - it('lima should be in one of the main subtrees', () => { const pathsToDelete = [paths.cache, paths.appHome, paths.config, paths.logs]; const platform = os.platform(); diff --git a/pkg/rancher-desktop/utils/paths.ts b/pkg/rancher-desktop/utils/paths.ts index 87718226dae..fa27a6640bb 100644 --- a/pkg/rancher-desktop/utils/paths.ts +++ b/pkg/rancher-desktop/utils/paths.ts @@ -8,7 +8,7 @@ import path from 'path'; import electron from 'electron'; -interface Paths { +export interface Paths { /** appHome: the location of the main appdata directory. */ appHome: string; /** altAppHome is a secondary directory for application data. */ @@ -53,6 +53,10 @@ export class UnixPaths implements Paths { deploymentProfileUser = ''; extensionRoot = ''; + constructor(pathsData: object) { + Object.assign(this, pathsData); + } + get wslDistro(): string { throw new Error('wslDistro not available for Unix'); } @@ -73,6 +77,10 @@ export class WindowsPaths implements Paths { wslDistro = ''; wslDistroData = ''; + constructor(pathsData: object) { + Object.assign(this, pathsData); + } + get lima(): string { throw new Error('lima not available for Windows'); } @@ -136,11 +144,11 @@ function getPaths(): Paths { switch (process.platform) { case 'darwin': - return pathsData as UnixPaths; + return new UnixPaths(pathsData); case 'linux': - return pathsData as UnixPaths; + return new UnixPaths(pathsData); case 'win32': - return pathsData as WindowsPaths; + return new WindowsPaths(pathsData); default: throw new Error(`Platform "${ process.platform }" is not supported.`); }