Skip to content

Commit 7bdbb14

Browse files
fix: add call to librarian tidy from migrate-sidekick command (#3163)
Invoke librarian runTidy from the migrate-sidekick command. This exports the librarian function runTidy to give migrate-sidekick command access to it. Additionally it renames the default librarian yaml file in migrate-sidekick to be librarian.yaml to align with default file in librarian.go. --------- Signed-off-by: ldetmer <[email protected]> Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
1 parent 4b6af8e commit 7bdbb14

File tree

11 files changed

+192
-13
lines changed

11 files changed

+192
-13
lines changed

devtools/cmd/migrate-sidekick/main.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"strings"
2828

2929
"github.com/googleapis/librarian/internal/config"
30+
"github.com/googleapis/librarian/internal/librarian"
3031
"github.com/googleapis/librarian/internal/yaml"
3132
"github.com/pelletier/go-toml/v2"
3233
)
@@ -39,6 +40,7 @@ var (
3940
errRepoNotFound = errors.New("-repo flag is required")
4041
errSidekickNotFound = errors.New(".sidekick.toml not found")
4142
errSrcNotFound = errors.New("src/generated directory not found")
43+
errTidyFailed = errors.New("librarian tidy failed")
4244
)
4345

4446
// SidekickConfig represents the structure of a .sidekick.toml file.
@@ -79,7 +81,7 @@ func main() {
7981
func run(args []string) error {
8082
flagSet := flag.NewFlagSet("migrate-sidekick", flag.ContinueOnError)
8183
repoPath := flagSet.String("repo", "", "Path to the google-cloud-rust repository (required)")
82-
outputPath := flagSet.String("output", "./.librarian.yaml", "Output file path (default: ./.librarian.yaml)")
84+
outputPath := flagSet.String("output", "./librarian.yaml", "Output file path (default: ./librarian.yaml)")
8385
if err := flagSet.Parse(args[1:]); err != nil {
8486
return err
8587
}
@@ -115,6 +117,11 @@ func run(args []string) error {
115117
}
116118
slog.Info("Wrote config to output file", "path", outputPath)
117119

120+
if err := librarian.RunTidy(); err != nil {
121+
slog.Error(errTidyFailed.Error(), "error", err)
122+
return errTidyFailed
123+
}
124+
118125
return nil
119126
}
120127

devtools/cmd/migrate-sidekick/main_test.go

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@ package main
1616

1717
import (
1818
"errors"
19+
"os"
1920
"testing"
2021

2122
"github.com/google/go-cmp/cmp"
2223
"github.com/googleapis/librarian/internal/config"
2324
)
2425

2526
func TestReadRootSidekick(t *testing.T) {
26-
t.Parallel()
2727
for _, test := range []struct {
2828
name string
2929
path string
@@ -73,7 +73,6 @@ func TestReadRootSidekick(t *testing.T) {
7373
},
7474
} {
7575
t.Run(test.name, func(t *testing.T) {
76-
t.Parallel()
7776
got, err := readRootSidekick(test.path)
7877
if test.wantErr != nil {
7978
if !errors.Is(err, test.wantErr) {
@@ -94,7 +93,6 @@ func TestReadRootSidekick(t *testing.T) {
9493
}
9594

9695
func TestFindSidekickFiles(t *testing.T) {
97-
t.Parallel()
9896
for _, test := range []struct {
9997
name string
10098
path string
@@ -116,7 +114,6 @@ func TestFindSidekickFiles(t *testing.T) {
116114
},
117115
} {
118116
t.Run(test.name, func(t *testing.T) {
119-
t.Parallel()
120117
got, err := findSidekickFiles(test.path)
121118
if test.wantErr != nil {
122119
if !errors.Is(err, test.wantErr) {
@@ -137,7 +134,6 @@ func TestFindSidekickFiles(t *testing.T) {
137134
}
138135

139136
func TestReadSidekickFiles(t *testing.T) {
140-
t.Parallel()
141137
for _, test := range []struct {
142138
name string
143139
files []string
@@ -249,7 +245,6 @@ func TestReadSidekickFiles(t *testing.T) {
249245
},
250246
} {
251247
t.Run(test.name, func(t *testing.T) {
252-
t.Parallel()
253248
got, err := readSidekickFiles(test.files)
254249
if test.wantErr != nil {
255250
if !errors.Is(err, test.wantErr) {
@@ -270,7 +265,6 @@ func TestReadSidekickFiles(t *testing.T) {
270265
}
271266

272267
func TestDeriveLibraryName(t *testing.T) {
273-
t.Parallel()
274268
for _, test := range []struct {
275269
name string
276270
api string
@@ -308,7 +302,6 @@ func TestDeriveLibraryName(t *testing.T) {
308302
},
309303
} {
310304
t.Run(test.name, func(t *testing.T) {
311-
t.Parallel()
312305
got := deriveLibraryName(test.api)
313306
if diff := cmp.Diff(test.want, got); diff != "" {
314307
t.Errorf("mismatch (-want +got):\n%s", diff)
@@ -318,7 +311,6 @@ func TestDeriveLibraryName(t *testing.T) {
318311
}
319312

320313
func TestBuildConfig(t *testing.T) {
321-
t.Parallel()
322314
for _, test := range []struct {
323315
name string
324316
libraries map[string]*config.Library
@@ -406,11 +398,52 @@ func TestBuildConfig(t *testing.T) {
406398
},
407399
} {
408400
t.Run(test.name, func(t *testing.T) {
409-
t.Parallel()
410401
got := buildConfig(test.libraries, test.defaults)
411402
if diff := cmp.Diff(test.want, got); diff != "" {
412403
t.Errorf("mismatch (-want +got):\n%s", diff)
413404
}
414405
})
415406
}
416407
}
408+
409+
func TestRunMigrateCommand(t *testing.T) {
410+
for _, test := range []struct {
411+
name string
412+
path string
413+
wantErr error
414+
}{
415+
{
416+
name: "success",
417+
path: "testdata/run/success",
418+
},
419+
{
420+
name: "tidy_command_fails",
421+
path: "testdata/run/tidy-fails",
422+
wantErr: errTidyFailed,
423+
},
424+
} {
425+
t.Run(test.name, func(t *testing.T) {
426+
427+
// ensure librarian.yaml generated is removed after the test,
428+
// even if the test fails
429+
outputPath := "librarian.yaml"
430+
t.Cleanup(func() {
431+
if err := os.Remove(outputPath); err != nil && !os.IsNotExist(err) {
432+
t.Logf("cleanup: remove %s: %v", outputPath, err)
433+
}
434+
})
435+
436+
if err := run([]string{"migrate-sidekick", "-repo", test.path}); err != nil {
437+
if test.wantErr == nil {
438+
t.Fatal(err)
439+
}
440+
if !errors.Is(err, test.wantErr) {
441+
t.Fatalf("expected error containing %q, got: %v", test.wantErr, err)
442+
}
443+
} else if test.wantErr != nil {
444+
t.Fatalf("expected error containing %q, got nil", test.wantErr)
445+
}
446+
447+
})
448+
}
449+
}

devtools/cmd/migrate-sidekick/testdata/run/success/.sidekick.toml

Whitespace-only changes.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
[general]
2+
specification-source = 'google/cloud/sql/v1'
3+
service-config = 'google/cloud/sql/v1/sqladmin_v1.yaml'
4+
5+
[[pagination-overrides]]
6+
id = '.google.cloud.sql.v1.SqlInstancesService.List'
7+
item-field = 'items'
8+
9+
[codec]
10+
version = '1.2.0'
11+
copyright-year = '2025'
12+
'package:lazy_static' = 'used-if=services,package=lazy_static,force-used=true'
13+
'package:gaxi' = 'used-if=services,package=google-cloud-gax-internal,feature=_internal-http-client,source=internal'
14+
15+
16+
[[documentation-overrides]]
17+
id = '.google.api.ProjectProperties'
18+
match = 'example match'
19+
replace = 'example replace'
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
[package]
2+
name = "google-cloud-sql-v1"
3+
description = "Google Cloud Client Libraries for Rust - Cloud SQL Admin API"
4+
edition.workspace = true
5+
authors.workspace = true
6+
license.workspace = true
7+
repository.workspace = true
8+
keywords.workspace = true
9+
categories.workspace = true
10+
rust-version.workspace = true
11+
publish = false
12+
13+
[dependencies]
14+
async-trait.workspace = true
15+
bytes.workspace = true
16+
gax.workspace = true
17+
gaxi = { workspace = true, features = ["_internal-http-client"] }
18+
lazy_static.workspace = true
19+
reqwest.workspace = true
20+
serde.workspace = true
21+
serde_json.workspace = true
22+
serde_with.workspace = true
23+
tracing.workspace = true
24+
wkt.workspace = true
25+
26+
[dev-dependencies]
27+
tokio-test.workspace = true

devtools/cmd/migrate-sidekick/testdata/run/tidy-fails/.sidekick.toml

Whitespace-only changes.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
[general]
2+
specification-source = 'google/cloud/sql/v1'
3+
service-config = 'google/cloud/sql/v1/sqladmin_v1.yaml'
4+
5+
[[pagination-overrides]]
6+
id = '.google.cloud.sql.v1.SqlInstancesService.List'
7+
item-field = 'items'
8+
9+
[codec]
10+
version = '1.2.0'
11+
copyright-year = '2025'
12+
'package:lazy_static' = 'used-if=services,package=lazy_static,force-used=true'
13+
'package:gaxi' = 'used-if=services,package=google-cloud-gax-internal,feature=_internal-http-client,source=internal'
14+
15+
16+
[[documentation-overrides]]
17+
id = '.google.api.ProjectProperties'
18+
match = 'example match'
19+
replace = 'example replace'
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
[package]
2+
name = "google-cloud-sql-v1"
3+
description = "Google Cloud Client Libraries for Rust - Cloud SQL Admin API"
4+
edition.workspace = true
5+
authors.workspace = true
6+
license.workspace = true
7+
repository.workspace = true
8+
keywords.workspace = true
9+
categories.workspace = true
10+
rust-version.workspace = true
11+
publish = false
12+
13+
[dependencies]
14+
async-trait.workspace = true
15+
bytes.workspace = true
16+
gax.workspace = true
17+
gaxi = { workspace = true, features = ["_internal-http-client"] }
18+
lazy_static.workspace = true
19+
reqwest.workspace = true
20+
serde.workspace = true
21+
serde_json.workspace = true
22+
serde_with.workspace = true
23+
tracing.workspace = true
24+
wkt.workspace = true
25+
26+
[dev-dependencies]
27+
tokio-test.workspace = true
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
[general]
2+
specification-source = 'google/cloud/sql/v1'
3+
service-config = 'google/cloud/sql/v1/sqladmin_v1.yaml'
4+
5+
[[pagination-overrides]]
6+
id = '.google.cloud.sql.v1.SqlInstancesService.List'
7+
item-field = 'items'
8+
9+
[codec]
10+
version = '1.2.0'
11+
copyright-year = '2025'
12+
'package:lazy_static' = 'used-if=services,package=lazy_static,force-used=true'
13+
'package:gaxi' = 'used-if=services,package=google-cloud-gax-internal,feature=_internal-http-client,source=internal'
14+
15+
16+
[[documentation-overrides]]
17+
id = '.google.api.ProjectProperties'
18+
match = 'example match'
19+
replace = 'example replace'
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
[package]
2+
name = "google-cloud-sql-v1"
3+
description = "Google Cloud Client Libraries for Rust - Cloud SQL Admin API"
4+
edition.workspace = true
5+
authors.workspace = true
6+
license.workspace = true
7+
repository.workspace = true
8+
keywords.workspace = true
9+
categories.workspace = true
10+
rust-version.workspace = true
11+
publish = false
12+
13+
[dependencies]
14+
async-trait.workspace = true
15+
bytes.workspace = true
16+
gax.workspace = true
17+
gaxi = { workspace = true, features = ["_internal-http-client"] }
18+
lazy_static.workspace = true
19+
reqwest.workspace = true
20+
serde.workspace = true
21+
serde_json.workspace = true
22+
serde_with.workspace = true
23+
tracing.workspace = true
24+
wkt.workspace = true
25+
26+
[dev-dependencies]
27+
tokio-test.workspace = true

0 commit comments

Comments
 (0)