Skip to content

Commit b9b875b

Browse files
authored
impl(sidekick): search roots for discovery files (#2277)
Search any configured `*-root` directories for the discovery file sources. We will use this to download the discovery specifications from https://github.com/googleapis/discovery-artifact-manager
1 parent b46e000 commit b9b875b

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed

internal/sidekick/internal/parser/disco.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,29 @@ package parser
1616

1717
import (
1818
"os"
19+
"path"
1920

2021
"github.com/googleapis/librarian/internal/sidekick/internal/api"
22+
"github.com/googleapis/librarian/internal/sidekick/internal/config"
2123
"github.com/googleapis/librarian/internal/sidekick/internal/parser/discovery"
2224
"google.golang.org/genproto/googleapis/api/serviceconfig"
2325
)
2426

2527
// ParseDisco reads discovery docs specifications and converts them into
2628
// the `api.API` model.
2729
func ParseDisco(source, serviceConfigFile string, options map[string]string) (*api.API, error) {
30+
for _, opt := range config.SourceRoots(options) {
31+
location, ok := options[opt]
32+
if !ok {
33+
// Ignore options that are not set
34+
continue
35+
}
36+
fullName := path.Join(location, source)
37+
if _, err := os.Stat(fullName); err == nil {
38+
source = fullName
39+
break
40+
}
41+
}
2842
contents, err := os.ReadFile(source)
2943
if err != nil {
3044
return nil, err

internal/sidekick/internal/parser/disco_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,29 @@ func TestDisco_Parse(t *testing.T) {
4444
}
4545
}
4646

47+
func TestDisco_FindSources(t *testing.T) {
48+
got, err := ParseDisco(discoSourceFileRelative, "", map[string]string{
49+
"test-root": testdataDir,
50+
"roots": "undefined,test",
51+
})
52+
if err != nil {
53+
t.Fatal(err)
54+
}
55+
wantName := "compute"
56+
wantTitle := "Compute Engine API"
57+
wantDescription := "Creates and runs virtual machines on Google Cloud Platform. "
58+
if got.Name != wantName {
59+
t.Errorf("want = %q; got = %q", wantName, got.Name)
60+
}
61+
if got.Title != wantTitle {
62+
t.Errorf("want = %q; got = %q", wantTitle, got.Title)
63+
}
64+
if diff := cmp.Diff(wantDescription, got.Description); diff != "" {
65+
t.Errorf("mismatch (-want +got):\n%s", diff)
66+
}
67+
68+
}
69+
4770
func TestDisco_ParseNoServiceConfig(t *testing.T) {
4871
got, err := ParseDisco(discoSourceFile, "", map[string]string{})
4972
if err != nil {

internal/sidekick/internal/parser/parser_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ import (
2525

2626
var (
2727
testdataDir, _ = filepath.Abs("../../testdata")
28-
discoSourceFile = path.Join(testdataDir, "disco/compute.v1.json")
28+
discoSourceFileRelative = "disco/compute.v1.json"
29+
discoSourceFile = path.Join(testdataDir, discoSourceFileRelative)
2930
secretManagerYamlRelative = "google/cloud/secretmanager/v1/secretmanager_v1.yaml"
3031
secretManagerYamlFullPath = path.Join(testdataDir, "googleapis", secretManagerYamlRelative)
3132
)

0 commit comments

Comments
 (0)