Skip to content

Commit d71fd6a

Browse files
authored
fix(internal/godocfx): make friendlyAPIName tolerant of missing packages (googleapis#14194)
friendlyAPIName was recently changed to return an empty string instead of an error when a package or metadata is not found. This matches the [old behavior](https://github.com/googleapis/google-cloud-go/blob/ec656b96fed7ec1fd31ebeb7e9e68cb2ae1e2c79/internal/godocfx/parse.go#L689) prior to when fixes were made to account for individual .json files. Without this change, our tests for alloydbconn, cloudqlconn will fail since they don't .repo-metadata.json files, and also don't live in this repository. In addition, the auth library generation also fails. None of these packages required a friendly API namer in the old behavior.
1 parent d5db0ee commit d71fd6a

File tree

3 files changed

+17
-29
lines changed

3 files changed

+17
-29
lines changed

internal/godocfx/godocfx_test.go

Lines changed: 15 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -309,17 +309,7 @@ func TestFriendlyAPIName(t *testing.T) {
309309
t.Fatal(err)
310310
}
311311

312-
// 3. Malformed metadata
313-
badDir := t.TempDir()
314-
badSubDir := filepath.Join(badDir, "apiv1")
315-
if err := os.MkdirAll(badSubDir, 0755); err != nil {
316-
t.Fatal(err)
317-
}
318-
if err := os.WriteFile(filepath.Join(badSubDir, ".repo-metadata.json"), []byte("invalid json"), 0644); err != nil {
319-
t.Fatal(err)
320-
}
321-
322-
// 4. Submodule avoidance
312+
// 3. Submodule avoidance
323313
pubsubDir := t.TempDir()
324314
pubsubV2Dir := filepath.Join(pubsubDir, "v2")
325315
if err := os.MkdirAll(pubsubV2Dir, 0755); err != nil {
@@ -335,13 +325,16 @@ func TestFriendlyAPIName(t *testing.T) {
335325
t.Fatal(err)
336326
}
337327

338-
namer := &friendlyAPINamer{}
328+
namer := &friendlyAPINamer{
329+
Fallbacks: map[string]string{
330+
"cloud.google.com/go/pubsub": "PubSub API",
331+
},
332+
}
339333

340334
tests := []struct {
341335
importPath string
342336
module *packages.Module
343337
want string
344-
wantErr bool
345338
}{
346339
{
347340
importPath: "cloud.google.com/go/storage",
@@ -367,33 +360,28 @@ func TestFriendlyAPIName(t *testing.T) {
367360
},
368361
want: "Storage API v1",
369362
},
370-
{
371-
importPath: "cloud.google.com/go/bad",
372-
module: &packages.Module{
373-
Path: "cloud.google.com/go/bad",
374-
Dir: badDir,
375-
},
376-
wantErr: true,
377-
},
378363
{
379364
importPath: "cloud.google.com/go/pubsub",
380365
module: &packages.Module{
381366
Path: "cloud.google.com/go/pubsub",
382367
Dir: pubsubDir,
383368
},
384-
wantErr: true, // Should NOT find v2's metadata.
369+
want: "PubSub API", // Should NOT find v2's metadata, should find fallback.
385370
},
386371
{
387-
importPath: "not found",
388-
module: nil,
389-
wantErr: true,
372+
importPath: "cloud.google.com/go/nonexistent",
373+
module: &packages.Module{
374+
Path: "cloud.google.com/go/nonexistent",
375+
Dir: t.TempDir(), // An empty dir with no metadata
376+
},
377+
want: "", // Should return empty string and no error
390378
},
391379
}
392380

393381
for _, test := range tests {
394382
got, err := namer.friendlyAPIName(test.importPath, test.module)
395-
if (err != nil) != test.wantErr {
396-
t.Errorf("friendlyAPIName(%q) got err: %v, wantErr %v", test.importPath, err, test.wantErr)
383+
if err != nil {
384+
t.Errorf("friendlyAPIName(%q) got err: %v", test.importPath, err)
397385
continue
398386
}
399387
if got != test.want {

internal/godocfx/parse.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -791,7 +791,7 @@ func (d *friendlyAPINamer) friendlyAPIName(importPath string, module *packages.M
791791
}
792792

793793
if description == "" {
794-
return "", fmt.Errorf("no description found for %q and no fallback provided", importPath)
794+
return "", nil
795795
}
796796

797797
if apiV := vNumberRE.FindString(importPath); apiV != "" {

internal/godocfx/testdata/golden/cloud.google.com/go/storage@v1.33.0/index.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ items:
131131
}
132132
</pre>
133133
<p>Objects are listed lexicographically by name. To filter objects
134-
lexicographically, <a href="#Query.StartOffset">Query.StartOffset</a> and/or <a href="#Query.EndOffset">Query.EndOffset</a> can be used:
134+
lexicographically, [Query.StartOffset] and/or [Query.EndOffset] can be used:
135135
<pre class="prettyprint">query := &amp;storage.Query{
136136
Prefix: &quot;&quot;,
137137
StartOffset: &quot;bar/&quot;, // Only list objects lexicographically &gt;= &quot;bar/&quot;

0 commit comments

Comments
 (0)