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 | 0751172 | 2019-07-12 15:41:10 -0600 | [diff] [blame] | 7 | "go.chromium.org/chromiumos/infra/go/internal/cmd" |
Jack Neus | fc3b577 | 2019-07-03 11:18:42 -0600 | [diff] [blame] | 8 | "gotest.tools/assert" |
Jack Neus | fc3b577 | 2019-07-03 11:18:42 -0600 | [diff] [blame] | 9 | "regexp" |
Jack Neus | fc3b577 | 2019-07-03 11:18:42 -0600 | [diff] [blame] | 10 | "testing" |
| 11 | ) |
| 12 | |
Jack Neus | 0751172 | 2019-07-12 15:41:10 -0600 | [diff] [blame] | 13 | func TestRunGit_success(t *testing.T) { |
| 14 | CommandRunnerImpl = cmd.FakeCommandRunner{ |
| 15 | ExpectedDir: "myrepo", |
| 16 | ExpectedCmd: []string{"git", "log"}, |
| 17 | Stdout: "success", |
| 18 | } |
| 19 | |
| 20 | output, err := RunGit("myrepo", []string{"log"}) |
| 21 | assert.NilError(t, err) |
| 22 | assert.Equal(t, output.Stdout, "success") |
Jack Neus | fc3b577 | 2019-07-03 11:18:42 -0600 | [diff] [blame] | 23 | } |
| 24 | |
Jack Neus | 0751172 | 2019-07-12 15:41:10 -0600 | [diff] [blame] | 25 | func TestRunGit_error(t *testing.T) { |
| 26 | CommandRunnerImpl = cmd.FakeCommandRunner{ |
| 27 | ExpectedDir: "myrepo", |
| 28 | ExpectedCmd: []string{"git", "log"}, |
| 29 | Stdout: "I don't feel so go--", |
| 30 | Stderr: "sudden death", |
| 31 | FailCommand: true, |
Jack Neus | fc3b577 | 2019-07-03 11:18:42 -0600 | [diff] [blame] | 32 | } |
Jack Neus | fc3b577 | 2019-07-03 11:18:42 -0600 | [diff] [blame] | 33 | |
Jack Neus | 0751172 | 2019-07-12 15:41:10 -0600 | [diff] [blame] | 34 | output, err := RunGit("myrepo", []string{"log"}) |
| 35 | assert.Assert(t, err != nil) |
| 36 | assert.Equal(t, output.Stderr, "sudden death") |
Jack Neus | fc3b577 | 2019-07-03 11:18:42 -0600 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | func TestStripRefsHead(t *testing.T) { |
| 40 | assert.Equal(t, StripRefsHead("refs/heads/foo"), "foo") |
| 41 | assert.Equal(t, StripRefsHead("foo"), "foo") |
| 42 | } |
| 43 | |
| 44 | func TestStripRefs(t *testing.T) { |
| 45 | assert.Equal(t, StripRefs("refs/remotes/origin/foo"), "foo") |
| 46 | assert.Equal(t, StripRefs("refs/heads/foo"), "foo") |
| 47 | assert.Equal(t, StripRefs("foo"), "foo") |
| 48 | } |
| 49 | |
| 50 | func TestNormalizeRef(t *testing.T) { |
| 51 | assert.Equal(t, NormalizeRef("refs/heads/foo"), "refs/heads/foo") |
| 52 | assert.Equal(t, NormalizeRef("foo"), "refs/heads/foo") |
| 53 | } |
| 54 | |
| 55 | func TestGetCurrentBranch_success(t *testing.T) { |
| 56 | fakeGitRepo := "top-secret-project" |
| 57 | fakeGitData := "refs/heads/current-branch" |
| 58 | |
Jack Neus | 0751172 | 2019-07-12 15:41:10 -0600 | [diff] [blame] | 59 | CommandRunnerImpl = cmd.FakeCommandRunner{ |
| 60 | ExpectedDir: fakeGitRepo, |
| 61 | Stdout: fakeGitData, |
Jack Neus | fc3b577 | 2019-07-03 11:18:42 -0600 | [diff] [blame] | 62 | } |
| 63 | assert.Equal(t, GetCurrentBranch(fakeGitRepo), "current-branch") |
| 64 | } |
| 65 | |
| 66 | func TestGetCurrentBranch_failure(t *testing.T) { |
Jack Neus | 0751172 | 2019-07-12 15:41:10 -0600 | [diff] [blame] | 67 | CommandRunnerImpl = cmd.FakeCommandRunner{ |
| 68 | FailCommand: true, |
Jack Neus | fc3b577 | 2019-07-03 11:18:42 -0600 | [diff] [blame] | 69 | } |
| 70 | assert.Equal(t, GetCurrentBranch("project"), "") |
| 71 | } |
| 72 | |
| 73 | func TestMatchBranchName_success(t *testing.T) { |
| 74 | fakeGitRepo := "top-secret-project" |
| 75 | fakeGitData := "e9cb56bd9af9365b43f82cecf28cc76d49df1f72 refs/heads/foo\n" + |
| 76 | "f9c1bb630f4475058d4a9db4aea52fc89d8f7b0d refs/heads/bar\n" + |
| 77 | "2102915989de21d9251c11f0a7b5307e175e7677 refs/heads/foobar\n" + |
| 78 | "04975f9439ff75502b33d9491155692736e05b07 refs/heads/baz\n" |
| 79 | |
Jack Neus | 0751172 | 2019-07-12 15:41:10 -0600 | [diff] [blame] | 80 | CommandRunnerImpl = cmd.FakeCommandRunner{ |
| 81 | ExpectedCmd: []string{"git", "ls-remote", fakeGitRepo}, |
| 82 | ExpectedDir: fakeGitRepo, |
| 83 | Stdout: fakeGitData, |
Jack Neus | fc3b577 | 2019-07-03 11:18:42 -0600 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | expectedMatches := []string{"refs/heads/foo", "refs/heads/foobar"} |
| 87 | branches, err := MatchBranchName(fakeGitRepo, regexp.MustCompile("Foo")) |
| 88 | assert.NilError(t, err) |
| 89 | assert.DeepEqual(t, expectedMatches, branches) |
| 90 | |
| 91 | expectedMatches = []string{"refs/heads/foo"} |
| 92 | branches, err = MatchBranchName(fakeGitRepo, regexp.MustCompile("Foo$")) |
| 93 | assert.NilError(t, err) |
| 94 | assert.DeepEqual(t, expectedMatches, branches) |
| 95 | } |
| 96 | |
| 97 | func TestGetRepoRevision(t *testing.T) { |
| 98 | sha := "6446dfef4b55689046395c2db7ba7c35377927fe" |
Jack Neus | 0751172 | 2019-07-12 15:41:10 -0600 | [diff] [blame] | 99 | CommandRunnerImpl = cmd.FakeCommandRunner{ |
| 100 | ExpectedCmd: []string{"git", "rev-parse", "HEAD"}, |
| 101 | ExpectedDir: "project", |
| 102 | Stdout: sha, |
Jack Neus | fc3b577 | 2019-07-03 11:18:42 -0600 | [diff] [blame] | 103 | } |
| 104 | res, err := GetGitRepoRevision("project") |
| 105 | assert.NilError(t, err) |
| 106 | assert.Equal(t, res, sha) |
| 107 | } |