From 05b6cb86990fc594ac253436aad8cc1d4159cc41 Mon Sep 17 00:00:00 2001 From: OpenWaygate <27287694+OpenWaygate@users.noreply.github.com> Date: Sat, 29 Aug 2026 17:27:31 +0800 Subject: [PATCH] :recycle: Use slices.SortFunc and t.Context() for modern Go idioms --- docs/HOW_TO_TEST.md | 2 +- internal/tools/cmdtestgen/main.go | 27 ++++++++++--------------- internal/tools/skillgen/types.go | 11 +++++----- pkg/auth/verifier_test.go | 7 +++---- pkg/caption/caption_test.go | 19 +++++++++-------- pkg/channelBanner/channelBanner_test.go | 7 +++---- pkg/common/common_test.go | 7 +++---- 7 files changed, 36 insertions(+), 44 deletions(-) diff --git a/docs/HOW_TO_TEST.md b/docs/HOW_TO_TEST.md index 3a705a2..cd38e87 100644 --- a/docs/HOW_TO_TEST.md +++ b/docs/HOW_TO_TEST.md @@ -96,7 +96,7 @@ Initialize the `youtube.Service` with the mock server's URL. ```go svc, _ := youtube.NewService( - context.Background(), + t.Context(), option.WithEndpoint(ts.URL), // Point to local test server option.WithAPIKey("test-key"), ) diff --git a/internal/tools/cmdtestgen/main.go b/internal/tools/cmdtestgen/main.go index 523b10d..efc6c28 100644 --- a/internal/tools/cmdtestgen/main.go +++ b/internal/tools/cmdtestgen/main.go @@ -4,11 +4,12 @@ package main import ( + "cmp" "flag" "fmt" "log" "os" - "sort" + "slices" "strings" "github.com/eat-pray-ai/yutu/cmd" @@ -117,16 +118,12 @@ func generate() string { } } - sort.Slice( - coreCommands, func(i, j int) bool { - return coreCommands[i].Name() < coreCommands[j].Name() - }, - ) - sort.Slice( - resourceCommands, func(i, j int) bool { - return resourceCommands[i].Name() < resourceCommands[j].Name() - }, - ) + slices.SortFunc(coreCommands, func(a, b *cobra.Command) int { + return cmp.Compare(a.Name(), b.Name()) + }) + slices.SortFunc(resourceCommands, func(a, b *cobra.Command) int { + return cmp.Compare(a.Name(), b.Name()) + }) b.WriteString("\n# yutu\n") for _, c := range coreCommands { @@ -148,11 +145,9 @@ func generate() string { } subs = append(subs, sub) } - sort.Slice( - subs, func(i, j int) bool { - return subs[i].Name() < subs[j].Name() - }, - ) + slices.SortFunc(subs, func(a, b *cobra.Command) int { + return cmp.Compare(a.Name(), b.Name()) + }) for _, sub := range subs { fmt.Fprintf(&b, "echo \"------- %s -------\"\n", sub.Name()) diff --git a/internal/tools/skillgen/types.go b/internal/tools/skillgen/types.go index 4d54c93..78a16ec 100644 --- a/internal/tools/skillgen/types.go +++ b/internal/tools/skillgen/types.go @@ -4,7 +4,8 @@ package main import ( - "sort" + "cmp" + "slices" "strings" "github.com/spf13/cobra" @@ -63,8 +64,8 @@ func collectResources(root *cobra.Command) []resourceEntry { } verbs = append(verbs, verbEntry{name: sub.Name(), short: sub.Short}) } - sort.Slice(verbs, func(i, j int) bool { - return verbs[i].name < verbs[j].name + slices.SortFunc(verbs, func(a, b verbEntry) int { + return cmp.Compare(a.name, b.name) }) resources = append(resources, resourceEntry{ name: name, @@ -75,8 +76,8 @@ func collectResources(root *cobra.Command) []resourceEntry { verbs: verbs, }) } - sort.Slice(resources, func(i, j int) bool { - return resources[i].name < resources[j].name + slices.SortFunc(resources, func(a, b resourceEntry) int { + return cmp.Compare(a.name, b.name) }) return resources } \ No newline at end of file diff --git a/pkg/auth/verifier_test.go b/pkg/auth/verifier_test.go index 4b03b72..ef1b036 100644 --- a/pkg/auth/verifier_test.go +++ b/pkg/auth/verifier_test.go @@ -4,7 +4,6 @@ package auth import ( - "context" "encoding/json" "errors" "net/http" @@ -31,7 +30,7 @@ func TestGoogleTokenVerifier_ValidToken(t *testing.T) { defer ts.Close() verifier := NewGoogleTokenVerifier(ts.URL) - info, err := verifier(context.Background(), "valid-google-token", nil) + info, err := verifier(t.Context(), "valid-google-token", nil) if err != nil { t.Fatalf("unexpected error: %v", err) } @@ -58,7 +57,7 @@ func TestGoogleTokenVerifier_InvalidToken(t *testing.T) { defer ts.Close() verifier := NewGoogleTokenVerifier(ts.URL) - _, err := verifier(context.Background(), "bad-token", nil) + _, err := verifier(t.Context(), "bad-token", nil) if err == nil { t.Fatal("expected error for invalid token") } @@ -74,7 +73,7 @@ func TestGoogleTokenVerifier_ServerError(t *testing.T) { defer ts.Close() verifier := NewGoogleTokenVerifier(ts.URL) - _, err := verifier(context.Background(), "any-token", nil) + _, err := verifier(t.Context(), "any-token", nil) if err == nil { t.Fatal("expected error for server failure") } diff --git a/pkg/caption/caption_test.go b/pkg/caption/caption_test.go index 0681220..26609e8 100644 --- a/pkg/caption/caption_test.go +++ b/pkg/caption/caption_test.go @@ -5,7 +5,6 @@ package caption import ( "bytes" - "context" "errors" "io" "net/http" @@ -725,7 +724,7 @@ func TestCaption_Insert_Error(t *testing.T) { _ = f.Close() }(f) - svc, _ := youtube.NewService(context.Background(), option.WithAPIKey("test")) + svc, _ := youtube.NewService(t.Context(), option.WithAPIKey("test")) // Test: File open error c := NewCaption(WithFile("non_existent.srt"), WithService(svc)) @@ -751,7 +750,7 @@ func TestCaption_Insert_Error(t *testing.T) { defer ts.Close() svc, _ = youtube.NewService( - context.Background(), option.WithEndpoint(ts.URL), option.WithAPIKey("test"), + t.Context(), option.WithEndpoint(ts.URL), option.WithAPIKey("test"), ) c = NewCaption(WithFile("test.srt"), WithService(svc)) if err := c.Insert(&bytes.Buffer{}); err == nil { @@ -784,7 +783,7 @@ func TestCaption_Update_Error(t *testing.T) { ) defer ts.Close() svc, _ := youtube.NewService( - context.Background(), option.WithEndpoint(ts.URL), option.WithAPIKey("test"), + t.Context(), option.WithEndpoint(ts.URL), option.WithAPIKey("test"), ) c := NewCaption( @@ -804,7 +803,7 @@ func TestCaption_Update_Error(t *testing.T) { ) defer tsError.Close() svcError, _ := youtube.NewService( - context.Background(), option.WithEndpoint(tsError.URL), + t.Context(), option.WithEndpoint(tsError.URL), option.WithAPIKey("test"), ) c = NewCaption(WithService(svcError), WithVideoId("v1")) @@ -823,7 +822,7 @@ func TestCaption_Update_Error(t *testing.T) { ) defer tsEmpty.Close() svcEmpty, _ := youtube.NewService( - context.Background(), option.WithEndpoint(tsEmpty.URL), + t.Context(), option.WithEndpoint(tsEmpty.URL), option.WithAPIKey("test"), ) c = NewCaption(WithService(svcEmpty), WithVideoId("v1")) @@ -846,7 +845,7 @@ func TestCaption_Update_Error(t *testing.T) { ) defer tsUpdateErr.Close() svcUpdateErr, _ := youtube.NewService( - context.Background(), option.WithEndpoint(tsUpdateErr.URL), + t.Context(), option.WithEndpoint(tsUpdateErr.URL), option.WithAPIKey("test"), ) c = NewCaption(WithService(svcUpdateErr), WithVideoId("v1")) @@ -865,7 +864,7 @@ func TestCaption_Delete_Error(t *testing.T) { ) defer ts.Close() svc, _ := youtube.NewService( - context.Background(), option.WithEndpoint(ts.URL), option.WithAPIKey("test"), + t.Context(), option.WithEndpoint(ts.URL), option.WithAPIKey("test"), ) c := NewCaption(WithService(svc), WithIds([]string{"id1"})) @@ -885,7 +884,7 @@ func TestCaption_Download_Error(t *testing.T) { ) defer ts.Close() svc, _ := youtube.NewService( - context.Background(), option.WithEndpoint(ts.URL), option.WithAPIKey("test"), + t.Context(), option.WithEndpoint(ts.URL), option.WithAPIKey("test"), ) c := NewCaption( @@ -905,7 +904,7 @@ func TestCaption_Download_Error(t *testing.T) { ) defer ts2.Close() svc2, _ := youtube.NewService( - context.Background(), option.WithEndpoint(ts2.URL), + t.Context(), option.WithEndpoint(ts2.URL), option.WithAPIKey("test"), ) // Use a directory as file path to trigger error. diff --git a/pkg/channelBanner/channelBanner_test.go b/pkg/channelBanner/channelBanner_test.go index 1a012b7..4fa25bf 100644 --- a/pkg/channelBanner/channelBanner_test.go +++ b/pkg/channelBanner/channelBanner_test.go @@ -5,7 +5,6 @@ package channelBanner import ( "bytes" - "context" "net/http" "net/http/httptest" "os" @@ -32,7 +31,7 @@ func TestChannelBanner_Insert_Error(t *testing.T) { _ = f.Close() }(f) - svc, _ := youtube.NewService(context.Background(), option.WithAPIKey("test")) + svc, _ := youtube.NewService(t.Context(), option.WithAPIKey("test")) // Test: File open error cb := NewChannelBanner(WithFile("non_existent.jpg"), WithService(svc)) @@ -55,7 +54,7 @@ func TestChannelBanner_Insert_Error(t *testing.T) { ) defer ts.Close() svc, _ = youtube.NewService( - context.Background(), option.WithEndpoint(ts.URL), option.WithAPIKey("test"), + t.Context(), option.WithEndpoint(ts.URL), option.WithAPIKey("test"), ) cb = NewChannelBanner( WithFile("test.jpg"), WithService(svc), WithChannelId("cid"), @@ -94,7 +93,7 @@ func TestChannelBanner_Insert_Output(t *testing.T) { ) defer ts.Close() svc, _ := youtube.NewService( - context.Background(), option.WithEndpoint(ts.URL), option.WithAPIKey("test"), + t.Context(), option.WithEndpoint(ts.URL), option.WithAPIKey("test"), ) tests := []struct { diff --git a/pkg/common/common_test.go b/pkg/common/common_test.go index 62835e6..3a21299 100644 --- a/pkg/common/common_test.go +++ b/pkg/common/common_test.go @@ -5,7 +5,6 @@ package common import ( "bytes" - "context" "fmt" "math" "net/http" @@ -263,7 +262,7 @@ func TestPrintResult(t *testing.T) { func TestSetContext(t *testing.T) { f := &Fields{} - ctx := context.Background() + ctx := t.Context() f.SetContext(ctx) if f.Ctx != ctx { t.Error("SetContext did not set context") @@ -300,7 +299,7 @@ func TestEnsureService_FallsBackWithoutContext(t *testing.T) { func TestEnsureService_FallsBackWithContextNoToken(t *testing.T) { f := &Fields{} - f.SetContext(context.Background()) + f.SetContext(t.Context()) // No TokenInfo in context → should fall back to file auth err := f.EnsureService() if err == nil { @@ -311,7 +310,7 @@ func TestEnsureService_FallsBackWithContextNoToken(t *testing.T) { } func TestTokenInfoFromContext_Nil(t *testing.T) { - info := sdkauth.TokenInfoFromContext(context.Background()) + info := sdkauth.TokenInfoFromContext(t.Context()) if info != nil { t.Error("expected nil TokenInfo from plain context") }