|
| 1 | +package commitlog |
| 2 | + |
| 3 | +import ( |
| 4 | + "strings" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/go-git/go-git/v5" |
| 8 | + "github.com/go-git/go-git/v5/plumbing" |
| 9 | +) |
| 10 | + |
| 11 | +func TestCommitLogSingleTag(t *testing.T) { |
| 12 | + secondCommit := expectedCommits[1] |
| 13 | + acceptedCommits := expectedCommits[2:] |
| 14 | + |
| 15 | + t.Log("Commits:", expectedCommits) |
| 16 | + t.Log("Tagged:", secondCommit) |
| 17 | + |
| 18 | + hash, err := repo.ResolveRevision(plumbing.Revision(secondCommit)) |
| 19 | + bail(err) |
| 20 | + |
| 21 | + _, err = repo.CreateTag("0.0.0", *hash, &git.CreateTagOptions{ |
| 22 | + Message: "0.0.0", |
| 23 | + }) |
| 24 | + bail(err) |
| 25 | + |
| 26 | + log, _ := CommitLog(repo, "", "", SupportedKeys, true) |
| 27 | + if log == "" { |
| 28 | + t.Fail() |
| 29 | + } |
| 30 | + |
| 31 | + if strings.Contains(log, expectedCommits[0]) { |
| 32 | + t.Fail() |
| 33 | + } |
| 34 | + |
| 35 | + for _, commit := range acceptedCommits { |
| 36 | + if !strings.Contains(log, commit) { |
| 37 | + t.Fail() |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + t.Log(log) |
| 42 | + |
| 43 | + // clean-up |
| 44 | + bail(repo.DeleteTag("0.0.0")) |
| 45 | + |
| 46 | +} |
| 47 | + |
| 48 | +// Test with 2 tags, one on the second commit and one on the 2nd last commit, |
| 49 | +// should only have the last commit in the log |
| 50 | +func TestCommitLogDualTag(t *testing.T) { |
| 51 | + secondCommit := expectedCommits[1] |
| 52 | + secondLastCommit := expectedCommits[len(expectedCommits)-2] |
| 53 | + acceptedCommit := expectedCommits[len(expectedCommits)-1] |
| 54 | + |
| 55 | + t.Log("Commits:", expectedCommits) |
| 56 | + t.Log("Tagged:", secondCommit, secondLastCommit) |
| 57 | + |
| 58 | + secondHash, err := repo.ResolveRevision(plumbing.Revision(secondCommit)) |
| 59 | + bail(err) |
| 60 | + |
| 61 | + secondLastHash, err := repo.ResolveRevision(plumbing.Revision(secondLastCommit)) |
| 62 | + bail(err) |
| 63 | + |
| 64 | + _, err = repo.CreateTag("0.0.0", *secondHash, &git.CreateTagOptions{ |
| 65 | + Message: "0.0.0", |
| 66 | + }) |
| 67 | + bail(err) |
| 68 | + |
| 69 | + _, err = repo.CreateTag("0.0.1", *secondLastHash, &git.CreateTagOptions{ |
| 70 | + Message: "0.0.1", |
| 71 | + }) |
| 72 | + bail(err) |
| 73 | + |
| 74 | + log, _ := CommitLog(repo, "", "", SupportedKeys, true) |
| 75 | + if log == "" { |
| 76 | + t.Fail() |
| 77 | + } |
| 78 | + |
| 79 | + for _, commit := range expectedCommits { |
| 80 | + if commit == acceptedCommit { |
| 81 | + if !strings.Contains(log, acceptedCommit) { |
| 82 | + t.Fail() |
| 83 | + } |
| 84 | + } else { |
| 85 | + if strings.Contains(log, commit) { |
| 86 | + t.Fail() |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + } |
| 91 | + |
| 92 | + t.Log(log) |
| 93 | + |
| 94 | + // clean-up |
| 95 | + bail(repo.DeleteTag("0.0.0")) |
| 96 | + bail(repo.DeleteTag("0.0.1")) |
| 97 | +} |
| 98 | + |
| 99 | +// Test with 2 tags, one on the second commit and one on the last commit, |
| 100 | +// should give all commits till the 1st tag |
| 101 | +func TestCommitLogHeadTag(t *testing.T) { |
| 102 | + secondCommit := expectedCommits[1] |
| 103 | + lastCommit := expectedCommits[len(expectedCommits)-1] |
| 104 | + |
| 105 | + t.Log("Commits:", expectedCommits) |
| 106 | + t.Log("Tagged:", secondCommit, lastCommit) |
| 107 | + |
| 108 | + secondHash, err := repo.ResolveRevision(plumbing.Revision(secondCommit)) |
| 109 | + bail(err) |
| 110 | + |
| 111 | + lastHash, err := repo.ResolveRevision(plumbing.Revision(lastCommit)) |
| 112 | + bail(err) |
| 113 | + |
| 114 | + _, err = repo.CreateTag("0.0.0", *secondHash, &git.CreateTagOptions{ |
| 115 | + Message: "0.0.0", |
| 116 | + }) |
| 117 | + bail(err) |
| 118 | + |
| 119 | + _, err = repo.CreateTag("0.0.1", *lastHash, &git.CreateTagOptions{ |
| 120 | + Message: "0.0.1", |
| 121 | + }) |
| 122 | + bail(err) |
| 123 | + |
| 124 | + log, _ := CommitLog(repo, "", "", SupportedKeys, true) |
| 125 | + if log == "" { |
| 126 | + t.Fail() |
| 127 | + } |
| 128 | + |
| 129 | + for index, commit := range expectedCommits { |
| 130 | + if index <= 1 { |
| 131 | + if strings.Contains(log, commit) { |
| 132 | + t.Fail() |
| 133 | + } |
| 134 | + } |
| 135 | + if index > 1 && !strings.Contains(log, commit) { |
| 136 | + t.Fail() |
| 137 | + } |
| 138 | + |
| 139 | + } |
| 140 | + |
| 141 | + t.Log(log) |
| 142 | + |
| 143 | + // clean-up |
| 144 | + bail(repo.DeleteTag("0.0.0")) |
| 145 | + bail(repo.DeleteTag("0.0.1")) |
| 146 | +} |
0 commit comments