Skip to content

Commit 989869d

Browse files
authored
fix(internal/serviceconfig): return empty string when service config is not found (#3191)
Previously, `Find` returned an error when no service config YAML file existed for an API path. This caused generation to fail for types-only libraries like google-cloud-apps-script-type that don't have service configs in googleapis. `Find` now returns an empty string with no error when a service config is not found. The sidekick parser already handles empty service configs correctly, so this allows types-only libraries to generate successfully.
1 parent 4d5ee4b commit 989869d

File tree

2 files changed

+36
-19
lines changed

2 files changed

+36
-19
lines changed

internal/serviceconfig/serviceconfig.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package serviceconfig
1818
import (
1919
"bufio"
2020
"encoding/json"
21-
"errors"
2221
"fmt"
2322
"os"
2423
"path/filepath"
@@ -41,8 +40,6 @@ type (
4140
OAuthRequirements = serviceconfig.OAuthRequirements
4241
)
4342

44-
var errServiceConfigNotFound = errors.New("service config file not found")
45-
4643
// Read reads a service config from a YAML file and returns it as a Service
4744
// proto. The file is parsed as YAML, converted to JSON, and then unmarshaled
4845
// into a Service proto.
@@ -108,7 +105,7 @@ func Find(googleapisDir, apiPath string) (string, error) {
108105
return filepath.Join(apiPath, name), nil
109106
}
110107
}
111-
return "", errServiceConfigNotFound
108+
return "", nil
112109
}
113110

114111
// isServiceConfigFile checks if the file contains "type: google.api.Service".

internal/serviceconfig/serviceconfig_test.go

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
package serviceconfig
1616

1717
import (
18-
"errors"
1918
"os"
2019
"path/filepath"
2120
"strings"
@@ -80,19 +79,40 @@ func TestNoGenprotoServiceConfigImports(t *testing.T) {
8079
}
8180

8281
func TestFind(t *testing.T) {
83-
got, err := Find("testdata/googleapis", "google/cloud/speech/v1")
84-
if err != nil {
85-
t.Fatal(err)
86-
}
87-
want := "google/cloud/speech/v1/speech_v1.yaml"
88-
if got != want {
89-
t.Errorf("got %q, want %q", got, want)
90-
}
91-
}
92-
93-
func TestFindThrowsErrorIfNotFound(t *testing.T) {
94-
_, err := Find("testdata/googleapis", "google/cloud/compute/v1")
95-
if !errors.Is(err, errServiceConfigNotFound) {
96-
t.Errorf("Find() error = %v, want %v", err, errServiceConfigNotFound)
82+
const googleapisDir = "testdata/googleapis"
83+
for _, test := range []struct {
84+
name string
85+
channel string
86+
want string
87+
wantErr bool
88+
}{
89+
{
90+
name: "found",
91+
channel: "google/cloud/speech/v1",
92+
want: "google/cloud/speech/v1/speech_v1.yaml",
93+
},
94+
{
95+
name: "not found",
96+
channel: "google/cloud/compute/v1",
97+
want: "",
98+
},
99+
{
100+
name: "directory does not exist",
101+
channel: "google/cloud/nonexistent/v1",
102+
wantErr: true,
103+
},
104+
} {
105+
t.Run(test.name, func(t *testing.T) {
106+
got, err := Find(googleapisDir, test.channel)
107+
if err != nil {
108+
if !test.wantErr {
109+
t.Fatal(err)
110+
}
111+
return
112+
}
113+
if got != test.want {
114+
t.Errorf("Find() = %q, want %q", got, test.want)
115+
}
116+
})
97117
}
98118
}

0 commit comments

Comments
 (0)