Skip to content

Commit

Permalink
chore: add unit test for env.Load() function
Browse files Browse the repository at this point in the history
  • Loading branch information
edmarfelipe committed Aug 24, 2024
1 parent 54b55eb commit 901286a
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions internal/env/env_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package env_test

import (
"os"
"testing"

"github.com/edmarfelipe/go-ci/internal/env"
)

func TestLoad(t *testing.T) {
testCases := []struct {
name string
env map[string]string
expected *env.Env
}{
{
name: "default values",
env: map[string]string{},
expected: &env.Env{
ServiceAddr: ":8080",
},
},
{
name: "custom values",
env: map[string]string{
"SERVICE_PORT": ":9090",
},
expected: &env.Env{
ServiceAddr: ":9090",
},
},
}

for _, tc := range testCases {
os.Clearenv()
for k, v := range tc.env {
os.Setenv(k, v)
}

env, err := env.Load()
if err != nil {
t.Fatalf("test %s: unexpected error: %v", tc.name, err)
}

if env.ServiceAddr != tc.expected.ServiceAddr {
t.Errorf("test %s: expected ServiceAddr %q, got %q", tc.name, tc.expected.ServiceAddr, env.ServiceAddr)
}
}
}

0 comments on commit 901286a

Please sign in to comment.