| 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 release |
| 16 |
|
| 17 |
import ( |
| 18 |
"context" |
| 19 |
"go/ast" |
| 20 |
"go/parser" |
| 21 |
"go/token" |
| 22 |
"os" |
| 23 |
"path/filepath" |
| 24 |
"strings" |
| 25 |
"testing" |
| 26 |
"time" |
| 27 |
|
| 28 |
"cloud.google.com/go/internal/postprocessor/librarian/librariangen/config" |
| 29 |
) |
| 30 |
|
| 31 |
func setupTestDirs(t *testing.T, initialRepoContent map[string]string, requestJSON string) (librarianDir, repoDir, outputDir string) { |
| 32 |
t.Helper() |
| 33 |
tmpDir := t.TempDir() |
| 34 |
|
| 35 |
librarianDir = filepath.Join(tmpDir, "librarian") |
| 36 |
repoDir = filepath.Join(tmpDir, "repo") |
| 37 |
outputDir = filepath.Join(tmpDir, "output") |
| 38 |
for _, dir := range []string{librarianDir, repoDir, outputDir} { |
| 39 |
if err := os.MkdirAll(dir, 0755); err != nil { |
| 40 |
t.Fatalf("failed to create dir: %v", err) |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
// Create the test request |
| 45 |
if err := os.WriteFile(filepath.Join(librarianDir, "release-init-request.json"), []byte(requestJSON), 0644); err != nil { |
| 46 |
t.Fatalf("failed to write request file: %v", err) |
| 47 |
} |
| 48 |
|
| 49 |
// Create generator-input/repo-config.yaml under the librarian directory. |
| 50 |
// An empty config file is valid, and any modules that are requested will |
| 51 |
// just get default values. |
| 52 |
configFile := filepath.Join(librarianDir, config.GeneratorInputDir, config.RepoConfigFile) |
| 53 |
if err := os.MkdirAll(filepath.Dir(configFile), 0755); err != nil { |
| 54 |
t.Fatalf("failed to create generator input dir: %v", err) |
| 55 |
} |
| 56 |
err := os.WriteFile(configFile, make([]byte, 0), 0644) |
| 57 |
if err != nil { |
| 58 |
t.Fatalf("failed to create file %s: %v", configFile, err) |
| 59 |
} |
| 60 |
|
| 61 |
// Populate /repo |
| 62 |
for path, content := range initialRepoContent { |
| 63 |
fullPath := filepath.Join(repoDir, path) |
| 64 |
if err := os.MkdirAll(filepath.Dir(fullPath), 0755); err != nil { |
| 65 |
t.Fatalf("failed to create repo content dir: %v", err) |
| 66 |
} |
| 67 |
if err := os.WriteFile(fullPath, []byte(content), 0644); err != nil { |
| 68 |
t.Fatalf("failed to write repo content: %v", err) |
| 69 |
} |
| 70 |
} |
| 71 |
return |
| 72 |
} |
| 73 |
|
| 74 |
func assertVersion(t *testing.T, versionGoPath, wantVersion string) { |
| 75 |
t.Helper() |
| 76 |
fset := token.NewFileSet() |
| 77 |
node, err := parser.ParseFile(fset, versionGoPath, nil, parser.ParseComments) |
| 78 |
if err != nil { |
| 79 |
t.Fatalf("failed to parse version.go file: %v", err) |
| 80 |
} |
| 81 |
for _, decl := range node.Decls { |
| 82 |
genDecl, ok := decl.(*ast.GenDecl) |
| 83 |
if !ok { |
| 84 |
continue |
| 85 |
} |
| 86 |
for _, spec := range genDecl.Specs { |
| 87 |
valueSpec, ok := spec.(*ast.ValueSpec) |
| 88 |
if !ok { |
| 89 |
continue |
| 90 |
} |
| 91 |
if valueSpec.Names[0].Name == "Version" { |
| 92 |
gotVersion := valueSpec.Values[0].(*ast.BasicLit).Value |
| 93 |
// trim quotes |
| 94 |
gotVersion = gotVersion[1 : len(gotVersion)-1] |
| 95 |
if gotVersion != wantVersion { |
| 96 |
t.Errorf("version.go Version = %q, want %q", gotVersion, wantVersion) |
| 97 |
} |
| 98 |
return |
| 99 |
} |
| 100 |
} |
| 101 |
} |
| 102 |
t.Errorf("could not find Version constant in version.go") |
| 103 |
} |
| 104 |
|
| 105 |
func TestInit(t *testing.T) { |
| 106 |
oldNow := now |
| 107 |
defer func() { now = oldNow }() |
| 108 |
now = func() time.Time { |
| 109 |
return time.Date(2025, 9, 11, 0, 0, 0, 0, time.UTC) |
| 110 |
} |
| 111 |
tests := []struct { |
| 112 |
name string |
| 113 |
requestJSON string |
| 114 |
initialRepoContent map[string]string |
| 115 |
wantChangelogSubstr string |
| 116 |
wantVersion string |
| 117 |
wantSnippetVersion string |
| 118 |
wantErr bool |
| 119 |
releaseNotTriggered bool |
| 120 |
changelogAlreadyUpToDate bool |
| 121 |
}{ |
| 122 |
{ |
| 123 |
name: "success", |
| 124 |
requestJSON: `{ |
| 125 |
"libraries": [{ |
| 126 |
"id": "secretmanager", "version": "1.16.0", "release_triggered": true, |
| 127 |
"apis": [{"path": "google/cloud/secretmanager/v1"}], |
| 128 |
"changes": [ |
| 129 |
{"type": "feat", "subject": "another feature", "source_commit_hash": "zxcvbn098765"}, |
| 130 |
{"type": "fix", "subject": "correct typo in documentation", "source_commit_hash": "123456abcdef"}, |
| 131 |
{"type": "feat", "subject": "add new GetSecret API", "source_commit_hash": "abcdef123456"} |
| 132 |
] |
| 133 |
}] |
| 134 |
}`, |
| 135 |
initialRepoContent: map[string]string{ |
| 136 |
"secretmanager/CHANGES.md": "# Changes\n\n## [1.15.0]\n- Old stuff.", |
| 137 |
"secretmanager/internal/version.go": `package internal; const Version = "1.15.0"`, |
| 138 |
"internal/generated/snippets/secretmanager/apiv1/snippet_metadata.google.cloud.secretmanager.v1.json": `{"version": "1.15.0"}`, |
| 139 |
}, |
| 140 |
wantChangelogSubstr: "## [1.16.0](https://github.com/googleapis/google-cloud-go/releases/tag/secretmanager%2Fv1.16.0) (2025-09-11)\n\n### Features\n\n* add new GetSecret API ([abcdef1](https://github.com/googleapis/google-cloud-go/commit/abcdef123456))\n* another feature ([zxcvbn0](https://github.com/googleapis/google-cloud-go/commit/zxcvbn098765))\n\n### Bug Fixes\n\n* correct typo in documentation ([123456a](https://github.com/googleapis/google-cloud-go/commit/123456abcdef))\n\n", |
| 141 |
wantVersion: "1.16.0", |
| 142 |
wantSnippetVersion: `"version": "1.16.0"`, |
| 143 |
}, |
| 144 |
{ |
| 145 |
name: "release not triggered", |
| 146 |
requestJSON: `{ "libraries": [{"id": "secretmanager", "version": "1.16.0", "release_triggered": false}] }`, |
| 147 |
releaseNotTriggered: true, |
| 148 |
}, |
| 149 |
{ |
| 150 |
name: "changelog already up-to-date", |
| 151 |
requestJSON: `{ "libraries": [ { "id": "secretmanager", "version": "1.16.0", "release_triggered": true, "apis": [{"path": "google/cloud/secretmanager/v1"}], "changes": [{"type": "feat", "subject": "add new GetSecret API"}] } ] }`, |
| 152 |
initialRepoContent: map[string]string{ |
| 153 |
"secretmanager/CHANGES.md": "# Changes\n\n## [1.16.0](https://github.com/googleapis/google-cloud-go/releases/tag/secretmanager%2Fv1.16.0)\n- Already there.", |
| 154 |
"internal/generated/snippets/secretmanager/apiv1/snippet_metadata.google.cloud.secretmanager.v1.json": `{"version": "1.15.0"}`, |
| 155 |
}, |
| 156 |
changelogAlreadyUpToDate: true, |
| 157 |
wantVersion: "1.16.0", |
| 158 |
wantSnippetVersion: `"version": "1.16.0"`, |
| 159 |
}, |
| 160 |
{ |
| 161 |
name: "malformed json", |
| 162 |
requestJSON: `{"libraries": [}`, |
| 163 |
wantErr: true, |
| 164 |
}, |
| 165 |
} |
| 166 |
|
| 167 |
for _, tt := range tests { |
| 168 |
t.Run(tt.name, func(t *testing.T) { |
| 169 |
|
| 170 |
librarianDir, repoDir, outputDir := setupTestDirs(t, tt.initialRepoContent, tt.requestJSON) |
| 171 |
cfg := &Config{ |
| 172 |
LibrarianDir: librarianDir, |
| 173 |
RepoDir: repoDir, |
| 174 |
OutputDir: outputDir, |
| 175 |
} |
| 176 |
|
| 177 |
err := Init(context.Background(), cfg) |
| 178 |
if (err != nil) != tt.wantErr { |
| 179 |
t.Fatalf("Init() error = %v, wantErr %v", err, tt.wantErr) |
| 180 |
} |
| 181 |
if tt.wantErr { |
| 182 |
return |
| 183 |
} |
| 184 |
|
| 185 |
if tt.releaseNotTriggered { |
| 186 |
files, _ := os.ReadDir(outputDir) |
| 187 |
if len(files) > 0 { |
| 188 |
t.Errorf("outputDir should be empty, but got %d files", len(files)) |
| 189 |
} |
| 190 |
return |
| 191 |
} |
| 192 |
|
| 193 |
if tt.changelogAlreadyUpToDate { |
| 194 |
_, err := os.Stat(filepath.Join(outputDir, "secretmanager/CHANGES.md")) |
| 195 |
if !os.IsNotExist(err) { |
| 196 |
t.Errorf("new changelog should not be created when already up-to-date") |
| 197 |
} |
| 198 |
} else { |
| 199 |
changelog, err := os.ReadFile(filepath.Join(outputDir, "secretmanager/CHANGES.md")) |
| 200 |
if err != nil { |
| 201 |
t.Fatalf("failed to read changelog: %v", err) |
| 202 |
} |
| 203 |
if !strings.Contains(string(changelog), tt.wantChangelogSubstr) { |
| 204 |
t.Errorf("changelog content = %q, want contains %q", string(changelog), tt.wantChangelogSubstr) |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
assertVersion(t, filepath.Join(outputDir, "secretmanager/internal/version.go"), tt.wantVersion) |
| 209 |
|
| 210 |
snippet, err := os.ReadFile(filepath.Join(outputDir, "internal/generated/snippets/secretmanager/apiv1/snippet_metadata.google.cloud.secretmanager.v1.json")) |
| 211 |
if err != nil { |
| 212 |
t.Fatalf("failed to read snippet: %v", err) |
| 213 |
} |
| 214 |
if !strings.Contains(string(snippet), tt.wantSnippetVersion) { |
| 215 |
t.Errorf("snippet content = %q, want contains %q", string(snippet), tt.wantSnippetVersion) |
| 216 |
} |
| 217 |
}) |
| 218 |
} |
| 219 |
} |