| 1 |
// Copyright 2025 Google LLC |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
package module |
| 16 |
|
| 17 |
import ( |
| 18 |
"go/ast" |
| 19 |
"go/parser" |
| 20 |
"go/token" |
| 21 |
"os" |
| 22 |
"path/filepath" |
| 23 |
"testing" |
| 24 |
|
| 25 |
"cloud.google.com/go/internal/postprocessor/librarian/librariangen/config" |
| 26 |
"cloud.google.com/go/internal/postprocessor/librarian/librariangen/request" |
| 27 |
"github.com/google/go-cmp/cmp" |
| 28 |
) |
| 29 |
|
| 30 |
func TestGenerateInternalVersionFile(t *testing.T) { |
| 31 |
tmpDir := t.TempDir() |
| 32 |
version := "1.2.3" |
| 33 |
if err := GenerateInternalVersionFile(tmpDir, version); err != nil { |
| 34 |
t.Fatalf("GenerateInternalVersionFile() error = %v", err) |
| 35 |
} |
| 36 |
|
| 37 |
versionFile := filepath.Join(tmpDir, "internal", "version.go") |
| 38 |
if _, err := os.Stat(versionFile); os.IsNotExist(err) { |
| 39 |
t.Errorf("file %s was not created", versionFile) |
| 40 |
} |
| 41 |
|
| 42 |
fset := token.NewFileSet() |
| 43 |
node, err := parser.ParseFile(fset, versionFile, nil, parser.ParseComments) |
| 44 |
if err != nil { |
| 45 |
t.Fatalf("failed to parse version.go file: %v", err) |
| 46 |
} |
| 47 |
for _, decl := range node.Decls { |
| 48 |
genDecl, ok := decl.(*ast.GenDecl) |
| 49 |
if !ok { |
| 50 |
continue |
| 51 |
} |
| 52 |
for _, spec := range genDecl.Specs { |
| 53 |
valueSpec, ok := spec.(*ast.ValueSpec) |
| 54 |
if !ok { |
| 55 |
continue |
| 56 |
} |
| 57 |
if valueSpec.Names[0].Name == "Version" { |
| 58 |
gotVersion := valueSpec.Values[0].(*ast.BasicLit).Value |
| 59 |
// trim quotes |
| 60 |
gotVersion = gotVersion[1 : len(gotVersion)-1] |
| 61 |
if gotVersion != version { |
| 62 |
t.Errorf("version.go Version = %q, want %q", gotVersion, version) |
| 63 |
} |
| 64 |
return |
| 65 |
} |
| 66 |
} |
| 67 |
} |
| 68 |
t.Errorf("could not find Version constant in version.go") |
| 69 |
} |
| 70 |
|
| 71 |
func TestUpdateSnippetsMetadata(t *testing.T) { |
| 72 |
testdata := []struct { |
| 73 |
name string |
| 74 |
lib *request.Library |
| 75 |
moduleConfig *config.ModuleConfig |
| 76 |
files map[string]string |
| 77 |
want map[string]string |
| 78 |
wantErr bool |
| 79 |
}{ |
| 80 |
{ |
| 81 |
name: "version placeholder", |
| 82 |
lib: &request.Library{ |
| 83 |
ID: "secretmanager", |
| 84 |
Version: "2.3.4", |
| 85 |
APIs: []request.API{ |
| 86 |
{ |
| 87 |
Path: "google/cloud/secretmanager/v1", |
| 88 |
}, |
| 89 |
}, |
| 90 |
}, |
| 91 |
moduleConfig: &config.ModuleConfig{ |
| 92 |
Name: "secretmanager", |
| 93 |
APIs: []*config.APIConfig{ |
| 94 |
{ |
| 95 |
Path: "google/cloud/secretmanager/v1", |
| 96 |
ProtoPackage: "google.cloud.secretmanager.v1", |
| 97 |
}, |
| 98 |
}, |
| 99 |
}, |
| 100 |
files: map[string]string{ |
| 101 |
"internal/generated/snippets/secretmanager/apiv1/snippet_metadata.google.cloud.secretmanager.v1.json": `{"clientLibrary":{"version":"$VERSION"}}`, |
| 102 |
}, |
| 103 |
want: map[string]string{ |
| 104 |
"internal/generated/snippets/secretmanager/apiv1/snippet_metadata.google.cloud.secretmanager.v1.json": `{"clientLibrary":{"version":"2.3.4"}}`, |
| 105 |
}, |
| 106 |
}, |
| 107 |
{ |
| 108 |
name: "existing version", |
| 109 |
lib: &request.Library{ |
| 110 |
ID: "workflows", |
| 111 |
Version: "5.6.7", |
| 112 |
APIs: []request.API{ |
| 113 |
{ |
| 114 |
Path: "google/cloud/workflows/v1", |
| 115 |
}, |
| 116 |
}, |
| 117 |
}, |
| 118 |
moduleConfig: &config.ModuleConfig{ |
| 119 |
Name: "workflows", |
| 120 |
APIs: []*config.APIConfig{ |
| 121 |
{ |
| 122 |
Path: "google/cloud/workflows/v1", |
| 123 |
ProtoPackage: "google.cloud.workflows.v1", |
| 124 |
}, |
| 125 |
}, |
| 126 |
}, |
| 127 |
files: map[string]string{ |
| 128 |
"internal/generated/snippets/workflows/apiv1/snippet_metadata.google.cloud.workflows.v1.json": `{"clientLibrary":{"version":"1.2.3"}}`, |
| 129 |
}, |
| 130 |
want: map[string]string{ |
| 131 |
"internal/generated/snippets/workflows/apiv1/snippet_metadata.google.cloud.workflows.v1.json": `{"clientLibrary":{"version":"5.6.7"}}`, |
| 132 |
}, |
| 133 |
}, |
| 134 |
{ |
| 135 |
name: "file not found", |
| 136 |
lib: &request.Library{ |
| 137 |
ID: "secretmanager", |
| 138 |
Version: "2.3.4", |
| 139 |
APIs: []request.API{ |
| 140 |
{ |
| 141 |
Path: "google/cloud/secretmanager/v1", |
| 142 |
}, |
| 143 |
}, |
| 144 |
}, |
| 145 |
moduleConfig: &config.ModuleConfig{ |
| 146 |
Name: "secretmanager", |
| 147 |
APIs: []*config.APIConfig{ |
| 148 |
{ |
| 149 |
Path: "google/cloud/secretmanager/v1", |
| 150 |
ProtoPackage: "google.cloud.secretmanager.v1", |
| 151 |
}, |
| 152 |
}, |
| 153 |
}, |
| 154 |
files: map[string]string{}, |
| 155 |
want: map[string]string{}, |
| 156 |
}, |
| 157 |
{ |
| 158 |
name: "no version string", |
| 159 |
lib: &request.Library{ |
| 160 |
ID: "secretmanager", |
| 161 |
Version: "2.3.4", |
| 162 |
APIs: []request.API{ |
| 163 |
{ |
| 164 |
Path: "google/cloud/secretmanager/v1", |
| 165 |
}, |
| 166 |
}, |
| 167 |
}, |
| 168 |
moduleConfig: &config.ModuleConfig{ |
| 169 |
Name: "secretmanager", |
| 170 |
APIs: []*config.APIConfig{ |
| 171 |
{ |
| 172 |
Path: "google/cloud/secretmanager/v1", |
| 173 |
ProtoPackage: "google.cloud.secretmanager.v1", |
| 174 |
}, |
| 175 |
}, |
| 176 |
}, |
| 177 |
files: map[string]string{ |
| 178 |
"internal/generated/snippets/secretmanager/apiv1/snippet_metadata.google.cloud.secretmanager.v1.json": `{"clientLibrary":{}}`, |
| 179 |
}, |
| 180 |
want: map[string]string{}, |
| 181 |
wantErr: true, |
| 182 |
}, |
| 183 |
{ |
| 184 |
name: "multiple api versions and a sub-API", |
| 185 |
lib: &request.Library{ |
| 186 |
ID: "secretmanager", |
| 187 |
Version: "1.0.0", |
| 188 |
APIs: []request.API{ |
| 189 |
{ |
| 190 |
Path: "google/cloud/secretmanager/v1", |
| 191 |
}, |
| 192 |
{ |
| 193 |
Path: "google/cloud/secretmanager/v2", |
| 194 |
}, |
| 195 |
{ |
| 196 |
Path: "google/cloud/secretmanager/subapi/v1", |
| 197 |
}, |
| 198 |
}, |
| 199 |
}, |
| 200 |
moduleConfig: &config.ModuleConfig{ |
| 201 |
Name: "secretmanager", |
| 202 |
APIs: []*config.APIConfig{ |
| 203 |
{ |
| 204 |
Path: "google/cloud/secretmanager/v1", |
| 205 |
ProtoPackage: "google.cloud.secretmanager.v1", |
| 206 |
}, |
| 207 |
{ |
| 208 |
Path: "google/cloud/secretmanager/v2", |
| 209 |
ProtoPackage: "google.cloud.secretmanager.v2", |
| 210 |
}, |
| 211 |
{ |
| 212 |
Path: "google/cloud/secretmanager/subapi/v1", |
| 213 |
ProtoPackage: "google.cloud.secretmanager.subapi.v1", |
| 214 |
}, |
| 215 |
}, |
| 216 |
}, |
| 217 |
files: map[string]string{ |
| 218 |
"internal/generated/snippets/secretmanager/apiv1/snippet_metadata.google.cloud.secretmanager.v1.json": `{"clientLibrary":{"version":"$VERSION"}}`, |
| 219 |
"internal/generated/snippets/secretmanager/apiv2/snippet_metadata.google.cloud.secretmanager.v2.json": `{"clientLibrary":{"version":"0.1.0"}}`, |
| 220 |
"internal/generated/snippets/secretmanager/subapi/apiv1/snippet_metadata.google.cloud.secretmanager.subapi.v1.json": `{"clientLibrary":{"version":"0.1.0"}}`, |
| 221 |
}, |
| 222 |
want: map[string]string{ |
| 223 |
"internal/generated/snippets/secretmanager/apiv1/snippet_metadata.google.cloud.secretmanager.v1.json": `{"clientLibrary":{"version":"1.0.0"}}`, |
| 224 |
"internal/generated/snippets/secretmanager/apiv2/snippet_metadata.google.cloud.secretmanager.v2.json": `{"clientLibrary":{"version":"1.0.0"}}`, |
| 225 |
"internal/generated/snippets/secretmanager/subapi/apiv1/snippet_metadata.google.cloud.secretmanager.subapi.v1.json": `{"clientLibrary":{"version":"1.0.0"}}`, |
| 226 |
}, |
| 227 |
}, |
| 228 |
} |
| 229 |
|
| 230 |
for _, tc := range testdata { |
| 231 |
t.Run(tc.name, func(t *testing.T) { |
| 232 |
sourceDir := t.TempDir() |
| 233 |
destDir := t.TempDir() |
| 234 |
|
| 235 |
for path, content := range tc.files { |
| 236 |
fullPath := filepath.Join(sourceDir, path) |
| 237 |
if err := os.MkdirAll(filepath.Dir(fullPath), 0755); err != nil { |
| 238 |
t.Fatalf("failed to create directory: %v", err) |
| 239 |
} |
| 240 |
if err := os.WriteFile(fullPath, []byte(content), 0644); err != nil { |
| 241 |
t.Fatalf("failed to write file: %v", err) |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
if err := UpdateSnippetsMetadata(tc.lib, sourceDir, destDir, tc.moduleConfig); (err != nil) != tc.wantErr { |
| 246 |
t.Errorf("UpdateSnippetsMetadata() error = %v, wantErr %v", err, tc.wantErr) |
| 247 |
} |
| 248 |
|
| 249 |
got := make(map[string]string) |
| 250 |
for path := range tc.want { |
| 251 |
content, err := os.ReadFile(filepath.Join(destDir, path)) |
| 252 |
if err != nil { |
| 253 |
// If the file is not found, it's a valid case for some tests. |
| 254 |
if os.IsNotExist(err) { |
| 255 |
continue |
| 256 |
} |
| 257 |
t.Fatalf("failed to read file: %v", err) |
| 258 |
} |
| 259 |
got[path] = string(content) |
| 260 |
} |
| 261 |
|
| 262 |
if diff := cmp.Diff(tc.want, got); diff != "" { |
| 263 |
t.Errorf("UpdateSnippetsMetadata() mismatch (-want +got):\n%s", diff) |
| 264 |
} |
| 265 |
}) |
| 266 |
} |
| 267 |
} |