updated ezconf
This commit is contained in:
@@ -1,21 +1,19 @@
|
||||
package ezconf
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestParseEnvComment(t *testing.T) {
|
||||
func TestParseEzconfTag(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
comment string
|
||||
tag string
|
||||
wantEnvVar *EnvVar
|
||||
expectError bool
|
||||
}{
|
||||
{
|
||||
name: "simple env variable",
|
||||
comment: "ENV LOG_LEVEL: Log level for the application",
|
||||
name: "simple env variable",
|
||||
tag: "LOG_LEVEL,description:Log level for the application",
|
||||
wantEnvVar: &EnvVar{
|
||||
Name: "LOG_LEVEL",
|
||||
Description: "Log level for the application",
|
||||
@@ -25,8 +23,8 @@ func TestParseEnvComment(t *testing.T) {
|
||||
expectError: false,
|
||||
},
|
||||
{
|
||||
name: "env variable with default",
|
||||
comment: "ENV LOG_LEVEL: Log level for the application (default: info)",
|
||||
name: "env variable with default",
|
||||
tag: "LOG_LEVEL,description:Log level for the application,default:info",
|
||||
wantEnvVar: &EnvVar{
|
||||
Name: "LOG_LEVEL",
|
||||
Description: "Log level for the application",
|
||||
@@ -36,8 +34,8 @@ func TestParseEnvComment(t *testing.T) {
|
||||
expectError: false,
|
||||
},
|
||||
{
|
||||
name: "required env variable",
|
||||
comment: "ENV DATABASE_URL: Database connection string (required)",
|
||||
name: "required env variable",
|
||||
tag: "DATABASE_URL,description:Database connection string,required",
|
||||
wantEnvVar: &EnvVar{
|
||||
Name: "DATABASE_URL",
|
||||
Description: "Database connection string",
|
||||
@@ -47,25 +45,36 @@ func TestParseEnvComment(t *testing.T) {
|
||||
expectError: false,
|
||||
},
|
||||
{
|
||||
name: "required with condition and default",
|
||||
comment: "ENV LOG_DIR: Directory for log files (required when LOG_OUTPUT is file) (default: /var/log)",
|
||||
name: "required with condition and default",
|
||||
tag: "LOG_DIR,description:Directory for log files,required:when LOG_OUTPUT is file,default:/var/log",
|
||||
wantEnvVar: &EnvVar{
|
||||
Name: "LOG_DIR",
|
||||
Description: "Directory for log files",
|
||||
Description: "Directory for log files (required when LOG_OUTPUT is file)",
|
||||
Required: true,
|
||||
Default: "/var/log",
|
||||
},
|
||||
expectError: false,
|
||||
},
|
||||
{
|
||||
name: "missing colon",
|
||||
comment: "ENV LOG_LEVEL Log level",
|
||||
name: "name only",
|
||||
tag: "SIMPLE_VAR",
|
||||
wantEnvVar: &EnvVar{
|
||||
Name: "SIMPLE_VAR",
|
||||
Description: "",
|
||||
Required: false,
|
||||
Default: "",
|
||||
},
|
||||
expectError: false,
|
||||
},
|
||||
{
|
||||
name: "empty tag",
|
||||
tag: "",
|
||||
wantEnvVar: nil,
|
||||
expectError: true,
|
||||
},
|
||||
{
|
||||
name: "not an ENV comment",
|
||||
comment: "This is a regular comment",
|
||||
name: "empty name",
|
||||
tag: ",description:some desc",
|
||||
wantEnvVar: nil,
|
||||
expectError: true,
|
||||
},
|
||||
@@ -73,7 +82,7 @@ func TestParseEnvComment(t *testing.T) {
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
envVar, err := parseEnvComment(tt.comment)
|
||||
envVar, err := parseEzconfTag(tt.tag)
|
||||
|
||||
if tt.expectError {
|
||||
if err == nil {
|
||||
@@ -103,32 +112,17 @@ func TestParseEnvComment(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseConfigFile(t *testing.T) {
|
||||
// Create a temporary test file
|
||||
tempDir := t.TempDir()
|
||||
testFile := filepath.Join(tempDir, "config.go")
|
||||
|
||||
content := `package testpkg
|
||||
|
||||
type Config struct {
|
||||
// ENV LOG_LEVEL: Log level for the application (default: info)
|
||||
LogLevel string
|
||||
|
||||
// ENV LOG_OUTPUT: Output destination (default: console)
|
||||
LogOutput string
|
||||
|
||||
// ENV DATABASE_URL: Database connection string (required)
|
||||
DatabaseURL string
|
||||
}
|
||||
`
|
||||
|
||||
if err := os.WriteFile(testFile, []byte(content), 0644); err != nil {
|
||||
t.Fatalf("failed to create test file: %v", err)
|
||||
func TestParseConfigStruct(t *testing.T) {
|
||||
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"`
|
||||
NoTag string
|
||||
}
|
||||
|
||||
envVars, err := ParseConfigFile(testFile)
|
||||
envVars, err := ParseConfigStruct(&TestConfig{})
|
||||
if err != nil {
|
||||
t.Fatalf("ParseConfigFile failed: %v", err)
|
||||
t.Fatalf("ParseConfigStruct failed: %v", err)
|
||||
}
|
||||
|
||||
if len(envVars) != 3 {
|
||||
@@ -152,51 +146,70 @@ type Config struct {
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseConfigPackage(t *testing.T) {
|
||||
// Test with actual hlog package
|
||||
hlogPath := filepath.Join("..", "hlog")
|
||||
if _, err := os.Stat(hlogPath); os.IsNotExist(err) {
|
||||
t.Skip("hlog package not found, skipping integration test")
|
||||
}
|
||||
|
||||
envVars, err := ParseConfigPackage(hlogPath)
|
||||
if err != nil {
|
||||
t.Fatalf("ParseConfigPackage failed: %v", err)
|
||||
}
|
||||
|
||||
if len(envVars) == 0 {
|
||||
t.Error("expected at least one env var from hlog package")
|
||||
}
|
||||
|
||||
// Check for known hlog variables
|
||||
foundLogLevel := false
|
||||
for _, envVar := range envVars {
|
||||
if envVar.Name == "LOG_LEVEL" {
|
||||
foundLogLevel = true
|
||||
t.Logf("Found LOG_LEVEL: %s", envVar.Description)
|
||||
}
|
||||
}
|
||||
|
||||
if !foundLogLevel {
|
||||
t.Error("expected to find LOG_LEVEL in hlog package")
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseConfigFile_InvalidFile(t *testing.T) {
|
||||
_, err := ParseConfigFile("/nonexistent/file.go")
|
||||
func TestParseConfigStruct_NilPointer(t *testing.T) {
|
||||
_, err := ParseConfigStruct(nil)
|
||||
if err == nil {
|
||||
t.Error("expected error for nonexistent file")
|
||||
t.Error("expected error for nil pointer")
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseConfigPackage_InvalidPath(t *testing.T) {
|
||||
envVars, err := ParseConfigPackage("/nonexistent/package")
|
||||
func TestParseConfigStruct_NotPointer(t *testing.T) {
|
||||
type TestConfig struct {
|
||||
Foo string `ezconf:"FOO,description:test"`
|
||||
}
|
||||
_, err := ParseConfigStruct(TestConfig{})
|
||||
if err == nil {
|
||||
t.Error("expected error for non-pointer")
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseConfigStruct_NotStruct(t *testing.T) {
|
||||
str := "not a struct"
|
||||
_, err := ParseConfigStruct(&str)
|
||||
if err == nil {
|
||||
t.Error("expected error for non-struct pointer")
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseConfigStruct_NoTags(t *testing.T) {
|
||||
type EmptyConfig struct {
|
||||
Foo string
|
||||
Bar int
|
||||
}
|
||||
|
||||
envVars, err := ParseConfigStruct(&EmptyConfig{})
|
||||
if err != nil {
|
||||
t.Fatalf("ParseConfigPackage should not error on invalid path: %v", err)
|
||||
t.Fatalf("ParseConfigStruct failed: %v", err)
|
||||
}
|
||||
|
||||
// Should return empty slice for invalid path
|
||||
if len(envVars) != 0 {
|
||||
t.Errorf("expected 0 env vars for invalid path, got %d", len(envVars))
|
||||
t.Errorf("expected 0 env vars for struct with no tags, got %d", len(envVars))
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseConfigStruct_UnexportedFields(t *testing.T) {
|
||||
type TestConfig struct {
|
||||
exported string `ezconf:"EXPORTED,description:An exported field"`
|
||||
unexported string `ezconf:"UNEXPORTED,description:An unexported field"`
|
||||
}
|
||||
|
||||
envVars, err := ParseConfigStruct(&TestConfig{})
|
||||
if err != nil {
|
||||
t.Fatalf("ParseConfigStruct failed: %v", err)
|
||||
}
|
||||
|
||||
if len(envVars) != 2 {
|
||||
t.Errorf("expected 2 env vars (both exported and unexported), got %d", len(envVars))
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseConfigStruct_InvalidTag(t *testing.T) {
|
||||
type TestConfig struct {
|
||||
Bad string `ezconf:",description:missing name"`
|
||||
}
|
||||
|
||||
_, err := ParseConfigStruct(&TestConfig{})
|
||||
if err == nil {
|
||||
t.Error("expected error for invalid tag")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user