updated ezconf

This commit is contained in:
2026-02-25 21:52:57 +11:00
parent 05be28d7f3
commit 9179736c90
7 changed files with 380 additions and 387 deletions

View File

@@ -2,11 +2,17 @@ package ezconf
import (
"os"
"path/filepath"
"strings"
"testing"
)
// testConfig is a Config struct used by multiple tests
type testConfig struct {
LogLevel string `ezconf:"LOG_LEVEL,description:Log level for the application,default:info"`
LogOutput string `ezconf:"LOG_OUTPUT,description:Output destination,default:console"`
DatabaseURL string `ezconf:"DATABASE_URL,description:Database connection string,required"`
}
func TestNew(t *testing.T) {
loader := New()
if loader == nil {
@@ -16,8 +22,8 @@ func TestNew(t *testing.T) {
if loader.configFuncs == nil {
t.Error("configFuncs map is nil")
}
if loader.packagePaths == nil {
t.Error("packagePaths slice is nil")
if loader.configStructs == nil {
t.Error("configStructs slice is nil")
}
if loader.extraEnvVars == nil {
t.Error("extraEnvVars slice is nil")
@@ -66,35 +72,39 @@ func TestAddConfigFunc_EmptyName(t *testing.T) {
}
}
func TestAddPackagePath(t *testing.T) {
func TestAddConfigStruct(t *testing.T) {
loader := New()
// Use current directory as test path
err := loader.AddPackagePath(".")
err := loader.AddConfigStruct(&testConfig{}, "Test")
if err != nil {
t.Errorf("AddPackagePath failed: %v", err)
t.Errorf("AddConfigStruct failed: %v", err)
}
if len(loader.packagePaths) != 1 {
t.Errorf("expected 1 package path, got %d", len(loader.packagePaths))
if len(loader.configStructs) != 1 {
t.Errorf("expected 1 config struct, got %d", len(loader.configStructs))
}
}
func TestAddPackagePath_InvalidPath(t *testing.T) {
func TestAddConfigStruct_NilPointer(t *testing.T) {
loader := New()
err := loader.AddPackagePath("/nonexistent/path")
err := loader.AddConfigStruct(nil, "Test")
if err == nil {
t.Error("expected error for nonexistent path")
t.Error("expected error for nil pointer")
}
}
func TestAddPackagePath_EmptyPath(t *testing.T) {
func TestAddConfigStruct_EmptyGroupName(t *testing.T) {
loader := New()
err := loader.AddPackagePath("")
if err == nil {
t.Error("expected error for empty path")
err := loader.AddConfigStruct(&testConfig{}, "")
if err != nil {
t.Errorf("AddConfigStruct failed: %v", err)
}
// Should default to "Other"
if loader.configStructs[0].groupName != "Other" {
t.Errorf("expected group name 'Other', got %s", loader.configStructs[0].groupName)
}
}
@@ -131,8 +141,8 @@ func TestLoad(t *testing.T) {
return testCfg, nil
})
// Add current package path
loader.AddPackagePath(".")
// Add config struct for tag parsing
loader.AddConfigStruct(&testConfig{}, "Test")
// Add an extra env var
loader.AddEnvVar(EnvVar{
@@ -249,8 +259,8 @@ func TestParseEnvVars(t *testing.T) {
return "test config", nil
})
// Add current package path
loader.AddPackagePath(".")
// Add config struct for tag parsing
loader.AddConfigStruct(&testConfig{}, "Test")
// Add an extra env var
loader.AddEnvVar(EnvVar{
@@ -353,8 +363,8 @@ func TestParseEnvVars_Then_LoadConfigs(t *testing.T) {
return testCfg, nil
})
// Add current package path
loader.AddPackagePath(".")
// Add config struct for tag parsing
loader.AddConfigStruct(&testConfig{}, "Test")
// Add an extra env var
loader.AddEnvVar(EnvVar{
@@ -398,63 +408,68 @@ func TestParseEnvVars_Then_LoadConfigs(t *testing.T) {
}
}
func TestLoad_Integration(t *testing.T) {
// Integration test with real hlog package
hlogPath := filepath.Join("..", "hlog")
if _, err := os.Stat(hlogPath); os.IsNotExist(err) {
t.Skip("hlog package not found, skipping integration test")
}
func TestParseEnvVars_GroupName(t *testing.T) {
loader := New()
// Add hlog package
if err := loader.AddPackagePath(hlogPath); err != nil {
t.Fatalf("failed to add hlog package: %v", err)
}
loader.AddConfigStruct(&testConfig{}, "MyGroup")
// Load without config function (just parse)
if err := loader.Load(); err != nil {
t.Fatalf("Load failed: %v", err)
err := loader.ParseEnvVars()
if err != nil {
t.Fatalf("ParseEnvVars failed: %v", err)
}
envVars := loader.GetEnvVars()
if len(envVars) == 0 {
t.Error("expected env vars from hlog package")
}
t.Logf("Found %d environment variables from hlog", len(envVars))
for _, ev := range envVars {
t.Logf(" %s: %s (default: %s, required: %t)", ev.Name, ev.Description, ev.Default, ev.Required)
if ev.Group != "MyGroup" {
t.Errorf("expected group 'MyGroup', got '%s' for var %s", ev.Group, ev.Name)
}
}
}
func TestParseEnvVars_GenerateEnvFile_Integration(t *testing.T) {
// Test the new separated ParseEnvVars functionality
hlogPath := filepath.Join("..", "hlog")
if _, err := os.Stat(hlogPath); os.IsNotExist(err) {
t.Skip("hlog package not found, skipping integration test")
}
func TestParseEnvVars_CurrentValues(t *testing.T) {
loader := New()
// Add hlog package
if err := loader.AddPackagePath(hlogPath); err != nil {
t.Fatalf("failed to add hlog package: %v", err)
loader.AddConfigStruct(&testConfig{}, "Test")
// Set an env var
t.Setenv("LOG_LEVEL", "debug")
err := loader.ParseEnvVars()
if err != nil {
t.Fatalf("ParseEnvVars failed: %v", err)
}
// Parse env vars without loading configs (this should work even if required env vars are missing)
envVars := loader.GetEnvVars()
for _, ev := range envVars {
if ev.Name == "LOG_LEVEL" {
if ev.CurrentValue != "debug" {
t.Errorf("expected CurrentValue 'debug', got '%s'", ev.CurrentValue)
}
return
}
}
t.Error("LOG_LEVEL not found in env vars")
}
func TestParseEnvVars_GenerateEnvFile_Integration(t *testing.T) {
loader := New()
// Add config struct for tag parsing
loader.AddConfigStruct(&testConfig{}, "Test")
// Parse env vars
if err := loader.ParseEnvVars(); err != nil {
t.Fatalf("ParseEnvVars failed: %v", err)
}
envVars := loader.GetEnvVars()
if len(envVars) == 0 {
t.Error("expected env vars from hlog package")
t.Error("expected env vars from config struct")
}
// Now test that we can generate an env file without calling Load()
tempDir := t.TempDir()
envFile := filepath.Join(tempDir, "test-generated.env")
envFile := tempDir + "/test-generated.env"
err := loader.GenerateEnvFile(envFile, false)
if err != nil {
@@ -472,16 +487,16 @@ func TestParseEnvVars_GenerateEnvFile_Integration(t *testing.T) {
t.Error("expected header in generated file")
}
// Should contain environment variables from hlog
foundHlogVar := false
// Should contain environment variables from config struct
foundVar := false
for _, ev := range envVars {
if strings.Contains(output, ev.Name) {
foundHlogVar = true
foundVar = true
break
}
}
if !foundHlogVar {
t.Error("expected to find at least one hlog environment variable in generated file")
if !foundVar {
t.Error("expected to find at least one environment variable in generated file")
}
t.Logf("Successfully generated env file with %d variables", len(envVars))