@@ -10,12 +10,16 @@ import (
1010func TestLoadConfig (t * testing.T ) {
1111 // Create a temporary .env file for testing
1212 tempEnvFile := ".env.test"
13- os .WriteFile (tempEnvFile , []byte ("DATABASE_URL=test_db_url\n SERVER_PORT=8081" ), 0644 )
14- defer os .Remove (tempEnvFile ) // Clean up the temporary file
13+ err := os .WriteFile (tempEnvFile , []byte ("DATABASE_URL=test_db_url\n SERVER_PORT=8081" ), 0644 )
14+ assert .NoError (t , err )
15+ defer func () {
16+ err := os .Remove (tempEnvFile )
17+ assert .NoError (t , err )
18+ }()
1519
1620 // Unset environment variables that might interfere with the test
17- os .Unsetenv ("DATABASE_URL" )
18- os .Unsetenv ("SERVER_PORT" )
21+ _ = os .Unsetenv ("DATABASE_URL" )
22+ _ = os .Unsetenv ("SERVER_PORT" )
1923
2024 cfg , err := LoadConfig (tempEnvFile )
2125
@@ -25,24 +29,34 @@ func TestLoadConfig(t *testing.T) {
2529 assert .Equal (t , "8081" , cfg .ServerPort )
2630
2731 // Test with missing .env file (should not return an error)
28- os .Remove (tempEnvFile )
32+ err = os .Remove (tempEnvFile )
33+ assert .NoError (t , err )
34+
2935 cfg , err = LoadConfig ()
3036 assert .NoError (t , err )
3137 assert .NotNil (t , cfg )
3238
3339 // Clean up environment variables set by the test
34- os .Unsetenv ("DATABASE_URL" )
35- os .Unsetenv ("SERVER_PORT" )
40+ _ = os .Unsetenv ("DATABASE_URL" )
41+ _ = os .Unsetenv ("SERVER_PORT" )
3642}
3743
3844func TestLoadConfig_EnvVarsOverride (t * testing.T ) {
3945 // Set environment variables directly
40- os .Setenv ("DATABASE_URL" , "env_db_url" )
41- os .Setenv ("SERVER_PORT" , "9000" )
42- // Create a temporary .env file (should be overridden by direct env vars)
46+ err := os .Setenv ("DATABASE_URL" , "env_db_url" )
47+ assert .NoError (t , err )
48+
49+ err = os .Setenv ("SERVER_PORT" , "9000" )
50+ assert .NoError (t , err )
51+
52+ // Create a temporary .env file (should be overridden by env vars)
4353 tempEnvFile := ".env.test"
44- os .WriteFile (tempEnvFile , []byte ("DATABASE_URL=file_db_url\n SERVER_PORT=8082" ), 0644 )
45- defer os .Remove (tempEnvFile )
54+ err = os .WriteFile (tempEnvFile , []byte ("DATABASE_URL=file_db_url\n SERVER_PORT=8082" ), 0644 )
55+ assert .NoError (t , err )
56+ defer func () {
57+ err := os .Remove (tempEnvFile )
58+ assert .NoError (t , err )
59+ }()
4660
4761 cfg , err := LoadConfig (tempEnvFile )
4862
0 commit comments