Files
simplegit/gitcmd/git_test.go
T
2026-07-15 11:06:48 -04:00

91 lines
1.9 KiB
Go

package gitcmd
import (
"context"
"os"
"os/exec"
"strings"
"testing"
)
func getEnvValue(envs []string, key string) (string, bool) {
prefix := key + "="
for _, e := range envs {
if v, ok := strings.CutPrefix(e, prefix); ok {
return v, true
}
}
return "", false
}
func TestHomeDir(t *testing.T) {
t.Setenv("GIT_HOME", "/custom/git-home")
if got := HomeDir(); got != "/custom/git-home" {
t.Errorf("HomeDir with GIT_HOME = %q, want /custom/git-home", got)
}
old, hadOld := os.LookupEnv("GIT_HOME")
os.Unsetenv("GIT_HOME")
defer func() {
if hadOld {
os.Setenv("GIT_HOME", old)
}
}()
if got := HomeDir(); got != os.TempDir() {
t.Errorf("HomeDir without GIT_HOME = %q, want %q", got, os.TempDir())
}
}
func TestCommonEnvs(t *testing.T) {
envs := CommonEnvs()
if len(envs) != 5 {
t.Errorf("len(CommonEnvs) = %d, want 5", len(envs))
}
if v, ok := getEnvValue(envs, "HOME"); !ok || v == "" {
t.Errorf("HOME missing or empty in %v", envs)
}
if v, _ := getEnvValue(envs, "HOME"); v != HomeDir() {
t.Errorf("HOME = %q, want HomeDir() = %q", v, HomeDir())
}
for _, want := range []string{
"GIT_CONFIG_NOSYSTEM=1",
"GIT_NO_REPLACE_OBJECTS=1",
"GIT_TERMINAL_PROMPT=0",
"LC_ALL=C",
} {
if _, ok := getEnvValue(envs, strings.SplitN(want, "=", 2)[0]); !ok {
t.Errorf("CommonEnvs missing %q in %v", want, envs)
}
}
}
func TestVersionContext(t *testing.T) {
if _, err := Version(context.Background()); err != nil {
t.Fatalf("Version: %v", err)
}
ctx, cancel := context.WithCancel(context.Background())
cancel()
if _, err := Version(ctx); err == nil {
t.Error("expected error from cancelled context, got nil")
}
}
func TestExecutableFoundViaLookPath(t *testing.T) {
exe, err := Executable()
if err != nil {
t.Fatalf("Executable: %v", err)
}
looked, err := exec.LookPath("git")
if err != nil {
t.Skip("git not on PATH")
}
if exe == "" {
t.Error("empty executable path")
}
_ = looked
}