Add --non-interactive / --yes / -y flag to bd bootstrap, matching the pattern already used by bd init. Also supports BD_NON_INTERACTIVE=1 environment variable. This allows CI environments to skip confirmation prompts without piping input. Fixes harry-miller-trimble/beads#19 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
113 lines
2.6 KiB
Go
113 lines
2.6 KiB
Go
package main
|
|
|
|
import (
|
|
"os"
|
|
"testing"
|
|
)
|
|
|
|
// TestIsNonInteractiveBootstrap tests the non-interactive detection logic for bootstrap.
|
|
func TestIsNonInteractiveBootstrap(t *testing.T) {
|
|
origCI := os.Getenv("CI")
|
|
origBDNI := os.Getenv("BD_NON_INTERACTIVE")
|
|
defer func() {
|
|
os.Setenv("CI", origCI)
|
|
os.Setenv("BD_NON_INTERACTIVE", origBDNI)
|
|
}()
|
|
|
|
tests := []struct {
|
|
name string
|
|
flagValue bool
|
|
envCI string
|
|
envBDNI string
|
|
want bool
|
|
}{
|
|
{
|
|
name: "flag true overrides everything",
|
|
flagValue: true,
|
|
envCI: "",
|
|
envBDNI: "",
|
|
want: true,
|
|
},
|
|
{
|
|
name: "BD_NON_INTERACTIVE=1",
|
|
flagValue: false,
|
|
envCI: "",
|
|
envBDNI: "1",
|
|
want: true,
|
|
},
|
|
{
|
|
name: "BD_NON_INTERACTIVE=true",
|
|
flagValue: false,
|
|
envCI: "",
|
|
envBDNI: "true",
|
|
want: true,
|
|
},
|
|
{
|
|
name: "CI=true",
|
|
flagValue: false,
|
|
envCI: "true",
|
|
envBDNI: "",
|
|
want: true,
|
|
},
|
|
{
|
|
name: "CI=1",
|
|
flagValue: false,
|
|
envCI: "1",
|
|
envBDNI: "",
|
|
want: true,
|
|
},
|
|
{
|
|
name: "no flag no env falls back to terminal detection",
|
|
flagValue: false,
|
|
envCI: "",
|
|
envBDNI: "",
|
|
// In test environment, stdin is piped (not a TTY), so non-interactive
|
|
want: true,
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
os.Setenv("CI", tt.envCI)
|
|
os.Setenv("BD_NON_INTERACTIVE", tt.envBDNI)
|
|
|
|
got := isNonInteractiveBootstrap(tt.flagValue)
|
|
if got != tt.want {
|
|
t.Errorf("isNonInteractiveBootstrap(%v) with CI=%q BD_NON_INTERACTIVE=%q = %v, want %v",
|
|
tt.flagValue, tt.envCI, tt.envBDNI, got, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// TestIsNonInteractiveBootstrapPrecedence tests that flag takes precedence over env vars.
|
|
func TestIsNonInteractiveBootstrapPrecedence(t *testing.T) {
|
|
origCI := os.Getenv("CI")
|
|
origBDNI := os.Getenv("BD_NON_INTERACTIVE")
|
|
defer func() {
|
|
os.Setenv("CI", origCI)
|
|
os.Setenv("BD_NON_INTERACTIVE", origBDNI)
|
|
}()
|
|
|
|
// Flag true should always win
|
|
os.Setenv("CI", "")
|
|
os.Setenv("BD_NON_INTERACTIVE", "")
|
|
if !isNonInteractiveBootstrap(true) {
|
|
t.Error("flag=true should always return true regardless of env")
|
|
}
|
|
|
|
// BD_NON_INTERACTIVE should take precedence over CI
|
|
os.Setenv("BD_NON_INTERACTIVE", "1")
|
|
os.Setenv("CI", "")
|
|
if !isNonInteractiveBootstrap(false) {
|
|
t.Error("BD_NON_INTERACTIVE=1 should return true")
|
|
}
|
|
}
|
|
|
|
// TestConfirmPromptNonInteractive verifies that confirmPrompt returns true
|
|
// when nonInteractive is set, without reading stdin.
|
|
func TestConfirmPromptNonInteractive(t *testing.T) {
|
|
if !confirmPrompt("Proceed?", true) {
|
|
t.Error("confirmPrompt should return true when nonInteractive=true")
|
|
}
|
|
}
|