| 1 | package git |
| 2 | |
| 3 | import ( |
| 4 | "testing" |
| 5 | |
| 6 | "github.com/stretchr/testify/assert" |
| 7 | ) |
| 8 | |
| 9 | func TestRefUpdateDefault(t *testing.T) { |
| 10 | pushModes := []string{"simple", ""} |
| 11 | for _, pushMode := range pushModes { |
| 12 | env := newEnv(map[string][]string{ |
| 13 | "push.default": []string{pushMode}, |
| 14 | "branch.local.remote": []string{"ignore"}, |
| 15 | "branch.local.merge": []string{"me"}, |
| 16 | }) |
| 17 | |
| 18 | u := NewRefUpdate(env, "origin", ParseRef("refs/heads/local", ""), nil) |
| 19 | assert.Equal(t, "local", u.RemoteRef().Name, "pushmode=%q", pushMode) |
| 20 | assert.Equal(t, RefTypeLocalBranch, u.RemoteRef().Type, "pushmode=%q", pushMode) |
| 21 | } |
| 22 | } |
| 23 | |
| 24 | func TestRefUpdateTrackedDefault(t *testing.T) { |
| 25 | pushModes := []string{"simple", "upstream", "tracking", ""} |
| 26 | for _, pushMode := range pushModes { |
| 27 | env := newEnv(map[string][]string{ |
| 28 | "push.default": []string{pushMode}, |
| 29 | "branch.local.remote": []string{"origin"}, |
| 30 | "branch.local.merge": []string{"refs/heads/tracked"}, |
| 31 | }) |
| 32 | |
| 33 | u := NewRefUpdate(env, "origin", ParseRef("refs/heads/local", ""), nil) |
| 34 | assert.Equal(t, "tracked", u.RemoteRef().Name, "pushmode=%s", pushMode) |
| 35 | assert.Equal(t, RefTypeLocalBranch, u.RemoteRef().Type, "pushmode=%q", pushMode) |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | func TestRefUpdateCurrentDefault(t *testing.T) { |
| 40 | env := newEnv(map[string][]string{ |
| 41 | "push.default": []string{"current"}, |
| 42 | "branch.local.remote": []string{"origin"}, |
| 43 | "branch.local.merge": []string{"tracked"}, |
| 44 | }) |
| 45 | |
| 46 | u := NewRefUpdate(env, "origin", ParseRef("refs/heads/local", ""), nil) |
| 47 | assert.Equal(t, "local", u.RemoteRef().Name) |
| 48 | assert.Equal(t, RefTypeLocalBranch, u.RemoteRef().Type) |
| 49 | } |
| 50 | |
| 51 | func TestRefUpdateExplicitLocalAndRemoteRefs(t *testing.T) { |
| 52 | u := NewRefUpdate(nil, "", ParseRef("refs/heads/local", "abc123"), ParseRef("refs/heads/remote", "def456")) |
| 53 | assert.Equal(t, "local", u.LocalRef().Name) |
| 54 | assert.Equal(t, "abc123", u.LocalRef().Sha) |
| 55 | assert.Equal(t, "abc123", u.LocalRefCommitish()) |
| 56 | assert.Equal(t, "remote", u.RemoteRef().Name) |
| 57 | assert.Equal(t, "def456", u.RemoteRef().Sha) |
| 58 | assert.Equal(t, "def456", u.RemoteRefCommitish()) |
| 59 | |
| 60 | u = NewRefUpdate(nil, "", ParseRef("refs/heads/local", ""), ParseRef("refs/heads/remote", "")) |
| 61 | assert.Equal(t, "local", u.LocalRef().Name) |
| 62 | assert.Equal(t, "", u.LocalRef().Sha) |
| 63 | assert.Equal(t, "local", u.LocalRefCommitish()) |
| 64 | assert.Equal(t, "remote", u.RemoteRef().Name) |
| 65 | assert.Equal(t, "", u.RemoteRef().Sha) |
| 66 | assert.Equal(t, "remote", u.RemoteRefCommitish()) |
| 67 | } |
| 68 | |
| 69 | func newEnv(m map[string][]string) *mapEnv { |
| 70 | return &mapEnv{data: m} |
| 71 | } |
| 72 | |
| 73 | type mapEnv struct { |
| 74 | data map[string][]string |
| 75 | } |
| 76 | |
| 77 | func (m *mapEnv) Get(key string) (string, bool) { |
| 78 | vals, ok := m.data[key] |
| 79 | if ok && len(vals) > 0 { |
| 80 | return vals[0], true |
| 81 | } |
| 82 | return "", false |
| 83 | } |