This plugin extends Kaholo's capabilities to create, read, append, search and replace text in text files.
Text files of all types typically end with a newline character. The plugin will therefore attempt to help you do this, for example if you append text that does NOT end with a newline, one will be added for you. In some very unusual cases, for example if you wanted a file to NOT end with a newline character, this could be problematic. There are several work-arounds, including method Replace Text in File with a RegExp to identify the unwanted newlines and replace them with nothing, or using sed or truncate with the Command Line plugin to remove only the final newline of a file like so:
file=mytestfile.txt
size=$(stat -c '%s' "$file")
truncate -s $(($size-1)) $file
Methods to Search and Replace/Remove text in the file rely on regular expressions of the type commonly found in Perl, JavaScript, and other programming languages. For those unfamiliar with these it is recommended that one build and test regular expressions on a purpose-built website such as Regex101.
In some cases a RegExp may be entered in plain format, for example to match the word "Kaholo" in the text you may simply use that word as the RegExp. Global and multiline options are assumed. However if you want a case insensitive (Option "i") and non-global (find first match only) search then you must use the flagged expression:
/kaholo/i
The plugin applies the RegExp using JavaScript String.prototype.match(RegExp).
Create a new file with specified content in the specified path.
The path and file name for the text file to be written on the Kaholo agent. Either absolute or relative paths may be used.
The text to be written to the file.
In the case where the file already exists and this option is enabled, the file will be overwritten with the provided content.
If enabled, after the file is written the content of the file is also returned to the Final Result in Kaholo. This is especially useful if the content is JSON and might be accessed by downstream actions using the code layer. For example after writing the JSON result from an automated test package to a file, this code might be used to post the "Success" status to a Slack channel:
`Test status: ${kaholo.actions.TextEditor1.result.status}`
Append the specified content to the end of the specified file. This method can also create new files.
The path and file name for the text file to be appended on the Kaholo agent. Either absolute or relative paths may be used.
This is the text Content to be appended. If it does not end with a final newline character, one will be added for you. See the above section on newlines if this causes issues in your use case.
By default if the file does not yet exist it will be created for you. If this parameter is enabled it will error instead.
If enabled, the entire contents of the file will be returned in the Kaholo execution's Final Result.
Search the content of the specified file for the specified RegExp pattern. The return value can be either simply "found": true
or an array of matching strings from the file.
The path and file name for the text file to be searched on the Kaholo agent. Either absolute or relative paths may be used.
The regular expression to use for the search. See section RegExp above for further detail.
If enabled, matching strings are returned in the Kaholo execution's Final Result. Otherwise a simple boolean is returned to indicate if the expression was matched or not.
This method is identical to method Search In File except it also replaces the strings that match the expression.
The text with which to replace matches in the text file. If left empty, the matches will be replaced by nothing, effectively just deleted from the file.
Reads and returns the content of the specified file in the Final Result of the Kaholo action's execution. This is particularly useful for reading JSON files to expose them as objects in the Kaholo code layer. For example the package.json from this plugin could be read, and then the eslint version might be referred to in code as:
kaholo.actions.TestEditor1.result.devDependencies.eslint
The path and file name for the text file to be read from the Kaholo agent. Either absolute or relative paths may be used.