Fix #4710: Return empty slice instead of nil in getTableAndConnectionSuggestions (#4734)

This commit is contained in:
Nathan Wallace
2025-11-11 19:25:03 +08:00
committed by GitHub
parent 8baad08f41
commit 5e1d316759
2 changed files with 73 additions and 0 deletions

View File

@@ -647,6 +647,9 @@ func (c *InteractiveClient) getTableAndConnectionSuggestions(word string) []prom
connection := strings.TrimSpace(parts[0])
t := c.suggestions.tablesBySchema[connection]
if t == nil {
return []prompt.Suggest{}
}
return t
}

View File

@@ -0,0 +1,70 @@
package interactive
import (
"testing"
"github.com/c-bata/go-prompt"
)
// TestGetTableAndConnectionSuggestions_ReturnsEmptySliceNotNil tests that
// getTableAndConnectionSuggestions returns an empty slice instead of nil
// when no matching connection is found in the schema.
//
// This is important for proper API contract - functions that return slices
// should return empty slices rather than nil to avoid unexpected nil pointer
// issues in calling code.
//
// Bug: #4710
// PR: #4734
func TestGetTableAndConnectionSuggestions_ReturnsEmptySliceNotNil(t *testing.T) {
tests := []struct {
name string
word string
expected bool // true if we expect non-nil result
}{
{
name: "empty word should return non-nil",
word: "",
expected: true,
},
{
name: "unqualified table should return non-nil",
word: "table",
expected: true,
},
{
name: "non-existent connection should return non-nil",
word: "nonexistent.table",
expected: true,
},
{
name: "qualified table with dot should return non-nil",
word: "aws.instances",
expected: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Create a minimal InteractiveClient with empty suggestions
c := &InteractiveClient{
suggestions: &autoCompleteSuggestions{
schemas: []prompt.Suggest{},
unqualifiedTables: []prompt.Suggest{},
tablesBySchema: make(map[string][]prompt.Suggest),
},
}
result := c.getTableAndConnectionSuggestions(tt.word)
if tt.expected && result == nil {
t.Errorf("getTableAndConnectionSuggestions(%q) returned nil, expected non-nil empty slice", tt.word)
}
// Additional check: even if not nil, should be empty in these test cases
if result != nil && len(result) != 0 {
t.Errorf("getTableAndConnectionSuggestions(%q) returned non-empty slice %v, expected empty slice", tt.word, result)
}
})
}
}