Add test for #4807: Potential type assertion panic in logRefreshConnectionResults

Fix test to verify panic is prevented, not expected

The test was written to expect a panic, but after the fix is applied,
the panic should NO LONGER occur. Updated the test to verify that:
1. No panic occurs when handling nil values
2. No panic occurs when handling wrong types
3. No panic occurs when handling nil cobra.Command pointers

This ensures the test passes after the fix is applied.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Nathan Wallace
2025-11-12 00:05:34 +08:00
parent 9c3791fafd
commit f176bdc9d7

View File

@@ -6,6 +6,10 @@ import (
"sync/atomic"
"testing"
"time"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/turbot/steampipe/v2/pkg/constants"
)
// TestExemplarSchemaMapConcurrentAccess tests concurrent access to exemplarSchemaMap
@@ -186,3 +190,92 @@ func TestRefreshConnectionState_ContextCancellation(t *testing.T) {
// Clean up - wait for all goroutines to finish
wg.Wait()
}
// TestLogRefreshConnectionResultsTypeAssertion tests the type assertion panic bug in logRefreshConnectionResults
// This test demonstrates issue #4807 - potential panic when viper.Get returns nil or wrong type
func TestLogRefreshConnectionResultsTypeAssertion(t *testing.T) {
// Save original viper value
originalValue := viper.Get(constants.ConfigKeyActiveCommand)
defer func() {
if originalValue != nil {
viper.Set(constants.ConfigKeyActiveCommand, originalValue)
} else {
// Clean up by setting to nil if it was nil
viper.Set(constants.ConfigKeyActiveCommand, nil)
}
}()
// Test case 1: viper.Get returns nil
t.Run("nil value does not panic", func(t *testing.T) {
viper.Set(constants.ConfigKeyActiveCommand, nil)
state := &refreshConnectionState{}
// After the fix, this should NOT panic
defer func() {
if r := recover(); r != nil {
t.Errorf("Unexpected panic occurred: %v", r)
}
}()
// This should handle nil gracefully after the fix
state.logRefreshConnectionResults()
// If we get here without panic, the fix is working
t.Log("Successfully handled nil value without panic")
})
// Test case 2: viper.Get returns wrong type
t.Run("wrong type does not panic", func(t *testing.T) {
viper.Set(constants.ConfigKeyActiveCommand, "not-a-cobra-command")
state := &refreshConnectionState{}
// After the fix, this should NOT panic
defer func() {
if r := recover(); r != nil {
t.Errorf("Unexpected panic occurred: %v", r)
}
}()
// This should handle wrong type gracefully after the fix
state.logRefreshConnectionResults()
// If we get here without panic, the fix is working
t.Log("Successfully handled wrong type without panic")
})
// Test case 3: viper.Get returns *cobra.Command but it's nil
t.Run("nil cobra.Command pointer does not panic", func(t *testing.T) {
var nilCmd *cobra.Command
viper.Set(constants.ConfigKeyActiveCommand, nilCmd)
state := &refreshConnectionState{}
// After the fix, this should NOT panic
defer func() {
if r := recover(); r != nil {
t.Errorf("Unexpected panic occurred: %v", r)
}
}()
// This should handle nil cobra.Command gracefully after the fix
state.logRefreshConnectionResults()
// If we get here without panic, the fix is working
t.Log("Successfully handled nil cobra.Command pointer without panic")
})
// Test case 4: Valid cobra.Command (should work)
t.Run("valid cobra.Command works", func(t *testing.T) {
cmd := &cobra.Command{
Use: "plugin-manager",
}
viper.Set(constants.ConfigKeyActiveCommand, cmd)
state := &refreshConnectionState{}
// This should work
state.logRefreshConnectionResults()
})
}