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 ( |
| 7 | "bytes" |
| 8 | "context" |
| 9 | "fmt" |
| 10 | "gotest.tools/assert" |
| 11 | "os/exec" |
| 12 | "regexp" |
| 13 | "strings" |
| 14 | "testing" |
| 15 | ) |
| 16 | |
| 17 | type fakeCommandRunner struct { |
| 18 | stdout string |
| 19 | stderr string |
| 20 | expectedCmd []string |
| 21 | expectedDir string |
| 22 | failCommand bool |
| 23 | } |
| 24 | |
| 25 | func equal(a, b []string) bool { |
| 26 | if len(a) != len(b) { |
| 27 | return false |
| 28 | } |
| 29 | for i := range a { |
| 30 | if a[i] != b[i] { |
| 31 | return false |
| 32 | } |
| 33 | } |
| 34 | return true |
| 35 | } |
| 36 | |
| 37 | func (c fakeCommandRunner) runCommand(ctx context.Context, stdoutBuf, stderrBuf *bytes.Buffer, dir, name string, args ...string) error { |
| 38 | stdoutBuf.WriteString(c.stdout) |
| 39 | stderrBuf.WriteString(c.stderr) |
| 40 | cmd := append([]string{name}, args...) |
| 41 | if len(c.expectedCmd) > 0 { |
| 42 | if !equal(cmd, c.expectedCmd) { |
| 43 | expectedCmd := strings.Join(c.expectedCmd, " ") |
| 44 | actualCmd := strings.Join(cmd, " ") |
| 45 | return fmt.Errorf("wrong cmd; expected %s got %s", expectedCmd, actualCmd) |
| 46 | } |
| 47 | } |
| 48 | if c.expectedDir != "" { |
| 49 | if dir != c.expectedDir { |
| 50 | return fmt.Errorf("wrong cmd dir; expected %s got %s", c.expectedDir, dir) |
| 51 | } |
| 52 | } |
| 53 | if c.failCommand { |
| 54 | return &exec.ExitError{} |
| 55 | } |
| 56 | return nil |
| 57 | } |
| 58 | |
| 59 | func TestStripRefsHead(t *testing.T) { |
| 60 | assert.Equal(t, StripRefsHead("refs/heads/foo"), "foo") |
| 61 | assert.Equal(t, StripRefsHead("foo"), "foo") |
| 62 | } |
| 63 | |
| 64 | func TestStripRefs(t *testing.T) { |
| 65 | assert.Equal(t, StripRefs("refs/remotes/origin/foo"), "foo") |
| 66 | assert.Equal(t, StripRefs("refs/heads/foo"), "foo") |
| 67 | assert.Equal(t, StripRefs("foo"), "foo") |
| 68 | } |
| 69 | |
| 70 | func TestNormalizeRef(t *testing.T) { |
| 71 | assert.Equal(t, NormalizeRef("refs/heads/foo"), "refs/heads/foo") |
| 72 | assert.Equal(t, NormalizeRef("foo"), "refs/heads/foo") |
| 73 | } |
| 74 | |
| 75 | func TestGetCurrentBranch_success(t *testing.T) { |
| 76 | fakeGitRepo := "top-secret-project" |
| 77 | fakeGitData := "refs/heads/current-branch" |
| 78 | |
| 79 | commandRunnerImpl = fakeCommandRunner{ |
| 80 | expectedDir: fakeGitRepo, |
| 81 | stdout: fakeGitData, |
| 82 | } |
| 83 | assert.Equal(t, GetCurrentBranch(fakeGitRepo), "current-branch") |
| 84 | } |
| 85 | |
| 86 | func TestGetCurrentBranch_failure(t *testing.T) { |
| 87 | commandRunnerImpl = fakeCommandRunner{ |
| 88 | failCommand: true, |
| 89 | } |
| 90 | assert.Equal(t, GetCurrentBranch("project"), "") |
| 91 | } |
| 92 | |
| 93 | func TestMatchBranchName_success(t *testing.T) { |
| 94 | fakeGitRepo := "top-secret-project" |
| 95 | fakeGitData := "e9cb56bd9af9365b43f82cecf28cc76d49df1f72 refs/heads/foo\n" + |
| 96 | "f9c1bb630f4475058d4a9db4aea52fc89d8f7b0d refs/heads/bar\n" + |
| 97 | "2102915989de21d9251c11f0a7b5307e175e7677 refs/heads/foobar\n" + |
| 98 | "04975f9439ff75502b33d9491155692736e05b07 refs/heads/baz\n" |
| 99 | |
| 100 | commandRunnerImpl = fakeCommandRunner{ |
| 101 | expectedCmd: []string{"git", "ls-remote", fakeGitRepo}, |
| 102 | expectedDir: fakeGitRepo, |
| 103 | stdout: fakeGitData, |
| 104 | } |
| 105 | |
| 106 | expectedMatches := []string{"refs/heads/foo", "refs/heads/foobar"} |
| 107 | branches, err := MatchBranchName(fakeGitRepo, regexp.MustCompile("Foo")) |
| 108 | assert.NilError(t, err) |
| 109 | assert.DeepEqual(t, expectedMatches, branches) |
| 110 | |
| 111 | expectedMatches = []string{"refs/heads/foo"} |
| 112 | branches, err = MatchBranchName(fakeGitRepo, regexp.MustCompile("Foo$")) |
| 113 | assert.NilError(t, err) |
| 114 | assert.DeepEqual(t, expectedMatches, branches) |
| 115 | } |
| 116 | |
| 117 | func TestGetRepoRevision(t *testing.T) { |
| 118 | sha := "6446dfef4b55689046395c2db7ba7c35377927fe" |
| 119 | commandRunnerImpl = fakeCommandRunner{ |
| 120 | expectedCmd: []string{"git", "rev-parse", "HEAD"}, |
| 121 | expectedDir: "project", |
| 122 | stdout: sha, |
| 123 | } |
| 124 | res, err := GetGitRepoRevision("project") |
| 125 | assert.NilError(t, err) |
| 126 | assert.Equal(t, res, sha) |
| 127 | } |