tarCommand.test.ts 1.3 KB

12345678910111213141516171819202122232425262728293031323334
  1. import { describe, expect, it, vi } from "vitest";
  2. import { isTarCommand, resolveHostTarCommand } from "../src/packaging/tarCommand.js";
  3. function stubPlatform(platform: NodeJS.Platform) {
  4. vi.spyOn(process, "platform", "get").mockReturnValue(platform);
  5. }
  6. describe("resolveHostTarCommand", () => {
  7. it("非 Windows 宿主原样返回 PATH 解析的 tar,不触碰 System32(CR 复审回归)", () => {
  8. stubPlatform("linux");
  9. // 曾在平台判断前无条件解析 System32/tar.exe,POSIX 归档会直接抛错。
  10. expect(resolveHostTarCommand()).toBe("tar");
  11. stubPlatform("darwin");
  12. expect(resolveHostTarCommand()).toBe("tar");
  13. });
  14. it("Windows 宿主缺失 System32 tar.exe 时 fail-fast,不回退 PATH", () => {
  15. stubPlatform("win32");
  16. const original = process.env.SystemRoot;
  17. process.env.SystemRoot = "Z:\\definitely-missing";
  18. try {
  19. expect(() => resolveHostTarCommand()).toThrow(/System32 tar\.exe missing/u);
  20. } finally {
  21. process.env.SystemRoot = original;
  22. }
  23. });
  24. it("isTarCommand 同时命中裸 tar 与 System32 绝对路径形态", () => {
  25. expect(isTarCommand("tar")).toBe(true);
  26. expect(isTarCommand("C:\\Windows\\System32\\tar.exe")).toBe(true);
  27. expect(isTarCommand("/usr/bin/tar")).toBe(true);
  28. expect(isTarCommand("zip")).toBe(false);
  29. });
  30. });