github.com/fredbi/git-chglog@v0.0.0-20190706071416-d35c598eac81/chglog_test.go (about) 1 package chglog 2 3 import ( 4 "bytes" 5 "os" 6 "path/filepath" 7 "strings" 8 "testing" 9 "time" 10 11 "github.com/stretchr/testify/assert" 12 gitcmd "github.com/tsuyoshiwada/go-gitcmd" 13 ) 14 15 var ( 16 cwd string 17 testRepoRoot = ".tmp" 18 internalTimeFormat = "2006-01-02 15:04:05" 19 ) 20 21 type commitFunc = func(date, subject, body string) 22 type tagFunc = func(name string) 23 24 func TestMain(m *testing.M) { 25 cwd, _ = os.Getwd() 26 cleanup() 27 code := m.Run() 28 cleanup() 29 os.Exit(code) 30 } 31 32 func setup(dir string, setupRepo func(commitFunc, tagFunc, gitcmd.Client)) { 33 testDir := filepath.Join(cwd, testRepoRoot, dir) 34 35 os.RemoveAll(testDir) 36 os.MkdirAll(testDir, os.ModePerm) 37 os.Chdir(testDir) 38 39 loc, _ := time.LoadLocation("UTC") 40 time.Local = loc 41 42 git := gitcmd.New(nil) 43 git.Exec("init") 44 git.Exec("config", "user.name", "test_user") 45 git.Exec("config", "user.email", "test@example.com") 46 47 var commit = func(date, subject, body string) { 48 msg := subject 49 if body != "" { 50 msg += "\n\n" + body 51 } 52 t, _ := time.Parse(internalTimeFormat, date) 53 d := t.Format("Mon Jan 2 15:04:05 2006 +0000") 54 git.Exec("commit", "--allow-empty", "--date", d, "-m", msg) 55 } 56 57 var tag = func(name string) { 58 git.Exec("tag", name) 59 } 60 61 setupRepo(commit, tag, git) 62 63 os.Chdir(cwd) 64 } 65 66 func cleanup() { 67 os.Chdir(cwd) 68 os.RemoveAll(filepath.Join(cwd, testRepoRoot)) 69 } 70 71 func TestGeneratorNotFoundTags(t *testing.T) { 72 assert := assert.New(t) 73 testName := "not_found" 74 75 setup(testName, func(commit commitFunc, _ tagFunc, _ gitcmd.Client) { 76 commit("2018-01-01 00:00:00", "feat(*): New feature", "") 77 }) 78 79 gen := NewGenerator(&Config{ 80 Bin: "git", 81 WorkingDir: filepath.Join(testRepoRoot, testName), 82 Template: filepath.Join(cwd, "testdata", testName+".md"), 83 Info: &Info{ 84 RepositoryURL: "https://github.com/git-chglog/git-chglog", 85 }, 86 Options: &Options{}, 87 }) 88 89 buf := &bytes.Buffer{} 90 err := gen.Generate(buf, "") 91 assert.Error(err) 92 assert.Contains(err.Error(), "git-tag does not exist") 93 assert.Equal("", buf.String()) 94 } 95 96 func TestGeneratorNotFoundCommits(t *testing.T) { 97 assert := assert.New(t) 98 testName := "not_found" 99 100 setup(testName, func(commit commitFunc, tag tagFunc, _ gitcmd.Client) { 101 commit("2018-01-01 00:00:00", "feat(*): New feature", "") 102 tag("1.0.0") 103 }) 104 105 gen := NewGenerator(&Config{ 106 Bin: "git", 107 WorkingDir: filepath.Join(testRepoRoot, testName), 108 Template: filepath.Join(cwd, "testdata", testName+".md"), 109 Info: &Info{ 110 RepositoryURL: "https://github.com/git-chglog/git-chglog", 111 }, 112 Options: &Options{}, 113 }) 114 115 buf := &bytes.Buffer{} 116 err := gen.Generate(buf, "foo") 117 assert.Error(err) 118 assert.Equal("", buf.String()) 119 } 120 121 func TestGeneratorNotFoundCommitsOne(t *testing.T) { 122 assert := assert.New(t) 123 testName := "not_found" 124 125 setup(testName, func(commit commitFunc, tag tagFunc, _ gitcmd.Client) { 126 commit("2018-01-01 00:00:00", "chore(*): First commit", "") 127 tag("1.0.0") 128 }) 129 130 gen := NewGenerator(&Config{ 131 Bin: "git", 132 WorkingDir: filepath.Join(testRepoRoot, testName), 133 Template: filepath.Join(cwd, "testdata", testName+".md"), 134 Info: &Info{ 135 RepositoryURL: "https://github.com/git-chglog/git-chglog", 136 }, 137 Options: &Options{ 138 CommitFilters: map[string][]string{}, 139 CommitSortBy: "Scope", 140 CommitGroupBy: "Type", 141 CommitGroupSortBy: "Title", 142 CommitGroupTitleMaps: map[string]string{}, 143 HeaderPattern: "^(\\w*)(?:\\(([\\w\\$\\.\\-\\*\\s]*)\\))?\\:\\s(.*)$", 144 HeaderPatternMaps: []string{ 145 "Type", 146 "Scope", 147 "Subject", 148 }, 149 IssuePrefix: []string{ 150 "#", 151 "gh-", 152 }, 153 RefActions: []string{}, 154 MergePattern: "^Merge pull request #(\\d+) from (.*)$", 155 MergePatternMaps: []string{ 156 "Ref", 157 "Source", 158 }, 159 RevertPattern: "^Revert \"([\\s\\S]*)\"$", 160 RevertPatternMaps: []string{ 161 "Header", 162 }, 163 NoteKeywords: []string{ 164 "BREAKING CHANGE", 165 }, 166 }, 167 }) 168 169 buf := &bytes.Buffer{} 170 err := gen.Generate(buf, "foo") 171 assert.Error(err) 172 assert.Contains(err.Error(), "\"foo\" was not found") 173 assert.Equal("", buf.String()) 174 } 175 176 func TestGeneratorWithTypeScopeSubject(t *testing.T) { 177 assert := assert.New(t) 178 testName := "type_scope_subject" 179 180 setup(testName, func(commit commitFunc, tag tagFunc, _ gitcmd.Client) { 181 commit("2018-01-01 00:00:00", "chore(*): First commit", "") 182 commit("2018-01-01 00:01:00", "feat(core): Add foo bar", "") 183 commit("2018-01-01 00:02:00", "docs(readme): Update usage #123", "") 184 tag("1.0.0") 185 186 commit("2018-01-02 00:00:00", "feat(parser): New some super options #333", "") 187 commit("2018-01-02 00:01:00", "Merge pull request #999 from tsuyoshiwada/patch-1", "") 188 commit("2018-01-02 00:02:00", "Merge pull request #1000 from tsuyoshiwada/patch-1", "") 189 commit("2018-01-02 00:03:00", "Revert \"feat(core): Add foo bar @mention and issue #987\"", "") 190 tag("1.1.0") 191 192 commit("2018-01-03 00:00:00", "feat(context): Online breaking change", "BREAKING CHANGE: Online breaking change message.") 193 commit("2018-01-03 00:01:00", "feat(router): Muliple breaking change", `This is body, 194 195 BREAKING CHANGE: 196 Multiple 197 breaking 198 change message.`) 199 tag("2.0.0-beta.0") 200 201 commit("2018-01-04 00:00:00", "refactor(context): gofmt", "") 202 commit("2018-01-04 00:01:00", "fix(core): Fix commit\n\nThis is body message.", "") 203 }) 204 205 gen := NewGenerator(&Config{ 206 Bin: "git", 207 WorkingDir: filepath.Join(testRepoRoot, testName), 208 Template: filepath.Join(cwd, "testdata", testName+".md"), 209 Info: &Info{ 210 Title: "CHANGELOG Example", 211 RepositoryURL: "https://github.com/git-chglog/git-chglog", 212 }, 213 Options: &Options{ 214 CommitFilters: map[string][]string{ 215 "Type": []string{ 216 "feat", 217 "fix", 218 }, 219 }, 220 CommitSortBy: "Scope", 221 CommitGroupBy: "Type", 222 CommitGroupSortBy: "Title", 223 CommitGroupTitleMaps: map[string]string{ 224 "feat": "Features", 225 "fix": "Bug Fixes", 226 }, 227 HeaderPattern: "^(\\w*)(?:\\(([\\w\\$\\.\\-\\*\\s]*)\\))?\\:\\s(.*)$", 228 HeaderPatternMaps: []string{ 229 "Type", 230 "Scope", 231 "Subject", 232 }, 233 IssuePrefix: []string{ 234 "#", 235 "gh-", 236 }, 237 RefActions: []string{}, 238 MergePattern: "^Merge pull request #(\\d+) from (.*)$", 239 MergePatternMaps: []string{ 240 "Ref", 241 "Source", 242 }, 243 RevertPattern: "^Revert \"([\\s\\S]*)\"$", 244 RevertPatternMaps: []string{ 245 "Header", 246 }, 247 NoteKeywords: []string{ 248 "BREAKING CHANGE", 249 }, 250 }, 251 }) 252 253 buf := &bytes.Buffer{} 254 err := gen.Generate(buf, "") 255 256 assert.Nil(err) 257 assert.Equal(`<a name="unreleased"></a> 258 ## [Unreleased] 259 260 ### Bug Fixes 261 - **core:** Fix commit 262 263 264 <a name="2.0.0-beta.0"></a> 265 ## [2.0.0-beta.0] - 2018-01-03 266 ### Features 267 - **context:** Online breaking change 268 - **router:** Muliple breaking change 269 270 ### BREAKING CHANGE 271 272 Multiple 273 breaking 274 change message. 275 276 Online breaking change message. 277 278 279 <a name="1.1.0"></a> 280 ## [1.1.0] - 2018-01-02 281 ### Features 282 - **parser:** New some super options #333 283 284 ### Reverts 285 - feat(core): Add foo bar @mention and issue #987 286 287 ### Pull Requests 288 - Merge pull request #1000 from tsuyoshiwada/patch-1 289 - Merge pull request #999 from tsuyoshiwada/patch-1 290 291 292 <a name="1.0.0"></a> 293 ## 1.0.0 - 2018-01-01 294 ### Features 295 - **core:** Add foo bar 296 297 298 [Unreleased]: https://github.com/git-chglog/git-chglog/compare/2.0.0-beta.0...HEAD 299 [2.0.0-beta.0]: https://github.com/git-chglog/git-chglog/compare/1.1.0...2.0.0-beta.0 300 [1.1.0]: https://github.com/git-chglog/git-chglog/compare/1.0.0...1.1.0`, strings.TrimSpace(buf.String())) 301 } 302 303 func TestGeneratorWithNextTag(t *testing.T) { 304 assert := assert.New(t) 305 testName := "type_scope_subject" 306 307 setup(testName, func(commit commitFunc, tag tagFunc, _ gitcmd.Client) { 308 commit("2018-01-01 00:00:00", "feat(core): version 1.0.0", "") 309 tag("1.0.0") 310 311 commit("2018-02-01 00:00:00", "feat(core): version 2.0.0", "") 312 tag("2.0.0") 313 314 commit("2018-03-01 00:00:00", "feat(core): version 3.0.0", "") 315 }) 316 317 gen := NewGenerator(&Config{ 318 Bin: "git", 319 WorkingDir: filepath.Join(testRepoRoot, testName), 320 Template: filepath.Join(cwd, "testdata", testName+".md"), 321 Info: &Info{ 322 Title: "CHANGELOG Example", 323 RepositoryURL: "https://github.com/git-chglog/git-chglog", 324 }, 325 Options: &Options{ 326 NextTag: "3.0.0", 327 CommitFilters: map[string][]string{ 328 "Type": []string{ 329 "feat", 330 }, 331 }, 332 CommitSortBy: "Scope", 333 CommitGroupBy: "Type", 334 CommitGroupSortBy: "Title", 335 CommitGroupTitleMaps: map[string]string{ 336 "feat": "Features", 337 }, 338 HeaderPattern: "^(\\w*)(?:\\(([\\w\\$\\.\\-\\*\\s]*)\\))?\\:\\s(.*)$", 339 HeaderPatternMaps: []string{ 340 "Type", 341 "Scope", 342 "Subject", 343 }, 344 }, 345 }) 346 347 buf := &bytes.Buffer{} 348 err := gen.Generate(buf, "") 349 350 assert.Nil(err) 351 assert.Equal(`<a name="unreleased"></a> 352 ## [Unreleased] 353 354 355 <a name="3.0.0"></a> 356 ## [3.0.0] - 2018-03-01 357 ### Features 358 - **core:** version 3.0.0 359 360 361 <a name="2.0.0"></a> 362 ## [2.0.0] - 2018-02-01 363 ### Features 364 - **core:** version 2.0.0 365 366 367 <a name="1.0.0"></a> 368 ## 1.0.0 - 2018-01-01 369 ### Features 370 - **core:** version 1.0.0 371 372 373 [Unreleased]: https://github.com/git-chglog/git-chglog/compare/3.0.0...HEAD 374 [3.0.0]: https://github.com/git-chglog/git-chglog/compare/2.0.0...3.0.0 375 [2.0.0]: https://github.com/git-chglog/git-chglog/compare/1.0.0...2.0.0`, strings.TrimSpace(buf.String())) 376 377 buf = &bytes.Buffer{} 378 err = gen.Generate(buf, "3.0.0") 379 380 assert.Nil(err) 381 assert.Equal(`<a name="unreleased"></a> 382 ## [Unreleased] 383 384 385 <a name="3.0.0"></a> 386 ## [3.0.0] - 2018-03-01 387 ### Features 388 - **core:** version 3.0.0 389 390 391 [Unreleased]: https://github.com/git-chglog/git-chglog/compare/3.0.0...HEAD 392 [3.0.0]: https://github.com/git-chglog/git-chglog/compare/2.0.0...3.0.0`, strings.TrimSpace(buf.String())) 393 }