beads/cmd/bd/init_noninteractive_test.go
matt wilkie ba46bd5f3b
fix: repair nightly test failures for TestInitCancel_E2E and dolt autostart (#2969)
TestInitCancel_E2E: The --contributor flag was rejected because
isNonInteractiveInit() detected non-interactive mode (CI=true + piped
stdin). Fix by supporting BD_NON_INTERACTIVE=0/false to explicitly
force interactive mode, and set it in the test env.

TestE2E_AutoStartedRepoLocalServerPersistsAcrossCommands: init
defaulted to embedded mode since a35edfd2, but the test needs server
mode for dolt status/start/stop. Fix by adding --server to init args.

Also adds unit tests for the new BD_NON_INTERACTIVE=0 override.

Amp-Thread-ID: https://ampcode.com/threads/T-019d4f2d-9137-742b-bf44-ed413fe59cd0

Co-authored-by: Amp <amp@ampcode.com>
2026-04-02 11:04:32 -07:00

128 lines
2.8 KiB
Go

package main
import (
"os"
"testing"
)
// TestIsNonInteractiveInit tests the non-interactive detection logic.
func TestIsNonInteractiveInit(t *testing.T) {
// Save original env vars and restore after tests
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: "CI=false does not trigger",
flagValue: false,
envCI: "false",
envBDNI: "",
// In test env, stdin is not a TTY, so this is still true
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,
},
{
name: "BD_NON_INTERACTIVE=0 forces interactive",
flagValue: false,
envCI: "true",
envBDNI: "0",
want: false,
},
{
name: "BD_NON_INTERACTIVE=false forces interactive",
flagValue: false,
envCI: "true",
envBDNI: "false",
want: false,
},
}
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 := isNonInteractiveInit(tt.flagValue)
if got != tt.want {
t.Errorf("isNonInteractiveInit(%v) with CI=%q BD_NON_INTERACTIVE=%q = %v, want %v",
tt.flagValue, tt.envCI, tt.envBDNI, got, tt.want)
}
})
}
}
// TestIsNonInteractiveInitPrecedence tests that flag takes precedence over env vars.
func TestIsNonInteractiveInitPrecedence(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 !isNonInteractiveInit(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 !isNonInteractiveInit(false) {
t.Error("BD_NON_INTERACTIVE=1 should return true")
}
}