-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathremote_test.go
More file actions
114 lines (107 loc) · 3.4 KB
/
Copy pathremote_test.go
File metadata and controls
114 lines (107 loc) · 3.4 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package clone
import (
"context"
"errors"
"slices"
"strings"
"testing"
)
func TestParseRemoteHeads(t *testing.T) {
out := "deadbeef\trefs/heads/main\n" +
"cafebabe\trefs/heads/7.2\n" +
"cafebabe\trefs/heads/7.2\n" +
"00000000\trefs/heads/6.4\n" +
"feedface\trefs/tags/v1\n"
want := []string{"6.4", "7.2", "main"}
if got := parseRemoteHeads(out); !slices.Equal(got, want) {
t.Errorf("parseRemoteHeads = %v, want %v", got, want)
}
}
func TestRemoteBranchesUsesHardenedGitInvocation(t *testing.T) {
var gotEnv, gotArgs []string
retry := Retry{
Run: func(_ context.Context, _ string, env []string, args ...string) (string, error) {
gotEnv = append([]string(nil), env...)
gotArgs = append([]string(nil), args...)
return "bbb\trefs/heads/z\n" +
"aaa\trefs/heads/main\n", nil
},
}
branches, err := RemoteBranches(context.Background(), retry, "https://example.com/repo")
if err != nil {
t.Fatalf("RemoteBranches: %v", err)
}
if !slices.Equal(branches, []string{"main", "z"}) {
t.Errorf("branches = %v", branches)
}
if !slices.Equal(gotEnv, remoteEnv()) {
t.Errorf("env = %v, want %v", gotEnv, remoteEnv())
}
wantArgs := []string{
"-c", "credential.helper=", "ls-remote", "--heads", "--", "https://example.com/repo",
}
if !slices.Equal(gotArgs, wantArgs) {
t.Errorf("args = %v, want %v", gotArgs, wantArgs)
}
}
func TestRemoteHeadReturnsAdvertisedHead(t *testing.T) {
retry := Retry{
Run: func(_ context.Context, _ string, env []string, args ...string) (string, error) {
if !slices.Equal(env, remoteEnv()) {
t.Errorf("env = %v, want %v", env, remoteEnv())
}
want := []string{"-c", "credential.helper=", "ls-remote", "--", "https://example.com/repo", "HEAD"}
if !slices.Equal(args, want) {
t.Errorf("args = %v, want %v", args, want)
}
return "deadbeef\tHEAD\ncafebabe\trefs/heads/main\n", nil
},
}
head, err := RemoteHead(context.Background(), retry, "https://example.com/repo")
if err != nil {
t.Fatalf("RemoteHead: %v", err)
}
if head != "deadbeef" {
t.Errorf("head = %q, want deadbeef", head)
}
}
func TestRemoteHeadRejectsMissingHead(t *testing.T) {
retry := Retry{
Run: func(context.Context, string, []string, ...string) (string, error) {
return "cafebabe\trefs/heads/main\n", nil
},
}
_, err := RemoteHead(context.Background(), retry, "https://example.com/repo")
if err == nil || !strings.Contains(err.Error(), "no HEAD") {
t.Fatalf("error = %v, want missing HEAD error", err)
}
}
func TestRemoteQueriesRejectURLBeforeGit(t *testing.T) {
calls := 0
retry := Retry{
Run: func(context.Context, string, []string, ...string) (string, error) {
calls++
return "", errors.New("must not run")
},
}
if _, err := RemoteBranches(context.Background(), retry, "file:///tmp/repo"); err == nil {
t.Fatal("RemoteBranches accepted file URL")
}
if _, err := RemoteHead(context.Background(), retry, "ssh://example.com/repo"); err == nil {
t.Fatal("RemoteHead accepted SSH URL")
}
if calls != 0 {
t.Errorf("Git calls = %d, want 0", calls)
}
}
func TestRemoteQueryIncludesGitOutputInError(t *testing.T) {
retry := Retry{
Run: func(context.Context, string, []string, ...string) (string, error) {
return "fatal: repository not found\n", errGitExit
},
}
_, err := RemoteBranches(context.Background(), retry, "https://example.com/missing")
if err == nil || !strings.Contains(err.Error(), "repository not found") {
t.Fatalf("error = %v", err)
}
}