mugit/internal/git/compare_test.go (view raw)
Oleksandr Smirnov
Oleksandr Smirnov
olexsmir@gmail.com fix: now /compare can compare a branch with a tag, 11 days ago
olexsmir@gmail.com fix: now /compare can compare a branch with a tag, 11 days ago
| 1 | package git |
| 2 | |
| 3 | import ( |
| 4 | "testing" |
| 5 | "time" |
| 6 | |
| 7 | "olexsmir.xyz/x/is" |
| 8 | ) |
| 9 | |
| 10 | func TestRepo_Compare(t *testing.T) { |
| 11 | t.Run("compares two refs", func(t *testing.T) { |
| 12 | r := newTestRepo(t) |
| 13 | base := r.commitFile("README.md", "base\n", "base commit") |
| 14 | r.createBranch("develop", base) |
| 15 | |
| 16 | r.commitFile("master.txt", "master only\n", "master change") |
| 17 | r.checkoutBranch("develop", false) |
| 18 | r.commitFile("develop.txt", "develop only\n", "develop change") |
| 19 | |
| 20 | cmp, err := r.open().Compare("master", "develop") |
| 21 | is.Err(t, err, nil) |
| 22 | is.Equal(t, cmp.BaseRef, "master") |
| 23 | is.Equal(t, cmp.HeadRef, "develop") |
| 24 | is.Equal(t, cmp.Behind, 1) |
| 25 | is.Equal(t, cmp.Ahead, 1) |
| 26 | is.Equal(t, cmp.MergeBase, base.String()) |
| 27 | is.Equal(t, len(cmp.Commits), 1) |
| 28 | is.Equal(t, cmp.Commits[0].Message, "develop change") |
| 29 | is.Equal(t, cmp.Diff.Stat.FilesChanged, 1) |
| 30 | is.Equal(t, cmp.Diff.Diff[0].Name.New, "develop.txt") |
| 31 | }) |
| 32 | |
| 33 | t.Run("returns empty range when refs are equal", func(t *testing.T) { |
| 34 | r := newTestRepo(t) |
| 35 | r.commitFile("README.md", "base\n", "base commit") |
| 36 | |
| 37 | cmp, err := r.open().Compare("master", "master") |
| 38 | is.Err(t, err, nil) |
| 39 | is.Equal(t, cmp.Behind, 0) |
| 40 | is.Equal(t, cmp.Ahead, 0) |
| 41 | is.Equal(t, len(cmp.Commits), 0) |
| 42 | is.Equal(t, cmp.Diff.Stat.FilesChanged, 0) |
| 43 | }) |
| 44 | |
| 45 | t.Run("fails on invalid ref", func(t *testing.T) { |
| 46 | r := newTestRepo(t) |
| 47 | r.commitFile("README.md", "base\n", "base commit") |
| 48 | |
| 49 | _, err := r.open().Compare("master", "does-not-exist") |
| 50 | is.Err(t, err, "resolving head ref") |
| 51 | }) |
| 52 | |
| 53 | t.Run("compares branch with lightweight tag", func(t *testing.T) { |
| 54 | r := newTestRepo(t) |
| 55 | base := r.commitFile("README.md", "base\n", "base commit") |
| 56 | r.createTag("v1.0", base) |
| 57 | r.createBranch("develop", base) |
| 58 | r.checkoutBranch("develop", false) |
| 59 | r.commitFile("develop.txt", "develop only\n", "develop change") |
| 60 | |
| 61 | cmp, err := r.open().Compare("v1.0", "develop") |
| 62 | is.Err(t, err, nil) |
| 63 | is.Equal(t, cmp.BaseRef, "v1.0") |
| 64 | is.Equal(t, cmp.HeadRef, "develop") |
| 65 | is.Equal(t, cmp.Behind, 0) |
| 66 | is.Equal(t, cmp.Ahead, 1) |
| 67 | is.Equal(t, cmp.Diff.Stat.FilesChanged, 1) |
| 68 | }) |
| 69 | |
| 70 | t.Run("compares branch with annotated tag", func(t *testing.T) { |
| 71 | r := newTestRepo(t) |
| 72 | base := r.commitFile("README.md", "base\n", "base commit") |
| 73 | r.createAnnotatedTag("v1.0", "v1 release", base, time.Now()) |
| 74 | r.createBranch("develop", base) |
| 75 | r.checkoutBranch("develop", false) |
| 76 | r.commitFile("develop.txt", "develop only\n", "develop change") |
| 77 | |
| 78 | cmp, err := r.open().Compare("v1.0", "develop") |
| 79 | is.Err(t, err, nil) |
| 80 | is.Equal(t, cmp.BaseRef, "v1.0") |
| 81 | is.Equal(t, cmp.HeadRef, "develop") |
| 82 | is.Equal(t, cmp.Behind, 0) |
| 83 | is.Equal(t, cmp.Ahead, 1) |
| 84 | }) |
| 85 | |
| 86 | t.Run("compares two annotated tags", func(t *testing.T) { |
| 87 | r := newTestRepo(t) |
| 88 | base := r.commitFile("README.md", "base\n", "base commit") |
| 89 | head := r.commitFile("develop.txt", "develop only\n", "develop change") |
| 90 | r.createAnnotatedTag("v1.0", "v1.0", base, time.Now()) |
| 91 | r.createAnnotatedTag("v2.0", "v2.0", head, time.Now()) |
| 92 | |
| 93 | cmp, err := r.open().Compare("v1.0", "v2.0") |
| 94 | is.Err(t, err, nil) |
| 95 | is.Equal(t, cmp.Ahead, 1) |
| 96 | }) |
| 97 | } |