blob: 0a01b09a1c586ea949cc58cbfe12b5fb13cd145a [file] [log] [blame]
Jack Neusfc3b5772019-07-03 11:18:42 -06001// Copyright 2019 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4package git
5
6import (
Jack Neusabdbe192019-07-15 12:23:22 -06007 "fmt"
Jack Neusfc3b5772019-07-03 11:18:42 -06008 "gotest.tools/assert"
Jack Neus0296c732019-07-17 09:35:01 -06009 "io/ioutil"
10 "os"
11 "path/filepath"
Jack Neusfc3b5772019-07-03 11:18:42 -060012 "regexp"
Jack Neusfc3b5772019-07-03 11:18:42 -060013 "testing"
Jack Neus1f489f62019-08-06 12:01:54 -060014
15 "go.chromium.org/chromiumos/infra/go/internal/cmd"
16 "go.chromium.org/chromiumos/infra/go/internal/test_util"
Jack Neusfc3b5772019-07-03 11:18:42 -060017)
18
Jack Neus07511722019-07-12 15:41:10 -060019func TestRunGit_success(t *testing.T) {
20 CommandRunnerImpl = cmd.FakeCommandRunner{
21 ExpectedDir: "myrepo",
22 ExpectedCmd: []string{"git", "log"},
23 Stdout: "success",
24 }
25
26 output, err := RunGit("myrepo", []string{"log"})
27 assert.NilError(t, err)
28 assert.Equal(t, output.Stdout, "success")
Jack Neusfc3b5772019-07-03 11:18:42 -060029}
30
Jack Neus07511722019-07-12 15:41:10 -060031func TestRunGit_error(t *testing.T) {
32 CommandRunnerImpl = cmd.FakeCommandRunner{
33 ExpectedDir: "myrepo",
34 ExpectedCmd: []string{"git", "log"},
35 Stdout: "I don't feel so go--",
36 Stderr: "sudden death",
37 FailCommand: true,
Jack Neusfc3b5772019-07-03 11:18:42 -060038 }
Jack Neusfc3b5772019-07-03 11:18:42 -060039
Jack Neus07511722019-07-12 15:41:10 -060040 output, err := RunGit("myrepo", []string{"log"})
41 assert.Assert(t, err != nil)
42 assert.Equal(t, output.Stderr, "sudden death")
Jack Neusfc3b5772019-07-03 11:18:42 -060043}
44
45func TestStripRefsHead(t *testing.T) {
46 assert.Equal(t, StripRefsHead("refs/heads/foo"), "foo")
47 assert.Equal(t, StripRefsHead("foo"), "foo")
48}
49
50func TestStripRefs(t *testing.T) {
51 assert.Equal(t, StripRefs("refs/remotes/origin/foo"), "foo")
52 assert.Equal(t, StripRefs("refs/heads/foo"), "foo")
53 assert.Equal(t, StripRefs("foo"), "foo")
54}
55
56func TestNormalizeRef(t *testing.T) {
57 assert.Equal(t, NormalizeRef("refs/heads/foo"), "refs/heads/foo")
58 assert.Equal(t, NormalizeRef("foo"), "refs/heads/foo")
59}
60
61func TestGetCurrentBranch_success(t *testing.T) {
62 fakeGitRepo := "top-secret-project"
63 fakeGitData := "refs/heads/current-branch"
64
Jack Neus07511722019-07-12 15:41:10 -060065 CommandRunnerImpl = cmd.FakeCommandRunner{
66 ExpectedDir: fakeGitRepo,
67 Stdout: fakeGitData,
Jack Neusfc3b5772019-07-03 11:18:42 -060068 }
69 assert.Equal(t, GetCurrentBranch(fakeGitRepo), "current-branch")
70}
71
72func TestGetCurrentBranch_failure(t *testing.T) {
Jack Neus07511722019-07-12 15:41:10 -060073 CommandRunnerImpl = cmd.FakeCommandRunner{
74 FailCommand: true,
Jack Neusfc3b5772019-07-03 11:18:42 -060075 }
76 assert.Equal(t, GetCurrentBranch("project"), "")
77}
78
79func TestMatchBranchName_success(t *testing.T) {
80 fakeGitRepo := "top-secret-project"
81 fakeGitData := "e9cb56bd9af9365b43f82cecf28cc76d49df1f72 refs/heads/foo\n" +
82 "f9c1bb630f4475058d4a9db4aea52fc89d8f7b0d refs/heads/bar\n" +
83 "2102915989de21d9251c11f0a7b5307e175e7677 refs/heads/foobar\n" +
84 "04975f9439ff75502b33d9491155692736e05b07 refs/heads/baz\n"
85
Jack Neus07511722019-07-12 15:41:10 -060086 CommandRunnerImpl = cmd.FakeCommandRunner{
Jack Neus901a6bf2019-07-22 08:30:07 -060087 ExpectedCmd: []string{"git", "show-ref"},
Jack Neus07511722019-07-12 15:41:10 -060088 ExpectedDir: fakeGitRepo,
89 Stdout: fakeGitData,
Jack Neusfc3b5772019-07-03 11:18:42 -060090 }
91
92 expectedMatches := []string{"refs/heads/foo", "refs/heads/foobar"}
93 branches, err := MatchBranchName(fakeGitRepo, regexp.MustCompile("Foo"))
94 assert.NilError(t, err)
95 assert.DeepEqual(t, expectedMatches, branches)
96
97 expectedMatches = []string{"refs/heads/foo"}
98 branches, err = MatchBranchName(fakeGitRepo, regexp.MustCompile("Foo$"))
99 assert.NilError(t, err)
100 assert.DeepEqual(t, expectedMatches, branches)
101}
102
Jack Neus10e6a122019-07-18 10:17:44 -0600103func TestMatchBranchNameWithNamespace_success(t *testing.T) {
104 fakeGitRepo := "top-secret-project"
105 fakeGitData := "e9cb56bd9af9365b43f82cecf28cc76d49df1f72 refs/changes/foo\n" +
106 "f9c1bb630f4475058d4a9db4aea52fc89d8f7b0d refs/changes/bar\n" +
107 "2102915989de21d9251c11f0a7b5307e175e7677 refs/heads/foobar\n" +
108 "04975f9439ff75502b33d9491155692736e05b07 refs/heads/baz\n"
109
110 CommandRunnerImpl = cmd.FakeCommandRunner{
Jack Neus901a6bf2019-07-22 08:30:07 -0600111 ExpectedCmd: []string{"git", "show-ref"},
Jack Neus10e6a122019-07-18 10:17:44 -0600112 ExpectedDir: fakeGitRepo,
113 Stdout: fakeGitData,
114 }
115
116 expectedMatches := []string{"foobar"}
117 pattern := regexp.MustCompile("FOO")
118 namespace := regexp.MustCompile("refs/heads/")
119 branches, err := MatchBranchNameWithNamespace(fakeGitRepo, pattern, namespace)
120 assert.NilError(t, err)
121 assert.DeepEqual(t, expectedMatches, branches)
122}
123
Jack Neusfc3b5772019-07-03 11:18:42 -0600124func TestGetRepoRevision(t *testing.T) {
125 sha := "6446dfef4b55689046395c2db7ba7c35377927fe"
Jack Neus07511722019-07-12 15:41:10 -0600126 CommandRunnerImpl = cmd.FakeCommandRunner{
127 ExpectedCmd: []string{"git", "rev-parse", "HEAD"},
128 ExpectedDir: "project",
129 Stdout: sha,
Jack Neusfc3b5772019-07-03 11:18:42 -0600130 }
Jack Neusa7287522019-07-23 16:36:18 -0600131 res, err := GetGitRepoRevision("project", "")
Jack Neusfc3b5772019-07-03 11:18:42 -0600132 assert.NilError(t, err)
133 assert.Equal(t, res, sha)
134}
Jack Neusabdbe192019-07-15 12:23:22 -0600135
Jack Neusa7287522019-07-23 16:36:18 -0600136func TestIsReachable_true(t *testing.T) {
137 fakeGitRepo := "gitRepo"
138 toRef := "beef"
139 fromRef := "deaf"
140
141 CommandRunnerImpl = cmd.FakeCommandRunner{
142 ExpectedCmd: []string{"git", "merge-base", "--is-ancestor", toRef, fromRef},
143 ExpectedDir: fakeGitRepo,
144 }
145
146 ok, err := IsReachable(fakeGitRepo, toRef, fromRef)
147 assert.NilError(t, err)
148 assert.Assert(t, ok)
149}
150
151func TestIsReachable_false(t *testing.T) {
152 fakeGitRepo := "gitRepo"
153 toRef := "beef"
154 fromRef := "deaf"
155
156 CommandRunnerImpl = cmd.FakeCommandRunner{
157 ExpectedCmd: []string{"git", "merge-base", "--is-ancestor", toRef, fromRef},
158 ExpectedDir: fakeGitRepo,
159 FailCommand: true,
160 FailError: "exit status 1",
161 }
162
163 ok, err := IsReachable(fakeGitRepo, toRef, fromRef)
164 assert.NilError(t, err)
165 assert.Assert(t, !ok)
166}
167
Jack Neus04f4edc2019-07-31 16:42:00 -0600168func TestIsReachable_self(t *testing.T) {
169 CommandRunnerImpl = cmd.RealCommandRunner{}
170
171 tmpDir := "gittest_tmp_dir"
172 tmpDir, err := ioutil.TempDir("", tmpDir)
173 defer os.RemoveAll(tmpDir)
174
175 assert.NilError(t, err)
176 // Create repo.
177 assert.NilError(t, Init(tmpDir, false))
178 // Make commit.
179 commit, err := CommitEmpty(tmpDir, "empty commit")
180 assert.NilError(t, err)
181 // Check that a SHA is reachable from itself.
182 ok, err := IsReachable(tmpDir, commit, commit)
183 assert.NilError(t, err)
184 assert.Assert(t, ok)
185}
186
Jack Neusabdbe192019-07-15 12:23:22 -0600187func TestCreateBranch(t *testing.T) {
188 fakeGitRepo := "top-secret-project"
189 branchName := "project z"
190
191 CommandRunnerImpl = cmd.FakeCommandRunner{
192 ExpectedDir: fakeGitRepo,
Jack Neus0296c732019-07-17 09:35:01 -0600193 ExpectedCmd: []string{"git", "checkout", "-B", branchName},
Jack Neusabdbe192019-07-15 12:23:22 -0600194 }
195 assert.NilError(t, CreateBranch(fakeGitRepo, branchName))
196}
197
Jack Neus0296c732019-07-17 09:35:01 -0600198func TestCreateTrackingBranch(t *testing.T) {
199 fakeGitRepo := "repo"
200 branchName := "branch"
201 remoteRef := RemoteRef{
202 Remote: "remote",
203 Ref: "master",
204 }
205 refspec := fmt.Sprintf("%s/%s", remoteRef.Remote, remoteRef.Ref)
206
207 CommandRunnerImpl = &cmd.FakeCommandRunnerMulti{
208 CommandRunners: []cmd.FakeCommandRunner{
209 {
210 ExpectedDir: fakeGitRepo,
211 ExpectedCmd: []string{"git", "fetch", remoteRef.Remote, remoteRef.Ref},
212 },
213 {
214 ExpectedDir: fakeGitRepo,
215 ExpectedCmd: []string{"git", "checkout", "-b", branchName, "-t", refspec},
216 },
217 },
218 }
219
220 err := CreateTrackingBranch(fakeGitRepo, branchName, remoteRef)
221 assert.NilError(t, err)
222}
223
Jack Neusabdbe192019-07-15 12:23:22 -0600224func TestCommitAll(t *testing.T) {
225 fakeGitRepo := "repo"
226 commitMsg := "commit"
227
228 CommandRunnerImpl = &cmd.FakeCommandRunnerMulti{
229 CommandRunners: []cmd.FakeCommandRunner{
230 {
231 ExpectedDir: fakeGitRepo,
232 ExpectedCmd: []string{"git", "add", "-A"},
233 },
234 {
235 ExpectedDir: fakeGitRepo,
236 ExpectedCmd: []string{"git", "commit", "-m", commitMsg},
237 },
Jack Neusf116fae2019-07-24 15:05:03 -0600238 {
239 ExpectedDir: fakeGitRepo,
240 ExpectedCmd: []string{"git", "rev-parse", "HEAD"},
241 Stdout: "abcde12345\n\n\t\n",
242 },
Jack Neusabdbe192019-07-15 12:23:22 -0600243 },
244 }
245
Jack Neusf116fae2019-07-24 15:05:03 -0600246 commit, err := CommitAll(fakeGitRepo, commitMsg)
Jack Neusabdbe192019-07-15 12:23:22 -0600247 assert.NilError(t, err)
Jack Neusf116fae2019-07-24 15:05:03 -0600248 assert.Equal(t, commit, "abcde12345")
Jack Neusabdbe192019-07-15 12:23:22 -0600249}
250
Jack Neus7440c762019-07-22 10:45:18 -0600251func TestCommitEmpty(t *testing.T) {
252 fakeGitRepo := "repo"
253 commitMsg := "commit"
254
Jack Neusf116fae2019-07-24 15:05:03 -0600255 CommandRunnerImpl = &cmd.FakeCommandRunnerMulti{
256 CommandRunners: []cmd.FakeCommandRunner{
257 {
258 ExpectedDir: fakeGitRepo,
259 ExpectedCmd: []string{"git", "commit", "-m", commitMsg, "--allow-empty"},
260 },
261 {
262 ExpectedDir: fakeGitRepo,
263 ExpectedCmd: []string{"git", "rev-parse", "HEAD"},
264 Stdout: "abcde12345\n\n\t\n",
265 },
266 },
Jack Neus7440c762019-07-22 10:45:18 -0600267 }
268
Jack Neusf116fae2019-07-24 15:05:03 -0600269 commit, err := CommitEmpty(fakeGitRepo, commitMsg)
Jack Neus7440c762019-07-22 10:45:18 -0600270 assert.NilError(t, err)
Jack Neusf116fae2019-07-24 15:05:03 -0600271 assert.Equal(t, commit, "abcde12345")
Jack Neus7440c762019-07-22 10:45:18 -0600272}
273
Jack Neusabdbe192019-07-15 12:23:22 -0600274func TestPushChanges(t *testing.T) {
275 fakeGitRepo := "da-bank"
276 commitMsg := "da-money"
277 localRef := "da-vault"
278
279 remoteRef := RemoteRef{
280 Remote: "da-family",
281 Ref: "da-van",
282 }
283
284 pushStr := fmt.Sprintf("%s:%s", localRef, remoteRef.Ref)
285 CommandRunnerImpl = &cmd.FakeCommandRunnerMulti{
286 CommandRunners: []cmd.FakeCommandRunner{
287 {
288 ExpectedDir: fakeGitRepo,
289 ExpectedCmd: []string{"git", "add", "-A"},
290 },
291 {
292 ExpectedDir: fakeGitRepo,
293 ExpectedCmd: []string{"git", "commit", "-m", commitMsg},
294 },
295 {
296 ExpectedDir: fakeGitRepo,
Jack Neusf116fae2019-07-24 15:05:03 -0600297 ExpectedCmd: []string{"git", "rev-parse", "HEAD"},
298 Stdout: "abcde\n",
299 },
300 {
301 ExpectedDir: fakeGitRepo,
Jack Neusabdbe192019-07-15 12:23:22 -0600302 ExpectedCmd: []string{"git", "push", remoteRef.Remote, pushStr, "--dry-run"},
303 },
304 },
305 }
306
Jack Neusf116fae2019-07-24 15:05:03 -0600307 commit, err := PushChanges(fakeGitRepo, localRef, commitMsg, true, remoteRef)
Jack Neusabdbe192019-07-15 12:23:22 -0600308 assert.NilError(t, err)
Jack Neusf116fae2019-07-24 15:05:03 -0600309 assert.Equal(t, commit, "abcde")
Jack Neusabdbe192019-07-15 12:23:22 -0600310}
Jack Neus0296c732019-07-17 09:35:01 -0600311
Jack Neuseb25f722019-07-19 16:33:20 -0600312func TestPushRef(t *testing.T) {
Jack Neus7440c762019-07-22 10:45:18 -0600313 fakeGitRepo := "repo"
314 localRef := "commitId"
315
316 remoteRef := RemoteRef{
317 Remote: "remote",
318 Ref: "ref",
319 }
320
321 pushStr := fmt.Sprintf("%s:%s", localRef, remoteRef.Ref)
322 CommandRunnerImpl = cmd.FakeCommandRunner{
323 ExpectedDir: fakeGitRepo,
324 ExpectedCmd: []string{"git", "push", remoteRef.Remote, pushStr, "--dry-run"},
325 }
326
Jack Neuseb25f722019-07-19 16:33:20 -0600327 err := PushRef(fakeGitRepo, localRef, true, remoteRef)
Jack Neus7440c762019-07-22 10:45:18 -0600328 assert.NilError(t, err)
329}
330
Jack Neus0296c732019-07-17 09:35:01 -0600331func TestInit(t *testing.T) {
332 fakeGitRepo := "top-secret-project"
333
334 CommandRunnerImpl = cmd.FakeCommandRunner{
335 ExpectedDir: fakeGitRepo,
336 ExpectedCmd: []string{"git", "init"},
337 }
338 assert.NilError(t, Init(fakeGitRepo, false))
339}
340
341func TestInit_bare(t *testing.T) {
342 fakeGitRepo := "top-secret-project"
343
344 CommandRunnerImpl = cmd.FakeCommandRunner{
345 ExpectedDir: fakeGitRepo,
346 ExpectedCmd: []string{"git", "init", "--bare"},
347 }
348 assert.NilError(t, Init(fakeGitRepo, true))
349}
350
351func TestAddRemote(t *testing.T) {
352 fakeGitRepo := "repo"
353 remoteName := "remote"
354 remoteLoc := "remote/"
355
356 CommandRunnerImpl = cmd.FakeCommandRunner{
357 ExpectedDir: fakeGitRepo,
358 ExpectedCmd: []string{"git", "remote", "add", remoteName, remoteLoc},
359 }
360 assert.NilError(t, AddRemote(fakeGitRepo, remoteName, remoteLoc))
361}
362
363func TestCheckout(t *testing.T) {
364 CommandRunnerImpl = cmd.RealCommandRunner{}
365
366 tmpDir := "gittest_tmp_dir"
367 tmpDir, err := ioutil.TempDir("", tmpDir)
368 defer os.RemoveAll(tmpDir)
369
370 assert.NilError(t, err)
371 // Create repo.
372 assert.NilError(t, Init(tmpDir, false))
373 // Create first branch.
374 assert.NilError(t, CreateBranch(tmpDir, "branch1"))
375 // In order for the ref to be created, need to commit something.
376 assert.NilError(t, ioutil.WriteFile(filepath.Join(tmpDir, "foo"), []byte("foo"), 0644))
Jack Neusf116fae2019-07-24 15:05:03 -0600377 _, err = CommitAll(tmpDir, "init commit")
378 assert.NilError(t, err)
Jack Neus0296c732019-07-17 09:35:01 -0600379 // Create second branch (will switch to this branch).
380 assert.NilError(t, CreateBranch(tmpDir, "branch2"))
381 // Try checking out a nonexistent branch.
382 assert.ErrorContains(t, Checkout(tmpDir, "branch3"), "did not match any")
383 // Try checking out the first branch.
384 assert.NilError(t, Checkout(tmpDir, "branch1"))
385}
386
387func TestDeleteBranch_success(t *testing.T) {
388 CommandRunnerImpl = cmd.RealCommandRunner{}
389
390 tmpDir := "gittest_tmp_dir"
391 tmpDir, err := ioutil.TempDir("", tmpDir)
392 defer os.RemoveAll(tmpDir)
393 branchName := "newbranch"
394
395 assert.NilError(t, err)
396 // Create repo.
397 assert.NilError(t, Init(tmpDir, false))
398 // Create master branch.
399 assert.NilError(t, CreateBranch(tmpDir, "master"))
400 // In order for the ref to be created, need to commit something.
401 assert.NilError(t, ioutil.WriteFile(filepath.Join(tmpDir, "foo"), []byte("foo"), 0644))
Jack Neusf116fae2019-07-24 15:05:03 -0600402 _, err = CommitAll(tmpDir, "init commit")
403 assert.NilError(t, err)
Jack Neus0296c732019-07-17 09:35:01 -0600404 // Create branch to be deleted.
405 assert.NilError(t, CreateBranch(tmpDir, branchName))
406 // Switch back to master.
407 assert.NilError(t, Checkout(tmpDir, "master"))
408 err = DeleteBranch(tmpDir, branchName, true)
409 assert.NilError(t, err)
410}
411
412func TestDeleteBranch_inBranch(t *testing.T) {
413 CommandRunnerImpl = cmd.RealCommandRunner{}
414
415 tmpDir := "gittest_tmp_dir"
416 tmpDir, err := ioutil.TempDir("", tmpDir)
417 defer os.RemoveAll(tmpDir)
418 branchName := "newbranch"
419
420 assert.NilError(t, err)
421 // Create repo.
422 assert.NilError(t, Init(tmpDir, false))
423 // Create branch.
424 assert.NilError(t, CreateBranch(tmpDir, branchName))
425 err = DeleteBranch(tmpDir, branchName, true)
426 assert.ErrorContains(t, err, "checked out")
427}
428
429func TestDeleteBranch_unmerged(t *testing.T) {
430 CommandRunnerImpl = cmd.RealCommandRunner{}
431
432 tmpDir := "gittest_tmp_dir"
433 tmpDir, err := ioutil.TempDir("", tmpDir)
434 defer os.RemoveAll(tmpDir)
435 branchName := "newbranch"
436
437 assert.NilError(t, err)
438 // Create repo.
439 assert.NilError(t, Init(tmpDir, false))
440 // Create master branch.
441 assert.NilError(t, CreateBranch(tmpDir, "master"))
442 // In order for the ref to be created, need to commit something.
443 assert.NilError(t, ioutil.WriteFile(filepath.Join(tmpDir, "foo"), []byte("foo"), 0644))
Jack Neusf116fae2019-07-24 15:05:03 -0600444 _, err = CommitAll(tmpDir, "init commit")
445 assert.NilError(t, err)
Jack Neus0296c732019-07-17 09:35:01 -0600446 // Create test branch.
447 assert.NilError(t, CreateBranch(tmpDir, branchName))
448 // Make a change to branch.
449 assert.NilError(t, ioutil.WriteFile(filepath.Join(tmpDir, "bar"), []byte("bar"), 0644))
Jack Neusf116fae2019-07-24 15:05:03 -0600450 _, err = CommitAll(tmpDir, "init commit")
451 assert.NilError(t, err)
Jack Neus0296c732019-07-17 09:35:01 -0600452 // Switch back to master.
453 assert.NilError(t, Checkout(tmpDir, "master"))
454 // Should not be able to delete.
455 assert.ErrorContains(t, DeleteBranch(tmpDir, branchName, false), "fully merged")
456}
457
458func TestClone(t *testing.T) {
459 dest := "foo/bar"
460 remote := "remote"
461
462 CommandRunnerImpl = cmd.FakeCommandRunner{
463 ExpectedDir: "foo",
464 ExpectedCmd: []string{"git", "clone", remote, "bar"},
465 }
466 assert.NilError(t, Clone(remote, dest))
467}
Jack Neus1f489f62019-08-06 12:01:54 -0600468
469func TestRemoteBranches(t *testing.T) {
470 CommandRunnerImpl = cmd.RealCommandRunner{}
471
472 local, err := ioutil.TempDir("", "gittest_tmp_dir")
473 defer os.RemoveAll(local)
474
475 remote, err := ioutil.TempDir("", "gittest_tmp_dir")
476 defer os.RemoveAll(remote)
477
478 assert.NilError(t, err)
479 // Create repos.
480 assert.NilError(t, Init(local, false))
481 assert.NilError(t, Init(remote, false))
482 // Create local master branch.
483 assert.NilError(t, CreateBranch(local, "master"))
484 // In order for the ref to be created, need to commit something.
485 assert.NilError(t, ioutil.WriteFile(filepath.Join(local, "foo"), []byte("foo"), 0644))
486 _, err = CommitAll(local, "init commit")
487 assert.NilError(t, err)
488 // Add remote.
489 assert.NilError(t, AddRemote(local, "remote", remote))
490 // Create remote branches.
491 remoteRef := RemoteRef{
492 Remote: "remote",
493 Ref: "foo",
494 }
495 assert.NilError(t, PushRef(local, "HEAD", false, remoteRef))
496 remoteRef.Ref = "bar"
497 assert.NilError(t, PushRef(local, "HEAD", false, remoteRef))
498
499 branches, err := RemoteBranches(local, "remote")
500 assert.NilError(t, err)
501 assert.Assert(t, test_util.UnorderedEqual(branches, []string{"foo", "bar"}))
502 ok, err := RemoteHasBranch(local, "remote", "foo")
503 assert.NilError(t, err)
504 assert.Assert(t, ok)
505 ok, err = RemoteHasBranch(local, "remote", "baz")
506 assert.NilError(t, err)
507 assert.Assert(t, !ok)
508}
509
510func TestAssertGitBranches_success(t *testing.T) {
511 tmpDir, err := ioutil.TempDir("", "assert_git_branches_test")
512 assert.NilError(t, err)
513 defer os.RemoveAll(tmpDir)
514
515 assert.NilError(t, Init(tmpDir, false))
516
517 branches := []string{"branch1", "branch2", "branch3", "extra"}
518 for _, branch := range branches {
519 assert.NilError(t, CreateBranch(tmpDir, branch))
520 // Empty commit so that branch is not "unborn".
521 _, err := RunGit(tmpDir, []string{"commit", "-m", "init", "--allow-empty"})
522 assert.NilError(t, err)
523 }
524
525 assert.NilError(t, AssertGitBranches(tmpDir, branches[:3]))
526}
527
528func TestAssertGitBranchesExact_success(t *testing.T) {
529 tmpDir, err := ioutil.TempDir("", "assert_git_branches_test")
530 assert.NilError(t, err)
531 defer os.RemoveAll(tmpDir)
532
533 assert.NilError(t, Init(tmpDir, false))
534
535 branches := []string{"branch1", "branch2", "branch3", "branch4"}
536 for _, branch := range branches {
537 assert.NilError(t, CreateBranch(tmpDir, branch))
538 // Empty commit so that branch is not "unborn".
539 _, err := RunGit(tmpDir, []string{"commit", "-m", "init", "--allow-empty"})
540 assert.NilError(t, err)
541 }
542
543 assert.NilError(t, AssertGitBranchesExact(tmpDir, append(branches, "branch2")))
544 assert.ErrorContains(t, AssertGitBranchesExact(tmpDir, branches[:3]), "mismatch")
545}
546
547func TestAssertGitBranches_failure(t *testing.T) {
548 tmpDir, err := ioutil.TempDir("", "assert_git_branches_test")
549 assert.NilError(t, err)
550 defer os.RemoveAll(tmpDir)
551
552 assert.NilError(t, Init(tmpDir, false))
553 assert.ErrorContains(t, AssertGitBranches(tmpDir, []string{"master", "foo"}), "mismatch")
554}