Skip to content

Commit

Permalink
Tests and comments for utils functions
Browse files Browse the repository at this point in the history
  • Loading branch information
janboll committed Nov 25, 2024
1 parent 4cb8f93 commit 2c6b1ba
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
3 changes: 3 additions & 0 deletions tooling/templatize/pkg/utils/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"strings"
)

// GetOsVariable looks up OS environment variables and returns them as a map.
// It also sets a special environment variable RUNS_IN_TEMPLATIZE to 1.
func GetOsVariable() map[string]string {
envVars := make(map[string]string)
envVars["RUNS_IN_TEMPLATIZE"] = "1"
Expand All @@ -23,6 +25,7 @@ func GetOsVariable() map[string]string {
return envVars
}

// MapToEnvVarArray converts a map of environment variables to an array of strings.
func MapToEnvVarArray(envVars map[string]string) []string {
envVarArray := make([]string, 0, len(envVars))
for k, v := range envVars {
Expand Down
22 changes: 22 additions & 0 deletions tooling/templatize/pkg/utils/env_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package utils

import (
"testing"

"gotest.tools/assert"
)

func TestGetOsVariable(t *testing.T) {
t.Setenv("FOO", "BAR")
envVars := GetOsVariable()
assert.Equal(t, "1", envVars["RUNS_IN_TEMPLATIZE"])
assert.Equal(t, "BAR", envVars["FOO"])
}

func TestMapToEnvVarArray(t *testing.T) {
envVars := map[string]string{
"FOO": "BAR",
}
envVarArray := MapToEnvVarArray(envVars)
assert.DeepEqual(t, []string{"FOO=BAR"}, envVarArray)
}

0 comments on commit 2c6b1ba

Please sign in to comment.