|
| 1 | +/** |
| 2 | + * Host environment probe — MSYS2 bash detection. |
| 3 | + * |
| 4 | + * Pins the Windows shell probe against native MSYS2 toolchains: a git whose |
| 5 | + * `git --exec-path` reports an `ucrt64` / `clang64` / `clangarm64` prefix |
| 6 | + * (e.g. `C:/msys64/ucrt64/libexec/git-core`) must walk back to the MSYS2 root |
| 7 | + * and resolve the shared bash at `usr\bin\bash.exe`, instead of failing to |
| 8 | + * detect any shell. |
| 9 | + * |
| 10 | + * All tests expect `probeHostEnvironment()` to be a pure function of injected |
| 11 | + * platform probes (no ambient state) so the same suite runs identically on |
| 12 | + * macOS/Linux/Windows CI runners. |
| 13 | + * |
| 14 | + * Ported from `packages/kaos/test/environment.test.ts` (the MSYS2 cases added |
| 15 | + * by the bash-detection fix); the v1 file carries the full POSIX / Git for |
| 16 | + * Windows / Scoop shim matrix, which the vendored probe shares verbatim. |
| 17 | + */ |
| 18 | + |
| 19 | +import { describe, expect, it } from 'vitest'; |
| 20 | + |
| 21 | +import { |
| 22 | + probeHostEnvironment, |
| 23 | + type HostEnvironmentProbeDeps, |
| 24 | +} from '#/_base/execEnv/environmentProbe'; |
| 25 | + |
| 26 | +interface StubOpts { |
| 27 | + readonly platform: string; |
| 28 | + readonly env?: Record<string, string | undefined>; |
| 29 | + readonly existingPaths?: readonly string[]; |
| 30 | + readonly execFileResults?: Readonly<Record<string, string>>; |
| 31 | +} |
| 32 | + |
| 33 | +/** Build a stub deps bag mimicking Node's `os` + `process` surface. */ |
| 34 | +function stubDeps(opts: StubOpts): HostEnvironmentProbeDeps { |
| 35 | + const existing = new Set(opts.existingPaths ?? []); |
| 36 | + return { |
| 37 | + platform: opts.platform, |
| 38 | + arch: 'x86_64', |
| 39 | + release: '1.2.3', |
| 40 | + homeDir: 'C:\\Users\\me', |
| 41 | + env: opts.env ?? {}, |
| 42 | + isFile: async (path: string) => existing.has(path), |
| 43 | + execFileText: async (file: string, args: readonly string[]) => |
| 44 | + opts.execFileResults?.[execFileKey(file, args)], |
| 45 | + }; |
| 46 | +} |
| 47 | + |
| 48 | +function execFileKey(file: string, args: readonly string[]): string { |
| 49 | + return [file, ...args].join('\0'); |
| 50 | +} |
| 51 | + |
| 52 | +describe('probeHostEnvironment', () => { |
| 53 | + it('resolves MSYS2 ucrt64 native git through git --exec-path', async () => { |
| 54 | + const gitExe = 'C:\\msys64\\ucrt64\\bin\\git.exe'; |
| 55 | + const env = await probeHostEnvironment( |
| 56 | + stubDeps({ |
| 57 | + platform: 'win32', |
| 58 | + env: { PATH: 'C:\\msys64\\ucrt64\\bin' }, |
| 59 | + execFileResults: { |
| 60 | + [execFileKey(gitExe, ['--exec-path'])]: 'C:/msys64/ucrt64/libexec/git-core\n', |
| 61 | + }, |
| 62 | + existingPaths: [gitExe, 'C:\\msys64\\usr\\bin\\bash.exe'], |
| 63 | + }), |
| 64 | + ); |
| 65 | + expect(env.shellName).toBe('bash'); |
| 66 | + expect(env.shellPath).toBe('C:\\msys64\\usr\\bin\\bash.exe'); |
| 67 | + }); |
| 68 | + |
| 69 | + it('resolves MSYS2 clang64 native git through git --exec-path', async () => { |
| 70 | + const gitExe = 'C:\\msys64\\clang64\\bin\\git.exe'; |
| 71 | + const env = await probeHostEnvironment( |
| 72 | + stubDeps({ |
| 73 | + platform: 'win32', |
| 74 | + env: { PATH: 'C:\\msys64\\clang64\\bin' }, |
| 75 | + execFileResults: { |
| 76 | + [execFileKey(gitExe, ['--exec-path'])]: 'C:/msys64/clang64/libexec/git-core\n', |
| 77 | + }, |
| 78 | + existingPaths: [gitExe, 'C:\\msys64\\usr\\bin\\bash.exe'], |
| 79 | + }), |
| 80 | + ); |
| 81 | + expect(env.shellName).toBe('bash'); |
| 82 | + expect(env.shellPath).toBe('C:\\msys64\\usr\\bin\\bash.exe'); |
| 83 | + }); |
| 84 | + |
| 85 | + it('resolves MSYS2 clangarm64 native git through git --exec-path', async () => { |
| 86 | + const gitExe = 'C:\\msys64\\clangarm64\\bin\\git.exe'; |
| 87 | + const env = await probeHostEnvironment( |
| 88 | + stubDeps({ |
| 89 | + platform: 'win32', |
| 90 | + env: { PATH: 'C:\\msys64\\clangarm64\\bin' }, |
| 91 | + execFileResults: { |
| 92 | + [execFileKey(gitExe, ['--exec-path'])]: 'C:/msys64/clangarm64/libexec/git-core\n', |
| 93 | + }, |
| 94 | + existingPaths: [gitExe, 'C:\\msys64\\usr\\bin\\bash.exe'], |
| 95 | + }), |
| 96 | + ); |
| 97 | + expect(env.shellName).toBe('bash'); |
| 98 | + expect(env.shellPath).toBe('C:\\msys64\\usr\\bin\\bash.exe'); |
| 99 | + }); |
| 100 | +}); |
0 commit comments