1

When I pass a text or string as a variable from table to feature, for some reason karate.exec is breaking the argument based on space.

I have main feature where the code is

#Example 1
* def calcModel = '":: decimal calcModel = get_calc_model();"' 

#Example 2
* text calcModel = 
  """
   :: decimal calcModel = get_calc_model();
   return calcModel;
  """

 * table calcDetails 
  | field | code                      | desc                         |
  | 31    | '":: return get_name();"  | '"this is name"'             |
  | 32    | calcModel                 | '"this is the calc model"' |

 * call read('classpath:scripts/SetCalcModel.feature') calcDetails

Inside SetCalcModel.feature the code is

* def setCalcModel = karate.exec('/opt/local/SetCalcModel.sh --timeout 100 -field ' + field + ' -code ' + code + ' -description '+desc)   

For row 1 of the table it works fine and executes following command:

command: [/opt/local/SetCalcModel.sh, --timeout, 100, -field, 31, -code, :: decimal calcModel = get_calc_model();, -description, this is the calc model], working dir: null

For row 2 it breaks with following command:

command: [/opt/local/SetCalcModel.sh, --timeout, 100, -field, 32, -code, ::, decimal, calcModel, =, get_calc_model();, -description, this is the calc model], working dir: null

I have tried this with example 1 and 2 and it keeps doing the same. I have also tried passing line json as argument to karate.exec(), that also has same issue. Is there a workaround here??

shrik18
  • 107
  • 4

1 Answers1

1

There is a way to pass arguments as an array of strings, use that approach instead.

For example:

* karate.exec({ args: [ 'curl', 'https://httpbin.org/anything' ] }) 

Refer: https://stackoverflow.com/a/73230200/143475

Peter Thomas
  • 54,465
  • 21
  • 84
  • 248
  • It doesn't seem to work when passed as a variable between features. Parse it again, or directly refer in the child feature(share scope), it will still break it at space. Now, I am storing it in file and then passing it to my .sh, which works for me. I was exploring this for multiline text, and even when used via variable it would have broken at script level. Thanks for your prompt response. I am standardizing Karate in my vertical, all thanks to this all rounder tool. – shrik18 Nov 08 '22 at 15:06
  • @shrik18 glad you like it. I'm not sure if we need to do anything to improve this part of Karate, so if you can replicate the problem (I think) you refer to above, see if you can follow this process: https://github.com/karatelabs/karate/wiki/How-to-Submit-an-Issue – Peter Thomas Nov 08 '22 at 16:07
  • @shrik18 one more thing, the `text` data type in Karate is designed specifically for multi-line text, so please check it out: https://github.com/karatelabs/karate#text – Peter Thomas Nov 08 '22 at 16:08