From 22ec415975bb1af0d3b5f320dce8aa2d19f69447 Mon Sep 17 00:00:00 2001 From: Luis Pater Date: Sun, 26 Jul 2026 23:06:27 +0800 Subject: [PATCH] fix(auth): replace return with break in filestore equality checks - Changed `return` to `break` in `filestore.go` to correctly exit the loop without prematurely terminating the function. Closes: #4585 --- sdk/auth/filestore.go | 4 +-- sdk/auth/filestore_test.go | 59 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 2 deletions(-) diff --git a/sdk/auth/filestore.go b/sdk/auth/filestore.go index 3f0f608c..90c6316f 100644 --- a/sdk/auth/filestore.go +++ b/sdk/auth/filestore.go @@ -124,7 +124,7 @@ func (s *FileTokenStore) Save(ctx context.Context, auth *cliproxyauth.Auth) (str } if existing, errRead := os.ReadFile(path); errRead == nil { if jsonEqual(existing, raw) { - return path, nil + break } file, errOpen := os.OpenFile(path, os.O_WRONLY|os.O_TRUNC, 0o600) if errOpen != nil { @@ -137,7 +137,7 @@ func (s *FileTokenStore) Save(ctx context.Context, auth *cliproxyauth.Auth) (str if errClose := file.Close(); errClose != nil { return "", fmt.Errorf("auth filestore: close existing failed: %w", errClose) } - return path, nil + break } else if !os.IsNotExist(errRead) { return "", fmt.Errorf("auth filestore: read existing failed: %w", errRead) } diff --git a/sdk/auth/filestore_test.go b/sdk/auth/filestore_test.go index fe552ad2..add3e9b5 100644 --- a/sdk/auth/filestore_test.go +++ b/sdk/auth/filestore_test.go @@ -87,6 +87,65 @@ func TestExtractAccessToken(t *testing.T) { } } +func TestFileTokenStoreSaveExistingMetadataSetsFileAttributes(t *testing.T) { + tests := []struct { + name string + existingToken string + savedToken string + }{ + {name: "unchanged content", existingToken: "token", savedToken: "token"}, + {name: "overwritten content", existingToken: "old-token", savedToken: "new-token"}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + baseDir := t.TempDir() + fileName := "antigravity-user.json" + path := filepath.Join(baseDir, fileName) + existing := []byte(`{"type":"antigravity","access_token":"` + tt.existingToken + `","disabled":false}`) + if errWrite := os.WriteFile(path, existing, 0o600); errWrite != nil { + t.Fatalf("write existing auth file: %v", errWrite) + } + + store := NewFileTokenStore() + store.SetBaseDir(baseDir) + auth := &cliproxyauth.Auth{ + ID: fileName, + FileName: fileName, + Metadata: map[string]any{ + "type": "antigravity", + "access_token": tt.savedToken, + }, + } + + savedPath, errSave := store.Save(context.Background(), auth) + if errSave != nil { + t.Fatalf("Save() error = %v", errSave) + } + if savedPath != path { + t.Fatalf("Save() path = %q, want %q", savedPath, path) + } + if got := auth.Attributes[cliproxyauth.AttributePath]; got != path { + t.Errorf("path attribute = %q, want %q", got, path) + } + if got := auth.Attributes[cliproxyauth.AttributeSource]; got != path { + t.Errorf("source attribute = %q, want %q", got, path) + } + if got := auth.Attributes[cliproxyauth.AttributeSourceBackend]; got != cliproxyauth.AuthSourceFile { + t.Errorf("source backend attribute = %q, want %q", got, cliproxyauth.AuthSourceFile) + } + persisted, errRead := os.ReadFile(path) + if errRead != nil { + t.Fatalf("read saved auth file: %v", errRead) + } + expected := []byte(`{"type":"antigravity","access_token":"` + tt.savedToken + `","disabled":false}`) + if !jsonEqual(persisted, expected) { + t.Errorf("saved auth file = %s, want JSON equal to %s", persisted, expected) + } + }) + } +} + func TestFileTokenStoreListExpandsPluginMultiAuths(t *testing.T) { baseDir := t.TempDir() path := filepath.Join(baseDir, "geminicli.json") -- 2.54.0