feat: 基于 process.argv 推断命令前缀,修复 bunx 误检测
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import { describe, it, expect } from "bun:test";
|
||||
import {
|
||||
inferFromEnvironment,
|
||||
inferFromProcess,
|
||||
getPmPrefix,
|
||||
DEFAULT_PREFIX,
|
||||
getFallbackNote,
|
||||
@@ -8,29 +8,105 @@ import {
|
||||
} from "../../src/core/pm.ts";
|
||||
import type { RuneConfig } from "../../src/types.ts";
|
||||
|
||||
describe("inferFromEnvironment", () => {
|
||||
it("execPath 包含 bun 时返回 bunx 前缀", () => {
|
||||
const result = inferFromEnvironment("/home/user/.bun/bin/bun", undefined);
|
||||
describe("inferFromProcess", () => {
|
||||
it("argv 包含 bun 缓存路径时返回 bunx 前缀", () => {
|
||||
const result = inferFromProcess(
|
||||
["/usr/local/bin/bun", "/home/user/.bun/cache/@lanyuanxiaoyao-rune/bin/rune.ts"],
|
||||
undefined,
|
||||
);
|
||||
expect(result).toBe("bunx @lanyuanxiaoyao/rune");
|
||||
});
|
||||
|
||||
it("npm_config_user_agent 包含 pnpm 时返回 pnpx 前缀", () => {
|
||||
const result = inferFromEnvironment("/usr/bin/node", "pnpm/9.0.0");
|
||||
expect(result).toBe("pnpx @lanyuanxiaoyao/rune");
|
||||
it("argv Windows 风格 bun 缓存路径时返回 bunx 前缀", () => {
|
||||
const result = inferFromProcess(
|
||||
[
|
||||
"C:\\Users\\user\\.bun\\bin\\bun.exe",
|
||||
"C:\\Users\\user\\.bun\\cache\\@lanyuanxiaoyao-rune\\bin\\rune.ts",
|
||||
],
|
||||
undefined,
|
||||
);
|
||||
expect(result).toBe("bunx @lanyuanxiaoyao/rune");
|
||||
});
|
||||
|
||||
it("npm_config_user_agent 包含 npm 时返回 npx 前缀", () => {
|
||||
const result = inferFromEnvironment("/usr/bin/node", "npm/10.0.0");
|
||||
it("argv 包含 _npx 路径时返回 npx 前缀", () => {
|
||||
const result = inferFromProcess(
|
||||
["/usr/local/bin/node", "/home/user/.npm/_npx/abc123/node_modules/.bin/rune"],
|
||||
undefined,
|
||||
);
|
||||
expect(result).toBe("npx @lanyuanxiaoyao/rune");
|
||||
});
|
||||
|
||||
it("pnpm 优先于 npm(userAgent 同时包含两者时)", () => {
|
||||
const result = inferFromEnvironment("/usr/bin/node", "pnpm/9.0.0 npm/10.0.0");
|
||||
it("argv 包含 npm-cache 路径时返回 npx 前缀", () => {
|
||||
const result = inferFromProcess(
|
||||
["/usr/local/bin/node", "/home/user/.npm-cache/_npx/abc123/bin/rune"],
|
||||
undefined,
|
||||
);
|
||||
expect(result).toBe("npx @lanyuanxiaoyao/rune");
|
||||
});
|
||||
|
||||
it("argv 包含 .pnpm 路径时返回 pnpx 前缀", () => {
|
||||
const result = inferFromProcess(
|
||||
["/usr/local/bin/node", "/home/user/.pnpm/store/@lanyuanxiaoyao-rune/bin/rune"],
|
||||
undefined,
|
||||
);
|
||||
expect(result).toBe("pnpx @lanyuanxiaoyao/rune");
|
||||
});
|
||||
|
||||
it("无法推断时返回 null", () => {
|
||||
const result = inferFromEnvironment("/usr/bin/node", undefined);
|
||||
it("argv 无法推断但 userAgent 包含 bun 时返回 bunx 前缀", () => {
|
||||
const result = inferFromProcess(
|
||||
["/usr/local/bin/bun", "/home/user/projects/app/run.ts"],
|
||||
"bun/1.0.0",
|
||||
);
|
||||
expect(result).toBe("bunx @lanyuanxiaoyao/rune");
|
||||
});
|
||||
|
||||
it("argv 无法推断但 userAgent 包含 pnpm 时返回 pnpx 前缀", () => {
|
||||
const result = inferFromProcess(
|
||||
["/usr/local/bin/node", "/home/user/projects/app/run.ts"],
|
||||
"pnpm/9.0.0",
|
||||
);
|
||||
expect(result).toBe("pnpx @lanyuanxiaoyao/rune");
|
||||
});
|
||||
|
||||
it("argv 无法推断但 userAgent 包含 yarn 时返回 yarn dlx 前缀", () => {
|
||||
const result = inferFromProcess(
|
||||
["/usr/local/bin/node", "/home/user/projects/app/run.ts"],
|
||||
"yarn/4.0.0",
|
||||
);
|
||||
expect(result).toBe("yarn dlx @lanyuanxiaoyao/rune");
|
||||
});
|
||||
|
||||
it("argv 无法推断但 userAgent 包含 npm 时返回 npx 前缀", () => {
|
||||
const result = inferFromProcess(
|
||||
["/usr/local/bin/node", "/home/user/projects/app/run.ts"],
|
||||
"npm/10.0.0",
|
||||
);
|
||||
expect(result).toBe("npx @lanyuanxiaoyao/rune");
|
||||
});
|
||||
|
||||
it("userAgent 同时包含 pnpm 和 npm 时 pnpm 优先", () => {
|
||||
const result = inferFromProcess(
|
||||
["/usr/local/bin/node", "/home/user/projects/app/run.ts"],
|
||||
"pnpm/9.0.0 npm/10.0.0",
|
||||
);
|
||||
expect(result).toBe("pnpx @lanyuanxiaoyao/rune");
|
||||
});
|
||||
|
||||
it("argv 和 userAgent 都无法推断时返回 null", () => {
|
||||
const result = inferFromProcess(
|
||||
["/usr/local/bin/node", "/home/user/projects/app/run.ts"],
|
||||
undefined,
|
||||
);
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
|
||||
it("argv 为空数组且无 userAgent 时返回 null", () => {
|
||||
const result = inferFromProcess([], undefined);
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
|
||||
it("argv 只有一个元素且无 userAgent 时返回 null", () => {
|
||||
const result = inferFromProcess(["/usr/local/bin/node"], undefined);
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user