-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathrun_test.go
More file actions
76 lines (71 loc) · 2.07 KB
/
Copy pathrun_test.go
File metadata and controls
76 lines (71 loc) · 2.07 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package clone
import (
"context"
"os"
"os/exec"
"path/filepath"
"slices"
"strings"
"testing"
"time"
)
func TestRemoteEnv(t *testing.T) {
got := remoteEnv()
for _, want := range []string{
"GIT_TERMINAL_PROMPT=0",
"GIT_PROTOCOL_FROM_USER=0",
"GIT_ALLOW_PROTOCOL=https",
} {
if !slices.Contains(got, want) {
t.Errorf("remoteEnv() = %v, missing %q", got, want)
}
}
// A caller that sets GIT_ALLOW_PROTOCOL keeps its own value; the
// function must not override it, or tests using file:// via insteadOf
// (and callers with a legitimate need for another protocol) break.
t.Setenv("GIT_ALLOW_PROTOCOL", "https:file")
if slices.Contains(remoteEnv(), "GIT_ALLOW_PROTOCOL=https") {
t.Errorf("remoteEnv() overrode caller's GIT_ALLOW_PROTOCOL: %v", remoteEnv())
}
}
func TestRunUsesDirectoryAndEnvironment(t *testing.T) {
requireGit(t)
dir := t.TempDir()
out, err := Run(context.Background(), dir, []string{"CLONE_RUN_TEST=value"},
"-c", "alias.test=!pwd && printf \"|%s\" \"$CLONE_RUN_TEST\"", "test")
if err != nil {
t.Fatalf("Run: %s: %v", out, err)
}
parts := strings.Split(strings.TrimSpace(out), "|")
resolvedDir, err := filepath.EvalSymlinks(dir)
if err != nil {
t.Fatal(err)
}
if len(parts) != 2 ||
strings.TrimSpace(parts[0]) != resolvedDir ||
strings.TrimSpace(parts[1]) != "value" {
t.Fatalf("Run output = %q, want %q and environment value", out, dir)
}
}
func TestRunnerWithWaitDelayBoundsLingeringChild(t *testing.T) {
if _, err := exec.LookPath("sh"); err != nil {
t.Skip("needs a POSIX shell")
}
bin := t.TempDir()
script := "#!/bin/sh\nsleep 30 &\nexit 0\n"
if err := os.WriteFile(filepath.Join(bin, "git"), []byte(script), 0o755); err != nil {
t.Fatal(err)
}
t.Setenv("PATH", bin+string(os.PathListSeparator)+os.Getenv("PATH"))
done := make(chan struct{})
go func() {
_, _ = RunnerWithWaitDelay(200*time.Millisecond)(
context.Background(), "", nil, "clone", "https://example.invalid/repo")
close(done)
}()
select {
case <-done:
case <-time.After(5 * time.Second):
t.Fatal("runner blocked on a lingering transport child")
}
}