112 lines
2.2 KiB
Go
112 lines
2.2 KiB
Go
package go_environment
|
|
|
|
import (
|
|
"os"
|
|
"testing"
|
|
)
|
|
|
|
func TestReplaceEnvironmentVariables(t *testing.T) {
|
|
testCases := []struct {
|
|
input string
|
|
environment map[string]string
|
|
expected string
|
|
}{
|
|
{},
|
|
{
|
|
input: "a",
|
|
environment: map[string]string{},
|
|
expected: "a",
|
|
},
|
|
{
|
|
input: "%testVar1%",
|
|
environment: map[string]string{},
|
|
expected: "%testVar1%",
|
|
},
|
|
{
|
|
input: "%testVar1%",
|
|
environment: map[string]string{
|
|
"testVar1": "b",
|
|
},
|
|
expected: "b",
|
|
},
|
|
{
|
|
input: "%testVar1%&%testVar1%",
|
|
environment: map[string]string{
|
|
"testVar1": "b",
|
|
},
|
|
expected: "b&b",
|
|
},
|
|
{
|
|
input: "%testVar1%&%testVar2%",
|
|
environment: map[string]string{},
|
|
expected: "%testVar1%&%testVar2%",
|
|
},
|
|
{
|
|
input: "%testVar1%&%testVar2%",
|
|
environment: map[string]string{
|
|
"testVar1": "b",
|
|
},
|
|
expected: "b&%testVar2%",
|
|
},
|
|
{
|
|
input: "%testVar2%&%testVar1%",
|
|
environment: map[string]string{
|
|
"testVar1": "b",
|
|
},
|
|
expected: "%testVar2%&b",
|
|
},
|
|
{
|
|
input: "$(testVar1)",
|
|
environment: map[string]string{},
|
|
expected: "$(testVar1)",
|
|
},
|
|
{
|
|
input: "$(testVar1)",
|
|
environment: map[string]string{
|
|
"testVar1": "b",
|
|
},
|
|
expected: "b",
|
|
},
|
|
{
|
|
input: "$(testVar1)&$(testVar1)",
|
|
environment: map[string]string{
|
|
"testVar1": "b",
|
|
},
|
|
expected: "b&b",
|
|
},
|
|
{
|
|
input: "$(testVar1)&$(testVar2)",
|
|
environment: map[string]string{},
|
|
expected: "$(testVar1)&$(testVar2)",
|
|
},
|
|
{
|
|
input: "$(testVar1)&$(testVar2)",
|
|
environment: map[string]string{
|
|
"testVar1": "b",
|
|
},
|
|
expected: "b&$(testVar2)",
|
|
},
|
|
{
|
|
input: "$(testVar2)&$(testVar1)",
|
|
environment: map[string]string{
|
|
"testVar1": "b",
|
|
},
|
|
expected: "$(testVar2)&b",
|
|
},
|
|
}
|
|
|
|
for i, testCase := range testCases {
|
|
for key, value := range testCase.environment {
|
|
os.Setenv(key, value)
|
|
}
|
|
|
|
if result := ReplaceEnvironmentVariables(testCase.input); result != testCase.expected {
|
|
t.Errorf("TestReplaceEnvironmentVariables: test case %d: expected '%s' but got '%s'", i+1, testCase.expected, result)
|
|
}
|
|
|
|
for key := range testCase.environment {
|
|
os.Unsetenv(key)
|
|
}
|
|
}
|
|
}
|