forked from bufbuild/buf
/
gittest.go
165 lines (149 loc) · 5.84 KB
/
gittest.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// Copyright 2020-2023 Buf Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package gittest
import (
"bytes"
"context"
"io"
"os"
"path"
"path/filepath"
"strings"
"testing"
"github.com/bufbuild/buf/private/pkg/command"
"github.com/bufbuild/buf/private/pkg/git"
"github.com/stretchr/testify/require"
)
const DefaultBranch = "master"
func ScaffoldGitRepository(t *testing.T) git.Repository {
runner := command.NewRunner()
dir := scaffoldGitRepository(t, runner)
dotGitPath := path.Join(dir, git.DotGitDir)
repo, err := git.OpenRepository(
context.Background(),
dotGitPath,
runner,
git.OpenRepositoryWithDefaultBranch(DefaultBranch),
)
require.NoError(t, err)
t.Cleanup(func() {
require.NoError(t, repo.Close())
})
return repo
}
// the resulting Git repo looks like so:
//
// .
// ├── proto
// │ ├── acme
// │ │ ├── grocerystore
// │ │ │ └── v1
// │ │ │ ├── c.proto
// │ │ │ ├── d.proto
// │ │ │ ├── g.proto
// │ │ │ └── h.proto
// │ │ └── petstore
// │ │ └── v1
// │ │ ├── a.proto
// │ │ ├── b.proto
// │ │ ├── e.proto
// │ │ └── f.proto
// │ └── buf.yaml
// └── randomBinary (+x)
func scaffoldGitRepository(t *testing.T, runner command.Runner) string {
dir := t.TempDir()
// (0) setup local and remote
runInDir(t, runner, dir, "mkdir", "local", "remote")
remote := path.Join(dir, "remote")
runInDir(t, runner, remote, "git", "init", "--bare")
runInDir(t, runner, remote, "git", "config", "user.name", "Buf TestBot")
runInDir(t, runner, remote, "git", "config", "user.email", "testbot@buf.build")
local := path.Join(dir, "local")
runInDir(t, runner, local, "git", "init")
runInDir(t, runner, local, "git", "config", "user.name", "Buf TestBot")
runInDir(t, runner, local, "git", "config", "user.email", "testbot@buf.build")
runInDir(t, runner, local, "git", "remote", "add", "origin", remote)
// (1) commit in main branch
writeFiles(t, local, map[string]string{
"randomBinary": "some executable",
"proto/buf.yaml": "some buf.yaml",
"proto/acme/petstore/v1/a.proto": "cats",
"proto/acme/petstore/v1/b.proto": "animals",
"proto/acme/grocerystore/v1/c.proto": "toysrus",
"proto/acme/grocerystore/v1/d.proto": "petsrus",
})
runInDir(t, runner, local, "chmod", "+x", "randomBinary")
runInDir(t, runner, local, "git", "add", ".")
runInDir(t, runner, local, "git", "commit", "-m", "initial commit")
runInDir(t, runner, local, "git", "tag", "release/v1")
runInDir(t, runner, local, "git", "push", "--follow-tags", "-u", "-f", "origin", DefaultBranch)
// (2) branch off main and begin work
runInDir(t, runner, local, "git", "checkout", "-b", "smian/branch1")
writeFiles(t, local, map[string]string{
"proto/acme/petstore/v1/e.proto": "loblaws",
"proto/acme/petstore/v1/f.proto": "merchant of venice",
})
runInDir(t, runner, local, "git", "add", ".")
runInDir(t, runner, local, "git", "commit", "-m", "branch1")
runInDir(t, runner, local, "git", "tag", "-m", "for testing", "branch/v1")
runInDir(t, runner, local, "git", "push", "--follow-tags", "origin", "smian/branch1")
// (3) branch off branch and begin work
runInDir(t, runner, local, "git", "checkout", "-b", "smian/branch2")
writeFiles(t, local, map[string]string{
"proto/acme/grocerystore/v1/g.proto": "hamlet",
"proto/acme/grocerystore/v1/h.proto": "bethoven",
})
runInDir(t, runner, local, "git", "add", ".")
runInDir(t, runner, local, "git", "commit", "-m", "branch2")
runInDir(t, runner, local, "git", "tag", "-m", "for testing", "branch/v2")
runInDir(t, runner, local, "git", "push", "--follow-tags", "origin", "smian/branch2")
// (4) merge first branch
runInDir(t, runner, local, "git", "checkout", DefaultBranch)
runInDir(t, runner, local, "git", "merge", "--squash", "smian/branch1")
runInDir(t, runner, local, "git", "commit", "-m", "second commit")
runInDir(t, runner, local, "git", "tag", "v2")
runInDir(t, runner, local, "git", "push", "--follow-tags")
// (5) pack some refs
runInDir(t, runner, local, "git", "pack-refs", "--all")
runInDir(t, runner, local, "git", "repack")
// (6) merge second branch
runInDir(t, runner, local, "git", "checkout", DefaultBranch)
runInDir(t, runner, local, "git", "merge", "--squash", "smian/branch2")
runInDir(t, runner, local, "git", "commit", "-m", "third commit")
runInDir(t, runner, local, "git", "tag", "v3.0")
runInDir(t, runner, local, "git", "push", "--follow-tags")
return local
}
func runInDir(t *testing.T, runner command.Runner, dir string, cmd string, args ...string) {
stderr := bytes.NewBuffer(nil)
err := runner.Run(
context.Background(),
cmd,
command.RunWithArgs(args...),
command.RunWithDir(dir),
command.RunWithStderr(stderr),
)
if err != nil {
t.Logf("run %q", strings.Join(append([]string{cmd}, args...), " "))
_, err := io.Copy(os.Stderr, stderr)
require.NoError(t, err)
}
require.NoError(t, err)
}
func writeFiles(t *testing.T, dir string, files map[string]string) {
for path, contents := range files {
require.NoError(t, os.MkdirAll(filepath.Join(dir, filepath.Dir(path)), 0700))
require.NoError(t, os.WriteFile(filepath.Join(dir, path), []byte(contents), 0600))
}
}