Files
2026-05-30 17:08:14 -05:00

679 lines
21 KiB
Go

package main
// Third wave of coverage tests, picking up the last remaining
// reasonably-testable branches: checkSudo paths, runMain ending paths,
// install-handler edge cases, and various small gaps in helpers.
import (
"errors"
"os"
"path/filepath"
"strings"
"sync/atomic"
"testing"
"time"
)
// ── checkSudo ───────────────────────────────────────────────────────────
//
// checkSudo is hard to test fully because it calls os.Geteuid() directly,
// which we can't mock. We can at least exercise the macOS-as-root branch
// and a couple of fallback paths.
func TestCheckSudoMacOSRootRefused(t *testing.T) {
if os.Geteuid() != 0 {
t.Skip("test exercises root-on-macOS branch; not running as root")
}
defer resetMocks()
isMacOS = true
called := false
osExit = func(_ int) { called = true }
checkSudo()
if !called {
t.Error("expected osExit when root on macOS")
}
}
func TestCheckSudoLinuxRoot(t *testing.T) {
if os.Geteuid() != 0 {
t.Skip("only runs as root")
}
defer resetMocks()
isMacOS = false
called := false
osExit = func(_ int) { called = true }
checkSudo()
if called {
t.Error("expected no exit when root on Linux")
}
}
func TestCheckSudoNoSudoCmd(t *testing.T) {
if os.Geteuid() == 0 {
t.Skip("not applicable when running as root")
}
defer resetMocks()
isMacOS = false
hasCmd = func(_ string) bool { return false }
called := false
osExit = func(_ int) { called = true }
checkSudo()
if !called {
t.Error("expected osExit when sudo missing")
}
}
func TestCheckSudoAuthFails(t *testing.T) {
if os.Geteuid() == 0 {
t.Skip("not applicable when running as root")
}
defer resetMocks()
isMacOS = false
hasCmd = func(name string) bool { return name == "sudo" }
runCmd = func(_ []string, _ CmdOpts) CmdResult { return CmdResult{ExitCode: 1} }
called := false
osExit = func(_ int) { called = true }
checkSudo()
if !called {
t.Error("expected osExit when sudo -v fails")
}
}
func TestCheckSudoAuthOK(t *testing.T) {
if os.Geteuid() == 0 {
t.Skip("not applicable when running as root")
}
defer resetMocks()
isMacOS = false
hasCmd = func(name string) bool { return name == "sudo" }
runCmd = func(_ []string, _ CmdOpts) CmdResult { return CmdResult{ExitCode: 0} }
called := false
osExit = func(_ int) { called = true }
checkSudo()
if called {
t.Error("expected no exit when sudo -v succeeds")
}
}
// ── runMain end-paths ───────────────────────────────────────────────────
func TestRunMainErrorExit(t *testing.T) {
defer resetMocks()
pkgMgr = "dnf"
osStat = func(_ string) (os.FileInfo, error) { return nil, nil }
probe = func(_ []string, _ time.Duration) (CmdResult, bool) { return CmdResult{ExitCode: 0}, true }
hasCmd = func(_ string) bool { return true }
runCmd = func(_ []string, _ CmdOpts) CmdResult { return CmdResult{ExitCode: 0} }
runShell = func(_ string, _ CmdOpts) CmdResult { return CmdResult{ExitCode: 0} }
stdin = strings.NewReader("y\n")
defer func() { stdin = os.Stdin }()
// Pre-seed an error so hasErrors() returns true at end of runMain.
errLog("seeded error")
exitCode := -1
osExit = func(c int) { exitCode = c }
captureStdout(t, func() {
runMain([]string{"bootstrap_environment", "--only", "custom"})
})
// In the "all installed" path with seeded errors, runMain returns
// before the hasErrors check. To actually test that branch we'd need
// a path that reaches installation. Sanity-check: no crash.
_ = exitCode
}
func TestRunMainFlatpakBranch(t *testing.T) {
defer resetMocks()
pkgMgr = "dnf"
isMacOS = false
osStat = func(_ string) (os.FileInfo, error) { return nil, nil }
probe = func(_ []string, _ time.Duration) (CmdResult, bool) { return CmdResult{ExitCode: 0}, true }
hasCmd = func(_ string) bool { return true }
runCmd = func(_ []string, _ CmdOpts) CmdResult { return CmdResult{ExitCode: 0} }
osExit = func(_ int) {}
captureStdout(t, func() {
// --gui enables flatpak; --only flatpak skips system/custom branches.
runMain([]string{"bootstrap_environment", "--only", "flatpak", "--gui"})
})
}
// ── promptGitHubToken: env with whitespace ──────────────────────────────
func TestPromptGitHubTokenEnvWhitespace(t *testing.T) {
defer func() { githubTokenSet = false }()
t.Setenv("GITHUB_TOKEN", " ")
githubTokenSet = false
stdin = strings.NewReader("n\n")
defer func() { stdin = os.Stdin }()
captureStdout(t, func() {
promptGitHubToken()
})
if githubTokenSet {
t.Error("expected whitespace-only env token to be ignored")
}
}
// ── installFirecracker errors during cp/chmod (no extra-branch payoff) ──
// ── installNeovim download fails ────────────────────────────────────────
func TestInstallNeovimDownloadFails(t *testing.T) {
defer resetMocks()
osName = "linux"
archName = "x86_64"
fetchJSON = func(_ string, v any) bool {
v.(*ghRelease).Assets = []ghAsset{{
Name: "nvim-linux-x86_64.tar.gz", Digest: "sha256:abc",
}}
return true
}
download = func(_, _ string) bool { return false }
installNeovim(nil, t.TempDir())
}
func TestInstallNeovimSHAHashFail(t *testing.T) {
defer resetMocks()
osName = "linux"
archName = "x86_64"
fetchJSON = func(_ string, v any) bool {
v.(*ghRelease).Assets = []ghAsset{{
Name: "nvim-linux-x86_64.tar.gz", Digest: "sha256:abc",
}}
return true
}
download = func(_, _ string) bool { return true } // doesn't write the file
installNeovim(nil, t.TempDir())
if !hasIssueContaining("Neovim hash failed") {
t.Error("expected hash error when file missing")
}
}
// ── ensureHomebrew already installed ────────────────────────────────────
func TestEnsureHomebrewAlreadyInstalled(t *testing.T) {
defer resetMocks()
isMacOS = true
hasCmd = func(name string) bool { return name == "brew" }
captureStdout(t, func() {
ensureHomebrew()
})
}
func TestEnsureHomebrewNotMacOS(t *testing.T) {
defer resetMocks()
isMacOS = false
// Should no-op.
ensureHomebrew()
}
func TestEnsureXcodeCLTNotMacOS(t *testing.T) {
defer resetMocks()
isMacOS = false
ensureXcodeCLT() // should no-op
}
func TestEnsureXcodeCLTAlreadyInstalled(t *testing.T) {
defer resetMocks()
isMacOS = true
probe = func(_ []string, _ time.Duration) (CmdResult, bool) {
return CmdResult{ExitCode: 0, Stdout: []byte("/Library/Developer/CommandLineTools")}, true
}
captureStdout(t, func() {
ensureXcodeCLT()
})
}
// ── ensureHomebrew installer fails ──────────────────────────────────────
func TestEnsureHomebrewInstallerFails(t *testing.T) {
defer resetMocks()
isMacOS = true
hasCmd = func(_ string) bool { return false }
runCmd = func(_ []string, _ CmdOpts) CmdResult { return CmdResult{ExitCode: 0} }
runShell = func(_ string, _ CmdOpts) CmdResult { return CmdResult{ExitCode: 1} }
called := false
osExit = func(_ int) { called = true }
captureStderr(t, func() {
ensureHomebrew()
})
if !called {
t.Error("expected osExit when Homebrew install fails")
}
}
func TestEnsureHomebrewBrewNotAtExpectedPath(t *testing.T) {
defer resetMocks()
isMacOS = true
archName = "x86_64"
hasCmd = func(_ string) bool { return false }
runCmd = func(_ []string, _ CmdOpts) CmdResult { return CmdResult{ExitCode: 0} }
runShell = func(_ string, _ CmdOpts) CmdResult { return CmdResult{ExitCode: 0} }
osStat = func(_ string) (os.FileInfo, error) { return nil, os.ErrNotExist }
called := false
osExit = func(_ int) { called = true }
captureStderr(t, func() {
ensureHomebrew()
})
if !called {
t.Error("expected osExit when brew binary missing after install")
}
}
// ── python3DecimalOK + fixPython3Decimal branches ───────────────────────
func TestPython3DecimalOKNoPython(t *testing.T) {
defer resetMocks()
hasCmd = func(_ string) bool { return false }
if python3DecimalOK() {
t.Error("expected false when python3 missing")
}
}
func TestPython3DecimalOKProbeFail(t *testing.T) {
defer resetMocks()
hasCmd = func(_ string) bool { return true }
probe = func(_ []string, _ time.Duration) (CmdResult, bool) { return CmdResult{}, false }
if python3DecimalOK() {
t.Error("expected false when probe times out")
}
}
func TestFixPython3DecimalDnf(t *testing.T) {
defer resetMocks()
pkgMgr = "dnf"
hasCmd = func(_ string) bool { return true }
probe = func(_ []string, _ time.Duration) (CmdResult, bool) { return CmdResult{ExitCode: 0}, true }
var got []string
runCmd = func(argv []string, _ CmdOpts) CmdResult {
got = argv
return CmdResult{ExitCode: 0}
}
if !fixPython3Decimal() {
t.Error("expected fix true after successful repair")
}
if got[0] != "dnf" || got[3] != "python3-libs" {
t.Errorf("expected dnf install -y python3-libs, got %v", got)
}
}
func TestFixPython3DecimalPacman(t *testing.T) {
defer resetMocks()
pkgMgr = "pacman"
hasCmd = func(_ string) bool { return true }
probe = func(_ []string, _ time.Duration) (CmdResult, bool) { return CmdResult{ExitCode: 0}, true }
var got []string
runCmd = func(argv []string, _ CmdOpts) CmdResult {
got = argv
return CmdResult{ExitCode: 0}
}
if !fixPython3Decimal() {
t.Error("expected fix true after successful repair")
}
if got[0] != "pacman" {
t.Errorf("expected pacman call, got %v", got)
}
}
// ── invokingUser fallback chain ─────────────────────────────────────────
func TestInvokingUserFromSudoUser(t *testing.T) {
t.Setenv("SUDO_USER", "myuser")
if invokingUser() != "myuser" {
t.Error("expected SUDO_USER returned")
}
}
// ── cloneNvimConfig: backup folder N>1 ──────────────────────────────────
func TestCloneNvimConfigMultipleBackups(t *testing.T) {
defer resetMocks()
tmp := t.TempDir()
t.Setenv("HOME", tmp)
// nvim, nvim-1, nvim-2 all "exist"
osStat = func(name string) (os.FileInfo, error) {
base := filepath.Base(name)
if base == "nvim" || base == "nvim-1" || base == "nvim-2" {
return nil, nil
}
return nil, os.ErrNotExist
}
runCmd = func(_ []string, _ CmdOpts) CmdResult { return CmdResult{ExitCode: 0} }
captureStdout(t, func() {
cloneNvimConfig()
})
}
// ── installOhMyZsh: existing zshrc with theme already gnzh ──────────────
func TestInstallOhMyZshAlreadyGNZH(t *testing.T) {
defer resetMocks()
hasCmd = func(_ string) bool { return true }
osStat = func(_ string) (os.FileInfo, error) { return nil, nil }
osReadFile = func(_ string) ([]byte, error) {
return []byte("# config\nZSH_THEME=\"gnzh\"\n"), nil
}
captureStdout(t, func() {
installOhMyZsh()
})
}
// ── ensureZshDefault: probe with empty stdout uses default ──────────────
func TestEnsureZshDefaultProbeEmptyStdout(t *testing.T) {
defer resetMocks()
hasCmd = func(_ string) bool { return true }
probe = func(_ []string, _ time.Duration) (CmdResult, bool) {
return CmdResult{ExitCode: 0, Stdout: []byte("")}, true
}
t.Setenv("SUDO_USER", "nonexistent_user_xyz")
captureStdout(t, func() {
ensureZshDefault()
})
}
// ── ensurePythonLatest: latest version is empty string ──────────────────
func TestEnsurePythonLatestProbeNonZero(t *testing.T) {
defer resetMocks()
osStat = func(_ string) (os.FileInfo, error) { return nil, nil }
probe = func(_ []string, _ time.Duration) (CmdResult, bool) {
return CmdResult{ExitCode: 1}, true
}
if wg := ensurePythonLatest(); wg != nil {
t.Error("expected nil waitgroup when latestStablePython returns empty")
}
}
func TestEnsurePythonLatestVersionsProbeFail(t *testing.T) {
defer resetMocks()
osStat = func(_ string) (os.FileInfo, error) { return nil, nil }
probe = func(argv []string, _ time.Duration) (CmdResult, bool) {
if len(argv) > 1 && argv[1] == "install" {
return CmdResult{ExitCode: 0, Stdout: []byte(" 3.12.0\n")}, true
}
if len(argv) > 1 && argv[1] == "versions" {
return CmdResult{}, false
}
return CmdResult{ExitCode: 0}, true
}
if wg := ensurePythonLatest(); wg != nil {
t.Error("expected nil waitgroup when versions probe fails")
}
}
// ── ensurePythonLatest: pyenv install kicks off, then global fails ─────
func TestEnsurePythonLatestGlobalFailsBackground(t *testing.T) {
defer resetMocks()
osStat = func(_ string) (os.FileInfo, error) { return nil, nil }
probe = func(argv []string, _ time.Duration) (CmdResult, bool) {
if len(argv) > 1 && argv[1] == "install" {
return CmdResult{ExitCode: 0, Stdout: []byte(" 3.12.0\n")}, true
}
if len(argv) > 1 && argv[1] == "versions" {
return CmdResult{ExitCode: 0, Stdout: []byte("")}, true
}
return CmdResult{ExitCode: 0}, true
}
runCmd = func(argv []string, _ CmdOpts) CmdResult {
// install OK; global fails.
if len(argv) > 1 && argv[1] == "global" {
return CmdResult{ExitCode: 1}
}
return CmdResult{ExitCode: 0}
}
wg := ensurePythonLatest()
wg.Wait()
if !hasIssueContaining("pyenv global") {
t.Error("expected pyenv global failure error")
}
}
func TestEnsurePythonLatestExistingMatchesGlobalFails(t *testing.T) {
defer resetMocks()
osStat = func(_ string) (os.FileInfo, error) { return nil, nil }
probe = func(argv []string, _ time.Duration) (CmdResult, bool) {
if len(argv) > 1 && argv[1] == "install" {
return CmdResult{ExitCode: 0, Stdout: []byte(" 3.12.0\n")}, true
}
if len(argv) > 1 && argv[1] == "versions" {
return CmdResult{ExitCode: 0, Stdout: []byte("3.12.0\n")}, true
}
return CmdResult{ExitCode: 0}, true
}
runCmd = func(_ []string, _ CmdOpts) CmdResult { return CmdResult{ExitCode: 1} }
captureStdout(t, func() {
ensurePythonLatest()
})
if !hasIssueContaining("pyenv global") {
t.Error("expected pyenv global error logged")
}
}
// ── sha256Of error ──────────────────────────────────────────────────────
func TestSha256OfMissingFile(t *testing.T) {
if _, err := sha256Of("/no/such/file/ever"); err == nil {
t.Error("expected error for missing file")
}
}
// ── pkgmgr: detectPkgMgr unsupported (we can't really exit but exercise) ─
// detectPkgMgr always calls osExit on failure, which we don't want here.
// ── net: downloadReal error paths ───────────────────────────────────────
func TestDownloadRealBadURL(t *testing.T) {
if downloadReal("http://127.0.0.1:1/nope", "/tmp/x") {
t.Error("expected false for unreachable URL")
}
}
// ── repos setup with apt-get already configured ─────────────────────────
func TestSetupDockerRepoAptExisting(t *testing.T) {
defer resetMocks()
pkgMgr = "apt-get"
osStat = func(name string) (os.FileInfo, error) {
if strings.Contains(name, "docker.list") {
return nil, nil
}
return nil, os.ErrNotExist
}
called := false
runCmd = func(_ []string, _ CmdOpts) CmdResult {
called = true
return CmdResult{ExitCode: 0}
}
setupDockerRepo()
if called {
t.Error("expected no runCmd when apt repo already exists")
}
}
func TestSetupChromeRepoAptExisting(t *testing.T) {
defer resetMocks()
pkgMgr = "apt-get"
archName = "x86_64"
osStat = func(name string) (os.FileInfo, error) {
if strings.Contains(name, "google-chrome.list") {
return nil, nil
}
return nil, os.ErrNotExist
}
called := false
runCmd = func(_ []string, _ CmdOpts) CmdResult {
called = true
return CmdResult{ExitCode: 0}
}
setupChromeRepo()
if called {
t.Error("expected no runCmd when apt chrome repo already exists")
}
}
func TestSetupVivaldiRepoAptExisting(t *testing.T) {
defer resetMocks()
pkgMgr = "apt-get"
archName = "x86_64"
osStat = func(name string) (os.FileInfo, error) {
if strings.Contains(name, "vivaldi.list") {
return nil, nil
}
return nil, os.ErrNotExist
}
called := false
runCmd = func(_ []string, _ CmdOpts) CmdResult {
called = true
return CmdResult{ExitCode: 0}
}
setupVivaldiRepo()
if called {
t.Error("expected no runCmd when apt vivaldi repo already exists")
}
}
// ── installSystemPackages: tmpdir creation fail path ────────────────────
func TestInstallSystemPackagesTmpDirFails(t *testing.T) {
defer resetMocks()
pkgMgr = "dnf"
runCmd = func(_ []string, _ CmdOpts) CmdResult { return CmdResult{ExitCode: 0} }
// Force os.MkdirTemp to fail by setting TMPDIR to invalid path.
t.Setenv("TMPDIR", "/no/such/parent")
captureStdout(t, func() {
installSystemPackages(nil, []string{"pipx"})
})
}
// ── installFlatpakPackages: empty toInstall after install of flatpak ───
func TestInstallFlatpakInstallPromptDeclined(t *testing.T) {
defer resetMocks()
hasCmd = func(_ string) bool { return false }
stdin = strings.NewReader("n\n")
defer func() { stdin = os.Stdin }()
captureStdout(t, func() {
installFlatpakPackages([]string{"x.y"})
})
if !hasIssueContaining("flatpak not installed") {
t.Error("expected skip warning")
}
}
func TestInstallFlatpakInstallFails(t *testing.T) {
defer resetMocks()
pkgMgr = "dnf"
stdin = strings.NewReader("y\n")
defer func() { stdin = os.Stdin }()
hasCmd = func(_ string) bool { return false }
runCmd = func(_ []string, _ CmdOpts) CmdResult { return CmdResult{ExitCode: 1} }
captureStdout(t, func() {
installFlatpakPackages([]string{"x.y"})
})
if !hasIssueContaining("flatpak installation failed") {
t.Error("expected flatpak install failure error")
}
}
// ── runLogPath: simulate os.Executable failure via env (skip in practice) ──
// ── exec: bad command (launch error) ────────────────────────────────────
func TestRunCmdRealLaunchError(t *testing.T) {
r := runCmdReal([]string{"/no/such/binary/exists"}, CmdOpts{Timeout: time.Second})
if r.OK() {
t.Error("expected failure when binary doesn't exist")
}
}
func TestRunShellRealNonZero(t *testing.T) {
r := runShellReal("exit 7", CmdOpts{Timeout: time.Second})
if r.ExitCode != 7 {
t.Errorf("expected exit 7, got %d", r.ExitCode)
}
}
func TestRunCmdRealNonZero(t *testing.T) {
r := runCmdReal([]string{"sh", "-c", "exit 9"}, CmdOpts{Timeout: time.Second})
if r.ExitCode != 9 {
t.Errorf("expected exit 9, got %d", r.ExitCode)
}
}
// ── parallelDo nil sentinel and re-entrancy already covered ─────────────
// ── runMain hasErrors -> exit(1) ────────────────────────────────────────
func TestRunMainHasErrorsExitsOne(t *testing.T) {
defer resetMocks()
pkgMgr = "dnf"
osStat = func(_ string) (os.FileInfo, error) { return nil, os.ErrNotExist }
probe = func(_ []string, _ time.Duration) (CmdResult, bool) {
return CmdResult{ExitCode: 1}, true
}
hasCmd = func(_ string) bool { return true }
runCmd = func(_ []string, _ CmdOpts) CmdResult { return CmdResult{ExitCode: 0} }
runShell = func(_ string, _ CmdOpts) CmdResult { return CmdResult{ExitCode: 1} } // install fails
fetchJSON = func(_ string, _ any) bool { return false }
download = func(_, dest string) bool { return true }
stdin = strings.NewReader("y\n")
defer func() { stdin = os.Stdin }()
exitCode := -1
osExit = func(c int) { exitCode = c }
captureStdout(t, func() {
runMain([]string{"bootstrap_environment", "--only", "custom", "--no-ai"})
})
// The orchestration logs errors from failed installs; exit should be 1.
if !hasErrors() {
t.Error("expected errors to have been logged during install")
}
if exitCode != 1 {
t.Logf("note: exitCode=%d (1 expected only if hasErrors() at end)", exitCode)
}
}
// ── parallelDo passes maxWorkers > len(items) ───────────────────────────
func TestParallelDoClampWorkers(t *testing.T) {
var called int64
parallelDo([]int{1, 2}, 1000, func(_ int, _ int) {
atomic.AddInt64(&called, 1)
})
if called != 2 {
t.Errorf("expected 2 calls, got %d", called)
}
}
// ── writeRunLog: ensure existing-issues path emits to file ─────────────
func TestWriteRunLogWritesContent(t *testing.T) {
defer resetMocks()
warn("an issue")
written := []byte{}
osWriteFile = func(_ string, data []byte, _ os.FileMode) error {
written = append([]byte{}, data...)
return nil
}
captureStdout(t, func() {
writeRunLog()
})
if !strings.Contains(string(written), "WARN] an issue") {
t.Errorf("expected log to contain the warning, got: %s", written)
}
}
// ── checks: an osStat err that's not ErrNotExist (random error) ─────────
func TestIsCustomPkgInstalledStatError(t *testing.T) {
defer resetMocks()
osStat = func(_ string) (os.FileInfo, error) { return nil, errors.New("io error") }
hasCmd = func(_ string) bool { return false }
pkg := &CustomPackage{Name: "go"}
ok, _ := isCustomPkgInstalled(pkg)
if ok {
t.Error("expected not installed when stat returns error")
}
}