package gitutil
import (
"context"
"os/exec"
"strings"
"testing"
)
// os/exec refuses to start a command that carries a Cancel it did not get from
// CommandContext, and the sandbox-wrapped commands in knotserver are exactly
// that shape, so containment has to degrade instead of breaking them
func TestContainContextlessCommand(t *testing.T) {
cmd := Contain(exec.Command("git", "version"))
out, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("%v: %s", err, out)
}
if !cmd.SysProcAttr.Setpgid {
t.Fatal("a contained command has to lead its own process group")
}
if cmd.WaitDelay == 0 {
t.Fatal("a contained command has to bound its wait")
}
}
func TestCommandSpeaksEnglishWhateverTheHostLocale(t *testing.T) {
t.Setenv("LC_ALL", "de_DE.UTF-8")
t.Setenv("LANG", "de_DE.UTF-8")
t.Setenv("LANGUAGE", "de")
out, err := Command(context.Background(), "-C", t.TempDir(), "rev-parse", "HEAD").CombinedOutput()
if err == nil {
t.Fatalf("rev-parse outside a repo succeeded: %s", out)
}
if !strings.Contains(string(out), "not a git repository") {
t.Fatalf("git said %q, want its English message so the callers' matches hold", out)
}
}
func TestCommandPinsTheLocaleInItsEnvironment(t *testing.T) {
t.Setenv("LC_ALL", "de_DE.UTF-8")
env := Command(context.Background(), "version").Env
if len(env) == 0 || env[len(env)-1] != "LC_ALL=C" {
t.Fatalf("env ends with %q, want LC_ALL=C last so it beats the host's", env[max(0, len(env)-1):])
}
}