blob: 0587600ef4246921ba1ba168450e2fbfee0b0649 [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 Neus0296c732019-07-17 09:35:01 -060010 "io/ioutil"
11 "os"
12 "path/filepath"
Jack Neusfc3b5772019-07-03 11:18:42 -060013 "regexp"
Jack Neusfc3b5772019-07-03 11:18:42 -060014 "testing"
15)
16
Jack Neus07511722019-07-12 15:41:10 -060017func TestRunGit_success(t *testing.T) {
18 CommandRunnerImpl = cmd.FakeCommandRunner{
19 ExpectedDir: "myrepo",
20 ExpectedCmd: []string{"git", "log"},
21 Stdout: "success",
22 }
23
24 output, err := RunGit("myrepo", []string{"log"})
25 assert.NilError(t, err)
26 assert.Equal(t, output.Stdout, "success")
Jack Neusfc3b5772019-07-03 11:18:42 -060027}
28
Jack Neus07511722019-07-12 15:41:10 -060029func TestRunGit_error(t *testing.T) {
30 CommandRunnerImpl = cmd.FakeCommandRunner{
31 ExpectedDir: "myrepo",
32 ExpectedCmd: []string{"git", "log"},
33 Stdout: "I don't feel so go--",
34 Stderr: "sudden death",
35 FailCommand: true,
Jack Neusfc3b5772019-07-03 11:18:42 -060036 }
Jack Neusfc3b5772019-07-03 11:18:42 -060037
Jack Neus07511722019-07-12 15:41:10 -060038 output, err := RunGit("myrepo", []string{"log"})
39 assert.Assert(t, err != nil)
40 assert.Equal(t, output.Stderr, "sudden death")
Jack Neusfc3b5772019-07-03 11:18:42 -060041}
42
43func TestStripRefsHead(t *testing.T) {
44 assert.Equal(t, StripRefsHead("refs/heads/foo"), "foo")
45 assert.Equal(t, StripRefsHead("foo"), "foo")
46}
47
48func TestStripRefs(t *testing.T) {
49 assert.Equal(t, StripRefs("refs/remotes/origin/foo"), "foo")
50 assert.Equal(t, StripRefs("refs/heads/foo"), "foo")
51 assert.Equal(t, StripRefs("foo"), "foo")
52}
53
54func TestNormalizeRef(t *testing.T) {
55 assert.Equal(t, NormalizeRef("refs/heads/foo"), "refs/heads/foo")
56 assert.Equal(t, NormalizeRef("foo"), "refs/heads/foo")
57}
58
59func TestGetCurrentBranch_success(t *testing.T) {
60 fakeGitRepo := "top-secret-project"
61 fakeGitData := "refs/heads/current-branch"
62
Jack Neus07511722019-07-12 15:41:10 -060063 CommandRunnerImpl = cmd.FakeCommandRunner{
64 ExpectedDir: fakeGitRepo,
65 Stdout: fakeGitData,
Jack Neusfc3b5772019-07-03 11:18:42 -060066 }
67 assert.Equal(t, GetCurrentBranch(fakeGitRepo), "current-branch")
68}
69
70func TestGetCurrentBranch_failure(t *testing.T) {
Jack Neus07511722019-07-12 15:41:10 -060071 CommandRunnerImpl = cmd.FakeCommandRunner{
72 FailCommand: true,
Jack Neusfc3b5772019-07-03 11:18:42 -060073 }
74 assert.Equal(t, GetCurrentBranch("project"), "")
75}
76
77func TestMatchBranchName_success(t *testing.T) {
78 fakeGitRepo := "top-secret-project"
79 fakeGitData := "e9cb56bd9af9365b43f82cecf28cc76d49df1f72 refs/heads/foo\n" +
80 "f9c1bb630f4475058d4a9db4aea52fc89d8f7b0d refs/heads/bar\n" +
81 "2102915989de21d9251c11f0a7b5307e175e7677 refs/heads/foobar\n" +
82 "04975f9439ff75502b33d9491155692736e05b07 refs/heads/baz\n"
83
Jack Neus07511722019-07-12 15:41:10 -060084 CommandRunnerImpl = cmd.FakeCommandRunner{
Jack Neus901a6bf2019-07-22 08:30:07 -060085 ExpectedCmd: []string{"git", "show-ref"},
Jack Neus07511722019-07-12 15:41:10 -060086 ExpectedDir: fakeGitRepo,
87 Stdout: fakeGitData,
Jack Neusfc3b5772019-07-03 11:18:42 -060088 }
89
90 expectedMatches := []string{"refs/heads/foo", "refs/heads/foobar"}
91 branches, err := MatchBranchName(fakeGitRepo, regexp.MustCompile("Foo"))
92 assert.NilError(t, err)
93 assert.DeepEqual(t, expectedMatches, branches)
94
95 expectedMatches = []string{"refs/heads/foo"}
96 branches, err = MatchBranchName(fakeGitRepo, regexp.MustCompile("Foo$"))
97 assert.NilError(t, err)
98 assert.DeepEqual(t, expectedMatches, branches)
99}
100
Jack Neus10e6a122019-07-18 10:17:44 -0600101func TestMatchBranchNameWithNamespace_success(t *testing.T) {
102 fakeGitRepo := "top-secret-project"
103 fakeGitData := "e9cb56bd9af9365b43f82cecf28cc76d49df1f72 refs/changes/foo\n" +
104 "f9c1bb630f4475058d4a9db4aea52fc89d8f7b0d refs/changes/bar\n" +
105 "2102915989de21d9251c11f0a7b5307e175e7677 refs/heads/foobar\n" +
106 "04975f9439ff75502b33d9491155692736e05b07 refs/heads/baz\n"
107
108 CommandRunnerImpl = cmd.FakeCommandRunner{
Jack Neus901a6bf2019-07-22 08:30:07 -0600109 ExpectedCmd: []string{"git", "show-ref"},
Jack Neus10e6a122019-07-18 10:17:44 -0600110 ExpectedDir: fakeGitRepo,
111 Stdout: fakeGitData,
112 }
113
114 expectedMatches := []string{"foobar"}
115 pattern := regexp.MustCompile("FOO")
116 namespace := regexp.MustCompile("refs/heads/")
117 branches, err := MatchBranchNameWithNamespace(fakeGitRepo, pattern, namespace)
118 assert.NilError(t, err)
119 assert.DeepEqual(t, expectedMatches, branches)
120}
121
Jack Neusfc3b5772019-07-03 11:18:42 -0600122func TestGetRepoRevision(t *testing.T) {
123 sha := "6446dfef4b55689046395c2db7ba7c35377927fe"
Jack Neus07511722019-07-12 15:41:10 -0600124 CommandRunnerImpl = cmd.FakeCommandRunner{
125 ExpectedCmd: []string{"git", "rev-parse", "HEAD"},
126 ExpectedDir: "project",
127 Stdout: sha,
Jack Neusfc3b5772019-07-03 11:18:42 -0600128 }
129 res, err := GetGitRepoRevision("project")
130 assert.NilError(t, err)
131 assert.Equal(t, res, sha)
132}
Jack Neusabdbe192019-07-15 12:23:22 -0600133
134func TestCreateBranch(t *testing.T) {
135 fakeGitRepo := "top-secret-project"
136 branchName := "project z"
137
138 CommandRunnerImpl = cmd.FakeCommandRunner{
139 ExpectedDir: fakeGitRepo,
Jack Neus0296c732019-07-17 09:35:01 -0600140 ExpectedCmd: []string{"git", "checkout", "-B", branchName},
Jack Neusabdbe192019-07-15 12:23:22 -0600141 }
142 assert.NilError(t, CreateBranch(fakeGitRepo, branchName))
143}
144
Jack Neus0296c732019-07-17 09:35:01 -0600145func TestCreateTrackingBranch(t *testing.T) {
146 fakeGitRepo := "repo"
147 branchName := "branch"
148 remoteRef := RemoteRef{
149 Remote: "remote",
150 Ref: "master",
151 }
152 refspec := fmt.Sprintf("%s/%s", remoteRef.Remote, remoteRef.Ref)
153
154 CommandRunnerImpl = &cmd.FakeCommandRunnerMulti{
155 CommandRunners: []cmd.FakeCommandRunner{
156 {
157 ExpectedDir: fakeGitRepo,
158 ExpectedCmd: []string{"git", "fetch", remoteRef.Remote, remoteRef.Ref},
159 },
160 {
161 ExpectedDir: fakeGitRepo,
162 ExpectedCmd: []string{"git", "checkout", "-b", branchName, "-t", refspec},
163 },
164 },
165 }
166
167 err := CreateTrackingBranch(fakeGitRepo, branchName, remoteRef)
168 assert.NilError(t, err)
169}
170
Jack Neusabdbe192019-07-15 12:23:22 -0600171func TestCommitAll(t *testing.T) {
172 fakeGitRepo := "repo"
173 commitMsg := "commit"
174
175 CommandRunnerImpl = &cmd.FakeCommandRunnerMulti{
176 CommandRunners: []cmd.FakeCommandRunner{
177 {
178 ExpectedDir: fakeGitRepo,
179 ExpectedCmd: []string{"git", "add", "-A"},
180 },
181 {
182 ExpectedDir: fakeGitRepo,
183 ExpectedCmd: []string{"git", "commit", "-m", commitMsg},
184 },
185 },
186 }
187
188 err := CommitAll(fakeGitRepo, commitMsg)
189 assert.NilError(t, err)
190}
191
Jack Neus7440c762019-07-22 10:45:18 -0600192func TestCommitEmpty(t *testing.T) {
193 fakeGitRepo := "repo"
194 commitMsg := "commit"
195
196 CommandRunnerImpl = cmd.FakeCommandRunner{
197 ExpectedDir: fakeGitRepo,
198 ExpectedCmd: []string{"git", "commit", "-m", commitMsg, "--allow-empty"},
199 }
200
201 err := CommitEmpty(fakeGitRepo, commitMsg)
202 assert.NilError(t, err)
203}
204
Jack Neusabdbe192019-07-15 12:23:22 -0600205func TestPushChanges(t *testing.T) {
206 fakeGitRepo := "da-bank"
207 commitMsg := "da-money"
208 localRef := "da-vault"
209
210 remoteRef := RemoteRef{
211 Remote: "da-family",
212 Ref: "da-van",
213 }
214
215 pushStr := fmt.Sprintf("%s:%s", localRef, remoteRef.Ref)
216 CommandRunnerImpl = &cmd.FakeCommandRunnerMulti{
217 CommandRunners: []cmd.FakeCommandRunner{
218 {
219 ExpectedDir: fakeGitRepo,
220 ExpectedCmd: []string{"git", "add", "-A"},
221 },
222 {
223 ExpectedDir: fakeGitRepo,
224 ExpectedCmd: []string{"git", "commit", "-m", commitMsg},
225 },
226 {
227 ExpectedDir: fakeGitRepo,
228 ExpectedCmd: []string{"git", "push", remoteRef.Remote, pushStr, "--dry-run"},
229 },
230 },
231 }
232
233 err := PushChanges(fakeGitRepo, localRef, commitMsg, true, remoteRef)
234 assert.NilError(t, err)
235}
Jack Neus0296c732019-07-17 09:35:01 -0600236
Jack Neus7440c762019-07-22 10:45:18 -0600237func TestPush(t *testing.T) {
238 fakeGitRepo := "repo"
239 localRef := "commitId"
240
241 remoteRef := RemoteRef{
242 Remote: "remote",
243 Ref: "ref",
244 }
245
246 pushStr := fmt.Sprintf("%s:%s", localRef, remoteRef.Ref)
247 CommandRunnerImpl = cmd.FakeCommandRunner{
248 ExpectedDir: fakeGitRepo,
249 ExpectedCmd: []string{"git", "push", remoteRef.Remote, pushStr, "--dry-run"},
250 }
251
252 err := Push(fakeGitRepo, localRef, true, remoteRef)
253 assert.NilError(t, err)
254}
255
Jack Neus0296c732019-07-17 09:35:01 -0600256func TestInit(t *testing.T) {
257 fakeGitRepo := "top-secret-project"
258
259 CommandRunnerImpl = cmd.FakeCommandRunner{
260 ExpectedDir: fakeGitRepo,
261 ExpectedCmd: []string{"git", "init"},
262 }
263 assert.NilError(t, Init(fakeGitRepo, false))
264}
265
266func TestInit_bare(t *testing.T) {
267 fakeGitRepo := "top-secret-project"
268
269 CommandRunnerImpl = cmd.FakeCommandRunner{
270 ExpectedDir: fakeGitRepo,
271 ExpectedCmd: []string{"git", "init", "--bare"},
272 }
273 assert.NilError(t, Init(fakeGitRepo, true))
274}
275
276func TestAddRemote(t *testing.T) {
277 fakeGitRepo := "repo"
278 remoteName := "remote"
279 remoteLoc := "remote/"
280
281 CommandRunnerImpl = cmd.FakeCommandRunner{
282 ExpectedDir: fakeGitRepo,
283 ExpectedCmd: []string{"git", "remote", "add", remoteName, remoteLoc},
284 }
285 assert.NilError(t, AddRemote(fakeGitRepo, remoteName, remoteLoc))
286}
287
288func TestCheckout(t *testing.T) {
289 CommandRunnerImpl = cmd.RealCommandRunner{}
290
291 tmpDir := "gittest_tmp_dir"
292 tmpDir, err := ioutil.TempDir("", tmpDir)
293 defer os.RemoveAll(tmpDir)
294
295 assert.NilError(t, err)
296 // Create repo.
297 assert.NilError(t, Init(tmpDir, false))
298 // Create first branch.
299 assert.NilError(t, CreateBranch(tmpDir, "branch1"))
300 // In order for the ref to be created, need to commit something.
301 assert.NilError(t, ioutil.WriteFile(filepath.Join(tmpDir, "foo"), []byte("foo"), 0644))
302 assert.NilError(t, CommitAll(tmpDir, "init commit"))
303 // Create second branch (will switch to this branch).
304 assert.NilError(t, CreateBranch(tmpDir, "branch2"))
305 // Try checking out a nonexistent branch.
306 assert.ErrorContains(t, Checkout(tmpDir, "branch3"), "did not match any")
307 // Try checking out the first branch.
308 assert.NilError(t, Checkout(tmpDir, "branch1"))
309}
310
311func TestDeleteBranch_success(t *testing.T) {
312 CommandRunnerImpl = cmd.RealCommandRunner{}
313
314 tmpDir := "gittest_tmp_dir"
315 tmpDir, err := ioutil.TempDir("", tmpDir)
316 defer os.RemoveAll(tmpDir)
317 branchName := "newbranch"
318
319 assert.NilError(t, err)
320 // Create repo.
321 assert.NilError(t, Init(tmpDir, false))
322 // Create master branch.
323 assert.NilError(t, CreateBranch(tmpDir, "master"))
324 // In order for the ref to be created, need to commit something.
325 assert.NilError(t, ioutil.WriteFile(filepath.Join(tmpDir, "foo"), []byte("foo"), 0644))
326 assert.NilError(t, CommitAll(tmpDir, "init commit"))
327 // Create branch to be deleted.
328 assert.NilError(t, CreateBranch(tmpDir, branchName))
329 // Switch back to master.
330 assert.NilError(t, Checkout(tmpDir, "master"))
331 err = DeleteBranch(tmpDir, branchName, true)
332 assert.NilError(t, err)
333}
334
335func TestDeleteBranch_inBranch(t *testing.T) {
336 CommandRunnerImpl = cmd.RealCommandRunner{}
337
338 tmpDir := "gittest_tmp_dir"
339 tmpDir, err := ioutil.TempDir("", tmpDir)
340 defer os.RemoveAll(tmpDir)
341 branchName := "newbranch"
342
343 assert.NilError(t, err)
344 // Create repo.
345 assert.NilError(t, Init(tmpDir, false))
346 // Create branch.
347 assert.NilError(t, CreateBranch(tmpDir, branchName))
348 err = DeleteBranch(tmpDir, branchName, true)
349 assert.ErrorContains(t, err, "checked out")
350}
351
352func TestDeleteBranch_unmerged(t *testing.T) {
353 CommandRunnerImpl = cmd.RealCommandRunner{}
354
355 tmpDir := "gittest_tmp_dir"
356 tmpDir, err := ioutil.TempDir("", tmpDir)
357 defer os.RemoveAll(tmpDir)
358 branchName := "newbranch"
359
360 assert.NilError(t, err)
361 // Create repo.
362 assert.NilError(t, Init(tmpDir, false))
363 // Create master branch.
364 assert.NilError(t, CreateBranch(tmpDir, "master"))
365 // In order for the ref to be created, need to commit something.
366 assert.NilError(t, ioutil.WriteFile(filepath.Join(tmpDir, "foo"), []byte("foo"), 0644))
367 assert.NilError(t, CommitAll(tmpDir, "init commit"))
368 // Create test branch.
369 assert.NilError(t, CreateBranch(tmpDir, branchName))
370 // Make a change to branch.
371 assert.NilError(t, ioutil.WriteFile(filepath.Join(tmpDir, "bar"), []byte("bar"), 0644))
372 assert.NilError(t, CommitAll(tmpDir, "init commit"))
373 // Switch back to master.
374 assert.NilError(t, Checkout(tmpDir, "master"))
375 // Should not be able to delete.
376 assert.ErrorContains(t, DeleteBranch(tmpDir, branchName, false), "fully merged")
377}
378
379func TestClone(t *testing.T) {
380 dest := "foo/bar"
381 remote := "remote"
382
383 CommandRunnerImpl = cmd.FakeCommandRunner{
384 ExpectedDir: "foo",
385 ExpectedCmd: []string{"git", "clone", remote, "bar"},
386 }
387 assert.NilError(t, Clone(remote, dest))
388}