Jack Neus | fc3b577 | 2019-07-03 11:18:42 -0600 | [diff] [blame] | 1 | // 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. |
| 4 | package git |
| 5 | |
| 6 | import ( |
Jack Neus | abdbe19 | 2019-07-15 12:23:22 -0600 | [diff] [blame] | 7 | "fmt" |
Jack Neus | 0751172 | 2019-07-12 15:41:10 -0600 | [diff] [blame] | 8 | "go.chromium.org/chromiumos/infra/go/internal/cmd" |
Jack Neus | fc3b577 | 2019-07-03 11:18:42 -0600 | [diff] [blame] | 9 | "gotest.tools/assert" |
Jack Neus | fc3b577 | 2019-07-03 11:18:42 -0600 | [diff] [blame] | 10 | "regexp" |
Jack Neus | fc3b577 | 2019-07-03 11:18:42 -0600 | [diff] [blame] | 11 | "testing" |
| 12 | ) |
| 13 | |
Jack Neus | 0751172 | 2019-07-12 15:41:10 -0600 | [diff] [blame] | 14 | func TestRunGit_success(t *testing.T) { |
| 15 | CommandRunnerImpl = cmd.FakeCommandRunner{ |
| 16 | ExpectedDir: "myrepo", |
| 17 | ExpectedCmd: []string{"git", "log"}, |
| 18 | Stdout: "success", |
| 19 | } |
| 20 | |
| 21 | output, err := RunGit("myrepo", []string{"log"}) |
| 22 | assert.NilError(t, err) |
| 23 | assert.Equal(t, output.Stdout, "success") |
Jack Neus | fc3b577 | 2019-07-03 11:18:42 -0600 | [diff] [blame] | 24 | } |
| 25 | |
Jack Neus | 0751172 | 2019-07-12 15:41:10 -0600 | [diff] [blame] | 26 | func TestRunGit_error(t *testing.T) { |
| 27 | CommandRunnerImpl = cmd.FakeCommandRunner{ |
| 28 | ExpectedDir: "myrepo", |
| 29 | ExpectedCmd: []string{"git", "log"}, |
| 30 | Stdout: "I don't feel so go--", |
| 31 | Stderr: "sudden death", |
| 32 | FailCommand: true, |
Jack Neus | fc3b577 | 2019-07-03 11:18:42 -0600 | [diff] [blame] | 33 | } |
Jack Neus | fc3b577 | 2019-07-03 11:18:42 -0600 | [diff] [blame] | 34 | |
Jack Neus | 0751172 | 2019-07-12 15:41:10 -0600 | [diff] [blame] | 35 | output, err := RunGit("myrepo", []string{"log"}) |
| 36 | assert.Assert(t, err != nil) |
| 37 | assert.Equal(t, output.Stderr, "sudden death") |
Jack Neus | fc3b577 | 2019-07-03 11:18:42 -0600 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | func TestStripRefsHead(t *testing.T) { |
| 41 | assert.Equal(t, StripRefsHead("refs/heads/foo"), "foo") |
| 42 | assert.Equal(t, StripRefsHead("foo"), "foo") |
| 43 | } |
| 44 | |
| 45 | func TestStripRefs(t *testing.T) { |
| 46 | assert.Equal(t, StripRefs("refs/remotes/origin/foo"), "foo") |
| 47 | assert.Equal(t, StripRefs("refs/heads/foo"), "foo") |
| 48 | assert.Equal(t, StripRefs("foo"), "foo") |
| 49 | } |
| 50 | |
| 51 | func TestNormalizeRef(t *testing.T) { |
| 52 | assert.Equal(t, NormalizeRef("refs/heads/foo"), "refs/heads/foo") |
| 53 | assert.Equal(t, NormalizeRef("foo"), "refs/heads/foo") |
| 54 | } |
| 55 | |
| 56 | func TestGetCurrentBranch_success(t *testing.T) { |
| 57 | fakeGitRepo := "top-secret-project" |
| 58 | fakeGitData := "refs/heads/current-branch" |
| 59 | |
Jack Neus | 0751172 | 2019-07-12 15:41:10 -0600 | [diff] [blame] | 60 | CommandRunnerImpl = cmd.FakeCommandRunner{ |
| 61 | ExpectedDir: fakeGitRepo, |
| 62 | Stdout: fakeGitData, |
Jack Neus | fc3b577 | 2019-07-03 11:18:42 -0600 | [diff] [blame] | 63 | } |
| 64 | assert.Equal(t, GetCurrentBranch(fakeGitRepo), "current-branch") |
| 65 | } |
| 66 | |
| 67 | func TestGetCurrentBranch_failure(t *testing.T) { |
Jack Neus | 0751172 | 2019-07-12 15:41:10 -0600 | [diff] [blame] | 68 | CommandRunnerImpl = cmd.FakeCommandRunner{ |
| 69 | FailCommand: true, |
Jack Neus | fc3b577 | 2019-07-03 11:18:42 -0600 | [diff] [blame] | 70 | } |
| 71 | assert.Equal(t, GetCurrentBranch("project"), "") |
| 72 | } |
| 73 | |
| 74 | func TestMatchBranchName_success(t *testing.T) { |
| 75 | fakeGitRepo := "top-secret-project" |
| 76 | fakeGitData := "e9cb56bd9af9365b43f82cecf28cc76d49df1f72 refs/heads/foo\n" + |
| 77 | "f9c1bb630f4475058d4a9db4aea52fc89d8f7b0d refs/heads/bar\n" + |
| 78 | "2102915989de21d9251c11f0a7b5307e175e7677 refs/heads/foobar\n" + |
| 79 | "04975f9439ff75502b33d9491155692736e05b07 refs/heads/baz\n" |
| 80 | |
Jack Neus | 0751172 | 2019-07-12 15:41:10 -0600 | [diff] [blame] | 81 | CommandRunnerImpl = cmd.FakeCommandRunner{ |
| 82 | ExpectedCmd: []string{"git", "ls-remote", fakeGitRepo}, |
| 83 | ExpectedDir: fakeGitRepo, |
| 84 | Stdout: fakeGitData, |
Jack Neus | fc3b577 | 2019-07-03 11:18:42 -0600 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | expectedMatches := []string{"refs/heads/foo", "refs/heads/foobar"} |
| 88 | branches, err := MatchBranchName(fakeGitRepo, regexp.MustCompile("Foo")) |
| 89 | assert.NilError(t, err) |
| 90 | assert.DeepEqual(t, expectedMatches, branches) |
| 91 | |
| 92 | expectedMatches = []string{"refs/heads/foo"} |
| 93 | branches, err = MatchBranchName(fakeGitRepo, regexp.MustCompile("Foo$")) |
| 94 | assert.NilError(t, err) |
| 95 | assert.DeepEqual(t, expectedMatches, branches) |
| 96 | } |
| 97 | |
| 98 | func TestGetRepoRevision(t *testing.T) { |
| 99 | sha := "6446dfef4b55689046395c2db7ba7c35377927fe" |
Jack Neus | 0751172 | 2019-07-12 15:41:10 -0600 | [diff] [blame] | 100 | CommandRunnerImpl = cmd.FakeCommandRunner{ |
| 101 | ExpectedCmd: []string{"git", "rev-parse", "HEAD"}, |
| 102 | ExpectedDir: "project", |
| 103 | Stdout: sha, |
Jack Neus | fc3b577 | 2019-07-03 11:18:42 -0600 | [diff] [blame] | 104 | } |
| 105 | res, err := GetGitRepoRevision("project") |
| 106 | assert.NilError(t, err) |
| 107 | assert.Equal(t, res, sha) |
| 108 | } |
Jack Neus | abdbe19 | 2019-07-15 12:23:22 -0600 | [diff] [blame] | 109 | |
| 110 | func TestCreateBranch(t *testing.T) { |
| 111 | fakeGitRepo := "top-secret-project" |
| 112 | branchName := "project z" |
| 113 | |
| 114 | CommandRunnerImpl = cmd.FakeCommandRunner{ |
| 115 | ExpectedDir: fakeGitRepo, |
| 116 | ExpectedCmd: []string{"git", "checkout", "-B", branchName, "HEAD"}, |
| 117 | } |
| 118 | assert.NilError(t, CreateBranch(fakeGitRepo, branchName)) |
| 119 | } |
| 120 | |
| 121 | func TestCommitAll(t *testing.T) { |
| 122 | fakeGitRepo := "repo" |
| 123 | commitMsg := "commit" |
| 124 | |
| 125 | CommandRunnerImpl = &cmd.FakeCommandRunnerMulti{ |
| 126 | CommandRunners: []cmd.FakeCommandRunner{ |
| 127 | { |
| 128 | ExpectedDir: fakeGitRepo, |
| 129 | ExpectedCmd: []string{"git", "add", "-A"}, |
| 130 | }, |
| 131 | { |
| 132 | ExpectedDir: fakeGitRepo, |
| 133 | ExpectedCmd: []string{"git", "commit", "-m", commitMsg}, |
| 134 | }, |
| 135 | }, |
| 136 | } |
| 137 | |
| 138 | err := CommitAll(fakeGitRepo, commitMsg) |
| 139 | assert.NilError(t, err) |
| 140 | } |
| 141 | |
| 142 | func TestPushChanges(t *testing.T) { |
| 143 | fakeGitRepo := "da-bank" |
| 144 | commitMsg := "da-money" |
| 145 | localRef := "da-vault" |
| 146 | |
| 147 | remoteRef := RemoteRef{ |
| 148 | Remote: "da-family", |
| 149 | Ref: "da-van", |
| 150 | } |
| 151 | |
| 152 | pushStr := fmt.Sprintf("%s:%s", localRef, remoteRef.Ref) |
| 153 | CommandRunnerImpl = &cmd.FakeCommandRunnerMulti{ |
| 154 | CommandRunners: []cmd.FakeCommandRunner{ |
| 155 | { |
| 156 | ExpectedDir: fakeGitRepo, |
| 157 | ExpectedCmd: []string{"git", "add", "-A"}, |
| 158 | }, |
| 159 | { |
| 160 | ExpectedDir: fakeGitRepo, |
| 161 | ExpectedCmd: []string{"git", "commit", "-m", commitMsg}, |
| 162 | }, |
| 163 | { |
| 164 | ExpectedDir: fakeGitRepo, |
| 165 | ExpectedCmd: []string{"git", "push", remoteRef.Remote, pushStr, "--dry-run"}, |
| 166 | }, |
| 167 | }, |
| 168 | } |
| 169 | |
| 170 | err := PushChanges(fakeGitRepo, localRef, commitMsg, true, remoteRef) |
| 171 | assert.NilError(t, err) |
| 172 | } |