blob: b63242670bd4953f9ed180cab17bbff5b9d03862 [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 Neus07511722019-07-12 15:41:10 -06008 "go.chromium.org/chromiumos/infra/go/internal/cmd"
Jack Neusfc3b5772019-07-03 11:18:42 -06009 "gotest.tools/assert"
Jack Neusfc3b5772019-07-03 11:18:42 -060010 "regexp"
Jack Neusfc3b5772019-07-03 11:18:42 -060011 "testing"
12)
13
Jack Neus07511722019-07-12 15:41:10 -060014func 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 Neusfc3b5772019-07-03 11:18:42 -060024}
25
Jack Neus07511722019-07-12 15:41:10 -060026func 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 Neusfc3b5772019-07-03 11:18:42 -060033 }
Jack Neusfc3b5772019-07-03 11:18:42 -060034
Jack Neus07511722019-07-12 15:41:10 -060035 output, err := RunGit("myrepo", []string{"log"})
36 assert.Assert(t, err != nil)
37 assert.Equal(t, output.Stderr, "sudden death")
Jack Neusfc3b5772019-07-03 11:18:42 -060038}
39
40func TestStripRefsHead(t *testing.T) {
41 assert.Equal(t, StripRefsHead("refs/heads/foo"), "foo")
42 assert.Equal(t, StripRefsHead("foo"), "foo")
43}
44
45func 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
51func 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
56func TestGetCurrentBranch_success(t *testing.T) {
57 fakeGitRepo := "top-secret-project"
58 fakeGitData := "refs/heads/current-branch"
59
Jack Neus07511722019-07-12 15:41:10 -060060 CommandRunnerImpl = cmd.FakeCommandRunner{
61 ExpectedDir: fakeGitRepo,
62 Stdout: fakeGitData,
Jack Neusfc3b5772019-07-03 11:18:42 -060063 }
64 assert.Equal(t, GetCurrentBranch(fakeGitRepo), "current-branch")
65}
66
67func TestGetCurrentBranch_failure(t *testing.T) {
Jack Neus07511722019-07-12 15:41:10 -060068 CommandRunnerImpl = cmd.FakeCommandRunner{
69 FailCommand: true,
Jack Neusfc3b5772019-07-03 11:18:42 -060070 }
71 assert.Equal(t, GetCurrentBranch("project"), "")
72}
73
74func 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 Neus07511722019-07-12 15:41:10 -060081 CommandRunnerImpl = cmd.FakeCommandRunner{
82 ExpectedCmd: []string{"git", "ls-remote", fakeGitRepo},
83 ExpectedDir: fakeGitRepo,
84 Stdout: fakeGitData,
Jack Neusfc3b5772019-07-03 11:18:42 -060085 }
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
98func TestGetRepoRevision(t *testing.T) {
99 sha := "6446dfef4b55689046395c2db7ba7c35377927fe"
Jack Neus07511722019-07-12 15:41:10 -0600100 CommandRunnerImpl = cmd.FakeCommandRunner{
101 ExpectedCmd: []string{"git", "rev-parse", "HEAD"},
102 ExpectedDir: "project",
103 Stdout: sha,
Jack Neusfc3b5772019-07-03 11:18:42 -0600104 }
105 res, err := GetGitRepoRevision("project")
106 assert.NilError(t, err)
107 assert.Equal(t, res, sha)
108}
Jack Neusabdbe192019-07-15 12:23:22 -0600109
110func 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
121func 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
142func 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}