github.com/git-chglog/git-chglog@v0.15.5-0.20240126074033-6a6993d52d69/commit_extractor_test.go (about) 1 package chglog 2 3 import ( 4 "testing" 5 6 "github.com/stretchr/testify/assert" 7 ) 8 9 func TestCommitExtractor(t *testing.T) { 10 assert := assert.New(t) 11 12 extractor := newCommitExtractor(&Options{ 13 CommitSortBy: "Scope", 14 CommitGroupBy: "Type", 15 CommitGroupSortBy: "Title", 16 CommitGroupTitleMaps: map[string]string{ 17 "bar": "BAR", 18 }, 19 }) 20 21 fixtures := []*Commit{ 22 // [0] 23 { 24 Type: "foo", 25 Scope: "c", 26 Header: "1", 27 Notes: []*Note{}, 28 }, 29 // [1] 30 { 31 Type: "foo", 32 Scope: "b", 33 Header: "2", 34 Notes: []*Note{ 35 {"note1-title", "note1-body"}, 36 {"note2-title", "note2-body"}, 37 }, 38 }, 39 // [2] 40 { 41 Type: "bar", 42 Scope: "d", 43 Header: "3", 44 Notes: []*Note{ 45 {"note1-title", "note1-body"}, 46 {"note3-title", "note3-body"}, 47 }, 48 }, 49 // [3] 50 { 51 Type: "foo", 52 Scope: "a", 53 Header: "4", 54 Notes: []*Note{ 55 {"note4-title", "note4-body"}, 56 }, 57 }, 58 // [4] 59 { 60 Type: "", 61 Scope: "", 62 Header: "Merge1", 63 Notes: []*Note{}, 64 Merge: &Merge{ 65 Ref: "123", 66 Source: "merges/merge1", 67 }, 68 }, 69 // [5] 70 { 71 Type: "", 72 Scope: "", 73 Header: "Revert1", 74 Notes: []*Note{}, 75 Revert: &Revert{ 76 Header: "REVERT1", 77 }, 78 }, 79 } 80 81 commitGroups, mergeCommits, revertCommits, noteGroups := extractor.Extract(fixtures) 82 83 assert.Equal([]*CommitGroup{ 84 { 85 RawTitle: "bar", 86 Title: "BAR", 87 Commits: []*Commit{ 88 fixtures[2], 89 }, 90 }, 91 { 92 RawTitle: "foo", 93 Title: "Foo", 94 Commits: []*Commit{ 95 fixtures[3], 96 fixtures[1], 97 fixtures[0], 98 }, 99 }, 100 }, commitGroups) 101 102 assert.Equal([]*Commit{ 103 fixtures[4], 104 }, mergeCommits) 105 106 assert.Equal([]*Commit{ 107 fixtures[5], 108 }, revertCommits) 109 110 assert.Equal([]*NoteGroup{ 111 { 112 Title: "note1-title", 113 Notes: []*Note{ 114 fixtures[1].Notes[0], 115 fixtures[2].Notes[0], 116 }, 117 }, 118 { 119 Title: "note2-title", 120 Notes: []*Note{ 121 fixtures[1].Notes[1], 122 }, 123 }, 124 { 125 Title: "note3-title", 126 Notes: []*Note{ 127 fixtures[2].Notes[1], 128 }, 129 }, 130 { 131 Title: "note4-title", 132 Notes: []*Note{ 133 fixtures[3].Notes[0], 134 }, 135 }, 136 }, noteGroups) 137 } 138 139 func TestCommitOrderExtractor(t *testing.T) { 140 assert := assert.New(t) 141 142 extractor := newCommitExtractor(&Options{ 143 CommitSortBy: "Scope", 144 CommitGroupBy: "Type", 145 CommitGroupSortBy: "Custom", 146 CommitGroupTitleOrder: []string{"foo", "bar"}, 147 CommitGroupTitleMaps: map[string]string{ 148 "bar": "BAR", 149 }, 150 }) 151 152 fixtures := []*Commit{ 153 // [0] 154 { 155 Type: "foo", 156 Scope: "c", 157 Header: "1", 158 Notes: []*Note{}, 159 }, 160 // [1] 161 { 162 Type: "foo", 163 Scope: "b", 164 Header: "2", 165 Notes: []*Note{ 166 {"note1-title", "note1-body"}, 167 {"note2-title", "note2-body"}, 168 }, 169 }, 170 // [2] 171 { 172 Type: "bar", 173 Scope: "d", 174 Header: "3", 175 Notes: []*Note{ 176 {"note1-title", "note1-body"}, 177 {"note3-title", "note3-body"}, 178 }, 179 }, 180 // [3] 181 { 182 Type: "foo", 183 Scope: "a", 184 Header: "4", 185 Notes: []*Note{ 186 {"note4-title", "note4-body"}, 187 }, 188 }, 189 // [4] 190 { 191 Type: "", 192 Scope: "", 193 Header: "Merge1", 194 Notes: []*Note{}, 195 Merge: &Merge{ 196 Ref: "123", 197 Source: "merges/merge1", 198 }, 199 }, 200 // [5] 201 { 202 Type: "", 203 Scope: "", 204 Header: "Revert1", 205 Notes: []*Note{}, 206 Revert: &Revert{ 207 Header: "REVERT1", 208 }, 209 }, 210 } 211 212 commitGroups, mergeCommits, revertCommits, noteGroups := extractor.Extract(fixtures) 213 214 assert.Equal([]*CommitGroup{ 215 { 216 RawTitle: "foo", 217 Title: "Foo", 218 Commits: []*Commit{ 219 fixtures[3], 220 fixtures[1], 221 fixtures[0], 222 }, 223 }, 224 { 225 RawTitle: "bar", 226 Title: "BAR", 227 Commits: []*Commit{ 228 fixtures[2], 229 }, 230 }, 231 }, commitGroups) 232 233 assert.Equal([]*Commit{ 234 fixtures[4], 235 }, mergeCommits) 236 237 assert.Equal([]*Commit{ 238 fixtures[5], 239 }, revertCommits) 240 241 assert.Equal([]*NoteGroup{ 242 { 243 Title: "note1-title", 244 Notes: []*Note{ 245 fixtures[1].Notes[0], 246 fixtures[2].Notes[0], 247 }, 248 }, 249 { 250 Title: "note2-title", 251 Notes: []*Note{ 252 fixtures[1].Notes[1], 253 }, 254 }, 255 { 256 Title: "note3-title", 257 Notes: []*Note{ 258 fixtures[2].Notes[1], 259 }, 260 }, 261 { 262 Title: "note4-title", 263 Notes: []*Note{ 264 fixtures[3].Notes[0], 265 }, 266 }, 267 }, noteGroups) 268 }