Skip to content

Commit

Permalink
Gradle - add quotes if needed to system properties (#225)
Browse files Browse the repository at this point in the history
  • Loading branch information
omerzi authored Dec 27, 2023
1 parent 646f2fc commit b0ec372
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 18 deletions.
21 changes: 15 additions & 6 deletions build/gradle.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
20 changes: 8 additions & 12 deletions build/gradle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

0 comments on commit b0ec372

Please sign in to comment.