blob: 745c835af4456a2168afd7c939e56c133401a39a [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 }
Jack Neusa7287522019-07-23 16:36:18 -0600129 res, err := GetGitRepoRevision("project", "")
Jack Neusfc3b5772019-07-03 11:18:42 -0600130 assert.NilError(t, err)
131 assert.Equal(t, res, sha)
132}
Jack Neusabdbe192019-07-15 12:23:22 -0600133
Jack Neusa7287522019-07-23 16:36:18 -0600134func TestIsReachable_true(t *testing.T) {
135 fakeGitRepo := "gitRepo"
136 toRef := "beef"
137 fromRef := "deaf"
138
139 CommandRunnerImpl = cmd.FakeCommandRunner{
140 ExpectedCmd: []string{"git", "merge-base", "--is-ancestor", toRef, fromRef},
141 ExpectedDir: fakeGitRepo,
142 }
143
144 ok, err := IsReachable(fakeGitRepo, toRef, fromRef)
145 assert.NilError(t, err)
146 assert.Assert(t, ok)
147}
148
149func TestIsReachable_false(t *testing.T) {
150 fakeGitRepo := "gitRepo"
151 toRef := "beef"
152 fromRef := "deaf"
153
154 CommandRunnerImpl = cmd.FakeCommandRunner{
155 ExpectedCmd: []string{"git", "merge-base", "--is-ancestor", toRef, fromRef},
156 ExpectedDir: fakeGitRepo,
157 FailCommand: true,
158 FailError: "exit status 1",
159 }
160
161 ok, err := IsReachable(fakeGitRepo, toRef, fromRef)
162 assert.NilError(t, err)
163 assert.Assert(t, !ok)
164}
165
Jack Neusabdbe192019-07-15 12:23:22 -0600166func TestCreateBranch(t *testing.T) {
167 fakeGitRepo := "top-secret-project"
168 branchName := "project z"
169
170 CommandRunnerImpl = cmd.FakeCommandRunner{
171 ExpectedDir: fakeGitRepo,
Jack Neus0296c732019-07-17 09:35:01 -0600172 ExpectedCmd: []string{"git", "checkout", "-B", branchName},
Jack Neusabdbe192019-07-15 12:23:22 -0600173 }
174 assert.NilError(t, CreateBranch(fakeGitRepo, branchName))
175}
176
Jack Neus0296c732019-07-17 09:35:01 -0600177func TestCreateTrackingBranch(t *testing.T) {
178 fakeGitRepo := "repo"
179 branchName := "branch"
180 remoteRef := RemoteRef{
181 Remote: "remote",
182 Ref: "master",
183 }
184 refspec := fmt.Sprintf("%s/%s", remoteRef.Remote, remoteRef.Ref)
185
186 CommandRunnerImpl = &cmd.FakeCommandRunnerMulti{
187 CommandRunners: []cmd.FakeCommandRunner{
188 {
189 ExpectedDir: fakeGitRepo,
190 ExpectedCmd: []string{"git", "fetch", remoteRef.Remote, remoteRef.Ref},
191 },
192 {
193 ExpectedDir: fakeGitRepo,
194 ExpectedCmd: []string{"git", "checkout", "-b", branchName, "-t", refspec},
195 },
196 },
197 }
198
199 err := CreateTrackingBranch(fakeGitRepo, branchName, remoteRef)
200 assert.NilError(t, err)
201}
202
Jack Neusabdbe192019-07-15 12:23:22 -0600203func TestCommitAll(t *testing.T) {
204 fakeGitRepo := "repo"
205 commitMsg := "commit"
206
207 CommandRunnerImpl = &cmd.FakeCommandRunnerMulti{
208 CommandRunners: []cmd.FakeCommandRunner{
209 {
210 ExpectedDir: fakeGitRepo,
211 ExpectedCmd: []string{"git", "add", "-A"},
212 },
213 {
214 ExpectedDir: fakeGitRepo,
215 ExpectedCmd: []string{"git", "commit", "-m", commitMsg},
216 },
217 },
218 }
219
220 err := CommitAll(fakeGitRepo, commitMsg)
221 assert.NilError(t, err)
222}
223
Jack Neus7440c762019-07-22 10:45:18 -0600224func TestCommitEmpty(t *testing.T) {
225 fakeGitRepo := "repo"
226 commitMsg := "commit"
227
228 CommandRunnerImpl = cmd.FakeCommandRunner{
229 ExpectedDir: fakeGitRepo,
230 ExpectedCmd: []string{"git", "commit", "-m", commitMsg, "--allow-empty"},
231 }
232
233 err := CommitEmpty(fakeGitRepo, commitMsg)
234 assert.NilError(t, err)
235}
236
Jack Neusabdbe192019-07-15 12:23:22 -0600237func TestPushChanges(t *testing.T) {
238 fakeGitRepo := "da-bank"
239 commitMsg := "da-money"
240 localRef := "da-vault"
241
242 remoteRef := RemoteRef{
243 Remote: "da-family",
244 Ref: "da-van",
245 }
246
247 pushStr := fmt.Sprintf("%s:%s", localRef, remoteRef.Ref)
248 CommandRunnerImpl = &cmd.FakeCommandRunnerMulti{
249 CommandRunners: []cmd.FakeCommandRunner{
250 {
251 ExpectedDir: fakeGitRepo,
252 ExpectedCmd: []string{"git", "add", "-A"},
253 },
254 {
255 ExpectedDir: fakeGitRepo,
256 ExpectedCmd: []string{"git", "commit", "-m", commitMsg},
257 },
258 {
259 ExpectedDir: fakeGitRepo,
260 ExpectedCmd: []string{"git", "push", remoteRef.Remote, pushStr, "--dry-run"},
261 },
262 },
263 }
264
265 err := PushChanges(fakeGitRepo, localRef, commitMsg, true, remoteRef)
266 assert.NilError(t, err)
267}
Jack Neus0296c732019-07-17 09:35:01 -0600268
Jack Neuseb25f722019-07-19 16:33:20 -0600269func TestPushRef(t *testing.T) {
Jack Neus7440c762019-07-22 10:45:18 -0600270 fakeGitRepo := "repo"
271 localRef := "commitId"
272
273 remoteRef := RemoteRef{
274 Remote: "remote",
275 Ref: "ref",
276 }
277
278 pushStr := fmt.Sprintf("%s:%s", localRef, remoteRef.Ref)
279 CommandRunnerImpl = cmd.FakeCommandRunner{
280 ExpectedDir: fakeGitRepo,
281 ExpectedCmd: []string{"git", "push", remoteRef.Remote, pushStr, "--dry-run"},
282 }
283
Jack Neuseb25f722019-07-19 16:33:20 -0600284 err := PushRef(fakeGitRepo, localRef, true, remoteRef)
Jack Neus7440c762019-07-22 10:45:18 -0600285 assert.NilError(t, err)
286}
287
Jack Neus0296c732019-07-17 09:35:01 -0600288func TestInit(t *testing.T) {
289 fakeGitRepo := "top-secret-project"
290
291 CommandRunnerImpl = cmd.FakeCommandRunner{
292 ExpectedDir: fakeGitRepo,
293 ExpectedCmd: []string{"git", "init"},
294 }
295 assert.NilError(t, Init(fakeGitRepo, false))
296}
297
298func TestInit_bare(t *testing.T) {
299 fakeGitRepo := "top-secret-project"
300
301 CommandRunnerImpl = cmd.FakeCommandRunner{
302 ExpectedDir: fakeGitRepo,
303 ExpectedCmd: []string{"git", "init", "--bare"},
304 }
305 assert.NilError(t, Init(fakeGitRepo, true))
306}
307
308func TestAddRemote(t *testing.T) {
309 fakeGitRepo := "repo"
310 remoteName := "remote"
311 remoteLoc := "remote/"
312
313 CommandRunnerImpl = cmd.FakeCommandRunner{
314 ExpectedDir: fakeGitRepo,
315 ExpectedCmd: []string{"git", "remote", "add", remoteName, remoteLoc},
316 }
317 assert.NilError(t, AddRemote(fakeGitRepo, remoteName, remoteLoc))
318}
319
320func TestCheckout(t *testing.T) {
321 CommandRunnerImpl = cmd.RealCommandRunner{}
322
323 tmpDir := "gittest_tmp_dir"
324 tmpDir, err := ioutil.TempDir("", tmpDir)
325 defer os.RemoveAll(tmpDir)
326
327 assert.NilError(t, err)
328 // Create repo.
329 assert.NilError(t, Init(tmpDir, false))
330 // Create first branch.
331 assert.NilError(t, CreateBranch(tmpDir, "branch1"))
332 // In order for the ref to be created, need to commit something.
333 assert.NilError(t, ioutil.WriteFile(filepath.Join(tmpDir, "foo"), []byte("foo"), 0644))
334 assert.NilError(t, CommitAll(tmpDir, "init commit"))
335 // Create second branch (will switch to this branch).
336 assert.NilError(t, CreateBranch(tmpDir, "branch2"))
337 // Try checking out a nonexistent branch.
338 assert.ErrorContains(t, Checkout(tmpDir, "branch3"), "did not match any")
339 // Try checking out the first branch.
340 assert.NilError(t, Checkout(tmpDir, "branch1"))
341}
342
343func TestDeleteBranch_success(t *testing.T) {
344 CommandRunnerImpl = cmd.RealCommandRunner{}
345
346 tmpDir := "gittest_tmp_dir"
347 tmpDir, err := ioutil.TempDir("", tmpDir)
348 defer os.RemoveAll(tmpDir)
349 branchName := "newbranch"
350
351 assert.NilError(t, err)
352 // Create repo.
353 assert.NilError(t, Init(tmpDir, false))
354 // Create master branch.
355 assert.NilError(t, CreateBranch(tmpDir, "master"))
356 // In order for the ref to be created, need to commit something.
357 assert.NilError(t, ioutil.WriteFile(filepath.Join(tmpDir, "foo"), []byte("foo"), 0644))
358 assert.NilError(t, CommitAll(tmpDir, "init commit"))
359 // Create branch to be deleted.
360 assert.NilError(t, CreateBranch(tmpDir, branchName))
361 // Switch back to master.
362 assert.NilError(t, Checkout(tmpDir, "master"))
363 err = DeleteBranch(tmpDir, branchName, true)
364 assert.NilError(t, err)
365}
366
367func TestDeleteBranch_inBranch(t *testing.T) {
368 CommandRunnerImpl = cmd.RealCommandRunner{}
369
370 tmpDir := "gittest_tmp_dir"
371 tmpDir, err := ioutil.TempDir("", tmpDir)
372 defer os.RemoveAll(tmpDir)
373 branchName := "newbranch"
374
375 assert.NilError(t, err)
376 // Create repo.
377 assert.NilError(t, Init(tmpDir, false))
378 // Create branch.
379 assert.NilError(t, CreateBranch(tmpDir, branchName))
380 err = DeleteBranch(tmpDir, branchName, true)
381 assert.ErrorContains(t, err, "checked out")
382}
383
384func TestDeleteBranch_unmerged(t *testing.T) {
385 CommandRunnerImpl = cmd.RealCommandRunner{}
386
387 tmpDir := "gittest_tmp_dir"
388 tmpDir, err := ioutil.TempDir("", tmpDir)
389 defer os.RemoveAll(tmpDir)
390 branchName := "newbranch"
391
392 assert.NilError(t, err)
393 // Create repo.
394 assert.NilError(t, Init(tmpDir, false))
395 // Create master branch.
396 assert.NilError(t, CreateBranch(tmpDir, "master"))
397 // In order for the ref to be created, need to commit something.
398 assert.NilError(t, ioutil.WriteFile(filepath.Join(tmpDir, "foo"), []byte("foo"), 0644))
399 assert.NilError(t, CommitAll(tmpDir, "init commit"))
400 // Create test branch.
401 assert.NilError(t, CreateBranch(tmpDir, branchName))
402 // Make a change to branch.
403 assert.NilError(t, ioutil.WriteFile(filepath.Join(tmpDir, "bar"), []byte("bar"), 0644))
404 assert.NilError(t, CommitAll(tmpDir, "init commit"))
405 // Switch back to master.
406 assert.NilError(t, Checkout(tmpDir, "master"))
407 // Should not be able to delete.
408 assert.ErrorContains(t, DeleteBranch(tmpDir, branchName, false), "fully merged")
409}
410
411func TestClone(t *testing.T) {
412 dest := "foo/bar"
413 remote := "remote"
414
415 CommandRunnerImpl = cmd.FakeCommandRunner{
416 ExpectedDir: "foo",
417 ExpectedCmd: []string{"git", "clone", remote, "bar"},
418 }
419 assert.NilError(t, Clone(remote, dest))
420}