11package config
22
33import (
4- "os"
54 "testing"
65 "time"
76)
87
98func TestLoad (t * testing.T ) {
109 // Set required environment variables
11- os .Setenv ("DATABASE_URL" , "postgres://test:test@localhost:5432/test" )
12- os .Setenv ("JWT_SECRET" , "this-is-a-very-long-secret-key-for-testing-purposes" )
13- os .Setenv ("S3_BUCKET" , "test-bucket" )
14- defer func () {
15- os .Unsetenv ("DATABASE_URL" )
16- os .Unsetenv ("JWT_SECRET" )
17- os .Unsetenv ("S3_BUCKET" )
18- }()
10+ t .Setenv ("DATABASE_URL" , "postgres://test:test@localhost:5432/test" )
11+ t .Setenv ("JWT_SECRET" , "this-is-a-very-long-secret-key-for-testing-purposes" )
12+ t .Setenv ("S3_BUCKET" , "test-bucket" )
1913
2014 cfg , err := Load ()
2115 if err != nil {
@@ -36,10 +30,8 @@ func TestLoad(t *testing.T) {
3630}
3731
3832func TestLoadMissingRequired (t * testing.T ) {
39- // Clear any existing env vars
40- os .Unsetenv ("DATABASE_URL" )
41- os .Unsetenv ("JWT_SECRET" )
42- os .Unsetenv ("S3_BUCKET" )
33+ // t.Setenv automatically clears after test
34+ // Don't set required vars to test missing validation
4335
4436 _ , err := Load ()
4537 if err == nil {
@@ -48,14 +40,9 @@ func TestLoadMissingRequired(t *testing.T) {
4840}
4941
5042func TestValidateJWTSecretLength (t * testing.T ) {
51- os .Setenv ("DATABASE_URL" , "postgres://test:test@localhost:5432/test" )
52- os .Setenv ("JWT_SECRET" , "short" ) // Too short
53- os .Setenv ("S3_BUCKET" , "test-bucket" )
54- defer func () {
55- os .Unsetenv ("DATABASE_URL" )
56- os .Unsetenv ("JWT_SECRET" )
57- os .Unsetenv ("S3_BUCKET" )
58- }()
43+ t .Setenv ("DATABASE_URL" , "postgres://test:test@localhost:5432/test" )
44+ t .Setenv ("JWT_SECRET" , "short" ) // Too short
45+ t .Setenv ("S3_BUCKET" , "test-bucket" )
5946
6047 _ , err := Load ()
6148 if err == nil {
@@ -64,49 +51,39 @@ func TestValidateJWTSecretLength(t *testing.T) {
6451}
6552
6653func TestGetEnvDefaults (t * testing.T ) {
67- os .Unsetenv ("TEST_VAR" )
68-
69- if got := getEnv ("TEST_VAR" , "default" ); got != "default" {
54+ if got := getEnv ("TEST_VAR_NONEXISTENT" , "default" ); got != "default" {
7055 t .Errorf ("expected default, got %s" , got )
7156 }
7257}
7358
7459func TestGetIntEnv (t * testing.T ) {
75- os .Setenv ("TEST_INT" , "42" )
76- defer os .Unsetenv ("TEST_INT" )
60+ t .Setenv ("TEST_INT" , "42" )
7761
7862 if got := getIntEnv ("TEST_INT" , 0 ); got != 42 {
7963 t .Errorf ("expected 42, got %d" , got )
8064 }
8165}
8266
8367func TestGetBoolEnv (t * testing.T ) {
84- os .Setenv ("TEST_BOOL" , "true" )
85- defer os .Unsetenv ("TEST_BOOL" )
68+ t .Setenv ("TEST_BOOL" , "true" )
8669
8770 if got := getBoolEnv ("TEST_BOOL" , false ); ! got {
8871 t .Error ("expected true" )
8972 }
9073}
9174
9275func TestGetDurationEnv (t * testing.T ) {
93- os .Setenv ("TEST_DURATION" , "30s" )
94- defer os .Unsetenv ("TEST_DURATION" )
76+ t .Setenv ("TEST_DURATION" , "30s" )
9577
9678 if got := getDurationEnv ("TEST_DURATION" , time .Second ); got != 30 * time .Second {
9779 t .Errorf ("expected 30s, got %v" , got )
9880 }
9981}
10082
10183func TestDefaultValues (t * testing.T ) {
102- os .Setenv ("DATABASE_URL" , "postgres://test:test@localhost:5432/test" )
103- os .Setenv ("JWT_SECRET" , "this-is-a-very-long-secret-key-for-testing-purposes" )
104- os .Setenv ("S3_BUCKET" , "test-bucket" )
105- defer func () {
106- os .Unsetenv ("DATABASE_URL" )
107- os .Unsetenv ("JWT_SECRET" )
108- os .Unsetenv ("S3_BUCKET" )
109- }()
84+ t .Setenv ("DATABASE_URL" , "postgres://test:test@localhost:5432/test" )
85+ t .Setenv ("JWT_SECRET" , "this-is-a-very-long-secret-key-for-testing-purposes" )
86+ t .Setenv ("S3_BUCKET" , "test-bucket" )
11087
11188 cfg , err := Load ()
11289 if err != nil {
0 commit comments