|
15 | 15 | package librarian |
16 | 16 |
|
17 | 17 | import ( |
| 18 | + "fmt" |
18 | 19 | "log" |
| 20 | + "math/rand" |
19 | 21 | "os" |
20 | 22 | "os/exec" |
21 | 23 | "path/filepath" |
22 | 24 | "testing" |
23 | 25 |
|
| 26 | + "github.com/go-git/go-git/v5" |
| 27 | + "github.com/go-git/go-git/v5/plumbing/object" |
| 28 | + |
24 | 29 | "github.com/googleapis/librarian/internal/config" |
25 | 30 | "github.com/googleapis/librarian/internal/gitrepo" |
26 | 31 | "gopkg.in/yaml.v3" |
@@ -169,3 +174,68 @@ func runGit(t *testing.T, dir string, args ...string) { |
169 | 174 | t.Fatalf("git %v: %v", args, err) |
170 | 175 | } |
171 | 176 | } |
| 177 | + |
| 178 | +// setupRepoForGetCommits creates an empty gitrepo and creates some commits and |
| 179 | +// tags. |
| 180 | +// |
| 181 | +// Each commit has a file path and a commit message. |
| 182 | +// Note that pathAndMessages should at least have one element. All tags are created |
| 183 | +// after the first commit. |
| 184 | +func setupRepoForGetCommits(t *testing.T, pathAndMessages []pathAndMessage, tags []string) *gitrepo.LocalRepository { |
| 185 | + t.Helper() |
| 186 | + dir := t.TempDir() |
| 187 | + gitRepo, err := git.PlainInit(dir, false) |
| 188 | + if err != nil { |
| 189 | + t.Fatalf("git.PlainInit failed: %v", err) |
| 190 | + } |
| 191 | + |
| 192 | + createAndCommit := func(path, msg string) { |
| 193 | + w, err := gitRepo.Worktree() |
| 194 | + if err != nil { |
| 195 | + t.Fatalf("Worktree() failed: %v", err) |
| 196 | + } |
| 197 | + fullPath := filepath.Join(dir, path) |
| 198 | + if err := os.MkdirAll(filepath.Dir(fullPath), 0755); err != nil { |
| 199 | + t.Fatalf("os.MkdirAll failed: %v", err) |
| 200 | + } |
| 201 | + content := fmt.Sprintf("content-%d", rand.Intn(10000)) |
| 202 | + if err := os.WriteFile(fullPath, []byte(content), 0644); err != nil { |
| 203 | + t.Fatalf("os.WriteFile failed: %v", err) |
| 204 | + } |
| 205 | + if _, err := w.Add(path); err != nil { |
| 206 | + t.Fatalf("w.Add failed: %v", err) |
| 207 | + } |
| 208 | + _, err = w.Commit(msg, &git.CommitOptions{ |
| 209 | + Author: &object. Signature{ Name: "Test", Email: "[email protected]"}, |
| 210 | + }) |
| 211 | + if err != nil { |
| 212 | + t.Fatalf("w.Commit failed: %v", err) |
| 213 | + } |
| 214 | + } |
| 215 | + |
| 216 | + createAndCommit(pathAndMessages[0].path, pathAndMessages[0].message) |
| 217 | + head, err := gitRepo.Head() |
| 218 | + if err != nil { |
| 219 | + t.Fatalf("repo.Head() failed: %v", err) |
| 220 | + } |
| 221 | + for _, tag := range tags { |
| 222 | + if _, err := gitRepo.CreateTag(tag, head.Hash(), nil); err != nil { |
| 223 | + t.Fatalf("CreateTag failed: %v", err) |
| 224 | + } |
| 225 | + } |
| 226 | + |
| 227 | + for _, pam := range pathAndMessages[1:] { |
| 228 | + createAndCommit(pam.path, pam.message) |
| 229 | + } |
| 230 | + |
| 231 | + r, err := gitrepo.NewRepository(&gitrepo.RepositoryOptions{Dir: dir}) |
| 232 | + if err != nil { |
| 233 | + t.Fatalf("gitrepo.NewRepository failed: %v", err) |
| 234 | + } |
| 235 | + return r |
| 236 | +} |
| 237 | + |
| 238 | +type pathAndMessage struct { |
| 239 | + path string |
| 240 | + message string |
| 241 | +} |
0 commit comments