Skip to content

Commit b3cb3df

Browse files
authored
chore: remove redundant function (#2614)
- Remove `findLibraryByID` as `LibrarianState.LibraryByID` has provided the same functionality. - Adding unit tests to verify the nil cases.
1 parent 4097542 commit b3cb3df

File tree

8 files changed

+106
-140
lines changed

8 files changed

+106
-140
lines changed

internal/librarian/command.go

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ type ContainerClient interface {
101101
type commitInfo struct {
102102
// branch is the base branch of the created pull request.
103103
branch string
104-
// commit declares whether or not to create a commit.
104+
// commit declares whether to create a commit.
105105
commit bool
106106
// commitMessage is used as the message on the actual git commit.
107107
commitMessage string
@@ -111,7 +111,7 @@ type commitInfo struct {
111111
prType pullRequestType
112112
// pullRequestLabels is a list of labels to add to the created pull request.
113113
pullRequestLabels []string
114-
// push declares whether or not to push the commits to GitHub.
114+
// push declares whether to push the commits to GitHub.
115115
push bool
116116
// languageRepo is the git repository containing the language-specific libraries.
117117
languageRepo gitrepo.Repository
@@ -129,7 +129,7 @@ type commitInfo struct {
129129
library string
130130
// prBodyBuilder is a callback function for building the pull request body
131131
prBodyBuilder func() (string, error)
132-
// isDraft declares whether or not to create the pull request as a draft.
132+
// isDraft declares whether to create the pull request as a draft.
133133
isDraft bool
134134
}
135135

@@ -269,18 +269,6 @@ func findLibraryIDByAPIPath(state *config.LibrarianState, apiPath string) string
269269
return ""
270270
}
271271

272-
func findLibraryByID(state *config.LibrarianState, libraryID string) *config.LibraryState {
273-
if state == nil {
274-
return nil
275-
}
276-
for _, lib := range state.Libraries {
277-
if lib.ID == libraryID {
278-
return lib
279-
}
280-
}
281-
return nil
282-
}
283-
284272
func formatTimestamp(t time.Time) string {
285273
const yyyyMMddHHmmss = "20060102T150405Z" // Expected format by time library
286274
return t.Format(yyyyMMddHHmmss)
@@ -289,7 +277,7 @@ func formatTimestamp(t time.Time) string {
289277
// cleanAndCopyLibrary cleans the files of the given library in repoDir and copies
290278
// the new files from outputDir.
291279
func cleanAndCopyLibrary(state *config.LibrarianState, repoDir, libraryID, outputDir string) error {
292-
library := findLibraryByID(state, libraryID)
280+
library := state.LibraryByID(libraryID)
293281
if library == nil {
294282
return fmt.Errorf("library %q not found during clean and copy, despite being found in earlier steps", libraryID)
295283
}
@@ -320,7 +308,7 @@ func cleanAndCopyLibrary(state *config.LibrarianState, repoDir, libraryID, outpu
320308
// If a file is being copied to the library's SourceRoots in the dest folder but the folder does
321309
// not exist, the copy fails.
322310
func copyLibraryFiles(state *config.LibrarianState, dest, libraryID, src string) error {
323-
library := findLibraryByID(state, libraryID)
311+
library := state.LibraryByID(libraryID)
324312
if library == nil {
325313
return fmt.Errorf("library %q not found", libraryID)
326314
}

internal/librarian/command_test.go

Lines changed: 0 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -32,56 +32,6 @@ import (
3232
"github.com/googleapis/librarian/internal/gitrepo"
3333
)
3434

35-
func TestFindLibraryByID(t *testing.T) {
36-
lib1 := &config.LibraryState{ID: "lib1"}
37-
lib2 := &config.LibraryState{ID: "lib2"}
38-
stateWithLibs := &config.LibrarianState{
39-
Libraries: []*config.LibraryState{lib1, lib2},
40-
}
41-
stateNoLibs := &config.LibrarianState{
42-
Libraries: []*config.LibraryState{},
43-
}
44-
45-
for _, test := range []struct {
46-
name string
47-
state *config.LibrarianState
48-
libraryID string
49-
want *config.LibraryState
50-
}{
51-
{
52-
name: "found first library",
53-
state: stateWithLibs,
54-
libraryID: "lib1",
55-
want: lib1,
56-
},
57-
{
58-
name: "found second library",
59-
state: stateWithLibs,
60-
libraryID: "lib2",
61-
want: lib2,
62-
},
63-
{
64-
name: "not found",
65-
state: stateWithLibs,
66-
libraryID: "lib3",
67-
want: nil,
68-
},
69-
{
70-
name: "empty libraries slice",
71-
state: stateNoLibs,
72-
libraryID: "lib1",
73-
want: nil,
74-
},
75-
} {
76-
t.Run(test.name, func(t *testing.T) {
77-
got := findLibraryByID(test.state, test.libraryID)
78-
if diff := cmp.Diff(test.want, got); diff != "" {
79-
t.Errorf("findLibraryByID() mismatch (-want +got):\n%s", diff)
80-
}
81-
})
82-
}
83-
}
84-
8535
func TestDeriveImage(t *testing.T) {
8636
for _, test := range []struct {
8737
name string

internal/librarian/generate_command.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ func (r *generateRunner) generateSingleLibrary(ctx context.Context, libraryID, o
226226
}
227227

228228
// At this point, we should have a library in the state.
229-
libraryState := findLibraryByID(r.state, libraryID)
229+
libraryState := r.state.LibraryByID(libraryID)
230230
if libraryState == nil {
231231
return nil, fmt.Errorf("library %q not configured yet, generation stopped", libraryID)
232232
}
@@ -261,7 +261,7 @@ func (r *generateRunner) generateSingleLibrary(ctx context.Context, libraryID, o
261261
}
262262

263263
func (r *generateRunner) needsConfigure() bool {
264-
return r.api != "" && r.library != "" && findLibraryByID(r.state, r.library) == nil
264+
return r.api != "" && r.library != "" && r.state.LibraryByID(r.library) == nil
265265
}
266266

267267
func (r *generateRunner) updateLastGeneratedCommitState(libraryID string) error {
@@ -375,7 +375,11 @@ func (r *generateRunner) runConfigureCommand(ctx context.Context, outputDir stri
375375

376376
// getExistingSrc returns source roots as-is of a given library ID, if the source roots exist in the language repo.
377377
func (r *generateRunner) getExistingSrc(libraryID string) []string {
378-
library := findLibraryByID(r.state, libraryID)
378+
library := r.state.LibraryByID(libraryID)
379+
if library == nil {
380+
return nil
381+
}
382+
379383
var existingSrc []string
380384
for _, src := range library.SourceRoots {
381385
relPath := filepath.Join(r.repo.GetDir(), src)

internal/librarian/generate_command_test.go

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1059,6 +1059,81 @@ func TestGenerateSingleLibraryCommand(t *testing.T) {
10591059
}
10601060
}
10611061

1062+
func TestGetExistingSrc(t *testing.T) {
1063+
t.Parallel()
1064+
for _, test := range []struct {
1065+
name string
1066+
libraryID string
1067+
paths []string
1068+
want []string
1069+
}{
1070+
{
1071+
name: "all_source_paths_existed",
1072+
libraryID: "some-library",
1073+
paths: []string{
1074+
"a/path",
1075+
"another/path",
1076+
},
1077+
want: []string{
1078+
"a/path",
1079+
"another/path",
1080+
},
1081+
},
1082+
{
1083+
name: "one_source_paths_existed",
1084+
libraryID: "some-library",
1085+
paths: []string{
1086+
"a/path",
1087+
},
1088+
want: []string{
1089+
"a/path",
1090+
},
1091+
},
1092+
{
1093+
name: "no_source_paths_existed",
1094+
libraryID: "some-library",
1095+
want: nil,
1096+
},
1097+
{
1098+
name: "no_library_existed",
1099+
libraryID: "another-library",
1100+
want: nil,
1101+
},
1102+
} {
1103+
t.Run(test.name, func(t *testing.T) {
1104+
t.Parallel()
1105+
repo := newTestGitRepo(t)
1106+
state := &config.LibrarianState{
1107+
Libraries: []*config.LibraryState{
1108+
{
1109+
ID: "some-library",
1110+
SourceRoots: []string{
1111+
"a/path",
1112+
"another/path",
1113+
},
1114+
},
1115+
},
1116+
}
1117+
for _, path := range test.paths {
1118+
relPath := filepath.Join(repo.GetDir(), path)
1119+
if err := os.MkdirAll(relPath, 0755); err != nil {
1120+
t.Fatal(err)
1121+
}
1122+
}
1123+
1124+
r := &generateRunner{
1125+
repo: repo,
1126+
state: state,
1127+
}
1128+
1129+
got := r.getExistingSrc(test.libraryID)
1130+
if diff := cmp.Diff(test.want, got); diff != "" {
1131+
t.Errorf("getExistingSrc() mismatch (-want +got):%s", diff)
1132+
}
1133+
})
1134+
}
1135+
}
1136+
10621137
func TestUpdateLastGeneratedCommitState(t *testing.T) {
10631138
t.Parallel()
10641139
sourceRepo := newTestGitRepo(t)

internal/librarian/generate_pull_request.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,10 @@ func formatOnboardPRBody(request *onboardPRRequest) (string, error) {
164164

165165
// getPiperID extracts the Piper ID from the commit message that onboarded the API.
166166
func getPiperID(state *config.LibrarianState, sourceRepo gitrepo.Repository, apiPath, library string) (string, error) {
167-
libraryState := findLibraryByID(state, library)
167+
libraryState := state.LibraryByID(library)
168+
if libraryState == nil {
169+
return "", fmt.Errorf("library %s not found", library)
170+
}
168171
serviceYaml := ""
169172
for _, api := range libraryState.APIs {
170173
if api.Path == apiPath {

internal/librarian/generate_pull_request_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -662,6 +662,20 @@ Language Image: %s`,
662662
wantErr: true,
663663
wantErrPhrase: errPiperNotFound.Error(),
664664
},
665+
{
666+
name: "library_not_found_in_state",
667+
state: &config.LibrarianState{
668+
Image: "go:1.21",
669+
Libraries: []*config.LibraryState{
670+
{
671+
ID: "one-library",
672+
},
673+
},
674+
},
675+
library: "another-library",
676+
wantErr: true,
677+
wantErrPhrase: "library another-library not found",
678+
},
665679
} {
666680
t.Run(test.name, func(t *testing.T) {
667681
req := &onboardPRRequest{

internal/librarian/generate_test.go

Lines changed: 0 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
package librarian
1616

1717
import (
18-
"os"
19-
"path/filepath"
2018
"testing"
2119

2220
"github.com/google/go-cmp/cmp"
@@ -92,72 +90,6 @@ func TestGenerateSingleLibrary(t *testing.T) {
9290
}
9391
}
9492

95-
func TestGetExistingSrc(t *testing.T) {
96-
t.Parallel()
97-
for _, test := range []struct {
98-
name string
99-
paths []string
100-
want []string
101-
}{
102-
{
103-
name: "all_source_paths_existed",
104-
paths: []string{
105-
"a/path",
106-
"another/path",
107-
},
108-
want: []string{
109-
"a/path",
110-
"another/path",
111-
},
112-
},
113-
{
114-
name: "one_source_paths_existed",
115-
paths: []string{
116-
"a/path",
117-
},
118-
want: []string{
119-
"a/path",
120-
},
121-
},
122-
{
123-
name: "no_source_paths_existed",
124-
want: nil,
125-
},
126-
} {
127-
t.Run(test.name, func(t *testing.T) {
128-
t.Parallel()
129-
repo := newTestGitRepo(t)
130-
state := &config.LibrarianState{
131-
Libraries: []*config.LibraryState{
132-
{
133-
ID: "some-library",
134-
SourceRoots: []string{
135-
"a/path",
136-
"another/path",
137-
},
138-
},
139-
},
140-
}
141-
for _, path := range test.paths {
142-
relPath := filepath.Join(repo.GetDir(), path)
143-
if err := os.MkdirAll(relPath, 0755); err != nil {
144-
t.Fatal(err)
145-
}
146-
}
147-
148-
r := &generateRunner{
149-
repo: repo,
150-
state: state,
151-
}
152-
153-
got := r.getExistingSrc("some-library")
154-
if diff := cmp.Diff(test.want, got); diff != "" {
155-
t.Errorf("getExistingSrc() mismatch (-want +got):%s", diff)
156-
}
157-
})
158-
}
159-
}
160-
16193
func TestGetSafeDirectoryName(t *testing.T) {
16294
for _, test := range []struct {
16395
name string

internal/librarian/release_init.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ func (r *initRunner) runInitCommand(ctx context.Context, outputDir string) error
133133
src := r.repo.GetDir()
134134
librariesToRelease := r.state.Libraries
135135
if r.library != "" {
136-
library := findLibraryByID(r.state, r.library)
136+
library := r.state.LibraryByID(r.library)
137137
if library == nil {
138138
return fmt.Errorf("unable to find library for release: %s", r.library)
139139
}

0 commit comments

Comments
 (0)