package tools

import (
	"os/exec"
	"testing"
)

// isolateToolpath neutralises every directory toolpath.Resolve searches that a
// test can control: the process PATH, and the HOME/GOBIN/GOPATH that seed
// ~/go/bin, the managed toolbin, and `go env`. It does NOT remove the hardcoded
// system prefixes (/opt/homebrew/bin, /usr/local/bin, /usr/bin) — those can't
// be unset from a test — so if a tool genuinely lives in one of those on this
// host, the "unavailable" assertion can't hold and the caller is skipped.
//
// This replaces the old `PATH=""` approach, which stopped proving absence once
// toolpath began discovering binaries beyond PATH (e.g. in ~/go/bin).
func isolateToolpath(t *testing.T) {
	t.Helper()
	empty := t.TempDir()
	t.Setenv("PATH", "")
	t.Setenv("HOME", empty)      // ~/go/bin and the managed toolbin resolve under here
	t.Setenv("GOBIN", "")        // no explicit go-install target
	t.Setenv("GOPATH", empty)    // GOPATH/bin resolves to an empty dir
	t.Setenv("USERPROFILE", empty)
}

// systemHasBinary reports whether a tool resolves from a hardcoded system
// prefix that isolateToolpath cannot clear, so a test can skip rather than
// falsely fail on a host where the tool is Homebrew/system-installed.
func systemHasBinary(name string) bool {
	for _, dir := range []string{"/opt/homebrew/bin", "/usr/local/bin", "/usr/bin"} {
		if _, err := exec.LookPath(dir + "/" + name); err == nil {
			return true
		}
	}
	return false
}
