Skip to content

Commit 04b0101

Browse files
authored
Merge pull request #141 from buildkite-plugins/SUP-5910-fix-env-parsing
Fix env parsing of complex values
2 parents 2ff02e0 + 8bf9e55 commit 04b0101

File tree

2 files changed

+66
-5
lines changed

2 files changed

+66
-5
lines changed

plugin.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -443,16 +443,16 @@ func parseEnv(raw interface{}) (map[string]string, error) {
443443

444444
result := make(map[string]string)
445445
for _, v := range raw.([]interface{}) {
446-
split := strings.Split(v.(string), "=")
447-
key, value := strings.TrimSpace(split[0]), split[1:]
446+
split := strings.SplitN(v.(string), "=", 2)
447+
key := strings.TrimSpace(split[0])
448448

449449
// only key exists. set value from env
450-
if len(key) > 0 && len(value) == 0 {
450+
if len(key) > 0 && len(split) == 1 {
451451
result[key] = env(key, "")
452452
}
453453

454-
if len(value) > 0 {
455-
result[key] = strings.TrimSpace(value[0])
454+
if len(split) == 2 {
455+
result[key] = strings.TrimSpace(split[1])
456456
}
457457
}
458458

plugin_test.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1026,3 +1026,64 @@ func TestPluginShouldPreserveDependsOnArray(t *testing.T) {
10261026
t.Fatalf("plugin diff (-want +got):\n%s", diff)
10271027
}
10281028
}
1029+
1030+
func TestPluginEnvWithEqualsSignsAndSpacesInValues(t *testing.T) {
1031+
param := `[{
1032+
"github.com/buildkite-plugins/monorepo-diff-buildkite-plugin#commit": {
1033+
"env": [
1034+
"EXTRA_BUILD_ARGS=--build-arg=ARG1=value1",
1035+
"QUOTED_ARGS=\"--build-arg=ARG1=value1\"",
1036+
"SPACE_VALUE=value with spaces",
1037+
"COMPLEX=\"--opt1=val1 --opt2=val2\""
1038+
],
1039+
"watch": [
1040+
{
1041+
"path": "services/",
1042+
"config": {
1043+
"command": "echo test"
1044+
}
1045+
}
1046+
]
1047+
}
1048+
}]`
1049+
1050+
got, err := initializePlugin(param)
1051+
assert.NoError(t, err)
1052+
1053+
expected := Plugin{
1054+
Diff: "git diff --name-only HEAD~1",
1055+
Wait: false,
1056+
LogLevel: "info",
1057+
Interpolation: true,
1058+
Env: map[string]string{
1059+
"EXTRA_BUILD_ARGS": "--build-arg=ARG1=value1",
1060+
"QUOTED_ARGS": "\"--build-arg=ARG1=value1\"",
1061+
"SPACE_VALUE": "value with spaces",
1062+
"COMPLEX": "\"--opt1=val1 --opt2=val2\"",
1063+
},
1064+
Watch: []WatchConfig{
1065+
{
1066+
Paths: []string{"services/"},
1067+
Step: Step{
1068+
Command: "echo test",
1069+
Env: map[string]string{
1070+
"EXTRA_BUILD_ARGS": "--build-arg=ARG1=value1",
1071+
"QUOTED_ARGS": "\"--build-arg=ARG1=value1\"",
1072+
"SPACE_VALUE": "value with spaces",
1073+
"COMPLEX": "\"--opt1=val1 --opt2=val2\"",
1074+
},
1075+
},
1076+
},
1077+
},
1078+
}
1079+
1080+
if diff := cmp.Diff(expected, got); diff != "" {
1081+
t.Fatalf("plugin diff (-want +got):\n%s", diff)
1082+
}
1083+
1084+
// Explicitly verify the env values are correct
1085+
assert.Equal(t, "--build-arg=ARG1=value1", got.Env["EXTRA_BUILD_ARGS"])
1086+
assert.Equal(t, "\"--build-arg=ARG1=value1\"", got.Env["QUOTED_ARGS"])
1087+
assert.Equal(t, "value with spaces", got.Env["SPACE_VALUE"])
1088+
assert.Equal(t, "\"--opt1=val1 --opt2=val2\"", got.Env["COMPLEX"])
1089+
}

0 commit comments

Comments
 (0)