Commons component plugin - Macros - Substitute keys


# SUBSTITUTE KEYS IN {files} USING {key_value_list} AS {processed_files}

What ?

This macro allows to replace specific keys by selected values in a bundle of files.

Underlying instruction :

LOAD {key_value_list} AS placeholder{%%rand2}.file
CONVERT placeholder{%%rand2}.file TO properties(structured) AS placeholder{%%rand3}.properties
LOAD {files} AS __bundle{%%rand1}
CONVERT __bundle{%%rand1} TO file(param) USING placeholder{%%rand3}.properties AS {processed_files}

> Input :

  • {files} : The bundle of files where you want to apply the substitution.
  • {key_value_list} : It can either be a path to a properties file or an inline command with keys and values.
Example of inline command : $(key1=value1 \n key2=value2).

> Output :

  • {processed_files} : The bundle of filtered files that have been processed.

Examples :

# SUBSTITUTE KEYS IN resources_folder USING resources_folder/file.properties AS result_bundle

Or

# SUBSTITUTE KEYS IN resources_folder USING $(oneKey=oneValue \n anotherKey=anotherValue) AS result_bundle

First file to process :

../../_images/substitute-keys-macro-example-file.png

Second file to process :

../../_images/substitute-keys-macro-example-file2.png

.properties File :

../../_images/substitute-keys-macro-properties-file.png

The folder containing files to process which corresponds to{files}  :

../../_images/substitute-keys-macro-resources-folder.png

SKF script :

../../_images/substitute-keys-macro.png

In order to check that the macro worked properly, we added in this example, the instruction “LOAD result AS resultOutput” to be able to see the result output.

You can access to the result output in the following folder which contains temporary files :

C:\Users\*user name*\AppData\Local\Temp\Squash_TA\20190909_121048_957615127627872437436\tests\tests\substitute_keys.ta\myResources2594181966007652622639979916temp
../../_images/substitute-keys-macro-temporary-file-folder.png

Result output for first file :

../../_images/substitute-keys-macro-example-file-output.png

Result output for second file :

../../_images/substitute-keys-macro-example-file-output2.png


# SUBSTITUTE KEYS IN {files} FOR FILES WHOSE NAMES MATCHING {regex} USING {key_value_list} AS {processed_files}

What ?

This macro allows to replace specific keys by selected values in a bundle of files whose names are matching a selected regular expression.

Underlying instruction :

LOAD {key_value_list} AS placeholder{%%rand2}.file
CONVERT placeholder{%%rand2}.file TO properties(structured) AS placeholder{%%rand3}.properties
LOAD {files} AS __bundle{%%rand1}
CONVERT __bundle{%%rand1} TO file(param) USING placeholder{%%rand3}.properties,$(squashtest.ta.param.include:{regex}) AS {processed_files}

> Input :

  • {files} : The bundle of files where you want to apply the substitution.
  • {regex} : The regular expression used to filter the files in the bundle.
  • {key_value_list} : It can either be a path to a properties file or an inline command with keys and values.
Example of inline command : $(key1=value1 \n key2=value2).

> Output :

  • {processed_files} : The bundle of filtered files that have been processed.

Examples :

# SUBSTITUTE KEYS IN resources_folder FOR FILES WHOSE NAMES MATCHING .xml USING resources_folder/file.properties AS result_bundle

Or

# SUBSTITUTE KEYS IN resources_folder FOR FILES WHOSE NAMES MATCHING .xml USING $(oneKey=oneValue \n anotherKey=anotherValue) AS result_bundle

This example is based on the previous one. For more details, please check here.

SKF script :

../../_images/substitute-keys-matching-regex-macro2.png

Or

../../_images/substitute-keys-matching-regex-macro.png

Console output :

../../_images/substitute-keys-matching-regex-macro-result.png

The .txt and .properties files which don’t match the selected regex are properly excluded.

Result output for first file :

../../_images/substitute-keys-matching-regex-macro-exemple-file-output1.png

Result output for second file :

../../_images/substitute-keys-matching-regex-macro-exemple-file-output2.png

The .xml file is the only one that has been processed.



# SUBSTITUTE KEYS IN {files} FOR FILES WHOSE NAMES NOT MATCHING {regex} USING {key_value_list} AS {processed_files}

What ?

This macro allows to replace specific keys by selected values in a bundle of files whose names are not matching a selected regular expression.

Underlying instruction :

LOAD {key_value_list} AS placeholder{%%rand2}.file
CONVERT placeholder{%%rand2}.file TO properties(structured) AS placeholder{%%rand3}.properties
LOAD {files} AS __bundle{%%rand1}
CONVERT __bundle{%%rand1} TO file(param) USING placeholder{%%rand3}.properties,$(squashtest.ta.param.exclude:{regex}) AS {processed_files}

> Input :

  • {files} : The bundle of files where you want to apply the substitution.
  • {regex} : The regular expression used to filter the files in the bundle.
  • {key_value_list} : It can either be a path to a properties file or an inline command with keys and values.
Example of inline command : $(key1=value1 \n key2=value2).

> Output :

  • {processed_files} : The bundle of filtered files that have been processed.

Examples :

# SUBSTITUTE KEYS IN resources_folder FOR FILES WHOSE NAMES NOT MATCHING .xml USING resources_folder/file.properties AS result_bundle

Or

# SUBSTITUTE KEYS IN resources_folder FOR FILES WHOSE NAMES NOT MATCHING .xml USING $(oneKey=oneValue,anotherKey \n anotherValue) AS result_bundle

This example is based on the previous one. For more details, please check here.

SKF script :

../../_images/substitute-keys-not-matching-regex-macro.png

Console output :

../../_images/substitute-keys-not-matching-regex-macro-result.png

The .xml file which match the selected regex is properly excluded.

Result output for first file :

../../_images/substitute-keys-not-matching-regex-macro-exemple-file-output1.png

Result output for second file :

../../_images/substitute-keys-not-matching-regex-macro-exemple-file-output2.png

The .xml file is not processed whereas the .txt is.