You've already forked sap-jenkins-library
							
							
				mirror of
				https://github.com/SAP/jenkins-library.git
				synced 2025-10-30 23:57:50 +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:
		| @@ -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) | ||||
|   | ||||
| @@ -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.") | ||||
|  | ||||
| } | ||||
|  | ||||
|   | ||||
| @@ -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) | ||||
| 	}) | ||||
| } | ||||
|   | ||||
| @@ -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 | ||||
|   | ||||
		Reference in New Issue
	
	Block a user