|
| 1 | +import os from 'node:os'; |
| 2 | +import path from 'node:path'; |
| 3 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
| 4 | + |
| 5 | +import { Utils } from './index.js'; |
| 6 | + |
| 7 | +describe('getShellRcFiles / spawn-flag agreement', () => { |
| 8 | + const homeDir = os.homedir(); |
| 9 | + const originalShell = process.env.SHELL; |
| 10 | + |
| 11 | + beforeEach(() => { |
| 12 | + vi.restoreAllMocks(); |
| 13 | + }); |
| 14 | + |
| 15 | + afterEach(() => { |
| 16 | + if (originalShell === undefined) { |
| 17 | + delete process.env.SHELL; |
| 18 | + } else { |
| 19 | + process.env.SHELL = originalShell; |
| 20 | + } |
| 21 | + vi.restoreAllMocks(); |
| 22 | + }); |
| 23 | + |
| 24 | + it('prefers .bashrc for non-login bash on macOS (the CI runner case)', () => { |
| 25 | + process.env.SHELL = '/bin/bash'; |
| 26 | + vi.spyOn(Utils, 'isLinux').mockReturnValue(false); |
| 27 | + vi.spyOn(Utils, 'isMacOS').mockReturnValue(true); |
| 28 | + |
| 29 | + expect(Utils.needsLoginShell()).toBe(false); |
| 30 | + expect(Utils.getPrimaryShellRc()).toBe(path.join(homeDir, '.bashrc')); |
| 31 | + }); |
| 32 | + |
| 33 | + it('prefers .bashrc for non-login bash on Linux', () => { |
| 34 | + process.env.SHELL = '/bin/bash'; |
| 35 | + vi.spyOn(Utils, 'isLinux').mockReturnValue(true); |
| 36 | + vi.spyOn(Utils, 'isMacOS').mockReturnValue(false); |
| 37 | + |
| 38 | + expect(Utils.needsLoginShell()).toBe(false); |
| 39 | + expect(Utils.getPrimaryShellRc()).toBe(path.join(homeDir, '.bashrc')); |
| 40 | + }); |
| 41 | + |
| 42 | + it('prefers .bash_profile when a login shell is used (SHELL unset)', () => { |
| 43 | + delete process.env.SHELL; |
| 44 | + vi.spyOn(os, 'userInfo').mockReturnValue({ shell: '/bin/bash' } as ReturnType<typeof os.userInfo>); |
| 45 | + |
| 46 | + expect(Utils.needsLoginShell()).toBe(true); |
| 47 | + expect(Utils.getPrimaryShellRc()).toBe(path.join(homeDir, '.bash_profile')); |
| 48 | + }); |
| 49 | + |
| 50 | + it('prefers .zshrc for zsh, which both login and non-login zsh source', () => { |
| 51 | + process.env.SHELL = '/bin/zsh'; |
| 52 | + |
| 53 | + expect(Utils.getPrimaryShellRc()).toBe(path.join(homeDir, '.zshrc')); |
| 54 | + }); |
| 55 | +}); |
0 commit comments