blob: 30e4f63cb180637655ae647abba6e3a3eada4806 [file] [log] [blame]
Jack Neus08ccd452019-08-13 09:35:41 -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.
Sean Abraham47cc5852020-06-30 09:32:41 -06004
Sean Abrahamc2c5a6a2020-06-23 17:54:41 -06005package branch
Jack Neus08ccd452019-08-13 09:35:41 -06006
7import (
Julio Hurtado516392f2020-11-12 20:19:35 +00008 "go.chromium.org/chromiumos/infra/go/internal/cmd"
9 "go.chromium.org/chromiumos/infra/go/internal/git"
Jack Neus08ccd452019-08-13 09:35:41 -060010 "go.chromium.org/chromiumos/infra/go/internal/repo"
11 "gotest.tools/assert"
Julio Hurtado516392f2020-11-12 20:19:35 +000012 "testing"
Jack Neus08ccd452019-08-13 09:35:41 -060013)
14
15func TestProjectFetchUrl(t *testing.T) {
Sean Abrahamc2c5a6a2020-06-23 17:54:41 -060016 WorkingManifest = repo.Manifest{
Jack Neus08ccd452019-08-13 09:35:41 -060017 Remotes: []repo.Remote{
18 {Name: "remote", Fetch: "file:///tmp/path/to/remote"},
19 },
20 Projects: []repo.Project{
21 {Path: "foo/bar/project", Name: "foo/bar/project", RemoteName: "remote"},
22 },
23 }
Sean Abrahamc2c5a6a2020-06-23 17:54:41 -060024 url, err := ProjectFetchUrl("foo/bar/project")
Jack Neus08ccd452019-08-13 09:35:41 -060025 assert.NilError(t, err)
26 assert.Equal(t, url, "file:///tmp/path/to/remote/foo/bar/project")
27}
Julio Hurtado516392f2020-11-12 20:19:35 +000028
29func TestGetProjectCheckoutFromUrl(t *testing.T) {
30 git.CommandRunnerImpl = &cmd.FakeCommandRunnerMulti{
31 CommandRunners: []cmd.FakeCommandRunner{
32 {
33 ExpectedDir: "",
34 ExpectedCmd: []string{"git", "init"},
35 },
36 {
37 ExpectedDir: "",
38 ExpectedCmd: []string{"git", "remote", "add", "origin", "localhost"},
39 },
40 {
41 ExpectedDir: "",
42 ExpectedCmd: []string{"git", "fetch", "origin"},
43 },
44 {
45 ExpectedDir: "",
46 ExpectedCmd: []string{"git", "checkout", "master"},
47 FailCommand: true,
48 FailError: "Failed to checkout master",
49 },
50 },
51 }
52
53 _, err := getProjectCheckoutFromUrl("localhost", nil)
54
55 if err != nil {
56 t.Error("Error: checkout out project reason: ", err.Error())
57 return
58 }
59}
60
61func TestGetProjectCheckoutFromUrl_coilcheck(t *testing.T) {
62 git.CommandRunnerImpl = &cmd.FakeCommandRunnerMulti{
63 CommandRunners: []cmd.FakeCommandRunner{
64 {
65 ExpectedDir: "",
66 ExpectedCmd: []string{"git", "init"},
67 },
68 {
69 ExpectedDir: "",
70 ExpectedCmd: []string{"git", "remote", "add", "origin", "localhost"},
71 },
72 {
73 ExpectedDir: "",
74 ExpectedCmd: []string{"git", "fetch", "origin"},
75 },
76 {
77 ExpectedDir: "",
78 ExpectedCmd: []string{"git", "checkout", "master"},
79 },
80 },
81 }
82
83 _, err := getProjectCheckoutFromUrl("localhost", nil)
84
85 if err != nil {
86 t.Error("Error: checkout out project reason: ", err.Error())
87 return
88 }
89}