|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import { runsWithoutInstallPrompt } from "./appleStubs.js"; |
| 3 | + |
| 4 | +/** A Mac whose filesystem holds exactly `files`; `link` is where xcode-select's choice points, if it made one. */ |
| 5 | +function mac(files: string[], { env = {}, link }: { env?: NodeJS.ProcessEnv; link?: string } = {}) { |
| 6 | + return { |
| 7 | + platform: "darwin" as const, |
| 8 | + env, |
| 9 | + exists: (path: string) => files.includes(path), |
| 10 | + executable: (path: string) => files.includes(path), |
| 11 | + realpath: (path: string) => (path === "/var/db/xcode_select_link" ? (link ?? null) : null), |
| 12 | + }; |
| 13 | +} |
| 14 | +const TOOLS = "/Library/Developer/CommandLineTools/usr/bin"; |
| 15 | + |
| 16 | +describe("runsWithoutInstallPrompt", () => { |
| 17 | + it("refuses macOS's git, xcrun and cc stubs when no developer tools are installed", () => { |
| 18 | + for (const command of ["/usr/bin/git", "xcrun", "/usr/bin/cc"]) |
| 19 | + expect(runsWithoutInstallPrompt(command, mac([])), command).toBe(false); |
| 20 | + }); |
| 21 | + |
| 22 | + it("runs them once the Command Line Tools are installed", () => { |
| 23 | + expect(runsWithoutInstallPrompt("/usr/bin/git", mac([TOOLS]))).toBe(true); |
| 24 | + }); |
| 25 | + |
| 26 | + it("runs them on a Mac with only Xcode.app, where xcode-select never chose a folder", () => { |
| 27 | + const xcode = "/Applications/Xcode.app/Contents/Developer/usr/bin"; |
| 28 | + for (const command of ["/usr/bin/git", "xcrun", "/usr/bin/cc"]) |
| 29 | + expect(runsWithoutInstallPrompt(command, mac([xcode])), command).toBe(true); |
| 30 | + }); |
| 31 | + |
| 32 | + it("follows the developer folder xcode-select chose, or DEVELOPER_DIR", () => { |
| 33 | + const xcode = "/Applications/Xcode-beta.app/Contents/Developer"; |
| 34 | + expect(runsWithoutInstallPrompt("xcrun", mac([`${xcode}/usr/bin`], { link: xcode }))).toBe( |
| 35 | + true, |
| 36 | + ); |
| 37 | + expect( |
| 38 | + runsWithoutInstallPrompt( |
| 39 | + "xcrun", |
| 40 | + mac([`${xcode}/usr/bin`], { env: { DEVELOPER_DIR: xcode } }), |
| 41 | + ), |
| 42 | + ).toBe(true); |
| 43 | + }); |
| 44 | + |
| 45 | + it("runs a git outside /usr/bin, a /usr/bin tool that is no stub, and anything off macOS", () => { |
| 46 | + expect(runsWithoutInstallPrompt("/opt/homebrew/bin/git", mac([]))).toBe(true); |
| 47 | + expect(runsWithoutInstallPrompt("/usr/bin/which", mac([]))).toBe(true); |
| 48 | + expect(runsWithoutInstallPrompt("/usr/bin/git", { ...mac([]), platform: "linux" })).toBe(true); |
| 49 | + }); |
| 50 | +}); |
| 51 | + |
| 52 | +describe("runsWithoutInstallPrompt with a bare name", () => { |
| 53 | + it("runs the first executable on PATH, found without running anything", () => { |
| 54 | + const env = { PATH: "/opt/homebrew/bin:/usr/bin" }; |
| 55 | + const brew = mac(["/opt/homebrew/bin/git", "/usr/bin/git"], { env }); |
| 56 | + expect(runsWithoutInstallPrompt("git", brew)).toBe(true); |
| 57 | + expect(runsWithoutInstallPrompt("git", mac(["/usr/bin/git"], { env }))).toBe(false); |
| 58 | + }); |
| 59 | +}); |
0 commit comments