Skip to content

Commit 9106b75

Browse files
lpcoxCopilot
andauthored
fix: resolve test failures on macOS (#3304)
- docker-manager-utils: Read actual root home from /etc/passwd instead of hardcoding /root (macOS uses /var/root) - docker-manager-cleanup: Mock getSafeHostUid/Gid to use real uid/gid so chownSync doesn't fail with EPERM on macOS (gid 501 gets clamped to 1000, which the current user can't chown to) Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 96582b7 commit 9106b75

2 files changed

Lines changed: 23 additions & 4 deletions

File tree

src/docker-manager-cleanup.test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,17 @@ import { mockExecaFn, mockExecaSync } from './test-helpers/mock-execa.test-utils
1111
// eslint-disable-next-line @typescript-eslint/no-require-imports
1212
jest.mock('execa', () => require('./test-helpers/mock-execa.test-utils').execaMockFactory());
1313

14+
// Mock host identity functions so chownSync uses the real uid/gid
15+
// (on macOS, gid < 1000 gets clamped to 1000 which causes EPERM)
16+
jest.mock('./host-env', () => {
17+
const actual = jest.requireActual('./host-env');
18+
return {
19+
...actual,
20+
getSafeHostUid: () => String(process.getuid?.() ?? 1000),
21+
getSafeHostGid: () => String(process.getgid?.() ?? 1000),
22+
};
23+
});
24+
1425
describe('docker-manager writeConfigs and cleanup', () => {
1526
describe('writeConfigs', () => {
1627
let testDir: string;

src/docker-manager-utils.test.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -217,8 +217,12 @@ describe('docker-manager utilities', () => {
217217
process.env.SUDO_USER = 'root';
218218
process.env.HOME = '/some/other/path';
219219

220-
// Should find root's home directory from /etc/passwd
221-
expect(getRealUserHome()).toBe('/root');
220+
// Read actual root home from /etc/passwd (differs by platform: /root on Linux, /var/root on macOS)
221+
const passwd = fs.readFileSync('/etc/passwd', 'utf-8');
222+
const rootLine = passwd.split('\n').find(line => line.startsWith('root:'));
223+
const expectedRootHome = rootLine ? rootLine.split(':')[5] : '/root';
224+
225+
expect(getRealUserHome()).toBe(expectedRootHome);
222226
});
223227

224228
it('should fall back to HOME when SUDO_USER not found in /etc/passwd', () => {
@@ -236,9 +240,13 @@ describe('docker-manager utilities', () => {
236240
process.env.SUDO_USER = 'root';
237241
process.env.HOME = '/custom/home';
238242

243+
// Read actual root home from /etc/passwd (differs by platform)
244+
const passwd = fs.readFileSync('/etc/passwd', 'utf-8');
245+
const rootLine = passwd.split('\n').find(line => line.startsWith('root:'));
246+
const expectedRootHome = rootLine ? rootLine.split(':')[5] : '/root';
247+
239248
// With getuid undefined, uid is undefined (falsy), so it attempts passwd lookup
240-
// Should find root's home directory from /etc/passwd
241-
expect(getRealUserHome()).toBe('/root');
249+
expect(getRealUserHome()).toBe(expectedRootHome);
242250
});
243251
});
244252

0 commit comments

Comments
 (0)