Skip to content

Commit

Permalink
Merge pull request #4160 from adamkpickering/3652-move-paths-to-rdctl
Browse files Browse the repository at this point in the history
Move paths tracking to `rdctl`
  • Loading branch information
ericpromislow committed Jul 5, 2023
2 parents 473cb7a + 40dadfa commit ba5ba77
Show file tree
Hide file tree
Showing 20 changed files with 611 additions and 125 deletions.
1 change: 1 addition & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ jobs:
with:
go-version: '^1.18'
- run: npm ci
- run: npm run build
- run: npm run lint:nofix
- name: Install shfmt
run: go install mvdan.cc/sh/v3/cmd/shfmt@v3.6.0
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@
/bats/bin/
/bats/bats.tar.gz
/bats/logs/
script_logs/
12 changes: 6 additions & 6 deletions pkg/rancher-desktop/main/__tests__/deploymentProfiles.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ describe('deployment profiles', () => {
[${ FULL_DEFAULTS_PATH }\\application\\Telemetry]
"ENABLED"=dword:1
[${ FULL_DEFAULTS_PATH }\\application\\extensions\\installed]
"bellingham"="WA"
"portland"="OR"
"shasta"="CA"
"elko"="NV"
[${ FULL_DEFAULTS_PATH }\\CONTAINERENGINE]
"name"="moby"
Expand All @@ -76,12 +82,6 @@ describe('deployment profiles', () => {
"montreal"=dword:1
"riviere du loup"=dword:0
"magog"=dword:0
[${ FULL_DEFAULTS_PATH }\\extensions]
"bellingham"="WA"
"portland"="OR"
"shasta"="CA"
"elko"="NV"
`;

const lockedUserRegFile = `Windows Registry Editor Version 5.00
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import os from 'os';
import path from 'path';

// The (mock) application directory.
let appDir = '';
let appDir = process.cwd();

// Mock Electron.app.getAppPath() to return appDir.
jest.mock('electron', () => {
Expand All @@ -29,6 +29,9 @@ jest.spyOn(fs.promises, 'readdir').mockImplementation((dir, encoding) => {
// eslint-disable-next-line import/first -- Need to mock first.
import { CheckerDockerCLISymlink } from '../dockerCliSymlinks';

// eslint-disable-next-line import/first -- Need to mock first.
import paths from '@pkg/utils/paths';

const { mkdtemp, rm } = jest.requireActual('fs/promises');
const describeUnix = process.platform === 'win32' ? describe.skip : describe;
const describeWin32 = process.platform === 'win32' ? describe : describe.skip;
Expand All @@ -39,13 +42,18 @@ describeUnix(CheckerDockerCLISymlink, () => {
const rdBinDir = path.join(os.homedir(), '.rd', 'bin');
const rdBinExecutable = path.join(rdBinDir, executable);
let appDirExecutable = '';
let replacedPathsResources: jest.ReplaceProperty<string>;

beforeAll(async() => {
appDir = await mkdtemp(path.join(os.tmpdir(), 'rd-diag-'));
await fs.promises.mkdir(path.join(appDir, 'resources'));
appDirExecutable = path.join(appDir, 'resources', os.platform(), 'bin', executable);
const resourcesDir = path.join(appDir, 'resources');

await fs.promises.mkdir(resourcesDir);
appDirExecutable = path.join(resourcesDir, os.platform(), 'bin', executable);
replacedPathsResources = jest.replaceProperty(paths, 'resources', resourcesDir);
});
afterAll(async() => {
replacedPathsResources.restore();
await rm(appDir, { recursive: true, force: true });
});

Expand Down
6 changes: 3 additions & 3 deletions pkg/rancher-desktop/utils/__tests__/dockerDirManager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ describe('DockerDirManager', () => {
});

describe('credHelperWorking', () => {
let resourcesPathMock: jest.SpyInstance<typeof paths.resources, []>;
let replacedPathsResources: jest.ReplaceProperty<string>;
let spawnMock: jest.SpiedFunction<typeof childProcess.spawnFile>;
const commonCredHelperExpectations: (...args: Parameters<typeof childProcess.spawnFile>) => void = (command, args, options) => {
expect(command).toEqual('docker-credential-mockhelper');
Expand All @@ -384,11 +384,11 @@ describe('DockerDirManager', () => {
};

beforeEach(() => {
resourcesPathMock = jest.spyOn(paths, 'resources', 'get').mockReturnValue('RESOURCES');
replacedPathsResources = jest.replaceProperty(paths, 'resources', 'RESOURCES');
});
afterEach(() => {
spawnMock.mockRestore();
resourcesPathMock.mockRestore();
replacedPathsResources.restore();
});

it('should return false when cred helper is not working', async() => {
Expand Down
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
Loading

0 comments on commit ba5ba77

Please sign in to comment.