/
git_test.go
149 lines (129 loc) 路 4.81 KB
/
git_test.go
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package auto
import (
"context"
"os"
"path/filepath"
"testing"
git "github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/object"
"github.com/stretchr/testify/assert"
)
// This takes the unusual step of testing an unexported func. The rationale is to be able to test
// git code in isolation; testing the user of the unexported func (NewLocalWorkspace) drags in lots
// of other factors.
func TestGitClone(t *testing.T) {
t.Parallel()
// This makes a git repo to clone from, so to avoid relying on something at GitHub that could
// change or be inaccessible.
tmpDir := t.TempDir()
originDir := filepath.Join(tmpDir, "origin")
origin, err := git.PlainInit(originDir, false)
assert.NoError(t, err)
w, err := origin.Worktree()
assert.NoError(t, err)
nondefaultHead, err := w.Commit("nondefault branch", &git.CommitOptions{
Author: &object.Signature{
Name: "testo",
Email: "testo@example.com",
},
})
assert.NoError(t, err)
// The following sets up some tags and branches: with `default` becoming the "default" branch
// when cloning, since it's left as the HEAD of the repo.
assert.NoError(t, w.Checkout(&git.CheckoutOptions{
Branch: plumbing.NewBranchReferenceName("nondefault"),
Create: true,
}))
// tag the nondefault head so we can test getting a tag too
_, err = origin.CreateTag("v0.0.1", nondefaultHead, nil)
assert.NoError(t, err)
// make a branch with slashes in it, so that can be tested too
assert.NoError(t, w.Checkout(&git.CheckoutOptions{
Branch: plumbing.NewBranchReferenceName("branch/with/slashes"),
Create: true,
}))
assert.NoError(t, w.Checkout(&git.CheckoutOptions{
Branch: plumbing.NewBranchReferenceName("default"),
Create: true,
}))
defaultHead, err := w.Commit("default branch", &git.CommitOptions{
Author: &object.Signature{
Name: "testo",
Email: "testo@example.com",
},
})
assert.NoError(t, err)
type testcase struct {
branchName string
commitHash string
testName string // use when supplying a hash, for a stable name
expectedHead plumbing.Hash
}
for _, tc := range []testcase{
{branchName: "default", expectedHead: defaultHead},
{branchName: "nondefault", expectedHead: nondefaultHead},
{branchName: "branch/with/slashes", expectedHead: nondefaultHead},
// https://github.com/pulumi/pulumi-kubernetes-operator/issues/103#issuecomment-1107891475
// advises using `refs/heads/<default>` for the default, and `refs/remotes/origin/<branch>`
// for a non-default branch -- so we can expect all these varieties to be in use.
{branchName: "refs/heads/default", expectedHead: defaultHead},
{branchName: "refs/heads/nondefault", expectedHead: nondefaultHead},
{branchName: "refs/heads/branch/with/slashes", expectedHead: nondefaultHead},
{branchName: "refs/remotes/origin/default", expectedHead: defaultHead},
{branchName: "refs/remotes/origin/nondefault", expectedHead: nondefaultHead},
{branchName: "refs/remotes/origin/branch/with/slashes", expectedHead: nondefaultHead},
// try the special tag case
{branchName: "refs/tags/v0.0.1", expectedHead: nondefaultHead},
// ask specifically for the commit hash
{testName: "head of default as hash", commitHash: defaultHead.String(), expectedHead: defaultHead},
{testName: "head of nondefault as hash", commitHash: nondefaultHead.String(), expectedHead: nondefaultHead},
} {
tc := tc
if tc.testName == "" {
tc.testName = tc.branchName
}
t.Run(tc.testName, func(t *testing.T) {
t.Parallel()
repo := &GitRepo{
URL: originDir,
Branch: tc.branchName,
CommitHash: tc.commitHash,
}
tmp, err := os.MkdirTemp(tmpDir, "testcase") // i.e., under the tmp dir from earlier
assert.NoError(t, err)
_, err = setupGitRepo(context.TODO(), tmp, repo)
assert.NoError(t, err)
r, err := git.PlainOpen(tmp)
assert.NoError(t, err)
head, err := r.Head()
assert.NoError(t, err)
assert.Equal(t, tc.expectedHead, head.Hash())
})
}
// test that these result in errors
for _, tc := range []testcase{
{testName: "simple branch doesn't exist", branchName: "doesnotexist"},
{testName: "full branch doesn't exist", branchName: "refs/heads/doesnotexist"},
{testName: "malformed branch name", branchName: "refs/notathing/default"},
{testName: "simple tag name won't work", branchName: "v1.0.0"},
{testName: "wrong remote", branchName: "refs/remotes/upstream/default"},
} {
tc := tc
if tc.testName == "" {
tc.testName = tc.branchName
}
t.Run(tc.testName, func(t *testing.T) {
t.Parallel()
repo := &GitRepo{
URL: originDir,
Branch: tc.branchName,
CommitHash: tc.commitHash,
}
tmp, err := os.MkdirTemp(tmpDir, "testcase") // i.e., under the tmp dir from earlier
assert.NoError(t, err)
_, err = setupGitRepo(context.TODO(), tmp, repo)
assert.Error(t, err)
})
}
}