test: stub gh cli spawn and refine PATH cleanup
This commit is contained in:
parent
7b9e20f2fa
commit
8e02cab307
@ -3,15 +3,60 @@ import * as gh from "./gh"
|
|||||||
|
|
||||||
describe("gh cli check", () => {
|
describe("gh cli check", () => {
|
||||||
describe("getGhCliInfo", () => {
|
describe("getGhCliInfo", () => {
|
||||||
it("returns gh cli info structure", async () => {
|
function createProc(opts: { stdout?: string; stderr?: string; exitCode?: number }) {
|
||||||
// #given
|
const stdoutText = opts.stdout ?? ""
|
||||||
// #when checking gh cli info
|
const stderrText = opts.stderr ?? ""
|
||||||
const info = await gh.getGhCliInfo()
|
const exitCode = opts.exitCode ?? 0
|
||||||
|
const encoder = new TextEncoder()
|
||||||
|
|
||||||
// #then should return valid info structure
|
return {
|
||||||
expect(typeof info.installed).toBe("boolean")
|
stdout: new ReadableStream({
|
||||||
expect(info.authenticated === true || info.authenticated === false).toBe(true)
|
start(controller) {
|
||||||
expect(Array.isArray(info.scopes)).toBe(true)
|
if (stdoutText) controller.enqueue(encoder.encode(stdoutText))
|
||||||
|
controller.close()
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
stderr: new ReadableStream({
|
||||||
|
start(controller) {
|
||||||
|
if (stderrText) controller.enqueue(encoder.encode(stderrText))
|
||||||
|
controller.close()
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
exited: Promise.resolve(exitCode),
|
||||||
|
exitCode,
|
||||||
|
} as unknown as ReturnType<typeof Bun.spawn>
|
||||||
|
}
|
||||||
|
|
||||||
|
it("returns gh cli info structure", async () => {
|
||||||
|
const spawnSpy = spyOn(Bun, "spawn").mockImplementation((cmd) => {
|
||||||
|
if (Array.isArray(cmd) && cmd[0] === "which" && cmd[1] === "gh") {
|
||||||
|
return createProc({ stdout: "/usr/bin/gh\n" })
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Array.isArray(cmd) && cmd[0] === "gh" && cmd[1] === "--version") {
|
||||||
|
return createProc({ stdout: "gh version 2.40.0\n" })
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Array.isArray(cmd) && cmd[0] === "gh" && cmd[1] === "auth" && cmd[2] === "status") {
|
||||||
|
return createProc({
|
||||||
|
exitCode: 0,
|
||||||
|
stderr: "Logged in to github.com account octocat (keyring)\nToken scopes: 'repo', 'read:org'\n",
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new Error(`Unexpected Bun.spawn call: ${Array.isArray(cmd) ? cmd.join(" ") : String(cmd)}`)
|
||||||
|
})
|
||||||
|
|
||||||
|
try {
|
||||||
|
const info = await gh.getGhCliInfo()
|
||||||
|
|
||||||
|
expect(info.installed).toBe(true)
|
||||||
|
expect(info.version).toBe("2.40.0")
|
||||||
|
expect(typeof info.authenticated).toBe("boolean")
|
||||||
|
expect(Array.isArray(info.scopes)).toBe(true)
|
||||||
|
} finally {
|
||||||
|
spawnSpy.mockRestore()
|
||||||
|
}
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
@ -24,13 +24,27 @@ describe("isServerInstalled", () => {
|
|||||||
console.error(`Failed to clean up temp dir: ${e}`)
|
console.error(`Failed to clean up temp dir: ${e}`)
|
||||||
}
|
}
|
||||||
|
|
||||||
const pathVal = savedEnv.PATH ?? savedEnv.Path
|
if (process.platform === "win32") {
|
||||||
if (pathVal === undefined) {
|
const pathVal = savedEnv.PATH ?? savedEnv.Path
|
||||||
delete process.env.PATH
|
if (pathVal === undefined) {
|
||||||
delete process.env.Path
|
delete process.env.PATH
|
||||||
|
delete process.env.Path
|
||||||
|
} else {
|
||||||
|
process.env.PATH = pathVal
|
||||||
|
process.env.Path = pathVal
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
process.env.PATH = pathVal
|
if (savedEnv.PATH === undefined) {
|
||||||
process.env.Path = pathVal
|
delete process.env.PATH
|
||||||
|
} else {
|
||||||
|
process.env.PATH = savedEnv.PATH
|
||||||
|
}
|
||||||
|
|
||||||
|
if (savedEnv.Path === undefined) {
|
||||||
|
delete process.env.Path
|
||||||
|
} else {
|
||||||
|
process.env.Path = savedEnv.Path
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const pathextVal = savedEnv.PATHEXT
|
const pathextVal = savedEnv.PATHEXT
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user