fix: error msg change, t.helper

This commit is contained in:
rupinder-syngh
2025-04-11 13:37:55 +05:30
parent 037fc02909
commit 36848cbd40
2 changed files with 40 additions and 53 deletions

View File

@@ -29,7 +29,7 @@ type Manager struct {
func validateMgrCfg(cfg *ManagerConfig) error { func validateMgrCfg(cfg *ManagerConfig) error {
if cfg.Root == "" { if cfg.Root == "" {
return fmt.Errorf("Root path cannot be empty") return fmt.Errorf("root path cannot be empty")
} }
return nil return nil
} }

View File

@@ -261,7 +261,8 @@ type mockRegistryLookup struct {
} }
// createTestZip creates a zip file with test content in a temporary directory. // createTestZip creates a zip file with test content in a temporary directory.
func createTestZip(t *testing.T) (string, func()) { func createTestZip(t *testing.T) string {
t.Helper()
// Create a temporary directory for the zip file // Create a temporary directory for the zip file
tempDir := t.TempDir() tempDir := t.TempDir()
zipPath := filepath.Join(tempDir, "test.zip") zipPath := filepath.Join(tempDir, "test.zip")
@@ -286,9 +287,7 @@ func createTestZip(t *testing.T) (string, func()) {
t.Fatalf("Failed to write to file: %v", err) t.Fatalf("Failed to write to file: %v", err)
} }
return zipPath, func() { return zipPath
os.RemoveAll(tempDir)
}
} }
// TestNewManagerSuccess tests the successful scenarios of the NewManager function. // TestNewManagerSuccess tests the successful scenarios of the NewManager function.
@@ -322,7 +321,7 @@ func TestNewManagerSuccess(t *testing.T) {
cfg: &ManagerConfig{ cfg: &ManagerConfig{
Root: t.TempDir(), Root: t.TempDir(),
RemoteRoot: func() string { RemoteRoot: func() string {
zipPath, _ := createTestZip(t) zipPath := createTestZip(t)
return zipPath return zipPath
}(), }(),
}, },
@@ -377,7 +376,7 @@ func TestNewManagerFailure(t *testing.T) {
Root: "", Root: "",
RemoteRoot: "", RemoteRoot: "",
}, },
expectedError: "Root path cannot be empty", expectedError: "root path cannot be empty",
}, },
{ {
name: "invalid config with nonexistent root", name: "invalid config with nonexistent root",
@@ -635,57 +634,45 @@ func TestSchemaValidatorFailure(t *testing.T) {
// TestRouterSuccess tests the successful scenarios of the Router method. // TestRouterSuccess tests the successful scenarios of the Router method.
func TestRouterSuccess(t *testing.T) { func TestRouterSuccess(t *testing.T) {
tests := []struct { t.Run("successful router creation", func(t *testing.T) {
name string cfg := &Config{
cfg *Config ID: "test-router",
plugin *mockRouterProvider Config: map[string]string{},
}{ }
{ plugin := &mockRouterProvider{
name: "successful router creation", router: &mockRouter{},
cfg: &Config{ errFunc: func() error { return nil },
ID: "test-router", }
Config: map[string]string{}, // Create a manager with the mock plugin.
}, m := &Manager{
plugin: &mockRouterProvider{ plugins: map[string]onixPlugin{
router: &mockRouter{}, cfg.ID: &mockPlugin{
errFunc: func() error { return nil }, symbol: plugin,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Create a manager with the mock plugin.
m := &Manager{
plugins: map[string]onixPlugin{
tt.cfg.ID: &mockPlugin{
symbol: tt.plugin,
},
}, },
closers: []func(){}, },
} closers: []func(){},
}
// Call Router. // Call Router.
router, err := m.Router(context.Background(), tt.cfg) router, err := m.Router(context.Background(), cfg)
// Check success case. // Check success case.
if err != nil { if err != nil {
t.Fatalf("unexpected error: %v", err) t.Fatalf("unexpected error: %v", err)
} }
if router == nil { if router == nil {
t.Fatal("expected non-nil router, got nil") t.Fatal("expected non-nil router, got nil")
} }
if router != tt.plugin.router { if router != plugin.router {
t.Fatal("router does not match expected instance") t.Fatal("router does not match expected instance")
} }
if len(m.closers) != 1 { if len(m.closers) != 1 {
t.Fatalf("Manager.closers has %d closers, expected 1", len(m.closers)) t.Fatalf("Manager.closers has %d closers, expected 1", len(m.closers))
} }
m.closers[0]() m.closers[0]()
}) })
}
} }
// TestRouterFailure tests the failure scenarios of the Router method. // TestRouterFailure tests the failure scenarios of the Router method.