♻️ Use slices.SortFunc and t.Context() for modern Go idioms

This commit is contained in:
OpenWaygate committed 2026-08-29 19:01:25 +08:00
1 parent 102413b19b
commit 05b6cb8699
7 files changed
+36 -44

No files matched your search

+3 -4
View File
@@ -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")
}
+9 -10
View File
@@ -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.
+3 -4
View File
@@ -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 {
+3 -4
View File
@@ -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")
}