From b0ec3722eac5d181ab81f61740bdc9a14c54d5eb Mon Sep 17 00:00:00 2001 From: Omer Zidkoni <50792403+omerzi@users.noreply.github.com> Date: Wed, 27 Dec 2023 16:20:56 +0200 Subject: [PATCH] Gradle - add quotes if needed to system properties (#225) --- build/gradle.go | 21 +++++++++++++++------ build/gradle_test.go | 20 ++++++++------------ 2 files changed, 23 insertions(+), 18 deletions(-) diff --git a/build/gradle.go b/build/gradle.go index 6099c9cd..3fb38bda 100644 --- a/build/gradle.go +++ b/build/gradle.go @@ -257,28 +257,37 @@ func (config *gradleRunConfig) GetCmd() *exec.Cmd { if config.initScript != "" { cmd = append(cmd, "--init-script", config.initScript) } - cmd = append(cmd, handleGradleCommandProperties(config.tasks)...) + cmd = append(cmd, formatCommandProperties(config.tasks)...) config.logger.Info("Running gradle command:", strings.Join(cmd, " ")) return exec.Command(cmd[0], cmd[1:]...) } -func handleGradleCommandProperties(tasks []string) []string { +func formatCommandProperties(tasks []string) []string { var cmdArgs []string for _, task := range tasks { - if isGradleSystemOrProjectProperty(task) { - propertyParts := strings.SplitN(task, "=", 2) - task = fmt.Sprintf(`%s="%s"`, propertyParts[0], propertyParts[1]) + if isSystemOrProjectProperty(task) { + task = quotePropertyIfNeeded(task) } cmdArgs = append(cmdArgs, task) } return cmdArgs } -func isGradleSystemOrProjectProperty(task string) bool { +func isSystemOrProjectProperty(task string) bool { hasPropertiesFlag := strings.HasPrefix(task, systemPropertiesFlag) || strings.HasPrefix(task, projectPropertiesFlag) return hasPropertiesFlag && strings.Contains(task, "=") } +// Wraps system or project property value in quotes if its value contain spaces, e.g., -Dkey=val ue => -Dkey='val ue' +func quotePropertyIfNeeded(task string) string { + parts := strings.SplitN(task, "=", 2) + if strings.Contains(parts[1], " ") { + return fmt.Sprintf(`%s='%s'`, parts[0], parts[1]) + } + + return task +} + func (config *gradleRunConfig) runCmd(stdout, stderr io.Writer) error { command := config.GetCmd() command.Env = os.Environ() diff --git a/build/gradle_test.go b/build/gradle_test.go index e813a67d..b2da7569 100644 --- a/build/gradle_test.go +++ b/build/gradle_test.go @@ -103,35 +103,31 @@ func TestParseGradleVersion(t *testing.T) { } } -func TestHandleGradleCommandProperties(t *testing.T) { +func TestFormatCommandProperties(t *testing.T) { tests := []struct { input []string expected []string }{ { input: []string{"clean", "-Dparam=value", "build", "-Pkey=value"}, - expected: []string{"clean", `-Dparam="value"`, "build", `-Pkey="value"`}, + expected: []string{"clean", "-Dparam=value", "build", "-Pkey=value"}, }, { - input: []string{"-Dprop1=value1", "test", "-Dprop2=value2"}, - expected: []string{`-Dprop1="value1"`, "test", `-Dprop2="value2"`}, + input: []string{"-Dprop1=value1", "test", "-Pprop2=value2"}, + expected: []string{"-Dprop1=value1", "test", "-Pprop2=value2"}, }, { - input: []string{"-Dparam1=value1", "-Pkey1=value1", "-Dparam2=value2", "-Pkey2=value2"}, - expected: []string{`-Dparam1="value1"`, `-Pkey1="value1"`, `-Dparam2="value2"`, `-Pkey2="value2"`}, - }, - { - input: []string{"-Dparam1=value1", "run", "-Pkey2=value2", "-Dparam2=value2"}, - expected: []string{`-Dparam1="value1"`, "run", `-Pkey2="value2"`, `-Dparam2="value2"`}, + input: []string{"-Dparam1=value1 value2", "-Pkey1=value1", "-Dparam2=value2", "-Pkey2=value1 value2"}, + expected: []string{"-Dparam1='value1 value2'", "-Pkey1=value1", "-Dparam2=value2", "-Pkey2='value1 value2'"}, }, { input: []string{"-Dparam1=value1", "run", "-Psign"}, - expected: []string{`-Dparam1="value1"`, "run", "-Psign"}, + expected: []string{"-Dparam1=value1", "run", "-Psign"}, }, } for _, test := range tests { - result := handleGradleCommandProperties(test.input) + result := formatCommandProperties(test.input) assert.ElementsMatch(t, test.expected, result) } }