Skip to content

Commit

Permalink
[#17] wrap env loading inside library implementation with unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
coderx31 committed Jan 15, 2025
1 parent 59d2476 commit 537b03f
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 0 deletions.
36 changes: 36 additions & 0 deletions parse.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package goconf

import "github.com/caarlos0/env/v11"

// ParseEnv parse the env values to given struct fields
// env variables are defined using struct tags. It utilizes the "github.com/caarlos0/env/v11"
// package for parse.
//
// Parameters:
// - config (interface{}): The struct to be populated by env variables. The struct should have
// env variables defined using tags such as `env:"USERNAME"`.
//
// Returns:
// - error: Returns nil if the struct created using env variables. if fails, it returns an error
// which provides details information about the error
//
// Usage Example:
//
// type Config struct {
// Name string `env:"MY_NAME"`
// Age int `env:"MY_AGE"`
// Team string `env:"MY_TEAM" envDefault:"Backend"`
// }
//
// var conf Config
// if err := Register(&conf); err != nil {
// // Handle env pass error, e.g., log or return
// }
//
// Note:
// - The function will panic if the `config` parameter is not a pointer to struct
//
// More env package information https://github.com/caarlos0/env/v11
func ParseEnv(config interface{}) error {
return env.Parse(config)
}
81 changes: 81 additions & 0 deletions parse_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package goconf

import (
"github.com/stretchr/testify/assert"
"os"
"testing"
)

type Conf struct {
Name string `env:"MY_NAME"`
Age int `env:"MY_AGE"`
Team string `env:"MY_TEAM"`
}

func TestParseEnv_Success(t *testing.T) {
updateEnv()
tests := []struct {
name string
input Conf
expectedOutput Conf
errorExpected bool
}{
{
name: "Successfully parsed the configs",
input: Conf{},
expectedOutput: Conf{
Name: "coderx",
Age: 99,
Team: "backend",
},
errorExpected: false,
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
err := ParseEnv(&test.input)
if !test.errorExpected {
assert.NoError(t, err)
assert.Equal(t, test.expectedOutput, test.input)
} else {
assert.Error(t, err)
}
})
}
}

func TestParseEnv_Failure(t *testing.T) {
updateEnv()
tests := []struct {
name string
input *Conf
expectedOutput *Conf
errorExpected bool
}{
{
name: "config parse failure scenario",
input: &Conf{},
expectedOutput: &Conf{},
errorExpected: true,
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
err := ParseEnv(&test.input)
if !test.errorExpected {
assert.NoError(t, err)
assert.Equal(t, test.expectedOutput, test.input)
} else {
assert.Error(t, err)
}
})
}
}

func updateEnv() {
_ = os.Setenv("MY_NAME", "coderx")
_ = os.Setenv("MY_AGE", "99")
_ = os.Setenv("MY_TEAM", "backend")
}

0 comments on commit 537b03f

Please sign in to comment.