~james/core

core/gitutil/command_test.go -rw-r--r-- 1.4 KiB
452f17b7 — james chore(import): snapshot james.dad.town/core at f70c71c7696a 14 hours ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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):])
	}
}