mirror of
https://github.com/turbot/steampipe.git
synced 2025-12-19 09:58:53 -05:00
* Add test for #4797: executeQueries doesn't check for nil Client This test demonstrates that executeQueries panics with a nil pointer dereference when Client is nil. The test currently fails with: panic: runtime error: invalid memory address or nil pointer dereference 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com> * Fix #4797: Add nil check for Client in executeQueries Added a nil check at the start of executeQueries to prevent nil pointer panics when Client is not initialized. Now returns an error message and counts all queries as failures instead of crashing. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com> --------- Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -99,6 +99,12 @@ func executeQueries(ctx context.Context, initData *query.InitData) int {
|
||||
utils.LogTime("queryexecute.executeQueries start")
|
||||
defer utils.LogTime("queryexecute.executeQueries end")
|
||||
|
||||
// Check if Client is nil - this can happen if initialization failed
|
||||
if initData.Client == nil {
|
||||
error_helpers.ShowWarning("cannot execute queries: database client is not initialized")
|
||||
return len(initData.Queries)
|
||||
}
|
||||
|
||||
// failures return the number of queries that failed and also the number of rows that
|
||||
// returned errors
|
||||
failures := 0
|
||||
|
||||
@@ -222,6 +222,36 @@ func TestExecuteQueries_EmptyQueriesList(t *testing.T) {
|
||||
assert.Equal(t, 0, failures, "Should return 0 failures for empty queries list")
|
||||
}
|
||||
|
||||
// TestExecuteQueries_NilClient tests that executeQueries handles nil Client gracefully
|
||||
// Related to issue #4797
|
||||
func TestExecuteQueries_NilClient(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
|
||||
// Create initData with nil Client but with queries
|
||||
// This simulates a scenario where initialization failed but queries were still provided
|
||||
initData := &query.InitData{
|
||||
InitData: *initialisation.NewInitData(),
|
||||
Queries: []*modconfig.ResolvedQuery{
|
||||
{
|
||||
Name: "test_query",
|
||||
ExecuteSQL: "SELECT 1",
|
||||
RawSQL: "SELECT 1",
|
||||
},
|
||||
},
|
||||
}
|
||||
// Explicitly set Client to nil to test the nil case
|
||||
initData.Client = nil
|
||||
|
||||
// This should not panic - it should handle nil Client gracefully
|
||||
// Currently this will panic with nil pointer dereference
|
||||
failures := executeQueries(ctx, initData)
|
||||
|
||||
// We expect 1 failure (the query should fail gracefully, not panic)
|
||||
if failures != 1 {
|
||||
t.Errorf("Expected 1 failure with nil client, got %d", failures)
|
||||
}
|
||||
}
|
||||
|
||||
// Test Suite: Context and Cancellation
|
||||
|
||||
func TestRunBatchSession_CancelHandlerSetup(t *testing.T) {
|
||||
|
||||
Reference in New Issue
Block a user