github.com/arsham/gitrelease@v0.3.2-0.20221207124258-6867180b2c2d/commit/commit_test.go (about) 1 package commit_test 2 3 import ( 4 "fmt" 5 "sort" 6 "strings" 7 "testing" 8 9 "github.com/arsham/gitrelease/commit" 10 "github.com/blokur/testament" 11 "github.com/google/go-cmp/cmp" 12 ) 13 14 func TestGroupFromCommit(t *testing.T) { 15 t.Parallel() 16 additional := `\n\n` + testament.RandomString(50) 17 tcs := map[string]struct { 18 line string 19 want commit.Group 20 }{ 21 "not special": { 22 line: "something", 23 want: commit.NewGroup("Misc", "", "something", false), 24 }, 25 "not special titled": { 26 line: "Something", 27 want: commit.NewGroup("Misc", "", "Something", false), 28 }, 29 "simply topic": { 30 line: "fix something", 31 want: commit.NewGroup("Fix", "", "something", false), 32 }, 33 "simply topic multi": { 34 line: "fix something" + additional, 35 want: commit.NewGroup("Fix", "", "something"+additional, false), 36 }, 37 "simply topic titled": { 38 line: "Fix Something", 39 want: commit.NewGroup("Fix", "", "Something", false), 40 }, 41 "topic section": { 42 line: "Fix(repo) something", 43 want: commit.NewGroup("Fix", "repo", "something", false), 44 }, 45 "topic section multi": { 46 line: "Fix(repo) something" + additional, 47 want: commit.NewGroup("Fix", "repo", "something"+additional, false), 48 }, 49 "topic section colon": { 50 line: "Fix(repo): something", 51 want: commit.NewGroup("Fix", "repo", "something", false), 52 }, 53 "topic section colon multi": { 54 line: "Fix(repo): something" + additional, 55 want: commit.NewGroup("Fix", "repo", "something"+additional, false), 56 }, 57 "ref": {line: "ref something", want: commit.NewGroup("Refactor", "", "something", false)}, 58 "refactor": {line: "refactor something", want: commit.NewGroup("Refactor", "", "something", false)}, 59 "feat": {line: "feat something", want: commit.NewGroup("Feature", "", "something", false)}, 60 "feature": {line: "feature something", want: commit.NewGroup("Feature", "", "something", false)}, 61 "fix": {line: "fix something", want: commit.NewGroup("Fix", "", "something", false)}, 62 "fixed": {line: "fixed something", want: commit.NewGroup("Fix", "", "something", false)}, 63 "chore": {line: "chore something", want: commit.NewGroup("Chore", "", "something", false)}, 64 "upgrade": {line: "upgrade something", want: commit.NewGroup("Upgrades", "", "something", false)}, 65 "enhance": {line: "enhance something", want: commit.NewGroup("Enhancements", "", "something", false)}, 66 "enhancement": {line: "enhancement something", want: commit.NewGroup("Enhancements", "", "something", false)}, 67 "enhancements": {line: "enhancements something", want: commit.NewGroup("Enhancements", "", "something", false)}, 68 "style": {line: "style something", want: commit.NewGroup("Style", "", "something", false)}, 69 "ci": {line: "ci: change something", want: commit.NewGroup("CI", "", "change something", false)}, 70 "comma sep": {line: "fix(git,commit): something", want: commit.NewGroup("Fix", "git,commit", "something", false)}, 71 "hyphen subj": {line: "fix(git-commit): something", want: commit.NewGroup("Fix", "git-commit", "something", false)}, 72 "underscore": {line: "fix(git_commit): something", want: commit.NewGroup("Fix", "git_commit", "something", false)}, 73 "docs": {line: "docs: change something", want: commit.NewGroup("Docs", "", "change something", false)}, 74 } 75 76 for name, tc := range tcs { 77 tc := tc 78 t.Run(name, func(t *testing.T) { 79 t.Parallel() 80 got := commit.GroupFromCommit(tc.line) 81 if diff := cmp.Diff(tc.want, got, commit.GroupComparer...); diff != "" { 82 t.Errorf("(-want +got):\n%s", diff) 83 } 84 }) 85 } 86 } 87 88 func TestGroup(t *testing.T) { 89 t.Parallel() 90 t.Run("DescriptionString", testGroupDescriptionString) 91 t.Run("ParseGroups", testGroupParseGroups) 92 } 93 94 func testGroupDescriptionString(t *testing.T) { 95 t.Parallel() 96 // the first letter will be uppercased. 97 letter := testament.RandomLowerString(1) 98 randomString := testament.RandomLowerString(30) 99 msg := letter + randomString 100 wantMsg := strings.ToUpper(letter) + randomString 101 additional := `\n\n` + testament.RandomString(50) 102 prefix := commit.ItemPrefix 103 issue := "Close #666" 104 105 tcs := map[string]struct { 106 group commit.Group 107 want string 108 }{ 109 "simple": { 110 group: commit.NewGroup("Fix", "", msg, false), 111 want: prefix + wantMsg, 112 }, 113 "with verb": { 114 group: commit.NewGroup("Fix", "repo", msg, false), 115 want: fmt.Sprintf("%s**Repo:** %s", prefix, wantMsg), 116 }, 117 "with ci verb": { 118 group: commit.NewGroup("Fix", "ci", msg, false), 119 want: fmt.Sprintf("%s**CI:** %s", prefix, wantMsg), 120 }, 121 "multi line": { 122 group: commit.NewGroup("Fix", "repo", msg+additional, false), 123 want: fmt.Sprintf("%s**Repo:** %s", prefix, wantMsg), 124 }, 125 "with issue ref": { 126 group: commit.NewGroup("Fix", "repo", msg+additional+`\n`+issue, false), 127 want: fmt.Sprintf("%s**Repo:** %s (%s)", prefix, wantMsg, issue), 128 }, 129 "with issue (ref)": { 130 group: commit.NewGroup("Fix", "repo", msg+additional+`\n(`+issue+`)`, false), 131 want: fmt.Sprintf("%s**Repo:** %s (%s)", prefix, wantMsg, issue), 132 }, 133 "multi issue refs": { 134 group: commit.NewGroup("Fix", "repo", msg+additional+`\n`+issue+`\n`+issue, false), 135 want: fmt.Sprintf("%s**Repo:** %s (%s, %s)", prefix, wantMsg, issue, issue), 136 }, 137 "comma separated": { 138 group: commit.NewGroup("Fix", "git,commit", msg, false), 139 want: fmt.Sprintf("%s**Git,Commit:** %s", prefix, wantMsg), 140 }, 141 "hyphenated subjects": { 142 group: commit.NewGroup("Fix", "git-commit", msg, false), 143 want: fmt.Sprintf("%s**Git-commit:** %s", prefix, wantMsg), 144 }, 145 "docs": { 146 group: commit.NewGroup("Docs", "README", msg, false), 147 want: fmt.Sprintf("%s**README:** %s", prefix, wantMsg), 148 }, 149 } 150 151 for name, tc := range tcs { 152 tc := tc 153 t.Run(name, func(t *testing.T) { 154 got := tc.group.DescriptionString() 155 if diff := cmp.Diff(tc.want, got, commit.GroupComparer...); diff != "" { 156 t.Errorf("(-want +got):\n%s", diff) 157 } 158 }) 159 } 160 } 161 162 func testGroupParseGroups(t *testing.T) { 163 t.Run("OneGroup", testGroupParseGroupsOneGroup) 164 t.Run("MultipleGroups", testGroupParseGroupsMultipleGroups) 165 t.Run("BreakingSign", testGroupParseGroupsBreakingSign) 166 t.Run("BreakingFooter", testGroupParseGroupsBreakingFooter) 167 } 168 169 func testGroupParseGroupsOneGroup(t *testing.T) { 170 t.Parallel() 171 logs := []string{"Feat(testing): this is a test"} 172 got := commit.ParseGroups(logs) 173 174 got = strings.TrimRight(got, "\n") 175 want := "### Feature\n\n- **Testing:** This is a test" 176 if diff := cmp.Diff(want, got); diff != "" { 177 t.Errorf("(-want +got):\n%s", diff) 178 } 179 } 180 181 func testGroupParseGroupsMultipleGroups(t *testing.T) { 182 t.Parallel() 183 logs := []string{ 184 "Feat(testing): this is a test", 185 "Misc: this is another test", 186 "feat: yet another", 187 } 188 got := commit.ParseGroups(logs) 189 190 want := []string{ 191 "### Feature\n\n- **Testing:** This is a test\n- Yet another", 192 "### Misc\n\n- This is another test", 193 } 194 gotS := strings.Split(got, "\n\n\n") 195 sort.Strings(gotS) 196 if diff := cmp.Diff(want, gotS); diff != "" { 197 t.Errorf("(-want +got):\n%s", diff) 198 } 199 } 200 201 func testGroupParseGroupsBreakingSign(t *testing.T) { 202 t.Run("NoScope", testGroupParseGroupsBreakingSignNoScope) 203 t.Run("BeforeScope", testGroupParseGroupsBreakingSignBeforeScope) 204 t.Run("AfterScope", testGroupParseGroupsBreakingSignAfterScope) 205 } 206 207 func testGroupParseGroupsBreakingSignNoScope(t *testing.T) { 208 t.Parallel() 209 logs := []string{ 210 "ref: nothing important", 211 "ref!: this is a test", 212 } 213 got := commit.ParseGroups(logs) 214 215 want := strings.Join([]string{ 216 "### Refactor\n", 217 "- Nothing important", 218 "- This is a test [**BREAKING CHANGE**]", 219 }, "\n") 220 if diff := cmp.Diff(want, got); diff != "" { 221 t.Errorf("(-want +got):\n%s", diff) 222 } 223 } 224 225 func testGroupParseGroupsBreakingSignBeforeScope(t *testing.T) { 226 t.Parallel() 227 logs := []string{ 228 "ref: nothing important", 229 "ref!(repo): this is a test", 230 } 231 got := commit.ParseGroups(logs) 232 233 want := strings.Join([]string{ 234 "### Refactor\n", 235 "- Nothing important", 236 "- **Repo:** This is a test [**BREAKING CHANGE**]", 237 }, "\n") 238 if diff := cmp.Diff(want, got); diff != "" { 239 t.Errorf("(-want +got):\n%s", diff) 240 } 241 } 242 243 func testGroupParseGroupsBreakingSignAfterScope(t *testing.T) { 244 t.Parallel() 245 logs := []string{ 246 "ref: nothing important", 247 "ref(repo)!: this is a test", 248 } 249 got := commit.ParseGroups(logs) 250 251 want := strings.Join([]string{ 252 "### Refactor\n", 253 "- Nothing important", 254 "- **Repo:** This is a test [**BREAKING CHANGE**]", 255 }, "\n") 256 if diff := cmp.Diff(want, got); diff != "" { 257 t.Errorf("(-want +got):\n%s", diff) 258 } 259 } 260 261 func testGroupParseGroupsBreakingFooter(t *testing.T) { 262 t.Parallel() 263 logs := []string{ 264 "ref(server): nothing special", 265 "ref(repo): this is a new api\n\nBREAKING CHANGE: this is a changed api", 266 } 267 got := commit.ParseGroups(logs) 268 269 want := strings.Join([]string{ 270 "### Refactor\n", 271 "- **Server:** Nothing special", 272 "- **Repo:** This is a new api [**BREAKING CHANGE**]", 273 }, "\n") 274 if diff := cmp.Diff(want, got); diff != "" { 275 t.Errorf("(-want +got):\n%s", diff) 276 } 277 }