repositories / pi-ext
pi-ext
bugabingas pi extensions
owned by bugabinga
extensions/uv/__tests__/harness.test.ts
RawFile outline
- t line 14
- fakeBin line 15
- afterEach line 17
- uv extension line 24
- loads in real Pi extension runtime line 25
- detects a controlled executable on PATH through the extension line 31
- warns through the extension when PATH has no uv executable line 52
- blocks pip-family commands with uv guidance line 67
- blocks venv and py_compile bytecode writes line 76
- leaves normal Python execution alone line 85
- does not expose extensionless shell shims to Windows line 92
import {
chmodSync,
copyFileSync,
mkdtempSync,
rmSync,
writeFileSync,
} from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { afterEach, describe, expect, it, vi } from "vitest";
import { createTestSession, type TestSession } from "../../../test/harness";
import uvExtension, { __test } from "../index";
let t: TestSession | undefined;
let fakeBin: string | undefined;
afterEach(() => {
t?.dispose();
if (fakeBin) rmSync(fakeBin, { recursive: true, force: true });
t = undefined;
fakeBin = undefined;
});
describe("uv extension", () => {
it("loads in real Pi extension runtime", async () => {
t = await createTestSession({ extensionFactories: [uvExtension] });
expect(t.session.extensionRunner.extensions).toHaveLength(1);
});
it("detects a controlled executable on PATH through the extension", async () => {
fakeBin = mkdtempSync(join(tmpdir(), "pi-uv-bin-"));
if (process.platform === "win32") {
copyFileSync(process.execPath, join(fakeBin, "uv.exe"));
} else {
const uv = join(fakeBin, "uv");
writeFileSync(uv, "#!/bin/sh\nexit 0\n", "utf8");
chmodSync(uv, 0o755);
}
expect(await __test.uvInstalled({ PATH: fakeBin }, process.platform)).toBe(
true,
);
t = await createTestSession({
env: { PATH: fakeBin },
extensionFactories: [uvExtension],
});
expect(t.events.uiCallsFor("notify")).toEqual([]);
});
it("warns through the extension when PATH has no uv executable", async () => {
fakeBin = mkdtempSync(join(tmpdir(), "pi-uv-bin-"));
t = await createTestSession({
env: { PATH: fakeBin },
extensionFactories: [uvExtension],
});
await vi.waitFor(() =>
expect(t?.events.uiCallsFor("notify").at(-1)?.args).toEqual([
"uv extension: uv not found on PATH",
"warning",
]),
);
});
it("blocks pip-family commands with uv guidance", () => {
expect(__test.getBlockedCommandMessage("pip install rich")).toContain(
"uv add PACKAGE",
);
expect(
__test.getBlockedCommandMessage("python -m pip install rich"),
).toContain("python -m pip");
});
it("blocks venv and py_compile bytecode writes", () => {
expect(__test.getBlockedCommandMessage("python3 -m venv .venv")).toContain(
"uv venv",
);
expect(
__test.getBlockedCommandMessage(".venv/bin/python -m py_compile x.py"),
).toContain("__pycache__");
});
it("leaves normal Python execution alone", () => {
expect(__test.getBlockedCommandMessage("python script.py")).toBeNull();
expect(
__test.getBlockedCommandMessage("uv run python script.py"),
).toBeNull();
});
it("does not expose extensionless shell shims to Windows", () => {
const ctx = {
command: "python --version",
cwd: "C:/repo",
env: { Path: "C:/Windows" },
};
expect(__test.prependInterceptedCommands(ctx, "win32")).toBe(ctx);
});
});