@@ -27,7 +27,7 @@ import (
2727 "github.com/googleapis/librarian/internal/semver"
2828)
2929
30- func TestShouldInclude (t * testing.T ) {
30+ func TestShouldIncludeForRelease (t * testing.T ) {
3131 t .Parallel ()
3232 for _ , test := range []struct {
3333 name string
@@ -135,9 +135,45 @@ func TestShouldInclude(t *testing.T) {
135135 } {
136136 t .Run (test .name , func (t * testing.T ) {
137137 t .Parallel ()
138- got := shouldInclude (test .files , test .sourceRoots , test .excludePaths )
138+ got := shouldIncludeForRelease (test .files , test .sourceRoots , test .excludePaths )
139139 if got != test .want {
140- t .Errorf ("shouldInclude(%v, %v, %v) = %v, want %v" , test .files , test .sourceRoots , test .excludePaths , got , test .want )
140+ t .Errorf ("shouldIncludeForRelease(%v, %v, %v) = %v, want %v" , test .files , test .sourceRoots , test .excludePaths , got , test .want )
141+ }
142+ })
143+ }
144+ }
145+
146+ func TestShouldIncludeForGeneration (t * testing.T ) {
147+ t .Parallel ()
148+ for _ , test := range []struct {
149+ name string
150+ files []string
151+ apiPaths []string
152+ want bool
153+ }{
154+ {
155+ name : "all_files_in_apiPaths" ,
156+ files : []string {"a/b/c.proto" },
157+ apiPaths : []string {"a" },
158+ want : true ,
159+ },
160+ {
161+ name : "some_files_in_apiPaths" ,
162+ files : []string {"a/b/c.proto" , "e/f/g.proto" },
163+ apiPaths : []string {"a" },
164+ want : true ,
165+ },
166+ {
167+ name : "no_files_in_apiPaths" ,
168+ files : []string {"a/b/c.proto" },
169+ apiPaths : []string {"b" },
170+ want : false ,
171+ },
172+ } {
173+ t .Run (test .name , func (t * testing.T ) {
174+ got := shouldIncludeForGeneration (test .files , test .apiPaths )
175+ if got != test .want {
176+ t .Errorf ("shouldIncludeForGeneration(%v, %v) = %v, want %v" , test .files , test .apiPaths , got , test .want )
141177 }
142178 })
143179 }
@@ -220,7 +256,7 @@ func TestGetConventionalCommitsSinceLastRelease(t *testing.T) {
220256 wantErrPhrase string
221257 }{
222258 {
223- name : "get_commits_for_foo " ,
259+ name : "found_matching_commits_for_foo " ,
224260 repo : repoWithCommits ,
225261 library : & config.LibraryState {
226262 ID : "foo" ,
@@ -246,6 +282,34 @@ func TestGetConventionalCommitsSinceLastRelease(t *testing.T) {
246282 },
247283 },
248284 },
285+ {
286+ name : "no_matching_commits_for_foo" ,
287+ repo : repoWithCommits ,
288+ library : & config.LibraryState {
289+ ID : "foo" ,
290+ Version : "1.0.0" ,
291+ TagFormat : "{id}-v{version}" ,
292+ SourceRoots : []string {"no_matching_dir" },
293+ },
294+ },
295+ {
296+ name : "apiPaths_has_no_impact_on_release" ,
297+ repo : repoWithCommits ,
298+ library : & config.LibraryState {
299+ ID : "foo" ,
300+ Version : "1.0.0" ,
301+ TagFormat : "{id}-v{version}" ,
302+ SourceRoots : []string {"no_matching_dir" }, // For release, only this is considered
303+ APIs : []* config.API {
304+ {
305+ Path : "foo" ,
306+ },
307+ {
308+ Path : "bar" ,
309+ },
310+ },
311+ },
312+ },
249313 {
250314 name : "GetCommitsForPathsSinceTag error" ,
251315 repo : & MockRepository {
@@ -281,7 +345,7 @@ func TestGetConventionalCommitsSinceLastRelease(t *testing.T) {
281345 },
282346 } {
283347 t .Run (test .name , func (t * testing.T ) {
284- got , err := GetConventionalCommitsSinceLastRelease (test .repo , test .library )
348+ got , err := getConventionalCommitsSinceLastRelease (test .repo , test .library )
285349 if test .wantErr {
286350 if err == nil {
287351 t .Fatal ("GetConventionalCommitsSinceLastRelease() should have failed" )
@@ -301,6 +365,108 @@ func TestGetConventionalCommitsSinceLastRelease(t *testing.T) {
301365 }
302366}
303367
368+ func TestGetConventionalCommitsSinceLastGeneration (t * testing.T ) {
369+ t .Parallel ()
370+ for _ , test := range []struct {
371+ name string
372+ repo gitrepo.Repository
373+ library * config.LibraryState
374+ want []* conventionalcommits.ConventionalCommit
375+ wantErr bool
376+ wantErrPhrase string
377+ }{
378+ {
379+ name : "found_matching_file_changes_for_foo" ,
380+ library : & config.LibraryState {
381+ ID : "foo" ,
382+ APIs : []* config.API {
383+ {
384+ Path : "foo" ,
385+ },
386+ },
387+ },
388+ repo : & MockRepository {
389+ GetCommitsForPathsSinceLastGenByCommit : map [string ][]* gitrepo.Commit {
390+ "1234" : {
391+ {Message : "feat(foo): a feature" },
392+ },
393+ },
394+ ChangedFilesInCommitValue : []string {"foo/a.proto" },
395+ },
396+ want : []* conventionalcommits.ConventionalCommit {
397+ {
398+ Type : "feat" ,
399+ Scope : "foo" ,
400+ Subject : "a feature" ,
401+ LibraryID : "foo" ,
402+ Footers : map [string ]string {},
403+ },
404+ },
405+ },
406+ {
407+ name : "no_matching_file_changes_for_foo" ,
408+ library : & config.LibraryState {
409+ ID : "foo" ,
410+ APIs : []* config.API {
411+ {
412+ Path : "foo" ,
413+ },
414+ },
415+ },
416+ repo : & MockRepository {
417+ GetCommitsForPathsSinceLastGenByCommit : map [string ][]* gitrepo.Commit {
418+ "1234" : {
419+ {Message : "feat(baz): a feature" },
420+ },
421+ },
422+ ChangedFilesInCommitValue : []string {"baz/a.proto" , "baz/b.proto" , "bar/a.proto" }, // file changed is not in foo/*
423+ },
424+ },
425+ {
426+ name : "sources_root_has_no_impact" ,
427+ library : & config.LibraryState {
428+ ID : "foo" ,
429+ APIs : []* config.API {
430+ {
431+ Path : "foo" , // For generation, only this is considered
432+ },
433+ },
434+ SourceRoots : []string {
435+ "baz/" ,
436+ "bar/" ,
437+ },
438+ },
439+ repo : & MockRepository {
440+ GetCommitsForPathsSinceLastGenByCommit : map [string ][]* gitrepo.Commit {
441+ "1234" : {
442+ {Message : "feat(baz): a feature" },
443+ },
444+ },
445+ ChangedFilesInCommitValue : []string {"baz/a.proto" , "baz/b.proto" , "bar/a.proto" }, // file changed is not in foo/*
446+ },
447+ },
448+ } {
449+ t .Run (test .name , func (t * testing.T ) {
450+ got , err := getConventionalCommitsSinceLastGeneration (test .repo , test .library , "1234" )
451+ if test .wantErr {
452+ if err == nil {
453+ t .Fatal ("getConventionalCommitsSinceLastGeneration() should have failed" )
454+ }
455+ if ! strings .Contains (err .Error (), test .wantErrPhrase ) {
456+ t .Errorf ("GetConventionalCommitsSinceLastRelease() returned error %q, want to contain %q" , err .Error (), test .wantErrPhrase )
457+ }
458+ return
459+ }
460+ if err != nil {
461+ t .Fatalf ("GetConventionalCommitsSinceLastRelease() failed: %v" , err )
462+ }
463+ if diff := cmp .Diff (test .want , got , cmpopts .IgnoreFields (conventionalcommits.ConventionalCommit {}, "SHA" , "CommitHash" , "Body" , "IsBreaking" , "When" )); diff != "" {
464+ t .Errorf ("GetConventionalCommitsSinceLastRelease() mismatch (-want +got):\n %s" , diff )
465+ }
466+ })
467+ }
468+ }
469+
304470func TestGetHighestChange (t * testing.T ) {
305471 t .Parallel ()
306472 for _ , test := range []struct {
0 commit comments