updated ezconf
This commit is contained in:
@@ -1,24 +1,34 @@
|
||||
package ezconf
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// Mock integration for testing
|
||||
// mockConfig is a test config struct with ezconf tags
|
||||
type mockConfig struct {
|
||||
Host string `ezconf:"MOCK_HOST,description:Host to connect to,default:localhost"`
|
||||
Port int `ezconf:"MOCK_PORT,description:Port to connect to,default:8080"`
|
||||
}
|
||||
|
||||
// mockConfig2 is a second test config struct
|
||||
type mockConfig2 struct {
|
||||
Token string `ezconf:"MOCK_TOKEN,description:API token,required"`
|
||||
}
|
||||
|
||||
// mockIntegration implements the Integration interface for testing
|
||||
type mockIntegration struct {
|
||||
name string
|
||||
packagePath string
|
||||
configFunc func() (interface{}, error)
|
||||
name string
|
||||
configPtr any
|
||||
configFunc func() (interface{}, error)
|
||||
groupName string
|
||||
}
|
||||
|
||||
func (m mockIntegration) Name() string {
|
||||
return m.name
|
||||
}
|
||||
|
||||
func (m mockIntegration) PackagePath() string {
|
||||
return m.packagePath
|
||||
func (m mockIntegration) ConfigPointer() any {
|
||||
return m.configPtr
|
||||
}
|
||||
|
||||
func (m mockIntegration) ConfigFunc() func() (interface{}, error) {
|
||||
@@ -26,15 +36,18 @@ func (m mockIntegration) ConfigFunc() func() (interface{}, error) {
|
||||
}
|
||||
|
||||
func (m mockIntegration) GroupName() string {
|
||||
return "Test Group"
|
||||
if m.groupName == "" {
|
||||
return "Test Group"
|
||||
}
|
||||
return m.groupName
|
||||
}
|
||||
|
||||
func TestRegisterIntegration(t *testing.T) {
|
||||
loader := New()
|
||||
|
||||
integration := mockIntegration{
|
||||
name: "test",
|
||||
packagePath: ".",
|
||||
name: "test",
|
||||
configPtr: &mockConfig{},
|
||||
configFunc: func() (interface{}, error) {
|
||||
return "test config", nil
|
||||
},
|
||||
@@ -45,9 +58,9 @@ func TestRegisterIntegration(t *testing.T) {
|
||||
t.Fatalf("RegisterIntegration failed: %v", err)
|
||||
}
|
||||
|
||||
// Verify package path was added
|
||||
if len(loader.packagePaths) != 1 {
|
||||
t.Errorf("expected 1 package path, got %d", len(loader.packagePaths))
|
||||
// Verify config struct was added
|
||||
if len(loader.configStructs) != 1 {
|
||||
t.Errorf("expected 1 config struct, got %d", len(loader.configStructs))
|
||||
}
|
||||
|
||||
// Verify config func was added
|
||||
@@ -68,14 +81,46 @@ func TestRegisterIntegration(t *testing.T) {
|
||||
if cfg != "test config" {
|
||||
t.Errorf("expected 'test config', got %v", cfg)
|
||||
}
|
||||
|
||||
// Verify env vars were parsed from struct tags
|
||||
envVars := loader.GetEnvVars()
|
||||
if len(envVars) != 2 {
|
||||
t.Errorf("expected 2 env vars, got %d", len(envVars))
|
||||
}
|
||||
|
||||
foundHost := false
|
||||
foundPort := false
|
||||
for _, ev := range envVars {
|
||||
if ev.Name == "MOCK_HOST" {
|
||||
foundHost = true
|
||||
if ev.Default != "localhost" {
|
||||
t.Errorf("expected default 'localhost', got '%s'", ev.Default)
|
||||
}
|
||||
if ev.Group != "Test Group" {
|
||||
t.Errorf("expected group 'Test Group', got '%s'", ev.Group)
|
||||
}
|
||||
}
|
||||
if ev.Name == "MOCK_PORT" {
|
||||
foundPort = true
|
||||
if ev.Default != "8080" {
|
||||
t.Errorf("expected default '8080', got '%s'", ev.Default)
|
||||
}
|
||||
}
|
||||
}
|
||||
if !foundHost {
|
||||
t.Error("MOCK_HOST not found in env vars")
|
||||
}
|
||||
if !foundPort {
|
||||
t.Error("MOCK_PORT not found in env vars")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRegisterIntegration_InvalidPath(t *testing.T) {
|
||||
func TestRegisterIntegration_NilConfigPointer(t *testing.T) {
|
||||
loader := New()
|
||||
|
||||
integration := mockIntegration{
|
||||
name: "test",
|
||||
packagePath: "/nonexistent/path",
|
||||
name: "test",
|
||||
configPtr: nil,
|
||||
configFunc: func() (interface{}, error) {
|
||||
return "test config", nil
|
||||
},
|
||||
@@ -83,7 +128,7 @@ func TestRegisterIntegration_InvalidPath(t *testing.T) {
|
||||
|
||||
err := loader.RegisterIntegration(integration)
|
||||
if err == nil {
|
||||
t.Error("expected error for invalid package path")
|
||||
t.Error("expected error for nil config pointer")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -91,16 +136,16 @@ func TestRegisterIntegrations(t *testing.T) {
|
||||
loader := New()
|
||||
|
||||
integration1 := mockIntegration{
|
||||
name: "test1",
|
||||
packagePath: ".",
|
||||
name: "test1",
|
||||
configPtr: &mockConfig{},
|
||||
configFunc: func() (interface{}, error) {
|
||||
return "config1", nil
|
||||
},
|
||||
}
|
||||
|
||||
integration2 := mockIntegration{
|
||||
name: "test2",
|
||||
packagePath: ".",
|
||||
name: "test2",
|
||||
configPtr: &mockConfig2{},
|
||||
configFunc: func() (interface{}, error) {
|
||||
return "config2", nil
|
||||
},
|
||||
@@ -130,22 +175,28 @@ func TestRegisterIntegrations(t *testing.T) {
|
||||
if cfg1 != "config1" || cfg2 != "config2" {
|
||||
t.Error("config values mismatch")
|
||||
}
|
||||
|
||||
// Should have env vars from both structs
|
||||
envVars := loader.GetEnvVars()
|
||||
if len(envVars) != 3 {
|
||||
t.Errorf("expected 3 env vars (2 from mockConfig + 1 from mockConfig2), got %d", len(envVars))
|
||||
}
|
||||
}
|
||||
|
||||
func TestRegisterIntegrations_PartialFailure(t *testing.T) {
|
||||
loader := New()
|
||||
|
||||
integration1 := mockIntegration{
|
||||
name: "test1",
|
||||
packagePath: ".",
|
||||
name: "test1",
|
||||
configPtr: &mockConfig{},
|
||||
configFunc: func() (interface{}, error) {
|
||||
return "config1", nil
|
||||
},
|
||||
}
|
||||
|
||||
integration2 := mockIntegration{
|
||||
name: "test2",
|
||||
packagePath: "/nonexistent",
|
||||
name: "test2",
|
||||
configPtr: nil, // This should cause failure
|
||||
configFunc: func() (interface{}, error) {
|
||||
return "config2", nil
|
||||
},
|
||||
@@ -161,52 +212,3 @@ func TestIntegration_Interface(t *testing.T) {
|
||||
// Verify that mockIntegration implements Integration interface
|
||||
var _ Integration = (*mockIntegration)(nil)
|
||||
}
|
||||
|
||||
func TestRegisterIntegration_RealPackage(t *testing.T) {
|
||||
// Integration test with real hlog package if available
|
||||
hlogPath := filepath.Join("..", "hlog")
|
||||
if _, err := os.Stat(hlogPath); os.IsNotExist(err) {
|
||||
t.Skip("hlog package not found, skipping integration test")
|
||||
}
|
||||
|
||||
loader := New()
|
||||
|
||||
// Create a simple integration for testing
|
||||
integration := mockIntegration{
|
||||
name: "hlog",
|
||||
packagePath: hlogPath,
|
||||
configFunc: func() (interface{}, error) {
|
||||
// Return a mock config instead of calling real ConfigFromEnv
|
||||
return struct{ LogLevel string }{LogLevel: "info"}, nil
|
||||
},
|
||||
}
|
||||
|
||||
err := loader.RegisterIntegration(integration)
|
||||
if err != nil {
|
||||
t.Fatalf("RegisterIntegration with real package failed: %v", err)
|
||||
}
|
||||
|
||||
if err := loader.Load(); err != nil {
|
||||
t.Fatalf("Load failed: %v", err)
|
||||
}
|
||||
|
||||
// Should have parsed env vars from hlog
|
||||
envVars := loader.GetEnvVars()
|
||||
if len(envVars) == 0 {
|
||||
t.Error("expected env vars from hlog package")
|
||||
}
|
||||
|
||||
// Check for known hlog variables
|
||||
foundLogLevel := false
|
||||
for _, ev := range envVars {
|
||||
if ev.Name == "LOG_LEVEL" {
|
||||
foundLogLevel = true
|
||||
t.Logf("Found LOG_LEVEL: %s", ev.Description)
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if !foundLogLevel {
|
||||
t.Error("expected to find LOG_LEVEL from hlog")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user