Skip to content

Commit c1f770e

Browse files
authored
feat(librarian): allow libraries to have no APIs (#1853)
This is required for handwritten libraries to be representable in Librarian state. For the moment, such libraries are skipped in generate calls. Fixes #1852.
1 parent 0dcfb0e commit c1f770e

File tree

5 files changed

+38
-15
lines changed

5 files changed

+38
-15
lines changed

internal/config/state.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -162,9 +162,6 @@ func (l *LibraryState) Validate() error {
162162
return fmt.Errorf("last_generated_commit must be 40 characters")
163163
}
164164
}
165-
if len(l.APIs) == 0 {
166-
return fmt.Errorf("apis cannot be empty")
167-
}
168165
for i, a := range l.APIs {
169166
if err := a.Validate(); err != nil {
170167
return fmt.Errorf("invalid api at index %d: %w", i, err)

internal/config/state_test.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,14 @@ func TestLibrary_Validate(t *testing.T) {
111111
},
112112
},
113113
},
114+
{
115+
name: "valid library with no APIs",
116+
library: &LibraryState{
117+
ID: "a/b",
118+
SourceRoots: []string{"src/a", "src/b"},
119+
},
120+
wantErr: false,
121+
},
114122
{
115123
name: "missing id",
116124
library: &LibraryState{},
@@ -146,15 +154,6 @@ func TestLibrary_Validate(t *testing.T) {
146154
wantErr: true,
147155
wantErrMsg: "source_roots cannot be empty",
148156
},
149-
{
150-
name: "missing apis",
151-
library: &LibraryState{
152-
ID: "a/b",
153-
SourceRoots: []string{"src/a", "src/b"},
154-
},
155-
wantErr: true,
156-
wantErrMsg: "apis cannot be empty",
157-
},
158157
{
159158
name: "valid version without v prefix",
160159
library: &LibraryState{

internal/librarian/generate.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,17 @@ func (r *generateRunner) generateSingleLibrary(ctx context.Context, libraryID, o
175175
libraryID = configuredLibraryID
176176
}
177177

178+
// At this point, we should have a library in the state.
179+
libraryState := findLibraryByID(r.state, libraryID)
180+
if libraryState == nil {
181+
return fmt.Errorf("library %q not configured yet, generation stopped", libraryID)
182+
}
183+
184+
if len(libraryState.APIs) == 0 {
185+
slog.Info("library has no APIs; skipping generation", "library", libraryID)
186+
return nil
187+
}
188+
178189
// For each library, create a separate output directory. This avoids
179190
// libraries interfering with each other, and makes it easier to see what
180191
// was generated for each library when debugging.
@@ -221,9 +232,6 @@ func (r *generateRunner) updateLastGeneratedCommitState(libraryID string) error
221232
// If successful, it returns the ID of the generated library; otherwise, it
222233
// returns an empty string and an error.
223234
func (r *generateRunner) runGenerateCommand(ctx context.Context, libraryID, outputDir string) (string, error) {
224-
if findLibraryByID(r.state, libraryID) == nil {
225-
return "", fmt.Errorf("library %q not configured yet, generation stopped", libraryID)
226-
}
227235
apiRoot, err := filepath.Abs(r.sourceRepo.GetDir())
228236
if err != nil {
229237
return "", err

internal/librarian/generate_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -928,6 +928,24 @@ func TestGenerateScenarios(t *testing.T) {
928928
build: true,
929929
wantErr: true,
930930
},
931+
{
932+
name: "generate skips libraries with no APIs",
933+
repo: newTestGitRepo(t),
934+
state: &config.LibrarianState{
935+
Image: "gcr.io/test/image:v1.2.3",
936+
Libraries: []*config.LibraryState{
937+
{
938+
ID: "some-library",
939+
},
940+
},
941+
},
942+
container: &mockContainerClient{},
943+
ghClient: &mockGitHubClient{},
944+
build: true,
945+
wantGenerateCalls: 0,
946+
wantBuildCalls: 0,
947+
wantConfigureCalls: 0,
948+
},
931949
} {
932950
t.Run(test.name, func(t *testing.T) {
933951
t.Parallel()

internal/librarian/mocks_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ func (m *mockContainerClient) Configure(ctx context.Context, request *docker.Con
150150

151151
var libraryBuilder strings.Builder
152152
libraryBuilder.WriteString(fmt.Sprintf("{\"id\":\"%s\"", request.State.Libraries[0].ID))
153+
libraryBuilder.WriteString(fmt.Sprintf(",\"apis\":[{\"path\":\"%s\"}]", request.State.Libraries[0].APIs[0].Path))
153154
if !m.noInitVersion {
154155
libraryBuilder.WriteString(",\"version\": \"0.1.0\"")
155156
}

0 commit comments

Comments
 (0)