Skip to content

Commit

Permalink
Update paths unit tests for rdctl paths changes
Browse files Browse the repository at this point in the history
Signed-off-by: Adam Pickering <adam.pickering@suse.com>
  • Loading branch information
adamkpickering committed Jun 20, 2023
1 parent 11b62f4 commit 8ab5238
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 28 deletions.
45 changes: 21 additions & 24 deletions pkg/rancher-desktop/utils/__tests__/paths.spec.ts
Original file line number Diff line number Diff line change
@@ -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<platform, string | Error>;
type Platform = 'darwin' | 'linux' | 'win32';
type expectedData = Record<Platform, string | Error>;

jest.mock('electron', () => {
return {
__esModule: true,
default: {
app: {
isPackaged: false,
getAppPath: () => process.cwd(),
},
},
};
});

describe('paths', () => {
const cases: Record<keyof Paths, expectedData> = {
Expand Down Expand Up @@ -84,26 +95,19 @@ 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, {
APPDATA: path.join(os.homedir(), 'AppData', 'Roaming'),
LOCALAPPDATA: path.join(os.homedir(), 'AppData', 'Local'),
});

const pathsConstructor: Record<platform, new() => 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();
Expand All @@ -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();
Expand Down
16 changes: 12 additions & 4 deletions pkg/rancher-desktop/utils/paths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down Expand Up @@ -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');
}
Expand All @@ -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');
}
Expand Down Expand Up @@ -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.`);
}
Expand Down

0 comments on commit 8ab5238

Please sign in to comment.