github.1git.de/goreleaser/goreleaser@v0.92.0/internal/pipe/changelog/changelog_test.go (about) 1 package changelog 2 3 import ( 4 "io/ioutil" 5 "path/filepath" 6 "testing" 7 8 "github.com/stretchr/testify/assert" 9 10 "github.com/goreleaser/goreleaser/internal/testlib" 11 "github.com/goreleaser/goreleaser/pkg/config" 12 "github.com/goreleaser/goreleaser/pkg/context" 13 ) 14 15 func TestDescription(t *testing.T) { 16 assert.NotEmpty(t, Pipe{}.String()) 17 } 18 19 func TestChangelogProvidedViaFlag(t *testing.T) { 20 var ctx = context.New(config.Project{}) 21 ctx.ReleaseNotes = "c0ff33 foo bar" 22 testlib.AssertSkipped(t, Pipe{}.Run(ctx)) 23 } 24 25 func TestSnapshot(t *testing.T) { 26 var ctx = context.New(config.Project{}) 27 ctx.Snapshot = true 28 testlib.AssertSkipped(t, Pipe{}.Run(ctx)) 29 } 30 31 func TestChangelog(t *testing.T) { 32 folder, back := testlib.Mktmp(t) 33 defer back() 34 testlib.GitInit(t) 35 testlib.GitCommit(t, "first") 36 testlib.GitTag(t, "v0.0.1") 37 testlib.GitCommit(t, "added feature 1") 38 testlib.GitCommit(t, "fixed bug 2") 39 testlib.GitCommit(t, "ignored: whatever") 40 testlib.GitCommit(t, "docs: whatever") 41 testlib.GitCommit(t, "something about cArs we dont need") 42 testlib.GitCommit(t, "feat: added that thing") 43 testlib.GitCommit(t, "Merge pull request #999 from goreleaser/some-branch") 44 testlib.GitCommit(t, "this is not a Merge pull request") 45 testlib.GitTag(t, "v0.0.2") 46 var ctx = context.New(config.Project{ 47 Dist: folder, 48 Changelog: config.Changelog{ 49 Filters: config.Filters{ 50 Exclude: []string{ 51 "docs:", 52 "ignored:", 53 "(?i)cars", 54 "^Merge pull request", 55 }, 56 }, 57 }, 58 }) 59 ctx.Git.CurrentTag = "v0.0.2" 60 assert.NoError(t, Pipe{}.Run(ctx)) 61 assert.Contains(t, ctx.ReleaseNotes, "## Changelog") 62 assert.NotContains(t, ctx.ReleaseNotes, "first") 63 assert.Contains(t, ctx.ReleaseNotes, "added feature 1") 64 assert.Contains(t, ctx.ReleaseNotes, "fixed bug 2") 65 assert.NotContains(t, ctx.ReleaseNotes, "docs") 66 assert.NotContains(t, ctx.ReleaseNotes, "ignored") 67 assert.NotContains(t, ctx.ReleaseNotes, "cArs") 68 assert.NotContains(t, ctx.ReleaseNotes, "from goreleaser/some-branch") 69 70 bts, err := ioutil.ReadFile(filepath.Join(folder, "CHANGELOG.md")) 71 assert.NoError(t, err) 72 assert.NotEmpty(t, string(bts)) 73 } 74 75 func TestChangelogSort(t *testing.T) { 76 _, back := testlib.Mktmp(t) 77 defer back() 78 testlib.GitInit(t) 79 testlib.GitCommit(t, "whatever") 80 testlib.GitTag(t, "v0.9.9") 81 testlib.GitCommit(t, "c: commit") 82 testlib.GitCommit(t, "a: commit") 83 testlib.GitCommit(t, "b: commit") 84 testlib.GitTag(t, "v1.0.0") 85 var ctx = context.New(config.Project{ 86 Changelog: config.Changelog{}, 87 }) 88 ctx.Git.CurrentTag = "v1.0.0" 89 90 for _, cfg := range []struct { 91 Sort string 92 Entries []string 93 }{ 94 { 95 Sort: "", 96 Entries: []string{ 97 "b: commit", 98 "a: commit", 99 "c: commit", 100 }, 101 }, 102 { 103 Sort: "asc", 104 Entries: []string{ 105 "a: commit", 106 "b: commit", 107 "c: commit", 108 }, 109 }, 110 { 111 Sort: "desc", 112 Entries: []string{ 113 "c: commit", 114 "b: commit", 115 "a: commit", 116 }, 117 }, 118 } { 119 t.Run("changelog sort='"+cfg.Sort+"'", func(t *testing.T) { 120 ctx.Config.Changelog.Sort = cfg.Sort 121 entries, err := buildChangelog(ctx) 122 assert.NoError(t, err) 123 assert.Len(t, entries, len(cfg.Entries)) 124 var changes []string 125 for _, line := range entries { 126 _, msg := extractCommitInfo(line) 127 changes = append(changes, msg) 128 } 129 assert.EqualValues(t, cfg.Entries, changes) 130 }) 131 } 132 } 133 134 func TestChangelogInvalidSort(t *testing.T) { 135 var ctx = context.New(config.Project{ 136 Changelog: config.Changelog{ 137 Sort: "dope", 138 }, 139 }) 140 assert.EqualError(t, Pipe{}.Run(ctx), ErrInvalidSortDirection.Error()) 141 } 142 143 func TestChangelogOfFirstRelease(t *testing.T) { 144 _, back := testlib.Mktmp(t) 145 defer back() 146 testlib.GitInit(t) 147 var msgs = []string{ 148 "initial commit", 149 "another one", 150 "one more", 151 "and finally this one", 152 } 153 for _, msg := range msgs { 154 testlib.GitCommit(t, msg) 155 } 156 testlib.GitTag(t, "v0.0.1") 157 var ctx = context.New(config.Project{}) 158 ctx.Git.CurrentTag = "v0.0.1" 159 assert.NoError(t, Pipe{}.Run(ctx)) 160 assert.Contains(t, ctx.ReleaseNotes, "## Changelog") 161 for _, msg := range msgs { 162 assert.Contains(t, ctx.ReleaseNotes, msg) 163 } 164 } 165 166 func TestChangelogFilterInvalidRegex(t *testing.T) { 167 _, back := testlib.Mktmp(t) 168 defer back() 169 testlib.GitInit(t) 170 testlib.GitCommit(t, "commitssss") 171 testlib.GitTag(t, "v0.0.3") 172 testlib.GitCommit(t, "commitzzz") 173 testlib.GitTag(t, "v0.0.4") 174 var ctx = context.New(config.Project{ 175 Changelog: config.Changelog{ 176 Filters: config.Filters{ 177 Exclude: []string{ 178 "(?iasdr4qasd)not a valid regex i guess", 179 }, 180 }, 181 }, 182 }) 183 ctx.Git.CurrentTag = "v0.0.4" 184 assert.EqualError(t, Pipe{}.Run(ctx), "error parsing regexp: invalid or unsupported Perl syntax: `(?ia`") 185 } 186 187 func TestChangelogNoTags(t *testing.T) { 188 _, back := testlib.Mktmp(t) 189 defer back() 190 testlib.GitInit(t) 191 testlib.GitCommit(t, "first") 192 var ctx = context.New(config.Project{}) 193 assert.Error(t, Pipe{}.Run(ctx)) 194 assert.Empty(t, ctx.ReleaseNotes) 195 } 196 197 func TestChangelogOnBranchWithSameNameAsTag(t *testing.T) { 198 _, back := testlib.Mktmp(t) 199 defer back() 200 testlib.GitInit(t) 201 var msgs = []string{ 202 "initial commit", 203 "another one", 204 "one more", 205 "and finally this one", 206 } 207 for _, msg := range msgs { 208 testlib.GitCommit(t, msg) 209 } 210 testlib.GitTag(t, "v0.0.1") 211 testlib.GitCheckoutBranch(t, "v0.0.1") 212 var ctx = context.New(config.Project{}) 213 ctx.Git.CurrentTag = "v0.0.1" 214 assert.NoError(t, Pipe{}.Run(ctx)) 215 assert.Contains(t, ctx.ReleaseNotes, "## Changelog") 216 for _, msg := range msgs { 217 assert.Contains(t, ctx.ReleaseNotes, msg) 218 } 219 }