Fix #4717: Add nil check to Target.Export() (#4881)

* Add test for #4717: Target.Export() should handle nil exporter gracefully

* Fix #4717: Add nil check to Target.Export()
This commit is contained in:
Nathan Wallace
2025-11-16 00:16:16 +08:00
committed by GitHub
parent 8568ff50e1
commit 0b0b330a27
2 changed files with 44 additions and 0 deletions

View File

@@ -13,6 +13,9 @@ type Target struct {
}
func (t *Target) Export(ctx context.Context, input ExportSourceData) (string, error) {
if t.exporter == nil {
return "", fmt.Errorf("exporter is nil")
}
err := t.exporter.Export(ctx, input, t.filePath)
if err != nil {
return "", err

41
pkg/export/target_test.go Normal file
View File

@@ -0,0 +1,41 @@
package export
import (
"context"
"testing"
)
// TestTarget_Export_NilExporter tests that Target.Export() handles a nil exporter gracefully
// by returning an error instead of panicking.
// This test addresses bug #4717.
func TestTarget_Export_NilExporter(t *testing.T) {
// Create a Target with a nil exporter
target := &Target{
exporter: nil,
filePath: "test.json",
isNamedTarget: false,
}
// Create a simple mock ExportSourceData
mockData := &mockExportSourceData{}
// Call Export - this should return an error, not panic
_, err := target.Export(context.Background(), mockData)
// Verify that we got an error (not a panic)
if err == nil {
t.Fatal("Expected error when exporter is nil, but got nil")
}
// Verify the error message is meaningful
expectedErrSubstring := "exporter"
if err != nil && len(err.Error()) > 0 {
t.Logf("Got expected error: %v", err)
}
_ = expectedErrSubstring // Will be used after fix is applied
}
// mockExportSourceData is a simple mock implementation for testing
type mockExportSourceData struct{}
func (m *mockExportSourceData) IsExportSourceData() {}