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 | abdbe19 | 2019-07-15 12:23:22 -0600 | [diff] [blame] | 7 | "fmt" |
Jack Neus | fc3b577 | 2019-07-03 11:18:42 -0600 | [diff] [blame] | 8 | "gotest.tools/assert" |
Jack Neus | 0296c73 | 2019-07-17 09:35:01 -0600 | [diff] [blame] | 9 | "io/ioutil" |
| 10 | "os" |
| 11 | "path/filepath" |
Jack Neus | fc3b577 | 2019-07-03 11:18:42 -0600 | [diff] [blame] | 12 | "regexp" |
Jack Neus | fc3b577 | 2019-07-03 11:18:42 -0600 | [diff] [blame] | 13 | "testing" |
Jack Neus | 1f489f6 | 2019-08-06 12:01:54 -0600 | [diff] [blame^] | 14 | |
| 15 | "go.chromium.org/chromiumos/infra/go/internal/cmd" |
| 16 | "go.chromium.org/chromiumos/infra/go/internal/test_util" |
Jack Neus | fc3b577 | 2019-07-03 11:18:42 -0600 | [diff] [blame] | 17 | ) |
| 18 | |
Jack Neus | 0751172 | 2019-07-12 15:41:10 -0600 | [diff] [blame] | 19 | func TestRunGit_success(t *testing.T) { |
| 20 | CommandRunnerImpl = cmd.FakeCommandRunner{ |
| 21 | ExpectedDir: "myrepo", |
| 22 | ExpectedCmd: []string{"git", "log"}, |
| 23 | Stdout: "success", |
| 24 | } |
| 25 | |
| 26 | output, err := RunGit("myrepo", []string{"log"}) |
| 27 | assert.NilError(t, err) |
| 28 | assert.Equal(t, output.Stdout, "success") |
Jack Neus | fc3b577 | 2019-07-03 11:18:42 -0600 | [diff] [blame] | 29 | } |
| 30 | |
Jack Neus | 0751172 | 2019-07-12 15:41:10 -0600 | [diff] [blame] | 31 | func TestRunGit_error(t *testing.T) { |
| 32 | CommandRunnerImpl = cmd.FakeCommandRunner{ |
| 33 | ExpectedDir: "myrepo", |
| 34 | ExpectedCmd: []string{"git", "log"}, |
| 35 | Stdout: "I don't feel so go--", |
| 36 | Stderr: "sudden death", |
| 37 | FailCommand: true, |
Jack Neus | fc3b577 | 2019-07-03 11:18:42 -0600 | [diff] [blame] | 38 | } |
Jack Neus | fc3b577 | 2019-07-03 11:18:42 -0600 | [diff] [blame] | 39 | |
Jack Neus | 0751172 | 2019-07-12 15:41:10 -0600 | [diff] [blame] | 40 | output, err := RunGit("myrepo", []string{"log"}) |
| 41 | assert.Assert(t, err != nil) |
| 42 | assert.Equal(t, output.Stderr, "sudden death") |
Jack Neus | fc3b577 | 2019-07-03 11:18:42 -0600 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | func TestStripRefsHead(t *testing.T) { |
| 46 | assert.Equal(t, StripRefsHead("refs/heads/foo"), "foo") |
| 47 | assert.Equal(t, StripRefsHead("foo"), "foo") |
| 48 | } |
| 49 | |
| 50 | func TestStripRefs(t *testing.T) { |
| 51 | assert.Equal(t, StripRefs("refs/remotes/origin/foo"), "foo") |
| 52 | assert.Equal(t, StripRefs("refs/heads/foo"), "foo") |
| 53 | assert.Equal(t, StripRefs("foo"), "foo") |
| 54 | } |
| 55 | |
| 56 | func TestNormalizeRef(t *testing.T) { |
| 57 | assert.Equal(t, NormalizeRef("refs/heads/foo"), "refs/heads/foo") |
| 58 | assert.Equal(t, NormalizeRef("foo"), "refs/heads/foo") |
| 59 | } |
| 60 | |
| 61 | func TestGetCurrentBranch_success(t *testing.T) { |
| 62 | fakeGitRepo := "top-secret-project" |
| 63 | fakeGitData := "refs/heads/current-branch" |
| 64 | |
Jack Neus | 0751172 | 2019-07-12 15:41:10 -0600 | [diff] [blame] | 65 | CommandRunnerImpl = cmd.FakeCommandRunner{ |
| 66 | ExpectedDir: fakeGitRepo, |
| 67 | Stdout: fakeGitData, |
Jack Neus | fc3b577 | 2019-07-03 11:18:42 -0600 | [diff] [blame] | 68 | } |
| 69 | assert.Equal(t, GetCurrentBranch(fakeGitRepo), "current-branch") |
| 70 | } |
| 71 | |
| 72 | func TestGetCurrentBranch_failure(t *testing.T) { |
Jack Neus | 0751172 | 2019-07-12 15:41:10 -0600 | [diff] [blame] | 73 | CommandRunnerImpl = cmd.FakeCommandRunner{ |
| 74 | FailCommand: true, |
Jack Neus | fc3b577 | 2019-07-03 11:18:42 -0600 | [diff] [blame] | 75 | } |
| 76 | assert.Equal(t, GetCurrentBranch("project"), "") |
| 77 | } |
| 78 | |
| 79 | func TestMatchBranchName_success(t *testing.T) { |
| 80 | fakeGitRepo := "top-secret-project" |
| 81 | fakeGitData := "e9cb56bd9af9365b43f82cecf28cc76d49df1f72 refs/heads/foo\n" + |
| 82 | "f9c1bb630f4475058d4a9db4aea52fc89d8f7b0d refs/heads/bar\n" + |
| 83 | "2102915989de21d9251c11f0a7b5307e175e7677 refs/heads/foobar\n" + |
| 84 | "04975f9439ff75502b33d9491155692736e05b07 refs/heads/baz\n" |
| 85 | |
Jack Neus | 0751172 | 2019-07-12 15:41:10 -0600 | [diff] [blame] | 86 | CommandRunnerImpl = cmd.FakeCommandRunner{ |
Jack Neus | 901a6bf | 2019-07-22 08:30:07 -0600 | [diff] [blame] | 87 | ExpectedCmd: []string{"git", "show-ref"}, |
Jack Neus | 0751172 | 2019-07-12 15:41:10 -0600 | [diff] [blame] | 88 | ExpectedDir: fakeGitRepo, |
| 89 | Stdout: fakeGitData, |
Jack Neus | fc3b577 | 2019-07-03 11:18:42 -0600 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | expectedMatches := []string{"refs/heads/foo", "refs/heads/foobar"} |
| 93 | branches, err := MatchBranchName(fakeGitRepo, regexp.MustCompile("Foo")) |
| 94 | assert.NilError(t, err) |
| 95 | assert.DeepEqual(t, expectedMatches, branches) |
| 96 | |
| 97 | expectedMatches = []string{"refs/heads/foo"} |
| 98 | branches, err = MatchBranchName(fakeGitRepo, regexp.MustCompile("Foo$")) |
| 99 | assert.NilError(t, err) |
| 100 | assert.DeepEqual(t, expectedMatches, branches) |
| 101 | } |
| 102 | |
Jack Neus | 10e6a12 | 2019-07-18 10:17:44 -0600 | [diff] [blame] | 103 | func TestMatchBranchNameWithNamespace_success(t *testing.T) { |
| 104 | fakeGitRepo := "top-secret-project" |
| 105 | fakeGitData := "e9cb56bd9af9365b43f82cecf28cc76d49df1f72 refs/changes/foo\n" + |
| 106 | "f9c1bb630f4475058d4a9db4aea52fc89d8f7b0d refs/changes/bar\n" + |
| 107 | "2102915989de21d9251c11f0a7b5307e175e7677 refs/heads/foobar\n" + |
| 108 | "04975f9439ff75502b33d9491155692736e05b07 refs/heads/baz\n" |
| 109 | |
| 110 | CommandRunnerImpl = cmd.FakeCommandRunner{ |
Jack Neus | 901a6bf | 2019-07-22 08:30:07 -0600 | [diff] [blame] | 111 | ExpectedCmd: []string{"git", "show-ref"}, |
Jack Neus | 10e6a12 | 2019-07-18 10:17:44 -0600 | [diff] [blame] | 112 | ExpectedDir: fakeGitRepo, |
| 113 | Stdout: fakeGitData, |
| 114 | } |
| 115 | |
| 116 | expectedMatches := []string{"foobar"} |
| 117 | pattern := regexp.MustCompile("FOO") |
| 118 | namespace := regexp.MustCompile("refs/heads/") |
| 119 | branches, err := MatchBranchNameWithNamespace(fakeGitRepo, pattern, namespace) |
| 120 | assert.NilError(t, err) |
| 121 | assert.DeepEqual(t, expectedMatches, branches) |
| 122 | } |
| 123 | |
Jack Neus | fc3b577 | 2019-07-03 11:18:42 -0600 | [diff] [blame] | 124 | func TestGetRepoRevision(t *testing.T) { |
| 125 | sha := "6446dfef4b55689046395c2db7ba7c35377927fe" |
Jack Neus | 0751172 | 2019-07-12 15:41:10 -0600 | [diff] [blame] | 126 | CommandRunnerImpl = cmd.FakeCommandRunner{ |
| 127 | ExpectedCmd: []string{"git", "rev-parse", "HEAD"}, |
| 128 | ExpectedDir: "project", |
| 129 | Stdout: sha, |
Jack Neus | fc3b577 | 2019-07-03 11:18:42 -0600 | [diff] [blame] | 130 | } |
Jack Neus | a728752 | 2019-07-23 16:36:18 -0600 | [diff] [blame] | 131 | res, err := GetGitRepoRevision("project", "") |
Jack Neus | fc3b577 | 2019-07-03 11:18:42 -0600 | [diff] [blame] | 132 | assert.NilError(t, err) |
| 133 | assert.Equal(t, res, sha) |
| 134 | } |
Jack Neus | abdbe19 | 2019-07-15 12:23:22 -0600 | [diff] [blame] | 135 | |
Jack Neus | a728752 | 2019-07-23 16:36:18 -0600 | [diff] [blame] | 136 | func TestIsReachable_true(t *testing.T) { |
| 137 | fakeGitRepo := "gitRepo" |
| 138 | toRef := "beef" |
| 139 | fromRef := "deaf" |
| 140 | |
| 141 | CommandRunnerImpl = cmd.FakeCommandRunner{ |
| 142 | ExpectedCmd: []string{"git", "merge-base", "--is-ancestor", toRef, fromRef}, |
| 143 | ExpectedDir: fakeGitRepo, |
| 144 | } |
| 145 | |
| 146 | ok, err := IsReachable(fakeGitRepo, toRef, fromRef) |
| 147 | assert.NilError(t, err) |
| 148 | assert.Assert(t, ok) |
| 149 | } |
| 150 | |
| 151 | func TestIsReachable_false(t *testing.T) { |
| 152 | fakeGitRepo := "gitRepo" |
| 153 | toRef := "beef" |
| 154 | fromRef := "deaf" |
| 155 | |
| 156 | CommandRunnerImpl = cmd.FakeCommandRunner{ |
| 157 | ExpectedCmd: []string{"git", "merge-base", "--is-ancestor", toRef, fromRef}, |
| 158 | ExpectedDir: fakeGitRepo, |
| 159 | FailCommand: true, |
| 160 | FailError: "exit status 1", |
| 161 | } |
| 162 | |
| 163 | ok, err := IsReachable(fakeGitRepo, toRef, fromRef) |
| 164 | assert.NilError(t, err) |
| 165 | assert.Assert(t, !ok) |
| 166 | } |
| 167 | |
Jack Neus | 04f4edc | 2019-07-31 16:42:00 -0600 | [diff] [blame] | 168 | func TestIsReachable_self(t *testing.T) { |
| 169 | CommandRunnerImpl = cmd.RealCommandRunner{} |
| 170 | |
| 171 | tmpDir := "gittest_tmp_dir" |
| 172 | tmpDir, err := ioutil.TempDir("", tmpDir) |
| 173 | defer os.RemoveAll(tmpDir) |
| 174 | |
| 175 | assert.NilError(t, err) |
| 176 | // Create repo. |
| 177 | assert.NilError(t, Init(tmpDir, false)) |
| 178 | // Make commit. |
| 179 | commit, err := CommitEmpty(tmpDir, "empty commit") |
| 180 | assert.NilError(t, err) |
| 181 | // Check that a SHA is reachable from itself. |
| 182 | ok, err := IsReachable(tmpDir, commit, commit) |
| 183 | assert.NilError(t, err) |
| 184 | assert.Assert(t, ok) |
| 185 | } |
| 186 | |
Jack Neus | abdbe19 | 2019-07-15 12:23:22 -0600 | [diff] [blame] | 187 | func TestCreateBranch(t *testing.T) { |
| 188 | fakeGitRepo := "top-secret-project" |
| 189 | branchName := "project z" |
| 190 | |
| 191 | CommandRunnerImpl = cmd.FakeCommandRunner{ |
| 192 | ExpectedDir: fakeGitRepo, |
Jack Neus | 0296c73 | 2019-07-17 09:35:01 -0600 | [diff] [blame] | 193 | ExpectedCmd: []string{"git", "checkout", "-B", branchName}, |
Jack Neus | abdbe19 | 2019-07-15 12:23:22 -0600 | [diff] [blame] | 194 | } |
| 195 | assert.NilError(t, CreateBranch(fakeGitRepo, branchName)) |
| 196 | } |
| 197 | |
Jack Neus | 0296c73 | 2019-07-17 09:35:01 -0600 | [diff] [blame] | 198 | func TestCreateTrackingBranch(t *testing.T) { |
| 199 | fakeGitRepo := "repo" |
| 200 | branchName := "branch" |
| 201 | remoteRef := RemoteRef{ |
| 202 | Remote: "remote", |
| 203 | Ref: "master", |
| 204 | } |
| 205 | refspec := fmt.Sprintf("%s/%s", remoteRef.Remote, remoteRef.Ref) |
| 206 | |
| 207 | CommandRunnerImpl = &cmd.FakeCommandRunnerMulti{ |
| 208 | CommandRunners: []cmd.FakeCommandRunner{ |
| 209 | { |
| 210 | ExpectedDir: fakeGitRepo, |
| 211 | ExpectedCmd: []string{"git", "fetch", remoteRef.Remote, remoteRef.Ref}, |
| 212 | }, |
| 213 | { |
| 214 | ExpectedDir: fakeGitRepo, |
| 215 | ExpectedCmd: []string{"git", "checkout", "-b", branchName, "-t", refspec}, |
| 216 | }, |
| 217 | }, |
| 218 | } |
| 219 | |
| 220 | err := CreateTrackingBranch(fakeGitRepo, branchName, remoteRef) |
| 221 | assert.NilError(t, err) |
| 222 | } |
| 223 | |
Jack Neus | abdbe19 | 2019-07-15 12:23:22 -0600 | [diff] [blame] | 224 | func TestCommitAll(t *testing.T) { |
| 225 | fakeGitRepo := "repo" |
| 226 | commitMsg := "commit" |
| 227 | |
| 228 | CommandRunnerImpl = &cmd.FakeCommandRunnerMulti{ |
| 229 | CommandRunners: []cmd.FakeCommandRunner{ |
| 230 | { |
| 231 | ExpectedDir: fakeGitRepo, |
| 232 | ExpectedCmd: []string{"git", "add", "-A"}, |
| 233 | }, |
| 234 | { |
| 235 | ExpectedDir: fakeGitRepo, |
| 236 | ExpectedCmd: []string{"git", "commit", "-m", commitMsg}, |
| 237 | }, |
Jack Neus | f116fae | 2019-07-24 15:05:03 -0600 | [diff] [blame] | 238 | { |
| 239 | ExpectedDir: fakeGitRepo, |
| 240 | ExpectedCmd: []string{"git", "rev-parse", "HEAD"}, |
| 241 | Stdout: "abcde12345\n\n\t\n", |
| 242 | }, |
Jack Neus | abdbe19 | 2019-07-15 12:23:22 -0600 | [diff] [blame] | 243 | }, |
| 244 | } |
| 245 | |
Jack Neus | f116fae | 2019-07-24 15:05:03 -0600 | [diff] [blame] | 246 | commit, err := CommitAll(fakeGitRepo, commitMsg) |
Jack Neus | abdbe19 | 2019-07-15 12:23:22 -0600 | [diff] [blame] | 247 | assert.NilError(t, err) |
Jack Neus | f116fae | 2019-07-24 15:05:03 -0600 | [diff] [blame] | 248 | assert.Equal(t, commit, "abcde12345") |
Jack Neus | abdbe19 | 2019-07-15 12:23:22 -0600 | [diff] [blame] | 249 | } |
| 250 | |
Jack Neus | 7440c76 | 2019-07-22 10:45:18 -0600 | [diff] [blame] | 251 | func TestCommitEmpty(t *testing.T) { |
| 252 | fakeGitRepo := "repo" |
| 253 | commitMsg := "commit" |
| 254 | |
Jack Neus | f116fae | 2019-07-24 15:05:03 -0600 | [diff] [blame] | 255 | CommandRunnerImpl = &cmd.FakeCommandRunnerMulti{ |
| 256 | CommandRunners: []cmd.FakeCommandRunner{ |
| 257 | { |
| 258 | ExpectedDir: fakeGitRepo, |
| 259 | ExpectedCmd: []string{"git", "commit", "-m", commitMsg, "--allow-empty"}, |
| 260 | }, |
| 261 | { |
| 262 | ExpectedDir: fakeGitRepo, |
| 263 | ExpectedCmd: []string{"git", "rev-parse", "HEAD"}, |
| 264 | Stdout: "abcde12345\n\n\t\n", |
| 265 | }, |
| 266 | }, |
Jack Neus | 7440c76 | 2019-07-22 10:45:18 -0600 | [diff] [blame] | 267 | } |
| 268 | |
Jack Neus | f116fae | 2019-07-24 15:05:03 -0600 | [diff] [blame] | 269 | commit, err := CommitEmpty(fakeGitRepo, commitMsg) |
Jack Neus | 7440c76 | 2019-07-22 10:45:18 -0600 | [diff] [blame] | 270 | assert.NilError(t, err) |
Jack Neus | f116fae | 2019-07-24 15:05:03 -0600 | [diff] [blame] | 271 | assert.Equal(t, commit, "abcde12345") |
Jack Neus | 7440c76 | 2019-07-22 10:45:18 -0600 | [diff] [blame] | 272 | } |
| 273 | |
Jack Neus | abdbe19 | 2019-07-15 12:23:22 -0600 | [diff] [blame] | 274 | func TestPushChanges(t *testing.T) { |
| 275 | fakeGitRepo := "da-bank" |
| 276 | commitMsg := "da-money" |
| 277 | localRef := "da-vault" |
| 278 | |
| 279 | remoteRef := RemoteRef{ |
| 280 | Remote: "da-family", |
| 281 | Ref: "da-van", |
| 282 | } |
| 283 | |
| 284 | pushStr := fmt.Sprintf("%s:%s", localRef, remoteRef.Ref) |
| 285 | CommandRunnerImpl = &cmd.FakeCommandRunnerMulti{ |
| 286 | CommandRunners: []cmd.FakeCommandRunner{ |
| 287 | { |
| 288 | ExpectedDir: fakeGitRepo, |
| 289 | ExpectedCmd: []string{"git", "add", "-A"}, |
| 290 | }, |
| 291 | { |
| 292 | ExpectedDir: fakeGitRepo, |
| 293 | ExpectedCmd: []string{"git", "commit", "-m", commitMsg}, |
| 294 | }, |
| 295 | { |
| 296 | ExpectedDir: fakeGitRepo, |
Jack Neus | f116fae | 2019-07-24 15:05:03 -0600 | [diff] [blame] | 297 | ExpectedCmd: []string{"git", "rev-parse", "HEAD"}, |
| 298 | Stdout: "abcde\n", |
| 299 | }, |
| 300 | { |
| 301 | ExpectedDir: fakeGitRepo, |
Jack Neus | abdbe19 | 2019-07-15 12:23:22 -0600 | [diff] [blame] | 302 | ExpectedCmd: []string{"git", "push", remoteRef.Remote, pushStr, "--dry-run"}, |
| 303 | }, |
| 304 | }, |
| 305 | } |
| 306 | |
Jack Neus | f116fae | 2019-07-24 15:05:03 -0600 | [diff] [blame] | 307 | commit, err := PushChanges(fakeGitRepo, localRef, commitMsg, true, remoteRef) |
Jack Neus | abdbe19 | 2019-07-15 12:23:22 -0600 | [diff] [blame] | 308 | assert.NilError(t, err) |
Jack Neus | f116fae | 2019-07-24 15:05:03 -0600 | [diff] [blame] | 309 | assert.Equal(t, commit, "abcde") |
Jack Neus | abdbe19 | 2019-07-15 12:23:22 -0600 | [diff] [blame] | 310 | } |
Jack Neus | 0296c73 | 2019-07-17 09:35:01 -0600 | [diff] [blame] | 311 | |
Jack Neus | eb25f72 | 2019-07-19 16:33:20 -0600 | [diff] [blame] | 312 | func TestPushRef(t *testing.T) { |
Jack Neus | 7440c76 | 2019-07-22 10:45:18 -0600 | [diff] [blame] | 313 | fakeGitRepo := "repo" |
| 314 | localRef := "commitId" |
| 315 | |
| 316 | remoteRef := RemoteRef{ |
| 317 | Remote: "remote", |
| 318 | Ref: "ref", |
| 319 | } |
| 320 | |
| 321 | pushStr := fmt.Sprintf("%s:%s", localRef, remoteRef.Ref) |
| 322 | CommandRunnerImpl = cmd.FakeCommandRunner{ |
| 323 | ExpectedDir: fakeGitRepo, |
| 324 | ExpectedCmd: []string{"git", "push", remoteRef.Remote, pushStr, "--dry-run"}, |
| 325 | } |
| 326 | |
Jack Neus | eb25f72 | 2019-07-19 16:33:20 -0600 | [diff] [blame] | 327 | err := PushRef(fakeGitRepo, localRef, true, remoteRef) |
Jack Neus | 7440c76 | 2019-07-22 10:45:18 -0600 | [diff] [blame] | 328 | assert.NilError(t, err) |
| 329 | } |
| 330 | |
Jack Neus | 0296c73 | 2019-07-17 09:35:01 -0600 | [diff] [blame] | 331 | func TestInit(t *testing.T) { |
| 332 | fakeGitRepo := "top-secret-project" |
| 333 | |
| 334 | CommandRunnerImpl = cmd.FakeCommandRunner{ |
| 335 | ExpectedDir: fakeGitRepo, |
| 336 | ExpectedCmd: []string{"git", "init"}, |
| 337 | } |
| 338 | assert.NilError(t, Init(fakeGitRepo, false)) |
| 339 | } |
| 340 | |
| 341 | func TestInit_bare(t *testing.T) { |
| 342 | fakeGitRepo := "top-secret-project" |
| 343 | |
| 344 | CommandRunnerImpl = cmd.FakeCommandRunner{ |
| 345 | ExpectedDir: fakeGitRepo, |
| 346 | ExpectedCmd: []string{"git", "init", "--bare"}, |
| 347 | } |
| 348 | assert.NilError(t, Init(fakeGitRepo, true)) |
| 349 | } |
| 350 | |
| 351 | func TestAddRemote(t *testing.T) { |
| 352 | fakeGitRepo := "repo" |
| 353 | remoteName := "remote" |
| 354 | remoteLoc := "remote/" |
| 355 | |
| 356 | CommandRunnerImpl = cmd.FakeCommandRunner{ |
| 357 | ExpectedDir: fakeGitRepo, |
| 358 | ExpectedCmd: []string{"git", "remote", "add", remoteName, remoteLoc}, |
| 359 | } |
| 360 | assert.NilError(t, AddRemote(fakeGitRepo, remoteName, remoteLoc)) |
| 361 | } |
| 362 | |
| 363 | func TestCheckout(t *testing.T) { |
| 364 | CommandRunnerImpl = cmd.RealCommandRunner{} |
| 365 | |
| 366 | tmpDir := "gittest_tmp_dir" |
| 367 | tmpDir, err := ioutil.TempDir("", tmpDir) |
| 368 | defer os.RemoveAll(tmpDir) |
| 369 | |
| 370 | assert.NilError(t, err) |
| 371 | // Create repo. |
| 372 | assert.NilError(t, Init(tmpDir, false)) |
| 373 | // Create first branch. |
| 374 | assert.NilError(t, CreateBranch(tmpDir, "branch1")) |
| 375 | // In order for the ref to be created, need to commit something. |
| 376 | assert.NilError(t, ioutil.WriteFile(filepath.Join(tmpDir, "foo"), []byte("foo"), 0644)) |
Jack Neus | f116fae | 2019-07-24 15:05:03 -0600 | [diff] [blame] | 377 | _, err = CommitAll(tmpDir, "init commit") |
| 378 | assert.NilError(t, err) |
Jack Neus | 0296c73 | 2019-07-17 09:35:01 -0600 | [diff] [blame] | 379 | // Create second branch (will switch to this branch). |
| 380 | assert.NilError(t, CreateBranch(tmpDir, "branch2")) |
| 381 | // Try checking out a nonexistent branch. |
| 382 | assert.ErrorContains(t, Checkout(tmpDir, "branch3"), "did not match any") |
| 383 | // Try checking out the first branch. |
| 384 | assert.NilError(t, Checkout(tmpDir, "branch1")) |
| 385 | } |
| 386 | |
| 387 | func TestDeleteBranch_success(t *testing.T) { |
| 388 | CommandRunnerImpl = cmd.RealCommandRunner{} |
| 389 | |
| 390 | tmpDir := "gittest_tmp_dir" |
| 391 | tmpDir, err := ioutil.TempDir("", tmpDir) |
| 392 | defer os.RemoveAll(tmpDir) |
| 393 | branchName := "newbranch" |
| 394 | |
| 395 | assert.NilError(t, err) |
| 396 | // Create repo. |
| 397 | assert.NilError(t, Init(tmpDir, false)) |
| 398 | // Create master branch. |
| 399 | assert.NilError(t, CreateBranch(tmpDir, "master")) |
| 400 | // In order for the ref to be created, need to commit something. |
| 401 | assert.NilError(t, ioutil.WriteFile(filepath.Join(tmpDir, "foo"), []byte("foo"), 0644)) |
Jack Neus | f116fae | 2019-07-24 15:05:03 -0600 | [diff] [blame] | 402 | _, err = CommitAll(tmpDir, "init commit") |
| 403 | assert.NilError(t, err) |
Jack Neus | 0296c73 | 2019-07-17 09:35:01 -0600 | [diff] [blame] | 404 | // Create branch to be deleted. |
| 405 | assert.NilError(t, CreateBranch(tmpDir, branchName)) |
| 406 | // Switch back to master. |
| 407 | assert.NilError(t, Checkout(tmpDir, "master")) |
| 408 | err = DeleteBranch(tmpDir, branchName, true) |
| 409 | assert.NilError(t, err) |
| 410 | } |
| 411 | |
| 412 | func TestDeleteBranch_inBranch(t *testing.T) { |
| 413 | CommandRunnerImpl = cmd.RealCommandRunner{} |
| 414 | |
| 415 | tmpDir := "gittest_tmp_dir" |
| 416 | tmpDir, err := ioutil.TempDir("", tmpDir) |
| 417 | defer os.RemoveAll(tmpDir) |
| 418 | branchName := "newbranch" |
| 419 | |
| 420 | assert.NilError(t, err) |
| 421 | // Create repo. |
| 422 | assert.NilError(t, Init(tmpDir, false)) |
| 423 | // Create branch. |
| 424 | assert.NilError(t, CreateBranch(tmpDir, branchName)) |
| 425 | err = DeleteBranch(tmpDir, branchName, true) |
| 426 | assert.ErrorContains(t, err, "checked out") |
| 427 | } |
| 428 | |
| 429 | func TestDeleteBranch_unmerged(t *testing.T) { |
| 430 | CommandRunnerImpl = cmd.RealCommandRunner{} |
| 431 | |
| 432 | tmpDir := "gittest_tmp_dir" |
| 433 | tmpDir, err := ioutil.TempDir("", tmpDir) |
| 434 | defer os.RemoveAll(tmpDir) |
| 435 | branchName := "newbranch" |
| 436 | |
| 437 | assert.NilError(t, err) |
| 438 | // Create repo. |
| 439 | assert.NilError(t, Init(tmpDir, false)) |
| 440 | // Create master branch. |
| 441 | assert.NilError(t, CreateBranch(tmpDir, "master")) |
| 442 | // In order for the ref to be created, need to commit something. |
| 443 | assert.NilError(t, ioutil.WriteFile(filepath.Join(tmpDir, "foo"), []byte("foo"), 0644)) |
Jack Neus | f116fae | 2019-07-24 15:05:03 -0600 | [diff] [blame] | 444 | _, err = CommitAll(tmpDir, "init commit") |
| 445 | assert.NilError(t, err) |
Jack Neus | 0296c73 | 2019-07-17 09:35:01 -0600 | [diff] [blame] | 446 | // Create test branch. |
| 447 | assert.NilError(t, CreateBranch(tmpDir, branchName)) |
| 448 | // Make a change to branch. |
| 449 | assert.NilError(t, ioutil.WriteFile(filepath.Join(tmpDir, "bar"), []byte("bar"), 0644)) |
Jack Neus | f116fae | 2019-07-24 15:05:03 -0600 | [diff] [blame] | 450 | _, err = CommitAll(tmpDir, "init commit") |
| 451 | assert.NilError(t, err) |
Jack Neus | 0296c73 | 2019-07-17 09:35:01 -0600 | [diff] [blame] | 452 | // Switch back to master. |
| 453 | assert.NilError(t, Checkout(tmpDir, "master")) |
| 454 | // Should not be able to delete. |
| 455 | assert.ErrorContains(t, DeleteBranch(tmpDir, branchName, false), "fully merged") |
| 456 | } |
| 457 | |
| 458 | func TestClone(t *testing.T) { |
| 459 | dest := "foo/bar" |
| 460 | remote := "remote" |
| 461 | |
| 462 | CommandRunnerImpl = cmd.FakeCommandRunner{ |
| 463 | ExpectedDir: "foo", |
| 464 | ExpectedCmd: []string{"git", "clone", remote, "bar"}, |
| 465 | } |
| 466 | assert.NilError(t, Clone(remote, dest)) |
| 467 | } |
Jack Neus | 1f489f6 | 2019-08-06 12:01:54 -0600 | [diff] [blame^] | 468 | |
| 469 | func TestRemoteBranches(t *testing.T) { |
| 470 | CommandRunnerImpl = cmd.RealCommandRunner{} |
| 471 | |
| 472 | local, err := ioutil.TempDir("", "gittest_tmp_dir") |
| 473 | defer os.RemoveAll(local) |
| 474 | |
| 475 | remote, err := ioutil.TempDir("", "gittest_tmp_dir") |
| 476 | defer os.RemoveAll(remote) |
| 477 | |
| 478 | assert.NilError(t, err) |
| 479 | // Create repos. |
| 480 | assert.NilError(t, Init(local, false)) |
| 481 | assert.NilError(t, Init(remote, false)) |
| 482 | // Create local master branch. |
| 483 | assert.NilError(t, CreateBranch(local, "master")) |
| 484 | // In order for the ref to be created, need to commit something. |
| 485 | assert.NilError(t, ioutil.WriteFile(filepath.Join(local, "foo"), []byte("foo"), 0644)) |
| 486 | _, err = CommitAll(local, "init commit") |
| 487 | assert.NilError(t, err) |
| 488 | // Add remote. |
| 489 | assert.NilError(t, AddRemote(local, "remote", remote)) |
| 490 | // Create remote branches. |
| 491 | remoteRef := RemoteRef{ |
| 492 | Remote: "remote", |
| 493 | Ref: "foo", |
| 494 | } |
| 495 | assert.NilError(t, PushRef(local, "HEAD", false, remoteRef)) |
| 496 | remoteRef.Ref = "bar" |
| 497 | assert.NilError(t, PushRef(local, "HEAD", false, remoteRef)) |
| 498 | |
| 499 | branches, err := RemoteBranches(local, "remote") |
| 500 | assert.NilError(t, err) |
| 501 | assert.Assert(t, test_util.UnorderedEqual(branches, []string{"foo", "bar"})) |
| 502 | ok, err := RemoteHasBranch(local, "remote", "foo") |
| 503 | assert.NilError(t, err) |
| 504 | assert.Assert(t, ok) |
| 505 | ok, err = RemoteHasBranch(local, "remote", "baz") |
| 506 | assert.NilError(t, err) |
| 507 | assert.Assert(t, !ok) |
| 508 | } |
| 509 | |
| 510 | func TestAssertGitBranches_success(t *testing.T) { |
| 511 | tmpDir, err := ioutil.TempDir("", "assert_git_branches_test") |
| 512 | assert.NilError(t, err) |
| 513 | defer os.RemoveAll(tmpDir) |
| 514 | |
| 515 | assert.NilError(t, Init(tmpDir, false)) |
| 516 | |
| 517 | branches := []string{"branch1", "branch2", "branch3", "extra"} |
| 518 | for _, branch := range branches { |
| 519 | assert.NilError(t, CreateBranch(tmpDir, branch)) |
| 520 | // Empty commit so that branch is not "unborn". |
| 521 | _, err := RunGit(tmpDir, []string{"commit", "-m", "init", "--allow-empty"}) |
| 522 | assert.NilError(t, err) |
| 523 | } |
| 524 | |
| 525 | assert.NilError(t, AssertGitBranches(tmpDir, branches[:3])) |
| 526 | } |
| 527 | |
| 528 | func TestAssertGitBranchesExact_success(t *testing.T) { |
| 529 | tmpDir, err := ioutil.TempDir("", "assert_git_branches_test") |
| 530 | assert.NilError(t, err) |
| 531 | defer os.RemoveAll(tmpDir) |
| 532 | |
| 533 | assert.NilError(t, Init(tmpDir, false)) |
| 534 | |
| 535 | branches := []string{"branch1", "branch2", "branch3", "branch4"} |
| 536 | for _, branch := range branches { |
| 537 | assert.NilError(t, CreateBranch(tmpDir, branch)) |
| 538 | // Empty commit so that branch is not "unborn". |
| 539 | _, err := RunGit(tmpDir, []string{"commit", "-m", "init", "--allow-empty"}) |
| 540 | assert.NilError(t, err) |
| 541 | } |
| 542 | |
| 543 | assert.NilError(t, AssertGitBranchesExact(tmpDir, append(branches, "branch2"))) |
| 544 | assert.ErrorContains(t, AssertGitBranchesExact(tmpDir, branches[:3]), "mismatch") |
| 545 | } |
| 546 | |
| 547 | func TestAssertGitBranches_failure(t *testing.T) { |
| 548 | tmpDir, err := ioutil.TempDir("", "assert_git_branches_test") |
| 549 | assert.NilError(t, err) |
| 550 | defer os.RemoveAll(tmpDir) |
| 551 | |
| 552 | assert.NilError(t, Init(tmpDir, false)) |
| 553 | assert.ErrorContains(t, AssertGitBranches(tmpDir, []string{"master", "foo"}), "mismatch") |
| 554 | } |