1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2025-01-20 05:19:40 +02:00

fix (shellExecute) including comma seperated strings as arguments (#3846)

* including comma seperated strings as arguments

* fix unit test

* adding unit test

* fix unit test no param case

Co-authored-by: anilkeshav27 <you@example.com>
This commit is contained in:
Anil Keshav 2022-07-06 08:41:44 +02:00 committed by GitHub
parent 6bc96faba9
commit 8187bf2ec5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 30 additions and 4 deletions

View File

@ -26,6 +26,10 @@ type shellExecuteUtilsBundle struct {
*piperhttp.Client
}
const (
argumentDelimter = ","
)
func newShellExecuteUtils() shellExecuteUtils {
utils := shellExecuteUtilsBundle{
Command: &command.Command{},
@ -71,7 +75,7 @@ func runShellExecute(config *shellExecuteOptions, telemetryData *telemetry.Custo
args := []string{}
if len(config.ScriptArguments) > 0 && isArgumentAtPosition(config.ScriptArguments, position) {
args = strings.Split(config.ScriptArguments[position], " ")
args = strings.Split(config.ScriptArguments[position], argumentDelimter)
}
log.Entry().Info("starting running script:", source)

View File

@ -117,7 +117,7 @@ func ShellExecuteCommand() *cobra.Command {
func addShellExecuteFlags(cmd *cobra.Command, stepConfig *shellExecuteOptions) {
cmd.Flags().StringSliceVar(&stepConfig.Sources, "sources", []string{}, "Scripts paths that must be present in the current workspace or https links to scripts. Only https urls from github are allowed and must be in the format :https://{githubBaseurl}/api/v3/repos/{owner}/{repository}/contents/{path to script} Authentication for the download is only supported via the 'githubToken' param. Make sure the script has the necessary execute permissions.")
cmd.Flags().StringVar(&stepConfig.GithubToken, "githubToken", os.Getenv("PIPER_githubToken"), "GitHub personal access token as per https://help.github.com/en/github/authenticating-to-github/creating-a-personal-access-token-for-the-command-line")
cmd.Flags().StringSliceVar(&stepConfig.ScriptArguments, "scriptArguments", []string{}, "scriptArguments that are needed to be passed to scripts. the scriptArguments list is a flat list and has a positional relationship to the `sources` param. For e.g. The scriptArguments string at position 1 will be considered as the argument(s) for script at position 1 in `sources` list")
cmd.Flags().StringSliceVar(&stepConfig.ScriptArguments, "scriptArguments", []string{}, "scriptArguments that are needed to be passed to scripts. the scriptArguments list is a flat list and has a positional relationship to the `sources` param. For e.g. The scriptArguments string at position 1 will be considered as the argument(s) for script at position 1 in `sources` list. For multiple arguments for a script please add them as a comma seperated string.")
}

View File

@ -121,7 +121,7 @@ func TestRunShellExecute(t *testing.T) {
t.Run("success case - multiple positional script arguments gets added to the correct script", func(t *testing.T) {
o := &shellExecuteOptions{
Sources: []string{"path1/script1.sh", "path2/script2.sh"},
ScriptArguments: []string{"arg1 arg2", "arg3 arg4"},
ScriptArguments: []string{"arg1,arg2", "arg3,arg4"},
}
u := newShellExecuteTestsUtils()
@ -136,4 +136,26 @@ func TestRunShellExecute(t *testing.T) {
assert.Equal(t, []string{"arg3", "arg4"}, u.ExecMockRunner.Calls[1].Params)
assert.NoError(t, err)
})
t.Run("success case - no argument for script 1, single argument for script 2 and multiple argument for script3 gets added to the correct script", func(t *testing.T) {
o := &shellExecuteOptions{
Sources: []string{"path1/script1.sh", "path2/script2.sh", "path3/script3.sh"},
ScriptArguments: []string{"", "arg2.1", "arg3.1,arg3.2"},
}
u := newShellExecuteTestsUtils()
u.AddFile("path1/script1.sh", []byte(`echo dummy1`))
u.AddFile("path2/script2.sh", []byte(`echo dummy2`))
u.AddFile("path3/script3.sh", []byte(`echo dummy3`))
err := runShellExecute(o, nil, u)
assert.Equal(t, "path1/script1.sh", u.ExecMockRunner.Calls[0].Exec)
assert.Equal(t, []string{}, u.ExecMockRunner.Calls[0].Params)
assert.Equal(t, "path2/script2.sh", u.ExecMockRunner.Calls[1].Exec)
assert.Equal(t, []string{"arg2.1"}, u.ExecMockRunner.Calls[1].Params)
assert.Equal(t, "path3/script3.sh", u.ExecMockRunner.Calls[2].Exec)
assert.Equal(t, []string{"arg3.1", "arg3.2"}, u.ExecMockRunner.Calls[2].Params)
assert.NoError(t, err)
})
}

View File

@ -43,7 +43,7 @@ spec:
- STAGES
- STEPS
description: scriptArguments that are needed to be passed to scripts. the scriptArguments list is a flat list and has a positional relationship to the `sources` param.
For e.g. The scriptArguments string at position 1 will be considered as the argument(s) for script at position 1 in `sources` list
For e.g. The scriptArguments string at position 1 will be considered as the argument(s) for script at position 1 in `sources` list. For multiple arguments for a script please add them as a comma seperated string.
mandatory: false
containers:
- name: shell