|
1 | 1 | package generator |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "strings" |
5 | 4 | "testing" |
| 5 | + "text/template" |
6 | 6 |
|
7 | 7 | "github.com/go-semantic-release/semantic-release/v2/pkg/generator" |
8 | 8 | "github.com/go-semantic-release/semantic-release/v2/pkg/semrel" |
| 9 | + "github.com/stretchr/testify/require" |
9 | 10 | ) |
10 | 11 |
|
| 12 | +var testCommits = []*semrel.Commit{ |
| 13 | + {}, |
| 14 | + { |
| 15 | + SHA: "123456789", Type: "feat", Scope: "app", Message: "commit message", |
| 16 | + Annotations: map[string]string{"author_login": "test"}, |
| 17 | + }, |
| 18 | + { |
| 19 | + SHA: "deadbeef", Type: "fix", Scope: "", Message: "commit message", |
| 20 | + Annotations: map[string]string{"author_login": "test"}, |
| 21 | + }, |
| 22 | + { |
| 23 | + SHA: "87654321", Type: "ci", Scope: "", Message: "commit message", |
| 24 | + Annotations: map[string]string{"author_login": "test"}, |
| 25 | + }, |
| 26 | + { |
| 27 | + SHA: "43218765", Type: "build", Scope: "", Message: "commit message", |
| 28 | + Annotations: map[string]string{"author_login": "test"}, |
| 29 | + }, |
| 30 | + { |
| 31 | + SHA: "12345678", Type: "yolo", Scope: "swag", Message: "commit message", |
| 32 | + }, |
| 33 | + { |
| 34 | + SHA: "12345678", Type: "chore", Scope: "", Message: "commit message", |
| 35 | + Raw: []string{"", "BREAKING CHANGE: test"}, |
| 36 | + Change: &semrel.Change{Major: true}, |
| 37 | + Annotations: map[string]string{"author_login": "test"}, |
| 38 | + }, |
| 39 | + { |
| 40 | + SHA: "12345679", Type: "chore!", Scope: "user", Message: "another commit message", |
| 41 | + Raw: []string{"another commit message", "changed ID int into UUID"}, |
| 42 | + Change: &semrel.Change{Major: true}, |
| 43 | + }, |
| 44 | + { |
| 45 | + SHA: "stop", Type: "chore", Scope: "", Message: "not included", |
| 46 | + }, |
| 47 | +} |
| 48 | + |
| 49 | +var testChangelogConfig = &generator.ChangelogGeneratorConfig{ |
| 50 | + Commits: testCommits, |
| 51 | + LatestRelease: &semrel.Release{SHA: "stop"}, |
| 52 | + NewVersion: "2.0.0", |
| 53 | +} |
| 54 | + |
11 | 55 | func TestDefaultGenerator(t *testing.T) { |
12 | | - changelogConfig := &generator.ChangelogGeneratorConfig{} |
13 | | - changelogConfig.Commits = []*semrel.Commit{ |
14 | | - {}, |
15 | | - {SHA: "123456789", Type: "feat", Scope: "app", Message: "commit message"}, |
16 | | - {SHA: "abcd", Type: "fix", Scope: "", Message: "commit message"}, |
17 | | - {SHA: "87654321", Type: "ci", Scope: "", Message: "commit message"}, |
18 | | - {SHA: "43218765", Type: "build", Scope: "", Message: "commit message"}, |
19 | | - {SHA: "12345678", Type: "yolo", Scope: "swag", Message: "commit message"}, |
20 | | - {SHA: "12345678", Type: "chore", Scope: "", Message: "commit message", Raw: []string{"", "BREAKING CHANGE: test"}, Change: &semrel.Change{Major: true}}, |
21 | | - {SHA: "12345679", Type: "chore!", Scope: "user", Message: "another commit message", Raw: []string{"another commit message", "changed ID int into UUID"}, Change: &semrel.Change{Major: true}}, |
22 | | - {SHA: "stop", Type: "chore", Scope: "", Message: "not included"}, |
23 | | - } |
24 | | - changelogConfig.LatestRelease = &semrel.Release{SHA: "stop"} |
25 | | - changelogConfig.NewVersion = "2.0.0" |
26 | | - generator := &DefaultChangelogGenerator{} |
27 | | - changelog := generator.Generate(changelogConfig) |
28 | | - if !strings.Contains(changelog, "* **app:** commit message (12345678)") || |
29 | | - !strings.Contains(changelog, "* commit message (abcd)") || |
30 | | - !strings.Contains(changelog, "#### yolo") || |
31 | | - !strings.Contains(changelog, "#### Build") || |
32 | | - !strings.Contains(changelog, "#### CI") || |
33 | | - !strings.Contains(changelog, "```\nBREAKING CHANGE: test\n```") || |
34 | | - strings.Contains(changelog, "not included") { |
35 | | - t.Fail() |
36 | | - } |
| 56 | + clGen := &DefaultChangelogGenerator{} |
| 57 | + require.NoError(t, clGen.Init(map[string]string{})) |
| 58 | + changelog := clGen.Generate(testChangelogConfig) |
| 59 | + |
| 60 | + require.Contains(t, changelog, "* **app:** commit message (12345678)") |
| 61 | + require.Contains(t, changelog, "* commit message (deadbeef)") |
| 62 | + require.Contains(t, changelog, "#### yolo") |
| 63 | + require.Contains(t, changelog, "#### Build") |
| 64 | + require.Contains(t, changelog, "#### CI") |
| 65 | + require.Contains(t, changelog, "```\nBREAKING CHANGE: test\n```") |
| 66 | + require.NotContains(t, changelog, "not included") |
37 | 67 | } |
38 | 68 |
|
39 | 69 | func TestEmojiGenerator(t *testing.T) { |
40 | | - changelogConfig := &generator.ChangelogGeneratorConfig{} |
41 | | - changelogConfig.Commits = []*semrel.Commit{ |
42 | | - {}, |
43 | | - {SHA: "123456789", Type: "feat", Scope: "app", Message: "commit message"}, |
44 | | - {SHA: "abcd", Type: "fix", Scope: "", Message: "commit message"}, |
45 | | - {SHA: "87654321", Type: "ci", Scope: "", Message: "commit message"}, |
46 | | - {SHA: "43218765", Type: "build", Scope: "", Message: "commit message"}, |
47 | | - {SHA: "12345678", Type: "yolo", Scope: "swag", Message: "commit message"}, |
48 | | - {SHA: "12345678", Type: "chore", Scope: "", Message: "commit message", Raw: []string{"", "BREAKING CHANGE: test"}, Change: &semrel.Change{Major: true}}, |
49 | | - {SHA: "12345679", Type: "chore!", Scope: "user", Message: "another commit message", Raw: []string{"another commit message", "changed ID int into UUID"}, Change: &semrel.Change{Major: true}}, |
50 | | - {SHA: "stop", Type: "chore", Scope: "", Message: "not included"}, |
| 70 | + clGen := &DefaultChangelogGenerator{} |
| 71 | + require.NoError(t, clGen.Init(map[string]string{"emojis": "true"})) |
| 72 | + changelog := clGen.Generate(testChangelogConfig) |
| 73 | + |
| 74 | + require.Contains(t, changelog, "* **app:** commit message (12345678)") |
| 75 | + require.Contains(t, changelog, "* commit message (deadbeef)") |
| 76 | + require.Contains(t, changelog, "#### 🎁 Feature") |
| 77 | + require.Contains(t, changelog, "#### 🐞 Bug Fixes") |
| 78 | + require.Contains(t, changelog, "#### 🔁 CI") |
| 79 | + require.Contains(t, changelog, "#### 📦 Build") |
| 80 | + require.Contains(t, changelog, "#### 📣 Breaking Changes") |
| 81 | + require.Contains(t, changelog, "#### yolo") |
| 82 | + require.Contains(t, changelog, "```\nBREAKING CHANGE: test\n```") |
| 83 | + require.NotContains(t, changelog, "not included") |
| 84 | +} |
| 85 | + |
| 86 | +func TestFormatCommit(t *testing.T) { |
| 87 | + testCases := []struct { |
| 88 | + tpl string |
| 89 | + commit *semrel.Commit |
| 90 | + expectedOutput string |
| 91 | + }{ |
| 92 | + { |
| 93 | + tpl: defaultFormatCommitTemplateStr, |
| 94 | + commit: &semrel.Commit{SHA: "123456789", Type: "feat", Scope: "", Message: "commit message"}, |
| 95 | + expectedOutput: "* commit message (12345678)", |
| 96 | + }, |
| 97 | + { |
| 98 | + tpl: defaultFormatCommitTemplateStr, |
| 99 | + commit: &semrel.Commit{SHA: "123", Type: "feat", Scope: "app", Message: "commit message"}, |
| 100 | + expectedOutput: "* **app:** commit message (123)", |
| 101 | + }, |
| 102 | + { |
| 103 | + tpl: `* {{.SHA}} - {{.Message}} {{- with index .Annotations "author_login" }} [by @{{.}}] {{- end}}`, |
| 104 | + commit: &semrel.Commit{SHA: "deadbeef", Type: "fix", Message: "custom template", Annotations: map[string]string{"author_login": "test"}}, |
| 105 | + expectedOutput: "* deadbeef - custom template [by @test]", |
| 106 | + }, |
51 | 107 | } |
52 | | - changelogConfig.LatestRelease = &semrel.Release{SHA: "stop"} |
53 | | - changelogConfig.NewVersion = "2.0.0" |
54 | | - generator := &DefaultChangelogGenerator{emojis: true} |
55 | | - changelog := generator.Generate(changelogConfig) |
56 | | - if !strings.Contains(changelog, "* **app:** commit message (12345678)") || |
57 | | - !strings.Contains(changelog, "* commit message (abcd)") || |
58 | | - !strings.Contains(changelog, "#### 🎁 Feature") || |
59 | | - !strings.Contains(changelog, "#### 🐞 Bug Fixes") || |
60 | | - !strings.Contains(changelog, "#### 🔁 CI") || |
61 | | - !strings.Contains(changelog, "#### 📦 Build") || |
62 | | - !strings.Contains(changelog, "#### 📣 Breaking Changes") || |
63 | | - !strings.Contains(changelog, "#### yolo") || |
64 | | - !strings.Contains(changelog, "```\nBREAKING CHANGE: test\n```") || |
65 | | - strings.Contains(changelog, "not included") { |
66 | | - t.Fail() |
| 108 | + for _, tc := range testCases { |
| 109 | + t.Run(tc.expectedOutput, func(t *testing.T) { |
| 110 | + tpl := template.Must(template.New("test").Funcs(templateFuncMap).Parse(tc.tpl)) |
| 111 | + output := formatCommit(tpl, tc.commit) |
| 112 | + require.Equal(t, tc.expectedOutput, output) |
| 113 | + }) |
67 | 114 | } |
68 | 115 | } |
| 116 | + |
| 117 | +func TestFormatCommitWithCustomTemplate(t *testing.T) { |
| 118 | + clGen := &DefaultChangelogGenerator{} |
| 119 | + require.NoError(t, clGen.Init(map[string]string{ |
| 120 | + "format_commit_template": "* `{{ trimSHA .SHA}}` - {{.Message}} {{- with index .Annotations \"author_login\" }} [by @{{.}}] {{- end}}", |
| 121 | + })) |
| 122 | + changelog := clGen.Generate(testChangelogConfig) |
| 123 | + require.Contains(t, changelog, "* `12345678` - commit message [by @test]") |
| 124 | + require.NotContains(t, changelog, "* `deadbeef` - commit message (deadbeef) [by @test]") |
| 125 | +} |
0 commit comments