diff --git a/envreader/main.go b/envreader/main.go index 9e685f3..dc641f6 100644 --- a/envreader/main.go +++ b/envreader/main.go @@ -1,21 +1,26 @@ package envreader -import "strings" +import ( + "os" + "strings" +) + +var Env func(key string) string + +func init() { + Env = os.Getenv +} // Create a new envReader -func NewEnvReader(env func(key string) string) *EnvReader { +func New() *EnvReader { return &EnvReader{ - Env: env, + Env: Env, } } -// Check if envReader Errs slice is not empty +// Check if envReader has errors func (r *EnvReader) HasErrors() bool { - if len(r.Errs) > 0 { - return true - } - - return false + return len(r.Errs) > 0 } // Returns envReader Errs slice items as a string diff --git a/test/envreader/bool_test.go b/test/envreader/bool_test.go index 7d1b4e9..7c005ac 100644 --- a/test/envreader/bool_test.go +++ b/test/envreader/bool_test.go @@ -2,49 +2,48 @@ package envreader_test import ( "testing" - + "github.com/escaletech/go-escale/envreader" "github.com/escaletech/go-escale/messages" - testUtils "github.com/escaletech/go-escale/test/utils" + "github.com/escaletech/go-escale/test/testutils" "github.com/stretchr/testify/assert" ) +func init() { + envreader.Env = testutils.GetFakeEnv +} + func TestBool(t *testing.T) { + var er *envreader.EnvReader = envreader.New() t.Run("boolOrDefault", func(t *testing.T) { - t.Run("Env has the required value", func(t *testing.T){ + t.Run("Env has the required value", func(t *testing.T) { t.Run("It should return the variable value and error's slice should be empty", func(t *testing.T) { - r := envreader.NewEnvReader(testUtils.GetFakeEnv) - - response := r.BoolOrDefault(testUtils.FakeEnvVarNameBool, false) + response := er.BoolOrDefault(testutils.FakeEnvVarNameBool, false) assert.Equal(t, true, response) - assert.Equal(t, false, r.HasErrors()) + assert.Equal(t, false, er.HasErrors()) }) t.Run("Env doesn't have the required key", func(t *testing.T) { t.Run("It should return a default value and error's slice should be empty", func(t *testing.T) { - r := envreader.NewEnvReader(testUtils.GetFakeEnv) - - response := r.BoolOrDefault("invalidEnvVarName", false) + response := er.BoolOrDefault("invalidEnvVarName", false) assert.Equal(t, false, response) - assert.Equal(t, false, r.HasErrors()) + assert.Equal(t, false, er.HasErrors()) }) }) t.Run("Env has the required key but its value isn't a boolean", func(t *testing.T) { t.Run("It should return false and an error message to the slice", func(t *testing.T) { - r := envreader.NewEnvReader(testUtils.GetFakeEnv) - - response := r.BoolOrDefault(testUtils.FakeEnvVarNameString, false) + response := er.BoolOrDefault(testutils.FakeEnvVarNameString, false) assert.Equal(t, false, response) - assert.Equal(t, true, r.HasErrors()) - assert.Equal(t, messages.UnableToConvertToBool(testUtils.FakeEnvVarNameString), r.Errs[0]) + assert.Equal(t, true, er.HasErrors()) + assert.Equal(t, messages.UnableToConvertToBool(testutils.FakeEnvVarNameString), er.Errs[0]) }) }) }) }) -} \ No newline at end of file +} diff --git a/test/envreader/int_test.go b/test/envreader/int_test.go index 28e9199..4596813 100644 --- a/test/envreader/int_test.go +++ b/test/envreader/int_test.go @@ -2,49 +2,48 @@ package envreader_test import ( "testing" - + "github.com/escaletech/go-escale/envreader" "github.com/escaletech/go-escale/messages" - testUtils "github.com/escaletech/go-escale/test/utils" + "github.com/escaletech/go-escale/test/testutils" "github.com/stretchr/testify/assert" ) func TestInt(t *testing.T) { - t.Run("intOrDefault", func(t *testing.T) { - t.Run("Env has the required value", func(t *testing.T){ + t.Run("Env has the required value", func(t *testing.T) { t.Run("It should return the variable value and error's slice should be empty", func(t *testing.T) { - r := envreader.NewEnvReader(testUtils.GetFakeEnv) + er := envreader.New() - response := r.IntOrDefault(testUtils.FakeEnvVarNameInt, 21) + response := er.IntOrDefault(testutils.FakeEnvVarNameInt, 21) assert.Equal(t, 13, response) - assert.Equal(t, false, r.HasErrors()) + assert.Equal(t, false, er.HasErrors()) }) t.Run("Env doesn't have the required key", func(t *testing.T) { t.Run("It should return a default value and error's slice should be empty", func(t *testing.T) { - r := envreader.NewEnvReader(testUtils.GetFakeEnv) + er := envreader.New() - response := r.IntOrDefault("invalidEnvVarName", 21) + response := er.IntOrDefault("invalidEnvVarName", 21) assert.Equal(t, 21, response) - assert.Equal(t, false, r.HasErrors()) + assert.Equal(t, false, er.HasErrors()) }) }) t.Run("Env has the required key but its value isn't a integer", func(t *testing.T) { t.Run("It should return 0 and an error message to the slice", func(t *testing.T) { - r := envreader.NewEnvReader(testUtils.GetFakeEnv) - - response := r.IntOrDefault(testUtils.FakeEnvVarNameString, 21) + er := envreader.New() + + response := er.IntOrDefault(testutils.FakeEnvVarNameString, 21) assert.Equal(t, 0, response) - assert.Equal(t, true, r.HasErrors()) - assert.Equal(t, messages.UnableToConvertToInt(testUtils.FakeEnvVarNameString), r.Errs[0]) + assert.Equal(t, true, er.HasErrors()) + assert.Equal(t, messages.UnableToConvertToInt(testutils.FakeEnvVarNameString), er.Errs[0]) }) }) }) }) -} \ No newline at end of file +} diff --git a/test/envreader/main_test.go b/test/envreader/main_test.go index e29f065..67ea528 100644 --- a/test/envreader/main_test.go +++ b/test/envreader/main_test.go @@ -5,7 +5,6 @@ import ( "github.com/escaletech/go-escale/envreader" "github.com/escaletech/go-escale/messages" - testUtils "github.com/escaletech/go-escale/test/utils" "github.com/stretchr/testify/assert" ) @@ -14,19 +13,19 @@ func TestMain(t *testing.T) { t.Run("HasErrors", func(t *testing.T) { t.Run("envReader errs slice is empty", func(t *testing.T) { t.Run("returns false", func(t *testing.T) { - r := envreader.NewEnvReader(testUtils.GetFakeEnv) + er := envreader.New() - assert.Equal(t, false, r.HasErrors()) + assert.Equal(t, false, er.HasErrors()) }) }) t.Run("envReader errs slice has items", func(t *testing.T) { t.Run("returns false", func(t *testing.T) { - r := envreader.NewEnvReader(testUtils.GetFakeEnv) + er := envreader.New() - r.StringRequired("WHATEVER") + er.StringRequired("WHATEVER") - assert.Equal(t, true, r.HasErrors()) + assert.Equal(t, true, er.HasErrors()) }) }) }) @@ -34,22 +33,22 @@ func TestMain(t *testing.T) { t.Run("GetErrors", func(t *testing.T) { t.Run("envReader errs slice has items", func(t *testing.T) { t.Run("returns items as a string", func(t *testing.T) { - r := envreader.NewEnvReader(testUtils.GetFakeEnv) + er := envreader.New() - r.StringRequired("WHATEVER") - r.StringRequired("WHATEVER2") + er.StringRequired("WHATEVER") + er.StringRequired("WHATEVER2") expectedErrs := messages.MissingRequiredEnv("WHATEVER") + ", " + messages.MissingRequiredEnv("WHATEVER2") - assert.Equal(t, expectedErrs, r.GetErrors()) + assert.Equal(t, expectedErrs, er.GetErrors()) }) }) t.Run("envReader errs slice is empty", func(t *testing.T) { t.Run("returns an empty string", func(t *testing.T) { - r := envreader.NewEnvReader(testUtils.GetFakeEnv) + er := envreader.New() - assert.Equal(t, "", r.GetErrors()) + assert.Equal(t, "", er.GetErrors()) }) }) }) diff --git a/test/envreader/string_test.go b/test/envreader/string_test.go index 8321deb..2b993b2 100644 --- a/test/envreader/string_test.go +++ b/test/envreader/string_test.go @@ -5,32 +5,36 @@ import ( "github.com/escaletech/go-escale/envreader" "github.com/escaletech/go-escale/messages" - testUtils "github.com/escaletech/go-escale/test/utils" + "github.com/escaletech/go-escale/test/testutils" "github.com/stretchr/testify/assert" ) +func init() { + envreader.Env = testutils.GetFakeEnv +} + func TestString(t *testing.T) { t.Run("StringRequired", func(t *testing.T) { t.Run("Env has the required value", func(t *testing.T) { t.Run("It should return the variable value and error's slice should be empty", func(t *testing.T) { - r := envreader.NewEnvReader(testUtils.GetFakeEnv) + er := envreader.New() - response := r.StringRequired(testUtils.FakeEnvVarNameString) + response := er.StringRequired(testutils.FakeEnvVarNameString) - assert.Equal(t, testUtils.FakeEnvVarValueString, response) - assert.Equal(t, false, r.HasErrors()) + assert.Equal(t, testutils.FakeEnvVarValueString, response) + assert.Equal(t, false, er.HasErrors()) }) }) t.Run("Env doesn't have the required value", func(t *testing.T) { t.Run("It should return an empty string and add an error message to the slice", func(t *testing.T) { - r := envreader.NewEnvReader(testUtils.GetFakeEnv) + er := envreader.New() - response := r.StringRequired("invalidEnvVarName") + response := er.StringRequired("invalidEnvVarName") assert.Equal(t, "", response) - assert.Equal(t, messages.MissingRequiredEnv("invalidEnvVarName"), r.Errs[0]) + assert.Equal(t, messages.MissingRequiredEnv("invalidEnvVarName"), er.Errs[0]) }) }) }) @@ -38,23 +42,23 @@ func TestString(t *testing.T) { t.Run("StringOrDefault", func(t *testing.T) { t.Run("Env has the required value", func(t *testing.T) { t.Run("It should return the variable value and error's slice should be empty", func(t *testing.T) { - r := envreader.NewEnvReader(testUtils.GetFakeEnv) + er := envreader.New() - response := r.StringOrDefault(testUtils.FakeEnvVarNameString, "defaultValue") + response := er.StringOrDefault(testutils.FakeEnvVarNameString, "defaultValue") - assert.Equal(t, testUtils.FakeEnvVarValueString, response) - assert.Equal(t, false, r.HasErrors()) + assert.Equal(t, testutils.FakeEnvVarValueString, response) + assert.Equal(t, false, er.HasErrors()) }) }) t.Run("Env doesn't have the required value", func(t *testing.T) { t.Run("It should return a default value and error's slice should be empty", func(t *testing.T) { - r := envreader.NewEnvReader(testUtils.GetFakeEnv) + er := envreader.New() - response := r.StringOrDefault("invalidEnvVarName", "defaultValue") + response := er.StringOrDefault("invalidEnvVarName", "defaultValue") assert.Equal(t, "defaultValue", response) - assert.Equal(t, false, r.HasErrors()) + assert.Equal(t, false, er.HasErrors()) }) }) }) diff --git a/test/utils/envReaderUtils.go b/test/testutils/envReaderUtils.go similarity index 51% rename from test/utils/envReaderUtils.go rename to test/testutils/envReaderUtils.go index 6815de0..5040aa8 100644 --- a/test/utils/envReaderUtils.go +++ b/test/testutils/envReaderUtils.go @@ -1,4 +1,4 @@ -package test_utils +package testutils var FakeEnvVarNameString = "fakeEnvString" var FakeEnvVarValueString = "fakeEnvValue" @@ -7,15 +7,14 @@ var FakeEnvVarValueBool = "true" var FakeEnvVarNameInt = "fakeEnvInt" var FakeEnvVarValueInt = "13" -func GetFakeEnv(key string) string { - switch { - case key == FakeEnvVarNameString: - return FakeEnvVarValueString - case key == FakeEnvVarNameBool: - return FakeEnvVarValueBool - case key == FakeEnvVarNameInt: - return FakeEnvVarValueInt - default: - return "" +func setFakeValues() map[string]string { + return map[string]string{ + FakeEnvVarNameString: FakeEnvVarValueString, + FakeEnvVarNameBool: FakeEnvVarValueBool, + FakeEnvVarNameInt: FakeEnvVarValueInt, } -} \ No newline at end of file +} + +func GetFakeEnv(key string) string { + return setFakeValues()[key] +}