|
| 1 | +import { promisify } from "node:util"; |
| 2 | +import { beforeEach, expect, it, jest } from "@jest/globals"; |
| 3 | +import { createSetupInputs } from "../helpers/setup-inputs"; |
| 4 | + |
| 5 | +const mockExecFile = |
| 6 | + jest.fn< |
| 7 | + (...args: unknown[]) => Promise<{ stdout: string; stderr: string }> |
| 8 | + >(); |
| 9 | +const inputs = createSetupInputs({ |
| 10 | + activateEnvironment: true, |
| 11 | + pythonVersion: "3.15t", |
| 12 | +}); |
| 13 | + |
| 14 | +jest.unstable_mockModule("node:child_process", () => ({ |
| 15 | + // execFile's custom promisifier returns both stdout and stderr. |
| 16 | + execFile: Object.assign(mockExecFile, { [promisify.custom]: mockExecFile }), |
| 17 | +})); |
| 18 | + |
| 19 | +const { getPythonRuntimeId } = await import("../../src/utils/python-runtime"); |
| 20 | + |
| 21 | +beforeEach(() => { |
| 22 | + mockExecFile.mockReset(); |
| 23 | + mockExecFile.mockResolvedValue({ |
| 24 | + stderr: "", |
| 25 | + stdout: '[{"key":"cpython-3.13.1-linux-x86_64-gnu"}]\r\n', |
| 26 | + }); |
| 27 | +}); |
| 28 | + |
| 29 | +it("does not query uv without environment activation", async () => { |
| 30 | + expect( |
| 31 | + await getPythonRuntimeId({ ...inputs, activateEnvironment: false }), |
| 32 | + ).toBe(""); |
| 33 | + expect(mockExecFile).not.toHaveBeenCalled(); |
| 34 | +}); |
| 35 | + |
| 36 | +it.each([ |
| 37 | + "cpython-3.13.1-linux-x86_64-gnu", |
| 38 | + "cpython-3.15.0rc1+freethreaded-macos-aarch64-none", |
| 39 | + "cpython-3.15.0rc2+freethreaded-windows-x86_64-none", |
| 40 | + "pypy-3.11.15-linux-x86_64-gnu", |
| 41 | +])("returns uv's opaque runtime key unchanged: %s", async (key) => { |
| 42 | + mockExecFile.mockResolvedValue({ |
| 43 | + stderr: "", |
| 44 | + stdout: `${JSON.stringify([{ key }])}\r\n`, |
| 45 | + }); |
| 46 | + expect(await getPythonRuntimeId(inputs)).toBe(key); |
| 47 | +}); |
| 48 | + |
| 49 | +it.each(['/runner temp/a "quoted" venv', "C:\\runner temp\\custom venv"])( |
| 50 | + "queries the exact venv directory: %s", |
| 51 | + async (venvPath) => { |
| 52 | + await getPythonRuntimeId({ ...inputs, venvPath }); |
| 53 | + expect(mockExecFile).toHaveBeenCalledWith( |
| 54 | + "uv", |
| 55 | + [ |
| 56 | + "python", |
| 57 | + "list", |
| 58 | + venvPath, |
| 59 | + "--only-installed", |
| 60 | + "--output-format", |
| 61 | + "json", |
| 62 | + ], |
| 63 | + { encoding: "utf8" }, |
| 64 | + ); |
| 65 | + }, |
| 66 | +); |
| 67 | + |
| 68 | +it.each([ |
| 69 | + new Error("uv failed"), |
| 70 | + "not JSON", |
| 71 | + "null", |
| 72 | + "{}", |
| 73 | + "[]", |
| 74 | + '[{"key":""}]', |
| 75 | + '[{"key":123}]', |
| 76 | + '[{"key":"first"},{"key":"second"}]', |
| 77 | +])("rejects uv failure or invalid results: %s", async (result) => { |
| 78 | + if (result instanceof Error) { |
| 79 | + mockExecFile.mockRejectedValue(result); |
| 80 | + } else { |
| 81 | + mockExecFile.mockResolvedValue({ stderr: "", stdout: result }); |
| 82 | + } |
| 83 | + await expect(getPythonRuntimeId(inputs)).rejects.toThrow( |
| 84 | + "Failed to identify the activated environment's Python runtime:", |
| 85 | + ); |
| 86 | +}); |
0 commit comments