From fb6efafc167d9a22e79864e1331501b3c03e76f5 Mon Sep 17 00:00:00 2001 From: Devon Carew Date: Fri, 7 Feb 2025 08:36:03 -0800 Subject: [PATCH] feat(generator/dart): update the generated package names --- generator/internal/dart/dart.go | 2 +- generator/internal/dart/dart_test.go | 48 +++++++++++++++++++ generator/internal/dart/darttemplate.go | 4 +- generator/internal/dart/darttemplate_test.go | 36 ++++++++++++++ .../dart/templates/pubspec.yaml.mustache | 2 +- .../golden/secretmanager/pubspec.yaml | 2 +- 6 files changed, 88 insertions(+), 6 deletions(-) create mode 100644 generator/internal/dart/dart_test.go create mode 100644 generator/internal/dart/darttemplate_test.go diff --git a/generator/internal/dart/dart.go b/generator/internal/dart/dart.go index 6d9c03b1f..2c5013506 100644 --- a/generator/internal/dart/dart.go +++ b/generator/internal/dart/dart.go @@ -180,7 +180,7 @@ func modelPackageName(api *api.API, packageNameOverride string) string { if len(packageNameOverride) > 0 { return packageNameOverride } - return api.Name + return "google_cloud_" + strcase.ToSnake(api.Name) } func validatePackageName(newPackage, elementName, sourceSpecificationPackageName string) error { diff --git a/generator/internal/dart/dart_test.go b/generator/internal/dart/dart_test.go new file mode 100644 index 000000000..130a88f4a --- /dev/null +++ b/generator/internal/dart/dart_test.go @@ -0,0 +1,48 @@ +// Copyright 2025 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package dart + +import ( + "io/fs" + "path/filepath" + "strings" + "testing" +) + +func TestDart_GeneratedFiles(t *testing.T) { + files := generatedFiles() + if len(files) == 0 { + t.Errorf("expected a non-empty list of template files from generatedFiles()") + } +} + +func TestDart_TemplatesAvailable(t *testing.T) { + var count = 0 + fs.WalkDir(dartTemplates, "templates", func(path string, d fs.DirEntry, err error) error { + if filepath.Ext(path) != ".mustache" { + return nil + } + if strings.Count(d.Name(), ".") == 1 { + // skip partials + return nil + } + count++ + return nil + }) + + if count == 0 { + t.Errorf("no go templates found") + } +} diff --git a/generator/internal/dart/darttemplate.go b/generator/internal/dart/darttemplate.go index c36b33b76..da2eaf620 100644 --- a/generator/internal/dart/darttemplate.go +++ b/generator/internal/dart/darttemplate.go @@ -31,7 +31,6 @@ type modelAnnotations struct { CopyrightYear string BoilerPlate []string DefaultHost string - DartPackage string DocLines []string } @@ -155,8 +154,7 @@ func annotateModel(model *api.API, options map[string]string) (*modelAnnotations } return "" }(), - DartPackage: "google_" + modelPackageName(model, packageNameOverride), - DocLines: strings.Split(model.Description, "\n"), + DocLines: strings.Split(model.Description, "\n"), } model.Codec = ann diff --git a/generator/internal/dart/darttemplate_test.go b/generator/internal/dart/darttemplate_test.go new file mode 100644 index 000000000..e95c7ff65 --- /dev/null +++ b/generator/internal/dart/darttemplate_test.go @@ -0,0 +1,36 @@ +// Copyright 2025 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package dart + +import ( + "testing" + + "github.com/google/go-cmp/cmp" + "github.com/googleapis/google-cloud-rust/generator/internal/api" +) + +func TestDart_AnnotateModel(t *testing.T) { + model := api.NewTestAPI([]*api.Message{}, []*api.Enum{}, []*api.Service{}) + _, err := annotateModel(model, map[string]string{}) + if err != nil { + t.Fatal(err) + } + + codec := model.Codec.(*modelAnnotations) + + if diff := cmp.Diff("google_cloud_test", codec.PackageName); diff != "" { + t.Errorf("mismatch in Codec.PackageName (-want, +got)\n:%s", diff) + } +} diff --git a/generator/internal/dart/templates/pubspec.yaml.mustache b/generator/internal/dart/templates/pubspec.yaml.mustache index 2719ac8cd..4fb4bc548 100644 --- a/generator/internal/dart/templates/pubspec.yaml.mustache +++ b/generator/internal/dart/templates/pubspec.yaml.mustache @@ -18,7 +18,7 @@ limitations under the License. #{{{.}}} {{/Codec.BoilerPlate}} -name: {{Codec.DartPackage}} +name: {{Codec.PackageName}} description: The Google Cloud client library for the {{Title}}. version: 0.1.0 diff --git a/generator/testdata/dart/protobuf/golden/secretmanager/pubspec.yaml b/generator/testdata/dart/protobuf/golden/secretmanager/pubspec.yaml index 57b967e19..73add6971 100755 --- a/generator/testdata/dart/protobuf/golden/secretmanager/pubspec.yaml +++ b/generator/testdata/dart/protobuf/golden/secretmanager/pubspec.yaml @@ -14,7 +14,7 @@ # # Code generated by sidekick. DO NOT EDIT. -name: google_secretmanager +name: google_cloud_secretmanager description: The Google Cloud client library for the Secret Manager API. version: 0.1.0