@@ -24,11 +24,12 @@ import (
2424 "github.com/googleapis/librarian/internal/config"
2525 "github.com/googleapis/librarian/internal/conventionalcommits"
2626 "github.com/googleapis/librarian/internal/gitrepo"
27+ "github.com/googleapis/librarian/internal/semver"
2728)
2829
2930func TestShouldExclude (t * testing.T ) {
3031 t .Parallel ()
31- for _ , tc := range []struct {
32+ for _ , test := range []struct {
3233 name string
3334 files []string
3435 excludePaths []string
@@ -65,18 +66,18 @@ func TestShouldExclude(t *testing.T) {
6566 want : true ,
6667 },
6768 } {
68- t .Run (tc .name , func (t * testing.T ) {
69- got := shouldExclude (tc .files , tc .excludePaths )
70- if got != tc .want {
71- t .Errorf ("shouldExclude(%v, %v) = %v, want %v" , tc .files , tc .excludePaths , got , tc .want )
69+ t .Run (test .name , func (t * testing.T ) {
70+ got := shouldExclude (test .files , test .excludePaths )
71+ if got != test .want {
72+ t .Errorf ("shouldExclude(%v, %v) = %v, want %v" , test .files , test .excludePaths , got , test .want )
7273 }
7374 })
7475 }
7576}
7677
7778func TestFormatTag (t * testing.T ) {
7879 t .Parallel ()
79- for _ , tc := range []struct {
80+ for _ , test := range []struct {
8081 name string
8182 library * config.LibraryState
8283 want string
@@ -108,10 +109,10 @@ func TestFormatTag(t *testing.T) {
108109 want : "v1.2.3" ,
109110 },
110111 } {
111- t .Run (tc .name , func (t * testing.T ) {
112- got := formatTag (tc .library )
113- if got != tc .want {
114- t .Errorf ("formatTag() = %q, want %q" , got , tc .want )
112+ t .Run (test .name , func (t * testing.T ) {
113+ got := formatTag (test .library )
114+ if got != test .want {
115+ t .Errorf ("formatTag() = %q, want %q" , got , test .want )
115116 }
116117 })
117118 }
@@ -230,3 +231,170 @@ func TestGetConventionalCommitsSinceLastRelease(t *testing.T) {
230231 })
231232 }
232233}
234+
235+ func TestGetHighestChange (t * testing.T ) {
236+ t .Parallel ()
237+ for _ , test := range []struct {
238+ name string
239+ commits []* conventionalcommits.ConventionalCommit
240+ expectedChange semver.ChangeLevel
241+ }{
242+ {
243+ name : "major change" ,
244+ commits : []* conventionalcommits.ConventionalCommit {
245+ {Type : "feat" , IsBreaking : true },
246+ {Type : "feat" },
247+ {Type : "fix" },
248+ },
249+ expectedChange : semver .Major ,
250+ },
251+ {
252+ name : "minor change" ,
253+ commits : []* conventionalcommits.ConventionalCommit {
254+ {Type : "feat" },
255+ {Type : "fix" },
256+ },
257+ expectedChange : semver .Minor ,
258+ },
259+ {
260+ name : "patch change" ,
261+ commits : []* conventionalcommits.ConventionalCommit {
262+ {Type : "fix" },
263+ },
264+ expectedChange : semver .Patch ,
265+ },
266+ {
267+ name : "no change" ,
268+ commits : []* conventionalcommits.ConventionalCommit {
269+ {Type : "docs" },
270+ {Type : "chore" },
271+ },
272+ expectedChange : semver .None ,
273+ },
274+ {
275+ name : "no commits" ,
276+ commits : []* conventionalcommits.ConventionalCommit {},
277+ expectedChange : semver .None ,
278+ },
279+ {
280+ name : "nested commit forces minor bump" ,
281+ commits : []* conventionalcommits.ConventionalCommit {
282+ {Type : "fix" },
283+ {Type : "feat" , IsNested : true },
284+ },
285+ expectedChange : semver .Minor ,
286+ },
287+ {
288+ name : "nested commit with breaking change forces minor bump" ,
289+ commits : []* conventionalcommits.ConventionalCommit {
290+ {Type : "feat" , IsBreaking : true , IsNested : true },
291+ {Type : "feat" },
292+ },
293+ expectedChange : semver .Minor ,
294+ },
295+ {
296+ name : "major change and nested commit" ,
297+ commits : []* conventionalcommits.ConventionalCommit {
298+ {Type : "feat" , IsBreaking : true },
299+ {Type : "fix" , IsNested : true },
300+ },
301+ expectedChange : semver .Major ,
302+ },
303+ {
304+ name : "nested commit before major change" ,
305+ commits : []* conventionalcommits.ConventionalCommit {
306+ {Type : "fix" , IsNested : true },
307+ {Type : "feat" , IsBreaking : true },
308+ },
309+ expectedChange : semver .Major ,
310+ },
311+ {
312+ name : "nested commit with only fixes forces minor bump" ,
313+ commits : []* conventionalcommits.ConventionalCommit {
314+ {Type : "fix" },
315+ {Type : "fix" , IsNested : true },
316+ },
317+ expectedChange : semver .Minor ,
318+ },
319+ } {
320+ t .Run (test .name , func (t * testing.T ) {
321+ highestChange := getHighestChange (test .commits )
322+ if diff := cmp .Diff (test .expectedChange , highestChange ); diff != "" {
323+ t .Errorf ("getHighestChange() returned diff (-want +got):\n %s" , diff )
324+ }
325+ })
326+ }
327+ }
328+
329+ func TestNextVersion (t * testing.T ) {
330+ t .Parallel ()
331+ for _ , test := range []struct {
332+ name string
333+ commits []* conventionalcommits.ConventionalCommit
334+ currentVersion string
335+ overrideNextVersion string
336+ wantVersion string
337+ wantErr bool
338+ }{
339+ {
340+ name : "with override version" ,
341+ commits : []* conventionalcommits.ConventionalCommit {},
342+ currentVersion : "1.0.0" ,
343+ overrideNextVersion : "2.0.0" ,
344+ wantVersion : "2.0.0" ,
345+ wantErr : false ,
346+ },
347+ {
348+ name : "without override version" ,
349+ commits : []* conventionalcommits.ConventionalCommit {
350+ {Type : "feat" },
351+ },
352+ currentVersion : "1.0.0" ,
353+ overrideNextVersion : "" ,
354+ wantVersion : "1.1.0" ,
355+ wantErr : false ,
356+ },
357+ {
358+ name : "derive next returns error" ,
359+ commits : []* conventionalcommits.ConventionalCommit {
360+ {Type : "feat" },
361+ },
362+ currentVersion : "invalid-version" ,
363+ overrideNextVersion : "" ,
364+ wantVersion : "" ,
365+ wantErr : true ,
366+ },
367+ {
368+ name : "breaking change on nested commit results in minor bump" ,
369+ commits : []* conventionalcommits.ConventionalCommit {
370+ {Type : "feat" , IsBreaking : true , IsNested : true },
371+ },
372+ currentVersion : "1.2.3" ,
373+ overrideNextVersion : "" ,
374+ wantVersion : "1.3.0" ,
375+ wantErr : false ,
376+ },
377+ {
378+ name : "major change before nested commit results in major bump" ,
379+ commits : []* conventionalcommits.ConventionalCommit {
380+ {Type : "feat" , IsBreaking : true },
381+ {Type : "fix" , IsNested : true },
382+ },
383+ currentVersion : "1.2.3" ,
384+ overrideNextVersion : "" ,
385+ wantVersion : "2.0.0" ,
386+ wantErr : false ,
387+ },
388+ } {
389+ t .Run (test .name , func (t * testing.T ) {
390+ gotVersion , err := NextVersion (test .commits , test .currentVersion , test .overrideNextVersion )
391+ if (err != nil ) != test .wantErr {
392+ t .Errorf ("NextVersion() error = %v, wantErr %v" , err , test .wantErr )
393+ return
394+ }
395+ if gotVersion != test .wantVersion {
396+ t .Errorf ("NextVersion() = %v, want %v" , gotVersion , test .wantVersion )
397+ }
398+ })
399+ }
400+ }
0 commit comments