Skip to content

Commit

Permalink
add more test
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelhenry committed Mar 3, 2023
1 parent e09b125 commit 487cf77
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
33 changes: 33 additions & 0 deletions envject_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,39 @@ func TestVenvInject(t *testing.T){
}
}

func TestVenvInjectWithInvalidFormatting(t *testing.T){

os.Setenv("KEY1", "This is value 1")
os.Setenv("KEY2", "This is value 2")
os.Setenv("KEY3", "This is value 3")
os.Setenv("KEY4", "This is value 4")
os.Setenv("KEY5", "This is value 5")

configText := `
struct AppConfig {
let key1 = "$KEY1"
let key2 = "${KEY2)"
let key3 = "$((KEY3))"
let key4 = "$(KEY4}"
let key5 = "$(KEY5"
}
`
want := `
struct AppConfig {
let key1 = "This is value 1"
let key2 = "${KEY2)"
let key3 = "$((KEY3))"
let key4 = "$(KEY4}"
let key5 = "$(KEY5"
}
`
got := ReplaceEnvVariables(configText)

if got != want {
t.Errorf("got %q, wanted %q", got, want)
}
}

func TestVenvInjectWithNoSetEnvValues(t *testing.T){

os.Unsetenv("KEY1")
Expand Down
4 changes: 3 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ import (
"strings"
)



func ReplaceEnvVariables(input string) string {
// Handling the following formats:
// - $ENV_NAME
// - ${ENV_NAME}
// - $(ENV_NAME)
envPattern := regexp.MustCompile(`\$([\{|\(]?(\w+)[\}|\)]?)\.?`)
envPattern := regexp.MustCompile(`\$(\w+)|\$(\{?(\w+)\})\.?|\$(\(?(\w+)\))\.?`)

// Replace all matches with the corresponding environment variable value
return envPattern.ReplaceAllStringFunc(input, func(match string) string {
Expand Down

0 comments on commit 487cf77

Please sign in to comment.