Skip to content

Commit 98908f0

Browse files
authored
feat(internal/librarian/golang): run init and tidy on new client (#4343)
For new Go client, run `go mod init` and `go mod tidy` after generation. For #3617
1 parent 1a1ea12 commit 98908f0

2 files changed

Lines changed: 40 additions & 3 deletions

File tree

internal/librarian/golang/generate.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package golang
1818
import (
1919
"context"
2020
_ "embed"
21+
"errors"
2122
"fmt"
2223
"io/fs"
2324
"os"
@@ -128,6 +129,13 @@ func generate(ctx context.Context, library *config.Library, googleapisDir string
128129
if err := updateSnippetMetadata(library, outdir); err != nil {
129130
return err
130131
}
132+
if _, err := os.Stat(filepath.Join(absModuleRoot, "go.mod")); err != nil {
133+
if errors.Is(err, fs.ErrNotExist) {
134+
// New client, init the module.
135+
return initModule(ctx, absModuleRoot, modulePath(library))
136+
}
137+
return err
138+
}
131139
return nil
132140
}
133141

@@ -384,6 +392,15 @@ func updateSnippetDirectory(baseDir, version string) error {
384392
})
385393
}
386394

395+
func initModule(ctx context.Context, dir, modPath string) error {
396+
initArgs := []string{"go", "mod", "init", modPath}
397+
if err := command.RunInDir(ctx, dir, initArgs[0], initArgs[1:]...); err != nil {
398+
return err
399+
}
400+
tidyArgs := []string{"go", "mod", "tidy"}
401+
return command.RunInDir(ctx, dir, tidyArgs[0], tidyArgs[1:]...)
402+
}
403+
387404
// releaseLevel determines the release level for an API based on the API path and the library's current version.
388405
func releaseLevel(apiPath, version string) (string, error) {
389406
apiVersion := filepath.Base(apiPath)

internal/librarian/golang/generate_test.go

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package golang
1616

1717
import (
1818
"errors"
19+
"io/fs"
1920
"os"
2021
"path/filepath"
2122
"strings"
@@ -229,7 +230,7 @@ func TestGenerate(t *testing.T) {
229230
libraryName: "secretmanager",
230231
apis: []*config.API{{Path: "google/cloud/secretmanager/v1"}},
231232
goModule: &config.GoModule{
232-
DeleteGenerationOutputPaths: []string{"secretmanager/apiv1/secretmanagerpb"},
233+
DeleteGenerationOutputPaths: []string{"secretmanager/apiv1/secret_manager_client.go"},
233234
GoAPIs: []*config.GoAPI{
234235
{
235236
ClientPackage: "secretmanager",
@@ -239,10 +240,10 @@ func TestGenerate(t *testing.T) {
239240
},
240241
},
241242
want: []string{
242-
"secretmanager/apiv1/secret_manager_client.go",
243+
"secretmanager/apiv1/secretmanagerpb/service.pb.go",
243244
},
244245
removed: []string{
245-
"secretmanager/apiv1/secretmanagerpb",
246+
"secretmanager/apiv1/secret_manager_client.go",
246247
},
247248
},
248249
{
@@ -1079,3 +1080,22 @@ func TestBuildGAPICOpts(t *testing.T) {
10791080
})
10801081
}
10811082
}
1083+
1084+
func TestInitModule(t *testing.T) {
1085+
testhelper.RequireCommand(t, "go")
1086+
outDir := t.TempDir()
1087+
// Write an import so go mod tidy can generate a go.sum file.
1088+
content := []byte("package main\nimport _ \"golang.org/x/text\"\n")
1089+
if err := os.WriteFile(filepath.Join(outDir, "main.go"), content, 0644); err != nil {
1090+
t.Fatal(err)
1091+
}
1092+
if err := initModule(t.Context(), outDir, "example.com/testmod"); err != nil {
1093+
t.Fatal(err)
1094+
}
1095+
if _, err := os.Stat(filepath.Join(outDir, "go.mod")); errors.Is(err, fs.ErrNotExist) {
1096+
t.Errorf("go.mod does not exist")
1097+
}
1098+
if _, err := os.Stat(filepath.Join(outDir, "go.sum")); errors.Is(err, fs.ErrNotExist) {
1099+
t.Errorf("go.sum does not exist")
1100+
}
1101+
}

0 commit comments

Comments
 (0)