|
| 1 | +package gitrefs |
| 2 | + |
| 3 | +import "testing" |
| 4 | + |
| 5 | +const ( |
| 6 | + commit1 = "a1b2c3d4e5f6" |
| 7 | + commit2 = "f6e5d4c3b2a1" |
| 8 | + commit3 = "1a2b3c4d5e6f" |
| 9 | +) |
| 10 | + |
| 11 | +func Test_latestTag(t *testing.T) { |
| 12 | + tests := []struct { |
| 13 | + name string |
| 14 | + args map[string]string |
| 15 | + wantTag string |
| 16 | + wantHash string |
| 17 | + wantErr bool |
| 18 | + }{ |
| 19 | + {"No refs", map[string]string{}, "", "", true}, |
| 20 | + {"No tags", map[string]string{ |
| 21 | + "refs/heads/master": commit1, |
| 22 | + "refs/heads/v1.0.0": commit2, |
| 23 | + }, "", "", true}, |
| 24 | + {"Single tag", map[string]string{ |
| 25 | + "refs/heads/master": commit1, |
| 26 | + "refs/heads/v1.0.0": commit2, |
| 27 | + "refs/tags/v1.0.0": commit3, |
| 28 | + }, "v1.0.0", commit3, false}, |
| 29 | + {"Multiple tags", map[string]string{ |
| 30 | + "refs/tags/v0.9.9": commit1, |
| 31 | + "refs/tags/v1.0.0": commit2, |
| 32 | + "refs/tags/v1.0.1": commit3, |
| 33 | + }, "v1.0.1", commit3, false}, |
| 34 | + {"Pre-release tags", map[string]string{ |
| 35 | + "refs/tags/v0.9.9": commit1, |
| 36 | + "refs/tags/v1.0.0-rc1": commit2, |
| 37 | + "refs/tags/v1.0.0-alpha7": commit3, |
| 38 | + }, "v0.9.9", commit1, false}, |
| 39 | + {"Equal tags", map[string]string{ |
| 40 | + "refs/tags/v1.0.0": commit1, |
| 41 | + "refs/tags/1.0.0": commit2, |
| 42 | + }, "1.0.0", commit2, false}, |
| 43 | + } |
| 44 | + for _, tt := range tests { |
| 45 | + t.Run(tt.name, func(t *testing.T) { |
| 46 | + tag, hash, err := latestTag(tt.args) |
| 47 | + if (err != nil) != tt.wantErr { |
| 48 | + t.Errorf("latestTag() error = %v, wantErr %v", err, tt.wantErr) |
| 49 | + return |
| 50 | + } |
| 51 | + if tag != tt.wantTag { |
| 52 | + t.Errorf("latestTag() tag = %v, wantTag %v", tag, tt.wantTag) |
| 53 | + } |
| 54 | + if hash != tt.wantHash { |
| 55 | + t.Errorf("latestTag() hash = %v, wantTag %v", hash, tt.wantHash) |
| 56 | + } |
| 57 | + }) |
| 58 | + } |
| 59 | +} |
0 commit comments