Skip to content

Commit

Permalink
feature: SetEnv & Set functions
Browse files Browse the repository at this point in the history
  • Loading branch information
abdahmed22 committed Aug 20, 2024
1 parent 9f2dd0a commit 34c0662
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 15 deletions.
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ module github.com/codescalersinternships/Dotenv-Abdelrahman-Mahmoud

go 1.22.4

require github.com/stretchr/testify v1.9.0

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/testify v1.9.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
35 changes: 32 additions & 3 deletions pkg/dotenv.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ var (
errWrongFormat = errors.New(".env is not in correct format")
errAlreadyExists = errors.New("key value pair already exists")

Check failure on line 15 in pkg/dotenv.go

View workflow job for this annotation

GitHub Actions / lint

var `errAlreadyExists` is unused (unused)
errMissingValue = errors.New("value for the given key is not found")
errEmptyMap = errors.New(" map does not has no key value pairs")
)

type EnvContent struct {
Expand Down Expand Up @@ -114,15 +115,43 @@ func (env *EnvContent) LoadFromFiles(fileNames []string) (map[string]string, err
}

// GetEnv retrives the key value pairs of the .env files
func (env *EnvContent) GetEnv() map[string]string {
return env.keyValuePairs
func (env *EnvContent) GetEnv() (map[string]string, error) {
emptyMap := make(map[string]string)

if fmt.Sprint(emptyMap) == fmt.Sprint(env.keyValuePairs) {
return emptyMap, errEmptyMap
}

return env.keyValuePairs, nil
}

// SetEnv sets the key value pairs to enviroment
func (env *EnvContent) SetEnv() error {

if env.keyValuePairs == nil {
return errEmptyMap
}

for key, value := range env.keyValuePairs {
os.Setenv(key, value)
}

return nil
}

// Get retrives a value for a specific key
// Get retrives a value for a specific key from the env map
func (env *EnvContent) Get(key string) (string, error) {
value := env.keyValuePairs[key]
if value == "" {
return value, errMissingValue
}
return value, nil
}

// Set sets a value for a specific key to the env map
func (env *EnvContent) Set(key string, value string) {
if env.keyValuePairs == nil {
env.keyValuePairs = make(map[string]string)
}
env.keyValuePairs[key] = value
}
64 changes: 53 additions & 11 deletions pkg/dotenv_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dotenv

import (
"os"
"reflect"
"testing"

Expand Down Expand Up @@ -29,9 +30,16 @@ type LoadFromFilesTestCase struct {
}

type GetEnvTestCase struct {
desc string
input string
expectedMap map[string]string
desc string
input string
expectedError error
expectedMap map[string]string
}

type SetEnvTestCase struct {
desc string
path string
expectedError error
}

type GetTestCase struct {
Expand Down Expand Up @@ -535,13 +543,15 @@ func TestENV_GetEnv(t *testing.T) {
emptyMap := make(map[string]string)
testCases := []GetEnvTestCase{
{
desc: "Empty Map",
input: "",
expectedMap: emptyMap,
desc: "Empty Map",
input: "",
expectedError: errEmptyMap,
expectedMap: emptyMap,
},
{
desc: "Only one key = value",
input: "key=value",
desc: "Only one key = value",
input: "key=value",
expectedError: nil,
expectedMap: map[string]string{
"key": "value",
},
Expand All @@ -552,6 +562,7 @@ func TestENV_GetEnv(t *testing.T) {
"key2:value2\n" +
"key3:value3\n" +
"key4:value4",
expectedError: nil,
expectedMap: map[string]string{
"key1": "value1",
"key2": "value2",
Expand All @@ -567,6 +578,7 @@ func TestENV_GetEnv(t *testing.T) {
"key4:value4\n" +
"key5:value5\n" +
"key6:value6",
expectedError: nil,
expectedMap: map[string]string{
"key1": "value1",
"key2": "value2",
Expand All @@ -586,6 +598,7 @@ func TestENV_GetEnv(t *testing.T) {
"key6:value6\n" +
"key7:value7\n" +
"key8:value8",
expectedError: nil,
expectedMap: map[string]string{
"key1": "value1",
"key2": "value2",
Expand All @@ -602,6 +615,7 @@ func TestENV_GetEnv(t *testing.T) {
input: "key1:value1\n " +
"key2:value2\n" +
"key3:value3",
expectedError: nil,
expectedMap: map[string]string{
"key1": "value1",
"key2": "value2",
Expand All @@ -620,6 +634,7 @@ func TestENV_GetEnv(t *testing.T) {
"key8:value8\n" +
"key9:value9\n" +
"key10:value10",
expectedError: nil,
expectedMap: map[string]string{
"key1": "value1",
"key2": "value2",
Expand All @@ -638,10 +653,10 @@ func TestENV_GetEnv(t *testing.T) {
for _, test := range testCases {
t.Run(test.desc, func(t *testing.T) {

_:
parser.LoadFromString(test.input)
resultedMap := parser.GetEnv()
_, _ = parser.LoadFromString(test.input)
resultedMap, resultedError := parser.GetEnv()

assert.Equal(t, test.expectedError, resultedError)
if !reflect.DeepEqual(test.expectedMap, resultedMap) {
t.Fail()
}
Expand All @@ -650,6 +665,32 @@ func TestENV_GetEnv(t *testing.T) {
}
}

func TestENV_SetEnv(t *testing.T) {
parser := EnvContent{}
testCases := []SetEnvTestCase{
{
desc: "Empty file as input",
path: "testdata/test_00.txt",
expectedError: errFileIsEmpty,
},
}
for _, test := range testCases {
t.Run(test.desc, func(t *testing.T) {
envMap, _ := parser.LoadFromFile(test.path)
actualError := parser.SetEnv()

assert.Equal(t, test.expectedError, actualError)

for key, expectedValue := range envMap {
actualValue := os.Getenv(key)

assert.Equal(t, expectedValue, actualValue)
}

})
}
}

func TestINI_Get(t *testing.T) {
parser := EnvContent{}
testCases := []GetTestCase{
Expand Down Expand Up @@ -716,6 +757,7 @@ func TestINI_Get(t *testing.T) {
expectedError: errMissingValue,
},
}

for _, test := range testCases {
t.Run(test.desc, func(t *testing.T) {
_, _ = parser.LoadFromString(test.input)
Expand Down

0 comments on commit 34c0662

Please sign in to comment.