feat(infra): funciones impuras Go para carga de env/config (dotenv, env_require, config_from_env, config_from_file)
- dotenv_load: parser .env con no-sobreescritura y soporte de comillas - env_require: os.Getenv con error descriptivo fail-fast - env_require_all: verifica multiples vars y lista todas las faltantes - config_from_env: reflection sobre struct tags env/default/required/secret, 5 tipos soportados - config_from_file: JSON via stdlib, YAML stub con not-implemented - 25 tests unitarios, todos PASS Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,74 @@
|
||||
package infra
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestEnvRequire(t *testing.T) {
|
||||
t.Run("retorna valor cuando variable esta seteada", func(t *testing.T) {
|
||||
t.Setenv("ENV_REQUIRE_TEST_KEY", "myvalue")
|
||||
got, err := EnvRequire("ENV_REQUIRE_TEST_KEY")
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
if got != "myvalue" {
|
||||
t.Errorf("expected myvalue, got %q", got)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("retorna error con mensaje descriptivo si variable no existe", func(t *testing.T) {
|
||||
got, err := EnvRequire("ENV_REQUIRE_DEFINITELY_NOT_SET_XYZ")
|
||||
if err == nil {
|
||||
t.Error("expected error for missing variable")
|
||||
}
|
||||
if got != "" {
|
||||
t.Errorf("expected empty string on error, got %q", got)
|
||||
}
|
||||
if err != nil && len(err.Error()) == 0 {
|
||||
t.Error("error message should not be empty")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("variable vacia se trata como no seteada", func(t *testing.T) {
|
||||
t.Setenv("ENV_REQUIRE_EMPTY_KEY", "")
|
||||
_, err := EnvRequire("ENV_REQUIRE_EMPTY_KEY")
|
||||
if err == nil {
|
||||
t.Error("expected error for empty variable")
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestEnvRequireAll(t *testing.T) {
|
||||
t.Run("retorna mapa con todos los valores cuando estan seteados", func(t *testing.T) {
|
||||
t.Setenv("REQUIRE_ALL_A", "alpha")
|
||||
t.Setenv("REQUIRE_ALL_B", "beta")
|
||||
got, err := EnvRequireAll([]string{"REQUIRE_ALL_A", "REQUIRE_ALL_B"})
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
if got["REQUIRE_ALL_A"] != "alpha" || got["REQUIRE_ALL_B"] != "beta" {
|
||||
t.Errorf("unexpected map: %v", got)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("acumula todos los errores de variables faltantes", func(t *testing.T) {
|
||||
_, err := EnvRequireAll([]string{"REQUIRE_ALL_MISSING_1", "REQUIRE_ALL_MISSING_2"})
|
||||
if err == nil {
|
||||
t.Error("expected error")
|
||||
}
|
||||
msg := err.Error()
|
||||
if len(msg) == 0 {
|
||||
t.Error("expected non-empty error message")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("lista vacia retorna mapa vacio sin error", func(t *testing.T) {
|
||||
got, err := EnvRequireAll([]string{})
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
if len(got) != 0 {
|
||||
t.Errorf("expected empty map, got %v", got)
|
||||
}
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user