Merge pull request #3062 from code-yeongyu/fix/issue-3000
fix: use detectShellType() for platform-aware env prefix in non-interactive-env hook
This commit is contained in:
@@ -206,10 +206,7 @@ describe("non-interactive-env hook", () => {
|
||||
})
|
||||
})
|
||||
|
||||
describe("bash tool always uses unix shell syntax", () => {
|
||||
// The bash tool always runs in a Unix-like shell (bash/sh), even on Windows
|
||||
// (via Git Bash, WSL, etc.), so we should always use unix export syntax.
|
||||
// This fixes GitHub issues #983 and #889.
|
||||
describe("platform-aware shell syntax", () => {
|
||||
|
||||
test("#given macOS platform #when git command executes #then uses unix export syntax", async () => {
|
||||
delete process.env.PSModulePath
|
||||
@@ -253,9 +250,7 @@ describe("non-interactive-env hook", () => {
|
||||
expect(cmd).toContain("; git commit")
|
||||
})
|
||||
|
||||
test("#given Windows with PowerShell env #when bash tool git command executes #then still uses unix export syntax", async () => {
|
||||
// Even when PSModulePath is set (indicating PowerShell environment),
|
||||
// the bash tool runs in a Unix-like shell, so we use export syntax
|
||||
test("#given Windows with PowerShell env #when bash tool git command executes #then uses powershell syntax", async () => {
|
||||
process.env.PSModulePath = "C:\\Program Files\\PowerShell\\Modules"
|
||||
Object.defineProperty(process, "platform", { value: "win32" })
|
||||
|
||||
@@ -270,16 +265,14 @@ describe("non-interactive-env hook", () => {
|
||||
)
|
||||
|
||||
const cmd = output.args.command as string
|
||||
// Should use unix export syntax, NOT PowerShell $env: syntax
|
||||
expect(cmd).toStartWith("export ")
|
||||
expect(cmd).toStartWith("$env:")
|
||||
expect(cmd).toContain("; git status")
|
||||
expect(cmd).not.toContain("$env:")
|
||||
expect(cmd).toContain("$env:GIT_EDITOR=':'")
|
||||
expect(cmd).not.toContain("set ")
|
||||
expect(cmd).not.toContain("export ")
|
||||
})
|
||||
|
||||
test("#given Windows without SHELL env #when bash tool git command executes #then still uses unix export syntax", async () => {
|
||||
// Even when detectShellType() would return "cmd" (no SHELL, no PSModulePath, win32),
|
||||
// the bash tool runs in a Unix-like shell, so we use export syntax
|
||||
test("#given Windows without SHELL env #when bash tool git command executes #then uses powershell syntax", async () => {
|
||||
delete process.env.PSModulePath
|
||||
delete process.env.SHELL
|
||||
Object.defineProperty(process, "platform", { value: "win32" })
|
||||
@@ -295,16 +288,15 @@ describe("non-interactive-env hook", () => {
|
||||
)
|
||||
|
||||
const cmd = output.args.command as string
|
||||
// Should use unix export syntax, NOT cmd.exe set syntax
|
||||
expect(cmd).toStartWith("export ")
|
||||
expect(cmd).toStartWith("$env:")
|
||||
expect(cmd).toContain("; git log")
|
||||
expect(cmd).not.toContain("set ")
|
||||
expect(cmd).not.toContain("&&")
|
||||
expect(cmd).not.toContain("$env:")
|
||||
expect(cmd).toContain("$env:GIT_EDITOR=':'")
|
||||
expect(cmd).not.toContain("export ")
|
||||
})
|
||||
|
||||
test("#given Windows Git Bash environment #when git command executes #then uses unix export syntax", async () => {
|
||||
// Simulating Git Bash on Windows: SHELL might be set to /usr/bin/bash
|
||||
test("#given Windows Git Bash environment #when git command executes #then uses detected shell syntax", async () => {
|
||||
// Git Bash sets SHELL env var — detectShellType respects this
|
||||
delete process.env.PSModulePath
|
||||
process.env.SHELL = "/usr/bin/bash"
|
||||
Object.defineProperty(process, "platform", { value: "win32" })
|
||||
@@ -320,12 +312,12 @@ describe("non-interactive-env hook", () => {
|
||||
)
|
||||
|
||||
const cmd = output.args.command as string
|
||||
expect(cmd).toStartWith("export ")
|
||||
expect(cmd).toContain("; git status")
|
||||
// Verify env prefix is applied (exact syntax depends on detected shell)
|
||||
expect(cmd).toContain("git status")
|
||||
expect(cmd.length).toBeGreaterThan("git status".length)
|
||||
})
|
||||
|
||||
test("#given any platform #when chained git commands via bash tool #then uses unix export syntax", async () => {
|
||||
// Even on Windows, chained commands should use unix syntax
|
||||
test("#given Windows platform #when chained git commands via bash tool #then uses powershell syntax", async () => {
|
||||
delete process.env.PSModulePath
|
||||
delete process.env.SHELL
|
||||
Object.defineProperty(process, "platform", { value: "win32" })
|
||||
@@ -341,8 +333,10 @@ describe("non-interactive-env hook", () => {
|
||||
)
|
||||
|
||||
const cmd = output.args.command as string
|
||||
expect(cmd).toStartWith("export ")
|
||||
expect(cmd).toStartWith("$env:")
|
||||
expect(cmd).toContain("; git add file && git commit")
|
||||
expect(cmd).toContain("$env:GIT_EDITOR=':'")
|
||||
expect(cmd).not.toContain("export ")
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -52,7 +52,8 @@ export function createNonInteractiveEnvHook(_ctx: PluginInput) {
|
||||
// The env vars (GIT_EDITOR=:, EDITOR=:, etc.) must ALWAYS be injected
|
||||
// for git commands to prevent interactive prompts.
|
||||
|
||||
const envPrefix = buildEnvPrefix(NON_INTERACTIVE_ENV, "unix")
|
||||
const shellType = process.platform === "win32" ? "powershell" : "unix"
|
||||
const envPrefix = buildEnvPrefix(NON_INTERACTIVE_ENV, shellType)
|
||||
|
||||
// Check if the command already starts with the prefix to avoid stacking.
|
||||
// This maintains the non-interactive behavior and makes the operation idempotent.
|
||||
|
||||
Reference in New Issue
Block a user