|
| 1 | +import { describe, expect, it, vi } from "vitest"; |
| 2 | +import { resolveManagedPythonCommand } from "./definitions.js"; |
| 3 | + |
| 4 | +describe("resolveManagedPythonCommand", () => { |
| 5 | + it("returns the first available candidate on POSIX hosts without probing", async () => { |
| 6 | + const commandExists = vi.fn(async (cmd: string) => cmd === "python3"); |
| 7 | + const runCommand = vi.fn(); |
| 8 | + |
| 9 | + await expect( |
| 10 | + resolveManagedPythonCommand(commandExists, "linux", runCommand as never) |
| 11 | + ).resolves.toBe("python3"); |
| 12 | + // POSIX hosts do not have Microsoft Store stubs; the helper must NOT |
| 13 | + // execute the candidate just to check the version. |
| 14 | + expect(runCommand).not.toHaveBeenCalled(); |
| 15 | + }); |
| 16 | + |
| 17 | + it("returns null when no candidate is on PATH", async () => { |
| 18 | + await expect( |
| 19 | + resolveManagedPythonCommand( |
| 20 | + vi.fn(async () => false), |
| 21 | + "linux" |
| 22 | + ) |
| 23 | + ).resolves.toBeNull(); |
| 24 | + }); |
| 25 | + |
| 26 | + it("on Windows, rejects a candidate whose `--version` prints nothing (Store stub)", async () => { |
| 27 | + // Windows ships zero-byte App Execution Aliases for `python` / |
| 28 | + // `python3`. `where.exe` reports them as present, but invoking them |
| 29 | + // silently exits with empty stdout/stderr because Python is not |
| 30 | + // actually installed. |
| 31 | + const commandExists = vi.fn(async () => true); |
| 32 | + const runCommand = vi.fn(async () => ({ stdout: "", stderr: "" })); |
| 33 | + |
| 34 | + await expect( |
| 35 | + resolveManagedPythonCommand(commandExists, "win32", runCommand) |
| 36 | + ).resolves.toBeNull(); |
| 37 | + expect(runCommand).toHaveBeenCalledTimes(2); |
| 38 | + expect(runCommand).toHaveBeenCalledWith( |
| 39 | + "python3", |
| 40 | + ["--version"], |
| 41 | + expect.objectContaining({ windowsHide: true }) |
| 42 | + ); |
| 43 | + expect(runCommand).toHaveBeenCalledWith( |
| 44 | + "python", |
| 45 | + ["--version"], |
| 46 | + expect.objectContaining({ windowsHide: true }) |
| 47 | + ); |
| 48 | + }); |
| 49 | + |
| 50 | + it("on Windows, accepts a candidate whose --version prints output on stdout", async () => { |
| 51 | + const commandExists = vi.fn(async () => true); |
| 52 | + const runCommand = vi.fn(async () => ({ stdout: "Python 3.12.0\n", stderr: "" })); |
| 53 | + |
| 54 | + await expect(resolveManagedPythonCommand(commandExists, "win32", runCommand)).resolves.toBe( |
| 55 | + "python3" |
| 56 | + ); |
| 57 | + }); |
| 58 | + |
| 59 | + it("on Windows, accepts a candidate whose --version prints to stderr (older Pythons)", async () => { |
| 60 | + // Pythons < 3.4 print the version banner to stderr instead of stdout. |
| 61 | + const commandExists = vi.fn(async () => true); |
| 62 | + const runCommand = vi.fn(async () => ({ stdout: "", stderr: "Python 2.7.18\n" })); |
| 63 | + |
| 64 | + await expect(resolveManagedPythonCommand(commandExists, "win32", runCommand)).resolves.toBe( |
| 65 | + "python3" |
| 66 | + ); |
| 67 | + }); |
| 68 | + |
| 69 | + it("on Windows, falls through to the next candidate when the first one's probe fails to spawn", async () => { |
| 70 | + const commandExists = vi.fn(async () => true); |
| 71 | + const runCommand = vi.fn(async (file: string) => { |
| 72 | + if (file === "python3") { |
| 73 | + // simulate spawn failure (file is a stub that can't be executed) |
| 74 | + throw new Error("spawn python3 ENOENT"); |
| 75 | + } |
| 76 | + return { stdout: "Python 3.12.0\n", stderr: "" }; |
| 77 | + }); |
| 78 | + |
| 79 | + await expect(resolveManagedPythonCommand(commandExists, "win32", runCommand)).resolves.toBe( |
| 80 | + "python" |
| 81 | + ); |
| 82 | + }); |
| 83 | + |
| 84 | + it("on Windows, returns null when both candidates are stubs that print nothing", async () => { |
| 85 | + const commandExists = vi.fn(async () => true); |
| 86 | + const runCommand = vi.fn(async () => ({ stdout: "", stderr: "" })); |
| 87 | + |
| 88 | + await expect( |
| 89 | + resolveManagedPythonCommand(commandExists, "win32", runCommand) |
| 90 | + ).resolves.toBeNull(); |
| 91 | + }); |
| 92 | +}); |
0 commit comments